Changes between Initial Version and Version 1 of Lock-Free_Data_Structures


Ignore:
Timestamp:
Feb 23, 2008, 4:19:01 AM (16 years ago)
Author:
trac
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Lock-Free_Data_Structures

    v1 v1  
     1[KEEP IN SYNC WITH LOCKFREE.H]
     2
     3overview
     4--------
     5
     6this module provides several implicitly thread-safe data structures.
     7rather than allowing only one thread to access them at a time, their
     8operations are carefully implemented such that they take effect in
     9one atomic step. data consistency problems are thus avoided.
     10this novel approach to synchronization has several advantages:
     11 * deadlocks are impossible;
     12 * overhead due to OS kernel entry is avoided;
     13 * graceful scaling to multiple processors is ensured.
     14
     15
     16mechanism
     17---------
     18
     19the basic primitive that makes this possible is "compare and swap",
     20a CPU instruction that performs both steps atomically. it compares a
     21machine word against the expected value; if equal, the new value is
     22written and an indication returned. otherwise, another thread must have
     23been writing to the same location; the operation is typically retried.
     24
     25this instruction is available on all modern architectures; in some cases,
     26emulation in terms of an alternate primitive (LL/SC) is necessary.
     27
     28
     29memory management
     30-----------------
     31
     32one major remaining problem is how to free no longer needed nodes in the
     33data structure. in general, we want to reclaim their memory for arbitrary use;
     34this isn't safe as long as other threads are still accessing them.
     35
     36the RCU algorithm recognizes that all CPUs having entered a quiescent
     37state means that no threads are still referencing data.
     38lacking such kernel support, we use a similar mechanism - "hazard pointers"
     39are set before accessing data; only if none are pointing to a node can it
     40be freed. until then, they are stored in a per-thread 'waiting list'.
     41
     42this approach has several advantages over previous algorithms
     43(typically involving reference count): the CAS primitive need only
     44operate on single machine words, and space/time overhead is much reduced.
     45
     46
     47usage notes
     48-----------
     49
     50useful "payload" in the data structures is allocated when inserting each
     51item: additional_bytes are appended. rationale: see struct Node definition.
     52
     53since lock-free algorithms are subtle and easy to get wrong, an extensive
     54self-test is included; #define PERFORM_SELF_TEST 1 to activate.
     55
     56
     57terminology
     58-----------
     59
     60; "atomic" : means indivisible; in this case, other CPUs cannot interfere with such an operation.
     61; "race conditions" : are potential data consistency problems resulting from lack of thread synchronization.
     62; "deadlock" : is a state where several threads are waiting on one another and no progress is possible.
     63; "thread-safety" : is understood to mean the preceding two problems do not occur.
     64; "scalability" : is a measure of how efficient synchronization is; overhead should not increase significantly with more processors.
     65; "linearization point" : denotes the time at which an external observer believes a lock-free operation to have taken effect.