2 * Written by Josh Dybnis and released to the public domain, as explained at
3 * http://creativecommons.org/licenses/publicdomain
5 * Harris-Michael lock-free list-based set
6 * http://www.research.ibm.com/people/m/michael/spaa-2002.pdf
26 node_t *node_alloc (uint64_t key, uint64_t value) {
27 node_t *item = (node_t *)nbd_malloc(sizeof(node_t));
28 memset(item, 0, sizeof(node_t));
34 list_t *list_alloc (void) {
35 list_t *list = (list_t *)nbd_malloc(sizeof(list_t));
36 list->head = node_alloc(0, 0);
37 list->last = node_alloc((uint64_t)-1, 0);
38 list->head->next = list->last;
42 static node_t *find_pred (node_t **pred_ptr, list_t *list, uint64_t key, int help_remove) {
43 node_t *pred = list->head;
44 node_t *item = pred->next;
45 TRACE("l3", "find_pred: searching for key %p in list (head is %p)", key, pred);
51 node_t *next = item->next;
52 TRACE("l3", "find_pred: visiting item %p (next %p)", item, next);
53 TRACE("l3", "find_pred: key %p", item->key, item->value);
55 // Marked items are logically removed, but not unlinked yet.
56 while (EXPECT_FALSE(IS_TAGGED(next))) {
58 // Skip over partially removed items.
60 item = (node_t *)STRIP_TAG(item->next);
65 // Unlink partially removed items.
67 if ((other = SYNC_CAS(&pred->next, item, STRIP_TAG(next))) == item) {
68 item = (node_t *)STRIP_TAG(next);
70 TRACE("l3", "find_pred: unlinked item %p from pred %p", item, pred);
71 TRACE("l3", "find_pred: now item is %p next is %p", item, next);
73 // The thread that completes the unlink should free the memory.
74 nbd_defer_free(other);
76 TRACE("l3", "find_pred: lost race to unlink item from pred %p; its link changed to %p", pred, other);
78 return find_pred(pred_ptr, list, key, help_remove); // retry
84 // If we reached the key (or passed where it should be), we found the right predesssor
85 if (item->key >= key) {
86 TRACE("l3", "find_pred: found pred %p item %p", pred, item);
87 if (pred_ptr != NULL) {
100 // Fast find. Do not help unlink partially removed nodes and do not return the found item's predecessor.
101 uint64_t list_lookup (list_t *list, uint64_t key) {
102 TRACE("l3", "list_lookup: searching for key %p in list %p", key, list);
103 node_t *item = find_pred(NULL, list, key, FALSE);
105 // If we found an <item> matching the <key> return its value.
106 return (item->key == key) ? item->value : DOES_NOT_EXIST;
109 // Insert the <key>, if it doesn't already exist in the <list>
110 uint64_t list_add (list_t *list, uint64_t key, uint64_t value) {
111 TRACE("l3", "list_add: inserting key %p value %p", key, value);
115 node_t *next = find_pred(&pred, list, key, TRUE);
117 // If a node matching <key> already exists in the list, return its value.
118 if (next->key == key) {
119 TRACE("l3", "list_add: there is already an item %p (value %p) with the same key", next, next->value);
120 if (EXPECT_FALSE(item != NULL)) { nbd_free(item); }
124 TRACE("l3", "list_add: attempting to insert item between %p and %p", pred, next);
125 if (EXPECT_TRUE(item == NULL)) { item = node_alloc(key, value); }
127 node_t *other = SYNC_CAS(&pred->next, next, item);
129 TRACE("l3", "list_add: successfully inserted item %p", item, 0);
130 return DOES_NOT_EXIST; // success
132 TRACE("l3", "list_add: failed to change pred's link: expected %p found %p", next, other);
137 uint64_t list_remove (list_t *list, uint64_t key) {
138 TRACE("l3", "list_remove: removing item with key %p from list %p", key, list);
140 node_t *item = find_pred(&pred, list, key, TRUE);
141 if (item->key != key) {
142 TRACE("l3", "list_remove: remove failed, an item with a matching key does not exist in the list", 0, 0);
143 return DOES_NOT_EXIST;
146 // Mark <item> removed. This must be atomic. If multiple threads try to remove the same item
147 // only one of them should succeed.
148 if (EXPECT_FALSE(IS_TAGGED(item->next))) {
149 TRACE("l3", "list_remove: %p is already marked for removal by another thread", item, 0);
150 return DOES_NOT_EXIST;
152 node_t *next = SYNC_FETCH_AND_OR(&item->next, TAG);
153 if (EXPECT_FALSE(IS_TAGGED(next))) {
154 TRACE("l3", "list_remove: lost race -- %p is already marked for removal by another thread", item, 0);
155 return DOES_NOT_EXIST;
158 uint64_t value = item->value;
160 // Unlink <item> from the list.
161 TRACE("l3", "list_remove: link item's pred %p to it's successor %p", pred, next);
163 if ((other = SYNC_CAS(&pred->next, item, next)) != item) {
164 TRACE("l3", "list_remove: unlink failed; pred's link changed from %p to %p", item, other);
165 // By marking the item earlier, we logically removed it. It is safe to leave the item.
166 // Another thread will finish physically removing it from the list.
170 // The thread that completes the unlink should free the memory.
171 nbd_defer_free(item);
175 void list_print (list_t *list) {
179 printf("0x%llx ", item->key);
186 #ifdef MAKE_list_test
189 #include <sys/time.h>
193 #define NUM_ITERATIONS 10000000
195 static volatile int wait_;
196 static long num_threads_;
197 static list_t *list_;
199 void *worker (void *arg) {
200 int id = (int)(size_t)arg;
202 unsigned int rand_seed = id+1;//rdtsc_l();
204 // Wait for all the worker threads to be ready.
205 SYNC_ADD(&wait_, -1);
208 for (int i = 0; i < NUM_ITERATIONS/num_threads_; ++i) {
209 int n = rand_r(&rand_seed);
210 int key = (n & 0xF) + 1;
212 list_add(list_, key, 1);
214 list_remove(list_, key);
223 int main (int argc, char **argv) {
225 //lwt_set_trace_level("m0l0");
227 char* program_name = argv[0];
228 pthread_t thread[MAX_NUM_THREADS];
231 fprintf(stderr, "Usage: %s num_threads\n", program_name);
239 num_threads_ = strtol(argv[1], NULL, 10);
241 fprintf(stderr, "%s: Invalid argument for number of threads\n", program_name);
244 if (num_threads_ <= 0) {
245 fprintf(stderr, "%s: Number of threads must be at least 1\n", program_name);
248 if (num_threads_ > MAX_NUM_THREADS) {
249 fprintf(stderr, "%s: Number of threads cannot be more than %d\n", program_name, MAX_NUM_THREADS);
254 list_ = list_alloc();
256 struct timeval tv1, tv2;
257 gettimeofday(&tv1, NULL);
259 wait_ = num_threads_;
261 for (int i = 0; i < num_threads_; ++i) {
262 int rc = nbd_thread_create(thread + i, i, worker, (void*)(size_t)i);
263 if (rc != 0) { perror("pthread_create"); return rc; }
266 for (int i = 0; i < num_threads_; ++i) {
267 pthread_join(thread[i], NULL);
270 gettimeofday(&tv2, NULL);
271 int ms = (int)(1000000*(tv2.tv_sec - tv1.tv_sec) + tv2.tv_usec - tv1.tv_usec) / 1000;
272 printf("Th:%ld Time:%dms\n", num_threads_, ms);