Update emails/links due to project site transition
[platform/upstream/libatomic_ops.git] / doc / README_stack.txt
1 Note that the AO_stack implementation is licensed under the GPL,
2 unlike the lower level routines.
3
4 The header file atomic_ops_stack.h defines a linked stack abstraction.
5 Stacks may be accessed by multiple concurrent threads.  The implementation
6 is 1-lock-free, i.e. it will continue to make progress if at most one
7 thread becomes inactive while operating on the data structure.
8
9 (The implementation can be built to be N-lock-free for any given N.  But that
10 seems to rarely be useful, especially since larger N involve some slowdown.)
11
12 This makes it safe to access these data structures from non-reentrant
13 signal handlers, provided at most one non-signal-handler thread is
14 accessing the data structure at once.  This latter condition can be
15 ensured by acquiring an ordinary lock around the non-handler accesses
16 to the data structure.
17
18 For details see:
19
20 Hans-J. Boehm, "An Almost Non-Blocking Stack", PODC 2004,
21 http://portal.acm.org/citation.cfm?doid=1011767.1011774
22 (This is not exactly the implementation described there, since the
23 interface was cleaned up in the interim.  But it should perform
24 very similarly.)
25
26 We use a fully lock-free implementation when the underlying hardware
27 makes that less expensive, i.e. when we have a double-wide compare-and-swap
28 operation available.  (The fully lock-free implementation uses an AO_t-
29 sized version count, and assumes it does not wrap during the time any
30 given operation is active.  This seems reasonably safe on 32-bit hardware,
31 and very safe on 64-bit hardware.) If a fully lock-free implementation
32 is used, the macro AO_STACK_IS_LOCK_FREE will be defined.
33
34 The implementation is interesting only because it allows reuse of
35 existing nodes.  This is necessary, for example, to implement a memory
36 allocator.
37
38 Since we want to leave the precise stack node type up to the client,
39 we insist only that each stack node contains a link field of type AO_t.
40 When a new node is pushed on the stack, the push operation expects to be
41 passed the pointer to this link field, which will then be overwritten by
42 this link field.  Similarly, the pop operation returns a pointer to the
43 link field of the object that previously was on the top of the stack.
44
45 The cleanest way to use these routines is probably to define the stack node
46 type with an initial AO_t link field, so that the conversion between the
47 link-field pointer and the stack element pointer is just a compile-time
48 cast.  But other possibilities exist.  (This would be cleaner in C++ with
49 templates.)
50
51 A stack is represented by an AO_stack_t structure.  (This is normally
52 2 or 3 times the size of a pointer.)  It may be statically initialized
53 by setting it to AO_STACK_INITIALIZER, or dynamically initialized to
54 an empty stack with AO_stack_init.  There are only three operations for
55 accessing stacks:
56
57 void AO_stack_init(AO_stack_t *list);
58 void AO_stack_push_release(AO_stack_t *list, AO_t *new_element);
59 AO_t * AO_stack_pop_acquire(volatile AO_stack_t *list);
60
61 We require that the objects pushed as list elements remain addressable
62 as long as any push or pop operation are in progress.  (It is OK for an object
63 to be "pop"ped off a stack and "deallocated" with a concurrent "pop" on
64 the same stack still in progress, but only if "deallocation" leaves the
65 object addressable.  The second "pop" may still read the object, but
66 the value it reads will not matter.)
67
68 We require that the headers (AO_stack objects) remain allocated and
69 valid as long as any operations on them are still in-flight.
70
71 We also provide macros AO_REAL_HEAD_PTR that converts an AO_stack_t
72 to a pointer to the link field in the next element, and AO_REAL_NEXT_PTR
73 that converts a link field to a real, dereferencable, pointer to the link field
74 in the next element.  This is intended only for debugging, or to traverse
75 the list after modification has ceased.  There is otherwise no guarantee that
76 walking a stack using this macro will produce any kind of consistent
77 picture of the data structure.