1
Note that the AO_stack implementation is licensed under the GPL,
2
unlike the lower level routines.
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.
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.)
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-hndler accesses
16
to the data structure.
20
Hans-J. Boehm, "An Almost Non-Blocking Stack", PODC 2004,
21
http://portal.acm.org/citation.cfm?doid=1011767.1011774, or
22
http://www.hpl.hp.com/techreports/2004/HPL-2004-105.html
23
(This is not exactly the implementation described there, since the
24
interface was cleaned up in the interim. But it should perform
27
We use a fully lock-free implementation when the underlying hardware
28
makes that less expensive, i.e. when we have a double-wide compare-and-swap
29
operation available. (The fully lock-free implementation uses an AO_t-
30
sized version count, and assumes it does not wrap during the time any
31
given operation is active. This seems reasonably safe on 32-bit hardware,
32
and very safe on 64-bit hardware.) If a fully lock-free implementation
33
is used, the macro AO_STACK_IS_LOCK_FREE will be defined.
35
The implementation is interesting only because it allows reuse of
36
existing nodes. This is necessary, for example, to implement a memory
39
Since we want to leave the precise stack node type up to the client,
40
we insist only that each stack node contains a link field of type AO_t.
41
When a new node is pushed on the stack, the push operation expects to be
42
passed the pointer to this link field, which will then be overwritten by
43
this link field. Similarly, the pop operation returns a pointer to the
44
link field of the object that previously was on the top of the stack.
46
The cleanest way to use these routines is probably to define the stack node
47
type with an initial AO_t link field, so that the conversion between the
48
link-field pointer and the stack element pointer is just a compile-time
49
cast. But other possibilities exist. (This would be cleaner in C++ with
52
A stack is represented by an AO_stack_t structure. (This is normally
53
2 or 3 times the size of a pointer.) It may be statically initialized
54
by setting it to AO_STACK_INITIALIZER, or dynamically initialized to
55
an empty stack with AO_stack_init. There are only three operations for
58
void AO_stack_init(AO_stack_t *list);
59
void AO_stack_push_release(AO_stack_t *list, AO_t *new_element);
60
AO_t * AO_stack_pop_acquire(volatile AO_stack_t *list);
62
We require that the objects pushed as list elements remain addressable
63
as long as any push or pop operation are in progress. (It is OK for an object
64
to be "pop"ped off a stack and "deallocated" with a concurrent "pop" on
65
the same stack still in progress, but only if "deallocation" leaves the
66
object addressable. The second "pop" may still read the object, but
67
the value it reads will not matter.)
69
We require that the headers (AO_stack objects) remain allocated and
70
valid as long as any operations on them are still in-flight.
72
We also provide macros AO_REAL_HEAD_PTR that converts an AO_stack_t
73
to a pointer to the link field in the next element, and AO_REAL_NEXT_PTR
74
that converts a link field to a real, dereferencable, pointer to the link field
75
in the next element. This is intended only for debugging, or to traverse
76
the list after modification has ceased. There is otherwise no guarantee that
77
walking a stack using this macro will produce any kind of consistent
78
picture of the data structure.