In my opinion, you want to implement rwlock with sema, which is a data member of Segment, but there is something wrong with it. Consider this situation: the first thread (T1) goes to C1(showed in bellow codes) and sleeps, and the second thread(T2) splits the same segment with INPLACE mode and changes the pattern. When T1 wakes up, it inserts the key-value into that segment, but, this key-value may not belong to that segment since the pattern may be not right, and it will be treated as invalid record and removed, which is an issue.
int Segment::Insert(Key_t& key, Value_t value, size_t loc, size_t key_hash) {
#ifdef INPLACE
if (sema == -1) return 2;
if ((key_hash >> (8*sizeof(key_hash)-local_depth)) != pattern) return 2;
auto lock = sema; #C1
int ret = 1;
while (!CAS(&sema, &lock, lock+1)) {
lock = sema;
}
.....
In my opinion, you want to implement rwlock with sema, which is a data member of Segment, but there is something wrong with it. Consider this situation: the first thread (T1) goes to C1(showed in bellow codes) and sleeps, and the second thread(T2) splits the same segment with INPLACE mode and changes the pattern. When T1 wakes up, it inserts the key-value into that segment, but, this key-value may not belong to that segment since the pattern may be not right, and it will be treated as invalid record and removed, which is an issue.