]> pd.if.org Git - nbds/blob - test/haz_test.c
work in progress
[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
21 typedef struct node {
22     struct node *next;
23 } node_t;
24
25 typedef struct lifo {
26     node_t *head;
27 } lifo_t;
28
29 static volatile int wait_;
30 static lifo_t *stk_;
31
32 void *worker (void *arg) {
33     int id = (int)(size_t)arg;
34     unsigned int r = (unsigned int)(id + 1) * 0x5bd1e995; // seed psuedo-random number generator
35     haz_t *hp0 = haz_get_static(0);
36
37     // Wait for all the worker threads to be ready.
38     (void)SYNC_ADD(&wait_, -1);
39     do {} while (wait_); 
40
41     int i;
42     for (i = 0; i < NUM_ITERATIONS; ++ i) {
43         r ^= r << 6; r ^= r >> 21; r ^= r << 7; // generate next psuedo-random number
44         if (r & 0x1000) {
45             // push
46             node_t *new_head = (node_t *)nbd_malloc(sizeof(node_t));
47             node_t *old_head = stk_->head;
48             node_t *temp;
49             do {
50                 temp = old_head;
51                 new_head->next = temp;
52             } while ((old_head = SYNC_CAS(&stk_->head, temp, new_head)) != temp);
53         } else {
54             // pop
55             node_t *temp;
56             node_t *head = stk_->head;
57             do {
58                 temp = head;
59                 if (temp == NULL)
60                     break;
61                 haz_set(hp0, temp);
62                 head = VOLATILE_DEREF(stk_).head;
63                 if (temp != head)
64                     continue;
65             } while ((head = SYNC_CAS(&stk_->head, temp, temp->next)) != temp);
66
67             if (temp != NULL) {
68                 haz_defer_free(temp, nbd_free);
69             }
70         }
71     }
72
73     return NULL;
74 }
75
76 int main (int argc, char **argv) {
77     //lwt_set_trace_level("m0r0");
78
79     int num_threads = MAX_NUM_THREADS;
80     if (argc == 2)
81     {
82         errno = 0;
83         num_threads = strtol(argv[1], NULL, 10);
84         if (errno) {
85             fprintf(stderr, "%s: Invalid argument for number of threads\n", argv[0]);
86             return -1;
87         }
88         if (num_threads <= 0) {
89             fprintf(stderr, "%s: Number of threads must be at least 1\n", argv[0]);
90             return -1;
91         }
92     }
93
94     stk_ = (lifo_t *)nbd_malloc(sizeof(lifo_t)); 
95     memset(stk_, 0, sizeof(lifo_t));
96
97     struct timeval tv1, tv2;
98     gettimeofday(&tv1, NULL);
99     wait_ = num_threads;
100
101     pthread_t thread[num_threads];
102     for (int i = 0; i < num_threads; ++i) {
103         int rc = nbd_thread_create(thread + i, i, worker, (void *)(size_t)i);
104         if (rc != 0) { perror("pthread_create"); return rc; }
105     }
106     for (int i = 0; i < num_threads; ++i) {
107         pthread_join(thread[i], NULL);
108     }
109
110     gettimeofday(&tv2, NULL);
111     int ms = (int)(1000000*(tv2.tv_sec - tv1.tv_sec) + tv2.tv_usec - tv1.tv_usec) / 1000;
112     printf("Th:%d Time:%dms\n\n", num_threads, ms);
113     fflush(stdout);
114
115     return 0;
116 }