]> pd.if.org Git - nbds/blob - test/haz_test.c
add hazard pointer implementation. buggy
[nbds] / test / haz_test.c
1 /* 
2  * Written by Josh Dybnis and released to the public domain, as explained at
3  * http://creativecommons.org/licenses/publicdomain
4  *
5  * hazard pointer test
6  *
7  */
8 #include <stdlib.h>
9 #include <string.h>
10 #include <stdio.h>
11 #include <errno.h>
12 #include <pthread.h>
13 #include <sys/time.h>
14 #include "common.h"
15 #include "mem.h"
16 #include "runtime.h"
17 #include "hazard.h"
18
19 #define NUM_ITERATIONS 10000000
20 #define MAX_NUM_THREADS 4
21
22 typedef struct node {
23     struct node *next;
24 } node_t;
25
26 typedef struct lifo {
27     node_t *head;
28 } lifo_t;
29
30 static volatile int wait_;
31 static lifo_t *stk_;
32
33 void *worker (void *arg) {
34     int id = (int)(size_t)arg;
35     unsigned int r = (unsigned int)(id + 1) * 0x5bd1e995; // seed "random" number generator
36     haz_t *hp0 = haz_get_static(0);
37
38     // Wait for all the worker threads to be ready.
39     __sync_fetch_and_add(&wait_, -1);
40     do {} while (wait_); 
41
42     int i;
43     for (i = 0; i < NUM_ITERATIONS; ++ i) {
44         r ^= r << 6; r ^= r >> 21; r ^= r << 7; // generate next "random" number
45         if (r & 0x1000) {
46             // push
47             node_t *new_head = (node_t *)nbd_malloc(sizeof(node_t));
48             node_t *old_head = stk_->head;
49             node_t *temp;
50             do {
51                 temp = old_head;
52                 new_head->next = temp;
53             } while ((old_head = __sync_val_compare_and_swap(&stk_->head, temp, new_head)) != temp);
54         } else {
55             // pop
56             node_t *temp;
57             node_t *head = stk_->head;
58             do {
59                 temp = head;
60                 if (temp == NULL)
61                     break;
62                 haz_set(hp0, temp);
63                 head = ((volatile lifo_t *)stk_)->head;
64                 if (temp != head)
65                     continue;
66             } while ((head = __sync_val_compare_and_swap(&stk_->head, temp, temp->next)) != temp);
67
68             if (temp != NULL) {
69                 haz_defer_free(temp, nbd_free);
70             }
71         }
72     }
73
74     return NULL;
75 }
76
77 int main (int argc, char **argv) {
78     //lwt_set_trace_level("m0r0");
79
80     int num_threads = MAX_NUM_THREADS;
81     if (argc == 2)
82     {
83         errno = 0;
84         num_threads = strtol(argv[1], NULL, 10);
85         if (errno) {
86             fprintf(stderr, "%s: Invalid argument for number of threads\n", argv[0]);
87             return -1;
88         }
89         if (num_threads <= 0) {
90             fprintf(stderr, "%s: Number of threads must be at least 1\n", argv[0]);
91             return -1;
92         }
93     }
94
95     stk_ = (lifo_t *)nbd_malloc(sizeof(lifo_t)); 
96     memset(stk_, 0, sizeof(lifo_t));
97
98     struct timeval tv1, tv2;
99     gettimeofday(&tv1, NULL);
100     wait_ = num_threads;
101
102     pthread_t thread[num_threads];
103     for (int i = 0; i < num_threads; ++i) {
104         int rc = nbd_thread_create(thread + i, i, worker, (void *)(size_t)i);
105         if (rc != 0) { perror("pthread_create"); return rc; }
106     }
107     for (int i = 0; i < num_threads; ++i) {
108         pthread_join(thread[i], NULL);
109     }
110
111     gettimeofday(&tv2, NULL);
112     int ms = (int)(1000000*(tv2.tv_sec - tv1.tv_sec) + tv2.tv_usec - tv1.tv_usec) / 1000;
113     printf("Th:%d Time:%dms\n\n", num_threads, ms);
114     fflush(stdout);
115
116     return 0;
117 }