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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Difference between macOS and FreeBSD
- Difference between macOS and GhostBSD
- Difference between macOS and DragonFlyBSD
- Difference between macOS and OpenBSD
- Difference between OpenSUSE and CentOS
- Difference between Android 1.0 and Android 4.2.2
- Difference between Android 1.0 and Android 4.3
- Difference between Android 1.0 and Android 4.4.4
- Difference between Android 1.0 and Android 6.0.1
- Difference between Android 1.0 and Android 4.4.1
- Difference between Android 1.0 and Android 5.0.1
- Difference between Android 1.0 and Android 4.4.2
- Difference between Android 1.0 and Android 5.0.2
- Difference between Android 1.0 and Android 5.1
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.