8 #define NUM_ITERATIONS 10000000
18 static volatile int wait_;
21 static lifo_t *lifo_alloc (void) {
22 lifo_t *stk = (lifo_t *)nbd_malloc(sizeof(lifo_t));
23 memset(stk, 0, sizeof(lifo_t));
27 static void lifo_aba_push (lifo_t *stk, node_t *x) {
30 head = ((volatile lifo_t *)stk)->head;
31 ((volatile node_t *)x)->next = head;
32 } while (__sync_val_compare_and_swap(&stk->head, head, x) != head);
35 node_t *lifo_aba_pop (lifo_t *stk) {
38 head = ((volatile lifo_t *)stk)->head;
41 } while (__sync_val_compare_and_swap(&stk->head, head, head->next) != head);
46 node_t *node_alloc (void) {
47 node_t *node = (node_t *)nbd_malloc(sizeof(node_t));
48 memset(node, 0, sizeof(node_t));
52 void *worker (void *arg) {
53 int id = (int)(size_t)arg;
54 unsigned int rand_seed = (unsigned int)id + 1;
56 // Wait for all the worker threads to be ready.
57 __sync_fetch_and_add(&wait_, -1);
61 for (i = 0; i < NUM_ITERATIONS; ++ i) {
62 int n = rand_r(&rand_seed);
64 lifo_aba_push(stk_, node_alloc());
66 node_t *x = lifo_aba_pop(stk_);
77 int main (int argc, char **argv) {
79 //lwt_set_trace_level("m0r0");
85 num_threads = strtol(argv[1], NULL, 10);
87 fprintf(stderr, "%s: Invalid argument for number of threads\n", argv[0]);
90 if (num_threads <= 0) {
91 fprintf(stderr, "%s: Number of threads must be at least 1\n", argv[0]);
99 pthread_t thread[num_threads];
100 for (int i = 0; i < num_threads; ++i) {
101 int rc = nbd_thread_create(thread + i, i, worker, (void *)(size_t)i);
102 if (rc != 0) { perror("pthread_create"); return rc; }
104 for (int i = 0; i < num_threads; ++i) {
105 pthread_join(thread[i], NULL);