Re: [PATCH 2/6] locking: Introduce range reader/writer lock

From: Davidlohr Bueso
Date: Tue May 16 2017 - 18:19:23 EST


On Mon, 15 May 2017, Peter Zijlstra wrote:

On Mon, May 15, 2017 at 02:07:21AM -0700, Davidlohr Bueso wrote:

+ * Fairness and freedom of starvation are guaranteed by the lack of lock
+ * stealing, thus range locks depend directly on interval tree semantics.
+ * This is particularly for iterations, where the key for the rbtree is
+ * given by the interval's low endpoint,


So suppose the lock is held at [a,n], and I want to acquire [g,z], this
conflicts, therefore I wait.

Ok, then, ref [g,z] = 1, ref [a,n] = 0 (lock owner). Per below,
at this point the tree will overlap with anything between [a,z],
which is the world.


While I wait, someone else comes in at [b,m], they too wait.

[b,m] intersects with both nodes above, thus ref [b,m] = 2.


[a,n] is released, per ordering [b,m] acquires, I still wait.

Now:
ref [g,z] = 0
ref [b,m] = 1

So due to reference counting [g,z] is acquired, despite [b,m]
being _put() before [g,z].

[a,n] returns to wait.

Similar to the [b,m] case, when [a,n] comes back, it too will
get a ref = 2 and hence "go back in line". Iow, lock order does
depend on have fifo semantics among contended ranges.


[b,m] releases, does the iteration then restart and grant it to [a,n] or
will I (at [g,z]) finally acquire?


Since the code always does range_interval_tree_foreach() it would appear
to me [b,m] will always win and [g,z] could be made to wait
indefinitely (by always contending with another range that has a lower
starting point).



and duplicates are walked as it
+ * would an inorder traversal of the tree.

Are duplicates ordered in FIFO ? Afaict the above is free of actual
semantics.

This will strictly depend on the rotation when you have duplicates
when more nodes are added later. But again that's the order of
walking the tree.

Thanks,
Davidlohr