README_stack.txt 3.9 KB

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