]> pd.if.org Git - nbds/blob - map/list.c
more header file refactoring
[nbds] / map / list.c
1 /* 
2  * Written by Josh Dybnis and released to the public domain, as explained at
3  * http://creativecommons.org/licenses/publicdomain
4  *
5  * Harris-Michael lock-free list-based set
6  * http://www.research.ibm.com/people/m/michael/spaa-2002.pdf
7  */
8
9 #include <stdio.h>
10 #include <string.h>
11
12 #include "common.h"
13 #include "list.h"
14 #include "mem.h"
15
16 typedef struct node {
17     void *key;
18     uint64_t val;
19     struct node *next;
20 } node_t;
21
22 struct ll {
23     node_t *head;
24     const datatype_t *key_type;
25 };
26
27 static node_t *node_alloc (void *key, uint64_t val) {
28     node_t *item = (node_t *)nbd_malloc(sizeof(node_t));
29     item->key = key;
30     item->val = val;
31     return item;
32 }
33
34 list_t *ll_alloc (const datatype_t *key_type) {
35     list_t *ll = (list_t *)nbd_malloc(sizeof(list_t));
36     ll->key_type = key_type;
37     ll->head = node_alloc(NULL, 0);
38     ll->head->next = NULL;
39     return ll;
40 }
41
42 void ll_free (list_t *ll) {
43     node_t *item = ll->head->next;
44     while (item) {
45         node_t *next = (node_t *)STRIP_TAG(item->next);
46         nbd_free(item);
47         item = next;
48     }
49 }
50
51 uint64_t ll_count (list_t *ll) {
52     uint64_t count = 0;
53     node_t *item = ll->head->next;
54     while (item) {
55         count++;
56         item = (node_t *)STRIP_TAG(item->next);
57     }
58     return count;
59 }
60
61 static int find_pred (node_t **pred_ptr, node_t **item_ptr, list_t *ll, void *key, int help_remove) {
62     node_t *pred = ll->head;
63     node_t *item = pred->next;
64     TRACE("l2", "find_pred: searching for key %p in list (head is %p)", key, pred);
65
66     while (item != NULL) {
67         node_t *next = item->next;
68
69         // A tag means an item is logically removed but not physically unlinked yet.
70         while (EXPECT_FALSE(IS_TAGGED(next))) {
71
72             // Skip over logically removed items.
73             if (!help_remove) {
74                 item = (node_t *)STRIP_TAG(item->next);
75                 if (EXPECT_FALSE(item == NULL))
76                     break;
77                 TRACE("l3", "find_pred: skipping marked item %p (next is %p)", item, next);
78                 next = item->next;
79                 continue;
80             }
81
82             // Unlink logically removed items.
83             node_t *other;
84             TRACE("l3", "find_pred: unlinking marked item %p next is %p", item, next);
85             if ((other = SYNC_CAS(&pred->next, item, STRIP_TAG(next))) == item) {
86                 TRACE("l2", "find_pred: unlinked item %p from pred %p", item, pred);
87                 item = (node_t *)STRIP_TAG(next);
88                 if (EXPECT_FALSE(item == NULL))
89                     break;
90                 next = item->next;
91                 TRACE("l3", "find_pred: now current item is %p next is %p", item, next);
92
93                 // The thread that completes the unlink should free the memory.
94                 if (ll->key_type != NULL) {
95                     nbd_defer_free((void*)other->key);
96                 }
97                 nbd_defer_free(other);
98             } else {
99                 TRACE("l2", "find_pred: lost a race to unlink item %p from pred %p", item, pred);
100                 TRACE("l2", "find_pred: pred's link changed to %p", other, 0);
101                 if (IS_TAGGED(other))
102                     return find_pred(pred_ptr, item_ptr, ll, key, help_remove); // retry
103                 item = other;
104                 if (EXPECT_FALSE(item == NULL))
105                     break;
106                 next = item->next;
107             }
108         }
109
110         if (EXPECT_FALSE(item == NULL))
111             break;
112
113         TRACE("l3", "find_pred: visiting item %p (next is %p)", item, next);
114         TRACE("l4", "find_pred: key %p val %p", item->key, item->val);
115
116         int d;
117         if (EXPECT_TRUE(ll->key_type == NULL)) {
118             d = (uint64_t)item->key - (uint64_t)key;
119         } else {
120             d = ll->key_type->cmp(item->key, key);
121         }
122
123         // If we reached the key (or passed where it should be), we found the right predesssor
124         if (d >= 0) {
125             if (pred_ptr != NULL) {
126                 *pred_ptr = pred;
127             }
128             *item_ptr = item;
129             if (d == 0) {
130                 TRACE("l2", "find_pred: found matching item %p in list, pred is %p", item, pred);
131                 return TRUE;
132             } 
133             TRACE("l2", "find_pred: found proper place for key %p in list, pred is %p", key, pred);
134             return FALSE;
135         }
136
137         pred = item;
138         item = next;
139     }
140
141     // <key> is not in <ll>.
142     if (pred_ptr != NULL) {
143         *pred_ptr = pred;
144     }
145     *item_ptr = NULL;
146     TRACE("l2", "find_pred: reached end of list. last item is %p", pred, 0);
147     return FALSE;
148 }
149
150 // Fast find. Do not help unlink partially removed nodes and do not return the found item's predecessor.
151 uint64_t ll_lookup (list_t *ll, void *key) {
152     TRACE("l1", "ll_lookup: searching for key %p in list %p", key, ll);
153     node_t *item;
154     int found = find_pred(NULL, &item, ll, key, FALSE);
155
156     // If we found an <item> matching the key return its value.
157     if (found) {
158         uint64_t val = item->val;
159         if (val != DOES_NOT_EXIST) {
160             TRACE("l1", "ll_lookup: found item %p. val %p. returning item", item, item->val);
161             return val;
162         }
163     }
164
165     TRACE("l1", "ll_lookup: no item in the list matched the key", 0, 0);
166     return DOES_NOT_EXIST;
167 }
168
169 uint64_t ll_cas (list_t *ll, void *key, uint64_t expectation, uint64_t new_val) {
170     TRACE("l1", "ll_cas: key %p list %p", key, ll);
171     TRACE("l1", "ll_cas: expectation %p new value %p", expectation, new_val);
172     ASSERT((int64_t)new_val > 0);
173
174     do {
175         node_t *pred, *old_item;
176         int found = find_pred(&pred, &old_item, ll, key, TRUE);
177         if (!found) {
178
179             // There was not an item in the list that matches the key. 
180             if (EXPECT_FALSE((int64_t)expectation > 0 || expectation == CAS_EXPECT_EXISTS)) {
181                 TRACE("l1", "ll_cas: the expectation was not met, the list was not changed", 0, 0);
182                 return DOES_NOT_EXIST; // failure
183             }
184
185             ASSERT(expectation == CAS_EXPECT_DOES_NOT_EXIST || expectation == CAS_EXPECT_WHATEVER);
186
187             // Create a new item and insert it into the list.
188             TRACE("l2", "ll_cas: attempting to insert item between %p and %p", pred, pred->next);
189             void *new_key  = (ll->key_type == NULL) ? key : ll->key_type->clone(key);
190             node_t *new_item = node_alloc(new_key, new_val);
191             node_t *next = new_item->next = old_item;
192             node_t *other = SYNC_CAS(&pred->next, next, new_item);
193             if (other == next) {
194                 TRACE("l1", "ll_cas: successfully inserted new item %p", new_item, 0);
195                 return DOES_NOT_EXIST; // success
196             }
197
198             // Lost a race. Failed to insert the new item into the list.
199             TRACE("l1", "ll_cas: lost a race. CAS failed. expected pred's link to be %p but found %p", next, other);
200             if (ll->key_type != NULL) {
201                 nbd_free(new_key);
202             }
203             nbd_free(new_item);
204             continue; // retry
205         }
206
207         // Found an item in the list that matches the key.
208         uint64_t old_item_val = old_item->val;
209         do {
210             // If the item's value is DOES_NOT_EXIST it means another thread removed the node out from under us.
211             if (EXPECT_FALSE(old_item_val == DOES_NOT_EXIST)) {
212                 TRACE("l2", "ll_cas: lost a race, found an item but another thread removed it. retry", 0, 0);
213                 break; // retry
214             }
215
216             if (EXPECT_FALSE(expectation == CAS_EXPECT_DOES_NOT_EXIST)) {
217                 TRACE("l1", "ll_cas: found an item %p in the list that matched the key. the expectation was "
218                         "not met, the list was not changed", old_item, old_item_val);
219                 return old_item_val; // failure
220             }
221
222             // Use a CAS and not a SWAP. If the node is in the process of being removed and we used a SWAP, we could
223             // replace DOES_NOT_EXIST with our value. Then another thread that is updating the value could think it
224             // succeeded and return our value even though we indicated that the node has been removed. If the CAS 
225             // fails it means another thread either removed the node or updated its value.
226             uint64_t ret_val = SYNC_CAS(&old_item->val, old_item_val, new_val);
227             if (ret_val == old_item_val) {
228                 TRACE("l1", "ll_cas: the CAS succeeded. updated the value of the item", 0, 0);
229                 return ret_val; // success
230             }
231             TRACE("l2", "ll_cas: lost a race. the CAS failed. another thread changed the item's value", 0, 0);
232
233             old_item_val = ret_val;
234         } while (1);
235     } while (1);
236 }
237
238 uint64_t ll_remove (list_t *ll, void *key) {
239     TRACE("l1", "ll_remove: removing item with key %p from list %p", key, ll);
240     node_t *pred;
241     node_t *item;
242     int found = find_pred(&pred, &item, ll, key, TRUE);
243     if (!found) {
244         TRACE("l1", "ll_remove: remove failed, an item with a matching key does not exist in the list", 0, 0);
245         return DOES_NOT_EXIST;
246     }
247
248     // Mark <item> removed. If multiple threads try to remove the same item only one of them should succeed.
249     node_t *next;
250     node_t *old_next = item->next;
251     do {
252         next = old_next;
253         old_next = SYNC_CAS(&item->next, next, TAG_VALUE(next));
254         if (IS_TAGGED(old_next)) {
255             TRACE("l1", "ll_remove: lost a race -- %p is already marked for removal by another thread", item, 0);
256             return DOES_NOT_EXIST;
257         }
258     } while (next != old_next);
259     TRACE("l2", "ll_remove: logically removed item %p", item, 0);
260     ASSERT(IS_TAGGED(item->next));
261
262     // Atomically swap out the item's value in case another thread is updating the item while we are 
263     // removing it. This establishes which operation occurs first logically, the update or the remove. 
264     uint64_t val = SYNC_SWAP(&item->val, DOES_NOT_EXIST); 
265     TRACE("l2", "ll_remove: replaced item's val %p with DOES_NOT_EXIT", val, 0);
266
267     // Unlink <item> from <ll>. If we lose a race to another thread just back off. It is safe to leave the
268     // item logically removed for a later call (or some other thread) to physically unlink. By marking the
269     // item earlier, we logically removed it. 
270     TRACE("l2", "ll_remove: unlink the item by linking its pred %p to its successor %p", pred, next);
271     node_t *other;
272     if ((other = SYNC_CAS(&pred->next, item, next)) != item) {
273         TRACE("l1", "ll_remove: unlink failed; pred's link changed from %p to %p", item, other);
274         return val;
275     } 
276
277     // The thread that completes the unlink should free the memory.
278     if (ll->key_type != NULL) {
279         nbd_defer_free(item->key);
280     }
281     nbd_defer_free(item);
282     TRACE("l1", "ll_remove: successfully unlinked item %p from the list", item, 0);
283     return val;
284 }
285
286 void ll_print (list_t *ll) {
287     node_t *item;
288     item = ll->head->next;
289     int i = 0;
290     while (item) {
291         node_t *next = item->next;
292         if (IS_TAGGED(item)) {
293             printf("*");
294         }
295         printf("%p:%p ", item, item->key);
296         fflush(stdout);
297         item = (node_t *)STRIP_TAG(next);
298         if (i++ > 30) {
299             printf("...");
300             break;
301         }
302     }
303     printf("\n");
304 }