From a66cea8f77aeafa8523d28ea63a159826cef38a3 Mon Sep 17 00:00:00 2001 From: KunoiSayami Date: Fri, 18 Feb 2022 11:07:36 +0800 Subject: feat(skiplist): Temporary use spin lock Signed-off-by: KunoiSayami --- db/skiplist.cuh | 14 ++++++++------ db/skiplist_test.cu | 7 ------- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/db/skiplist.cuh b/db/skiplist.cuh index 6f0cc6c..1fd7091 100644 --- a/db/skiplist.cuh +++ b/db/skiplist.cuh @@ -228,11 +228,10 @@ struct SkipList::Node { __device__ bool SetNextSafe(int n, Node *x) { assert(n >= 0); Node * origin = next_[n].load(cuda::memory_order_acquire); - //assert(origin != x); - if (origin == nullptr) { - next_[n].store(x); - x->NoBarrier_SetNext(n, origin); - return true; + assert(origin != x); + if (origin == nullptr && next_[n].compare_exchange_weak(origin, x)) { + x->NoBarrier_SetNext(n, origin); + return true; } if (origin->key < x->key) { return false; @@ -414,7 +413,7 @@ __device__ SkipList::SkipList(Comparator cmp, Arena* arena) head_(NewNode(0 /* any key will do */, kMaxHeight)), max_height_(1), rnd_(0xdeadbeef), - arena_lock_(), + arena_lock_(true), find_lock_() { for (int i = 0; i < kMaxHeight; i++) { head_->SetNext(i, nullptr); @@ -431,6 +430,8 @@ __device__ void SkipList::Insert(const Key& key) { // Our data structure does not allow duplicate insertion assert(x == nullptr || !Equal(key, x->key)); + // TODO: Remove spin lock in feature + this->find_lock_.lock(); int height = RandomHeight(); if (height > GetMaxHeight()) { @@ -462,6 +463,7 @@ __device__ void SkipList::Insert(const Key& key) { /*x->NoBarrier_SetNext(i, prev[i]->NoBarrier_Next(i)); prev[i]->SetNext(i, x);*/ } + this->find_lock_.unlock(); } template diff --git a/db/skiplist_test.cu b/db/skiplist_test.cu index d73e433..e5a1256 100644 --- a/db/skiplist_test.cu +++ b/db/skiplist_test.cu @@ -431,17 +431,10 @@ __global__ void testKeysIsEqualLists(SkipList * skiplist, const SkipList::Iterator iter(skiplist); iter.SeekToFirst(); - bool need_break = false; - int break_after = 20; for (unsigned i = 0; i < SKIPLIST_TEST_SIZE ; i++ ) { assert(iter.Valid()); //printf("%d %lu %lu\n", i, iter.key(), sorted_keys[i]); - if (iter.key() != sorted_keys[i]) { - need_break = true; - } - if (need_break && !--break_after) - break; assert(iter.key() == sorted_keys[i]); iter.Next(); } -- cgit v1.3.1