* See also Kir Fraser's dissertation "Practical Lock Freedom".
* www.cl.cam.ac.uk/techreports/UCAM-CL-TR-579.pdf
*
- * This code is written for the x86 memory-model. The algorithim depends on certain stores and
- * loads being ordered. Be careful, this code won't work correctly on platforms with weaker memory
- * models if you don't add memory barriers in the right places.
+ * I've generalized the data structure to support update operations like set() and CAS() in addition to
+ * the normal add() and remove() operations.
+ *
+ * Warning: This code is written for the x86 memory-model. The algorithim depends on certain stores
+ * and loads being ordered. This code won't work correctly on platforms with weaker memory models if
+ * you don't add memory barriers in the right places.
*/
#include <stdio.h>
#include <string.h>
-#define ENABLE_TRACE
-
#include "common.h"
#include "runtime.h"
#include "mlocal.h"
+#include "skiplist.h"
#include "mem.h"
-#include "tls.h"
-// Setting MAX_LEVEL to 0 essentially makes this data structure the Harris-Michael lock-free list
-// (in list.c).
+// Setting MAX_LEVEL to 0 essentially makes this data structure the Harris-Michael lock-free list (in list.c).
#define MAX_LEVEL 31
typedef struct node {
struct sl {
node_t *head;
- cmp_fun_t cmp_fun;
- clone_fun_t clone_fun;
+ const datatype_t *key_type;
};
static const map_impl_t sl_map_impl = {
return item;
}
-skiplist_t *sl_alloc (cmp_fun_t cmp_fun, hash_fun_t hash_fun, clone_fun_t clone_fun) {
+skiplist_t *sl_alloc (const datatype_t *key_type) {
skiplist_t *sl = (skiplist_t *)nbd_malloc(sizeof(skiplist_t));
- sl->cmp_fun = cmp_fun;
- sl->clone_fun = clone_fun;
+ sl->key_type = key_type;
sl->head = node_alloc(MAX_LEVEL, NULL, 0);
memset(sl->head->next, 0, (MAX_LEVEL+1) * sizeof(skiplist_t *));
return sl;
// The thread that completes the unlink should free the memory.
if (level == 0) {
- if (sl->clone_fun != NULL) {
+ if (sl->key_type != NULL) {
nbd_defer_free((void*)other->key);
}
nbd_defer_free(other);
TRACE("s4", "find_preds: visiting item %p (next is %p)", item, next);
TRACE("s4", "find_preds: key %p val %p", STRIP_TAG(item->key), item->val);
- if (EXPECT_TRUE(sl->cmp_fun == NULL)) {
+ if (EXPECT_TRUE(sl->key_type == NULL)) {
d = (uint64_t)item->key - (uint64_t)key;
} else {
- d = sl->cmp_fun(item->key, key);
+ d = sl->key_type->cmp(item->key, key);
}
if (d >= 0) {
// First insert <new_item> into the bottom level.
TRACE("s3", "sl_cas: attempting to insert item between %p and %p", preds[0], nexts[0]);
- void *new_key = (sl->clone_fun == NULL) ? key : sl->clone_fun(key);
+ void *new_key = (sl->key_type == NULL) ? key : sl->key_type->clone(key);
new_item = node_alloc(n, new_key, new_val);
node_t *pred = preds[0];
node_t *next = new_item->next[0] = nexts[0];
break; // success
}
TRACE("s3", "sl_cas: failed to change pred's link: expected %p found %p", next, other);
- if (sl->clone_fun != NULL) {
+ if (sl->key_type != NULL) {
nbd_free(new_key);
}
nbd_free(new_item);
int d = -1;
if (!IS_TAGGED(other)) {
- if (EXPECT_TRUE(sl->cmp_fun == NULL)) {
+ if (EXPECT_TRUE(sl->key_type == NULL)) {
d = (uint64_t)item->key - (uint64_t)other->key;
} else {
- d = sl->cmp_fun(item->key, other->key);
+ d = sl->key_type->cmp(item->key, other->key);
}
}
if (d > 0) {
TRACE("s1", "sl_remove: marked item %p removed at level 0", item, 0);
// Atomically swap out the item's value in case another thread is updating the item while we are
- // removing it. This establishes which one occurs first, the update or the remove.
+ // removing it. This establishes which operation occurs first logically, the update or the remove.
uint64_t val = SYNC_SWAP(&item->val, DOES_NOT_EXIST);
TRACE("s2", "sl_remove: replaced item %p's value with DOES_NOT_EXIT", item, 0);
if (SYNC_CAS(&pred->next[0], item, STRIP_TAG(next))) {
TRACE("s2", "sl_remove: unlinked item %p from the skiplist at level 0", item, 0);
// The thread that completes the unlink should free the memory.
- if (sl->clone_fun != NULL) {
+ if (sl->key_type != NULL) {
nbd_defer_free(item->key);
}
nbd_defer_free(item);
printf("\n");
fflush(stdout);
}
-
node_t *item = sl->head;
int i = 0;
while (item) {