Non-blocking linked list

From Wikipedia, the free encyclopedia

A non-blocking linked list is an example of non-blocking data structures designed to implement a linked list in shared memory using synchronization primitives:

Several strategies for implementing non-blocking lists have been suggested.

Review: linked lists[edit]

(Singly) linked lists are fundamental data structures that are widely used as is, or to build other data structures. They consist of "nodes", or "links", that are put in some order indicated by a "next" pointer on each node. The last node in the list (the "tail") has a nil next pointer. The first node (the "head") is a sentinel: it stores no interesting information and is only used for its next pointer.

The operations that must be supported on lists are as follows.

  • Given a node n that is not yet part of the list, and a pointer p to a node in the list (perhaps the head), insert n after p.
  • Given a pointer p, delete p.next from the list.

Both operations must support concurrent use: two or more threads of execution must be able to perform insertions and deletions without interfering with each other's work (see diagram).

Harris's solution[edit]

A linked list in an inconsistent state, caused by application of the naive lock-free deletion algorithm. Dotted lines are links that exist in intermediate states; solid lines represent the final state. Deletion of the node holding a has executed simultaneously with insertion of b after a, causing the insertion to be undone.

In a 2001 paper, Harris gives a solution to concurrent maintenance of ordered linked list that is non-blocking, using a compare-and-swap (cas) primitive.[1] Insertion of n after p is simple:

  1. next ← p.next
  2. n.next ← next
  3. cas(address-of(p.next), next, n)
  4. If the cas was not successful, go back to 1.

Deletion of p.next is more involved. The naive solution of resetting this pointer with a single CAS runs the risk of losing data when another thread is simultaneously inserting (see diagram). This is specific case of the ABA problem. Instead, two invocations of cas are needed for a correct algorithm. The first marks the pointer p.next as deleted, changing its value but in such a way that the original pointer can still be reconstructed. The second actually deletes the node by resetting p.next.

Operations on lock-free linked lists[edit]

Insert[edit]

Delete (Naive approach)[edit]

  • search for the right spot in the list
  • delete using Compare-and-swap

Contains[edit]

  • search for a specific value in the list and return whether it is present or not
  • this is a read only operation, does not pose any concurrency issues

Problems[edit]

Concurrent insert and delete[edit]

  • a process deleting node B requires an atomic action on the node's predecessor
  • concurrently another process tries to insert a node C after node B (B.next=C)
  • node B is deleted from the list but C is gone along with it

Solutions[edit]

  • Harris [1]
    • place a 'mark' in the next pointer of the soon-to-be deleted node
    • fail when we try to CAS the 'mark'
    • when detected go back to start of the list and restart
  • Zhang et al.[2]
    • search the list to see if the value to be deleted exists, if exists mark the node logically deleted
    • a subsequent traversal of the list will do garbage collection of logically deleted nodes

Concurrent deletions[edit]

  • two processes concurrently delete an adjacent node: node B and node C respectively
  • the delete of node C is undone by the delete of node B

Solutions[edit]

Valois [3]

  • make use of auxiliary nodes which contain only a next field
  • each regular node must have an auxiliary node as its predecessor and successor
  • deletion will result in an extra auxiliary node being left behind, which means the delete will have to keep trying to clean up the extra auxiliary nodes
  • use an extra 'back_link' field so the delete operation can traverse back to a node that has not been deleted from the list

Further reading[edit]

  • High Performance Dynamic Lock-Free Hash Tables and List-Based Sets, Maged M. Michael
  • Fomitchev, Mikhail; Ruppert, Eric (2004). Lock-free linked lists and skip lists (PDF). Proc. Annual ACM Symp. on Principles of Distributed Computing (PODC). pp. 50–59. doi:10.1145/1011767.1011776. ISBN 1581138024.
  • Two-handed emulation: how to build non-blocking implementations of complex data-structures using DCAS, Michael Greewald
  • Highly-Concurrent Multi-word Synchronization, Hagit Attiya, Eshcar Hillel
  • Lock-free deques and doubly linked lists, Håkan Sundell, Philippas Tsigas

References[edit]

  1. ^ a b Harris, T. (2001), A Pragmatic Implementation of Non-Blocking Linked Lists, DISC '01 Proceedings of the 15th International Conference on Distributed Computing, Springer-Verlag London, UK, pp. 300–314, ISBN 9783540426059{{citation}}: CS1 maint: location missing publisher (link)
  2. ^ Zhang, K.; Zhao, Y.; Yang, Y.; Liu, Y.; Spear, M. (2013), Practical Non-Blocking Unordered Lists, DISC '13 Proceedings of the 27th International Conference on Distributed Computing, Jerusalem, Israel{{citation}}: CS1 maint: location missing publisher (link)
  3. ^ Valois, J. (1995), "Lock-Free Linked Lists Using Compare-and-Swap", Proceedings of the fourteenth annual ACM symposium on Principles of distributed computing - PODC '95, PODC '95 Proceedings of the fourteenth annual ACM symposium on Principles of distributed computing, New York, NY, USA, pp. 214–222, doi:10.1145/224964.224988, ISBN 0897917103, S2CID 2047186{{citation}}: CS1 maint: location missing publisher (link)