var curr: ?*Node = pred.nexts[level - 1];
while (curr != null and lessThan(context, curr.?.key.?, key)) {
pred = curr.?;
curr = pred.nexts[level - 1];
// Reference counting
_ = pred.refs.fetchAdd(1, .monotonic);
// WARN: Keep candidate in a separate variable, as every pred.nexts[] call could result
// in a different pointer (as other threads can change them) which results in deadlocks
// as we loose the pointer and can not decrement the refcound properly.
var candidate = pred.nexts[level - 1];
if (candidate != null) {
_ = candidate.?.refs.fetchAdd(1, .monotonic);
}
// TODO: move into the previous if...
// Move the candidate right in this level
while (candidate != null and lessThan(context, candidate.?.key.?, key)) {
// At this point we will drop pred and candidate.next becomes candidate, so shift
// refs right too.
_ = pred.refs.fetchSub(1, .monotonic);
// WARN: Keep next in a separate variable, as every candidate.nexts[] call could result
// in a different pointer (as other threads can change them) which results in deadlocks
// as we loose the pointer and can not decrement the refcound properly.
const next = candidate.?.nexts[level - 1];
if (next != null) {
_ = next.?.refs.fetchAdd(1, .monotonic);
}
// Actually move the candidate right one step
pred = candidate.?;
candidate = next;