make it compile again

master
leitner 15 years ago
parent 25f6b4640a
commit 89b88f036d

36
CAS.h

@ -1,6 +1,8 @@
#ifndef _CAS_H
#define _CAS_H
#include <stddef.h>
/* Atomic operations for lock-free data structures.
* We operate on machine words and use size_t as a type.
* CAS stands for Compare And Swap, the most common operation. */
@ -11,6 +13,23 @@
#define USE_BUILTINS
#endif
/* if (*x == oldval) { *x=newval; return 1; } else return 0; */
static inline int compare_and_swap(volatile size_t* x,size_t oldval,size_t newval) {
#ifdef USE_BUILTINS
__sync_bool_compare_and_swap(x,oldval,newval);
#elif defined(__i386__)
char result;
asm volatile ("lock; cmpxchgl %3, %0; setz %1" : "=m"(*x), "=q" (result) : "m" (*x), "r" (newval), "a" (oldval) : "memory");
return result;
#elif defined(__x86_64__)
char result;
asm volatile ("lock; cmpxchgq %3, %0; setz %1" : "=m"(*x), "=q" (result) : "m" (*x), "r" (newval), "a" (oldval) : "memory");
return result;
#else
#error architecture not supported and gcc too old, edit CAS.h
#endif
}
/* *x += val; */
static inline void atomic_add(size_t* x,size_t val) {
#ifdef USE_BUILTINS
@ -95,23 +114,6 @@ static inline void atomic_and(volatile size_t* x,size_t val) {
#endif
}
/* if (*x == oldval) { *x=newval; return 1; } else return 0; */
static inline int compare_and_swap(volatile size_t* x,size_t oldval,size_t newval) {
#ifdef USE_BUILTINS
__sync_bool_compare_and_swap(x,oldval,newval);
#elif defined(__i386__)
char result;
asm volatile ("lock; cmpxchgl %3, %0; setz %1" : "=m"(*x), "=q" (result) : "m" (*x), "r" (newval), "a" (oldval) : "memory");
return result;
#elif defined(__x86_64__)
char result;
asm volatile ("lock; cmpxchgq %3, %0; setz %1" : "=m"(*x), "=q" (result) : "m" (*x), "r" (newval), "a" (oldval) : "memory");
return result;
#else
#error architecture not supported and gcc too old, edit CAS.h
#endif
}
/* Optimization barrier */
/* The "volatile" is due to gcc bugs */
#define barrier() __asm__ __volatile__("": : :"memory")

22
t.c

@ -24,11 +24,15 @@
#include "safemult.h"
#include "iarray.h"
#include "CAS.h"
#include "io_internal.h"
#define rdtscl(low) \
__asm__ __volatile__ ("rdtsc" : "=a" (low) : : "edx")
// #define atomic_add(mem,val) asm volatile ("lock; add%z0 %1, %0": "+m" (mem): "ir" (val))
int64 writecb(int64 fd,const void* buf,uint64 n) {
(void)fd;
(void)buf;
@ -44,6 +48,23 @@ int64 writecb(int64 fd,const void* buf,uint64 n) {
}
int main(int argc,char* argv[]) {
static size_t x;
x=23;
atomic_add(&x,3);
printf("%u\n",x);
printf("%u\n",atomic_add_return(&x,-3));
printf("%u\n",compare_and_swap(&x,26,17));
printf("%u\n",compare_and_swap(&x,23,17));
#if 0
atomic_add(&x,3); printf("%u\n",x);
x=23;
atomic_add(&x,3); assert(x==26);
atomic_or(&x,1); assert(x==27);
atomic_and(&x,-2); assert(x==26);
#endif
#if 0
iarray a;
char* c;
iarray_init(&a,sizeof(io_entry));
@ -51,6 +72,7 @@ int main(int argc,char* argv[]) {
printf("23 -> %p\n",c=iarray_allocate(&a,23));
printf("1234567 -> %p\n",c=iarray_allocate(&a,1234567));
printf("23 -> %p\n",iarray_get(&a,23));
#endif
#if 0
io_batch* b=iob_new(1234);
int64 fd=open("t.c",0);

Loading…
Cancel
Save