< prev index next >

src/hotspot/share/utilities/lockFreeStack.hpp

Print this page
rev 57095 : [mq]: use
rev 57096 : [mq]: trailing_semi


  52 //
  53 // \tparam T is the class of the elements in the stack.
  54 //
  55 // \tparam next_ptr is a function pointer.  Applying this function to
  56 // an object of type T must return a pointer to the list entry member
  57 // of the object associated with the LockFreeStack type.
  58 template<typename T, T* volatile* (*next_ptr)(T&)>
  59 class LockFreeStack {
  60   T* volatile _top;
  61 
  62   void prepend_impl(T* first, T* last) {
  63     T* cur = top();
  64     T* old;
  65     do {
  66       old = cur;
  67       set_next(*last, cur);
  68       cur = Atomic::cmpxchg(&_top, cur, first);
  69     } while (old != cur);
  70   }
  71 
  72   // Noncopyable.
  73   LockFreeStack(const LockFreeStack&);
  74   LockFreeStack& operator=(const LockFreeStack&);
  75 
  76 public:
  77   LockFreeStack() : _top(NULL) {}
  78   ~LockFreeStack() { assert(empty(), "stack not empty"); }
  79 
  80   // Atomically removes the top object from this stack and returns a
  81   // pointer to that object, or NULL if this stack is empty. Acts as a
  82   // full memory barrier. Subject to ABA behavior; callers must ensure
  83   // usage is safe.
  84   T* pop() {
  85     T* result = top();
  86     T* old;
  87     do {
  88       old = result;
  89       T* new_top = NULL;
  90       if (result != NULL) {
  91         new_top = next(*result);
  92       }
  93       // CAS even on empty pop, for consistent membar bahavior.
  94       result = Atomic::cmpxchg(&_top, result, new_top);




  52 //
  53 // \tparam T is the class of the elements in the stack.
  54 //
  55 // \tparam next_ptr is a function pointer.  Applying this function to
  56 // an object of type T must return a pointer to the list entry member
  57 // of the object associated with the LockFreeStack type.
  58 template<typename T, T* volatile* (*next_ptr)(T&)>
  59 class LockFreeStack {
  60   T* volatile _top;
  61 
  62   void prepend_impl(T* first, T* last) {
  63     T* cur = top();
  64     T* old;
  65     do {
  66       old = cur;
  67       set_next(*last, cur);
  68       cur = Atomic::cmpxchg(&_top, cur, first);
  69     } while (old != cur);
  70   }
  71 
  72   NONCOPYABLE(LockFreeStack);


  73 
  74 public:
  75   LockFreeStack() : _top(NULL) {}
  76   ~LockFreeStack() { assert(empty(), "stack not empty"); }
  77 
  78   // Atomically removes the top object from this stack and returns a
  79   // pointer to that object, or NULL if this stack is empty. Acts as a
  80   // full memory barrier. Subject to ABA behavior; callers must ensure
  81   // usage is safe.
  82   T* pop() {
  83     T* result = top();
  84     T* old;
  85     do {
  86       old = result;
  87       T* new_top = NULL;
  88       if (result != NULL) {
  89         new_top = next(*result);
  90       }
  91       // CAS even on empty pop, for consistent membar bahavior.
  92       result = Atomic::cmpxchg(&_top, result, new_top);


< prev index next >