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.
- Sorting larger file with smaller RAM
- Best-Fit Allocation in Operating System
- First-Fit Allocation in Operating Systems
- C-LOOK Disk Scheduling Algorithm
- Difference between Concurrency and Parallelism
- Microsoft Windows (10) Vs macOS (Mojave)
- Most asked Computer Science Subjects Interview Questions in Amazon, Microsoft, Flipkart
- Difference between Deadlock and Starvation in OS
- Difference Between Security and Protection
- Difference between Application Software and Operating System
- Difference Between Paging and Segmentation
- Difference between Internal and External fragmentation
- User View Vs Hardware View Vs System View of Operating System
- Stack Implementation in Operating System uses by Processor
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.