Skip to content
Related Articles

Related Articles

Read-Copy Update (RCU)

View Discussion
Improve Article
Save Article
  • Last Updated : 13 Jun, 2019
View Discussion
Improve Article
Save Article

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:

struct Node
  int key, val;       
  /* pointer to the "next" and "prev"(previous) node */         
  struct Node *next, *prev;    
/* Searches for a given key node in a list li and, 
 returns the value corresponding to that key*/
int search(struct Node *li, int key)  
    int ret= -1;
    /* Disabling the preempt */ 
      /* The required key is found */
      if(li->key == key)   
        /* Assigning "ret" the value of that particular key */
        ret = li->val;     
     /* moving on to the next list value */
    /* Enabling the interrupt which was disabled earlier */
    /* Return the value of the key that is found */
    return ret;       
/* Deletes a given node */
void delete (struct Node *node)     
  /* Take a spin lock on the given node */ 
  node->next->prev = node->prev;   
  node->prev->next = node->next;
  /* Unlock the lock on the node, 
   because the deletion has been done */

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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!