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
25 node_t *node_alloc (uint64_t key, uint64_t value) {
26 node_t *item = (node_t *)nbd_malloc(sizeof(node_t));
27 memset(item, 0, sizeof(node_t));
33 list_t *ll_alloc (void) {
34 list_t *ll = (list_t *)nbd_malloc(sizeof(list_t));
35 ll->head = node_alloc((uint64_t)-1, 0);
36 ll->head->next = NULL;
40 static node_t *find_pred (node_t **pred_ptr, list_t *ll, uint64_t key, int help_remove) {
41 node_t *pred = ll->head;
42 node_t *item = pred->next;
43 TRACE("l3", "find_pred: searching for key %p in ll (head is %p)", key, pred);
45 while (item != NULL) {
46 node_t *next = item->next;
47 TRACE("l3", "find_pred: visiting item %p (next %p)", item, next);
48 TRACE("l3", "find_pred: key %p", item->key, item->value);
50 // Marked items are logically removed, but not unlinked yet.
51 while (EXPECT_FALSE(IS_TAGGED(next))) {
53 // Skip over partially removed items.
55 item = (node_t *)STRIP_TAG(item->next);
56 if (EXPECT_FALSE(item == NULL))
62 // Unlink partially removed items.
64 if ((other = SYNC_CAS(&pred->next, item, STRIP_TAG(next))) == item) {
65 item = (node_t *)STRIP_TAG(next);
66 if (EXPECT_FALSE(item == NULL))
69 TRACE("l3", "find_pred: unlinked item %p from pred %p", item, pred);
70 TRACE("l3", "find_pred: now item is %p next is %p", item, next);
72 // The thread that completes the unlink should free the memory.
73 nbd_defer_free(other);
75 TRACE("l3", "find_pred: lost race to unlink from pred %p; its link changed to %p", pred, other);
77 return find_pred(pred_ptr, ll, key, help_remove); // retry
79 if (EXPECT_FALSE(item == NULL))
85 if (EXPECT_FALSE(item == NULL))
88 // If we reached the key (or passed where it should be), we found the right predesssor
89 if (item->key >= key) {
90 TRACE("l3", "find_pred: found pred %p item %p", pred, item);
91 if (pred_ptr != NULL) {
102 // <key> is not in <ll>.
103 if (pred_ptr != NULL) {
109 // Fast find. Do not help unlink partially removed nodes and do not return the found item's predecessor.
110 uint64_t ll_lookup (list_t *ll, uint64_t key) {
111 TRACE("l3", "ll_lookup: searching for key %p in ll %p", key, ll);
112 node_t *item = find_pred(NULL, ll, key, FALSE);
114 // If we found an <item> matching the <key> return its value.
115 return (item && item->key == key) ? item->value : DOES_NOT_EXIST;
118 // Insert the <key>, if it doesn't already exist in <ll>
119 uint64_t ll_add (list_t *ll, uint64_t key, uint64_t value) {
120 TRACE("l3", "ll_add: inserting key %p value %p", key, value);
124 node_t *next = find_pred(&pred, ll, key, TRUE);
126 // If a node matching <key> already exists in <ll>, return its value.
127 if (next != NULL && next->key == key) {
128 TRACE("l3", "ll_add: there is already an item %p (value %p) with the same key", next, next->value);
129 if (EXPECT_FALSE(item != NULL)) { nbd_free(item); }
133 TRACE("l3", "ll_add: attempting to insert item between %p and %p", pred, next);
134 if (EXPECT_TRUE(item == NULL)) { item = node_alloc(key, value); }
136 node_t *other = SYNC_CAS(&pred->next, next, item);
138 TRACE("l3", "ll_add: successfully inserted item %p", item, 0);
139 return DOES_NOT_EXIST; // success
141 TRACE("l3", "ll_add: failed to change pred's link: expected %p found %p", next, other);
146 uint64_t ll_remove (list_t *ll, uint64_t key) {
147 TRACE("l3", "ll_remove: removing item with key %p from ll %p", key, ll);
149 node_t *item = find_pred(&pred, ll, key, TRUE);
150 if (item == NULL || item->key != key) {
151 TRACE("l3", "ll_remove: remove failed, an item with a matching key does not exist in the ll", 0, 0);
152 return DOES_NOT_EXIST;
155 // Mark <item> removed. This must be atomic. If multiple threads try to remove the same item
156 // only one of them should succeed.
157 if (EXPECT_FALSE(IS_TAGGED(item->next))) {
158 TRACE("l3", "ll_remove: %p is already marked for removal by another thread", item, 0);
159 return DOES_NOT_EXIST;
161 node_t *next = SYNC_FETCH_AND_OR(&item->next, TAG);
162 if (EXPECT_FALSE(IS_TAGGED(next))) {
163 TRACE("l3", "ll_remove: lost race -- %p is already marked for removal by another thread", item, 0);
164 return DOES_NOT_EXIST;
167 uint64_t value = item->value;
169 // Unlink <item> from <ll>.
170 TRACE("l3", "ll_remove: link item's pred %p to it's successor %p", pred, next);
172 if ((other = SYNC_CAS(&pred->next, item, next)) != item) {
173 TRACE("l3", "ll_remove: unlink failed; pred's link changed from %p to %p", item, other);
174 // By marking the item earlier, we logically removed it. It is safe to leave the item.
175 // Another thread will finish physically removing it from the ll.
179 // The thread that completes the unlink should free the memory.
180 nbd_defer_free(item);
184 void ll_print (list_t *ll) {
186 item = ll->head->next;
188 printf("0x%llx ", item->key);
195 #ifdef MAKE_list_test
198 #include <sys/time.h>
202 #define NUM_ITERATIONS 10000000
204 static volatile int wait_;
205 static long num_threads_;
208 void *worker (void *arg) {
210 // Wait for all the worker threads to be ready.
211 SYNC_ADD(&wait_, -1);
214 for (int i = 0; i < NUM_ITERATIONS/num_threads_; ++i) {
215 unsigned r = nbd_rand();
229 int main (int argc, char **argv) {
231 //lwt_set_trace_level("m0l0");
233 char* program_name = argv[0];
234 pthread_t thread[MAX_NUM_THREADS];
237 fprintf(stderr, "Usage: %s num_threads\n", program_name);
245 num_threads_ = strtol(argv[1], NULL, 10);
247 fprintf(stderr, "%s: Invalid argument for number of threads\n", program_name);
250 if (num_threads_ <= 0) {
251 fprintf(stderr, "%s: Number of threads must be at least 1\n", program_name);
254 if (num_threads_ > MAX_NUM_THREADS) {
255 fprintf(stderr, "%s: Number of threads cannot be more than %d\n", program_name, MAX_NUM_THREADS);
262 struct timeval tv1, tv2;
263 gettimeofday(&tv1, NULL);
265 wait_ = num_threads_;
267 for (int i = 0; i < num_threads_; ++i) {
268 int rc = nbd_thread_create(thread + i, i, worker, (void*)(size_t)i);
269 if (rc != 0) { perror("pthread_create"); return rc; }
272 for (int i = 0; i < num_threads_; ++i) {
273 pthread_join(thread[i], NULL);
276 gettimeofday(&tv2, NULL);
277 int ms = (int)(1000000*(tv2.tv_sec - tv1.tv_sec) + tv2.tv_usec - tv1.tv_usec) / 1000;
279 printf("Th:%ld Time:%dms\n", num_threads_, ms);