11 #define NUM_ITERATIONS 10000000
21 static volatile int wait_;
24 static lifo_t *lifo_alloc (void) {
25 lifo_t *stk = (lifo_t *)nbd_malloc(sizeof(lifo_t));
26 memset(stk, 0, sizeof(lifo_t));
30 static void lifo_aba_push (lifo_t *stk, node_t *x) {
33 head = VOLATILE_DEREF(stk).head;
34 VOLATILE_DEREF(x).next = head;
35 } while (SYNC_CAS(&stk->head, head, x) != head);
38 node_t *lifo_aba_pop (lifo_t *stk) {
41 head = VOLATILE_DEREF(stk).head;
44 } while (SYNC_CAS(&stk->head, head, head->next) != head);
49 node_t *node_alloc (void) {
50 node_t *node = (node_t *)nbd_malloc(sizeof(node_t));
51 memset(node, 0, sizeof(node_t));
55 void *worker (void *arg) {
58 // Wait for all the worker threads to be ready.
59 (void)__sync_fetch_and_add(&wait_, -1);
63 for (i = 0; i < NUM_ITERATIONS; ++ i) {
66 lifo_aba_push(stk_, node_alloc());
68 node_t *x = lifo_aba_pop(stk_);
79 int main (int argc, char **argv) {
81 lwt_set_trace_level("m3r3");
83 int num_threads = sysconf(_SC_NPROCESSORS_CONF);
87 num_threads = strtol(argv[1], NULL, 10);
89 fprintf(stderr, "%s: Invalid argument for number of threads\n", argv[0]);
92 if (num_threads <= 0) {
93 fprintf(stderr, "%s: Number of threads must be at least 1\n", argv[0]);
101 struct timeval tv1, tv2;
102 gettimeofday(&tv1, NULL);
105 pthread_t thread[num_threads];
106 for (int i = 0; i < num_threads; ++i) {
107 int rc = pthread_create(thread + i, NULL, worker, (void *)(size_t)i);
108 if (rc != 0) { perror("pthread_create"); return rc; }
110 for (int i = 0; i < num_threads; ++i) {
111 pthread_join(thread[i], NULL);
114 gettimeofday(&tv2, NULL);
115 int ms = (int)(1000000*(tv2.tv_sec - tv1.tv_sec) + tv2.tv_usec - tv1.tv_usec) / 1000;
116 printf("Th:%d Time:%dms\n\n", num_threads, ms);