#include #include #include #include #include #define N 128 #define LOOPS 2000 #define THREADS 20 #define ABORT 0xf0000000 /* f + 7x0 */ #define BACKOFF 1 /* initial backoff in microseconds */ #define STM 0 #define LOCK 1 #define ATOMIC 2 int version; struct cell { int value; int counter; }; struct memory { pthread_mutex_t lock; struct cell* volatile values[N]; volatile int clock; }; struct tx { int readSet[N]; int writeSet[N]; int values[N]; /* valeurs */ int clock; long long backoff; }; struct memory memory; __thread struct tx tx; struct cell* newValue(int value, int counter) { struct cell* res = malloc(sizeof(struct cell)); res->value = value; res->counter = counter; return res; } void initMemory() { pthread_mutex_init(&memory.lock, 0); for(int i=0; icounter >= memory.clock) return ABORT; return value->value; } /* * ABORT: abort, other: ok */ int commitTX() { pthread_mutex_lock(&memory.lock); for(int i=0; icounter >= tx.clock) { pthread_mutex_unlock(&memory.lock); return ABORT; } for(int i=0; ivalue++; pthread_mutex_unlock(&memory.lock); } else { __sync_fetch_and_add(&memory.values[0]->value, 1); } } return 0; } int main(int argc, char **argv) { if(argc != 2) { fprintf(stderr, "usage: %s [0: STM, 1: LOCK, 2: ATOMIC_ADD]\n", argv[0]); return 1; } version = atoi(argv[1]); pthread_t threads[THREADS]; initMemory(); struct timeval start; gettimeofday(&start, 0); for(int i=0; ivalue != THREADS*LOOPS) { printf("value = %d while we expect %d\n", memory.values[0]->value, THREADS*LOOPS); printf(" => error!!!\n"); } else { printf("Elapsed time: %0.3f ms\n", 1e3*end.tv_sec + end.tv_usec*1e-3 - start.tv_sec*1e3 - start.tv_usec*1e-3); } return 0; } /* gcc -Wall -Werror -o main main.c -lpthread */