Read-Copy Update (RCU)
A Lock Primitive which is widely used in Linux kernel is read-copy update (RCU) lock. It is a synchronization mechanism which was added to the Linux Kernel in October 2002. It has achieved improvements by enabling reads to occur simultaneously with updates. It supports concurrency between multiple readers and a single updater. There are no overheads in the RCU’s read-side primitive. It is one of the safest data structure designed to operate safely during simultaneous access because it uses cache line and memory efficiently.
Read-Copy Update (CPU) locks have a lock-free read-critical section. RCU locks work for workloads where a writer is compatible with the lock-free readers. Preemption is not allowed in the read critical section.
Consider the following code:
In the above code, the delete update of next (i.e., node->prev->next = node->next) is atomic, because the store operations are atomic. If a delete is executing concurrently, the search routine will either see the new list or the old list depending on whether next is updated or not. Also, the delete operation can execute concurrently with the search operation because the updates that matter for search is the update of the next field of a linked list node.
In read-copy update scheme, a free operation is delayed until the updater is 100 % sure that the other threads do not have a reference to the object that is going to be deleted. As preemption is not allowed in the read-critical section, invocation of the RCU schedule suggests that the particular core is not executing a read-critical section.
In the RCU scheme, a writer calls rcu_free instead of free .rcu_free ensures that all the readers have exited atleast once from their critical section after the rcu_free is called. This check ensures that the other threads do not have an active reference to the object which is going to be deleted.
This is done, when rcu_free calls wait_for_rcu which forces a quiescent state on every core.wait_for_rcu schedules the current thread on all cores to enforce a safe point for a pending free.
Advantages and Disadvantages:
RCU locks cannot be used for a data structure like Tree because a Tree search may depend on multiple updates that cannot be done atomically.
One disadvantage of read-copy update locks is that it requires disabling of preemption due to which they cannot be used in user-mode.