Lockless Task Scheduler  v1.0a
A lockless task scheduler
lockfreenodestack.h
1 // ***********************************************************************
2 // <copyright file="lockfreenodestack.h" >
3 // Copyright (c) viknash. All rights reserved.
4 // </copyright>
5 // <summary></summary>
6 // ***********************************************************************
7 #pragma once
8 
9 #include <atomic>
10 
11 #include "atomics.h"
12 #include "lockfreenode.h"
13 
14 namespace task_scheduler {
15  template < typename T, class TMemInterface > class class_alignment lock_free_node_stack : public TMemInterface
16  {
19 
20  public:
22  {
23  head.clear();
24  ts_debug_only(debug.counter = 0;);
25  }
26 
27  void push_front(node_type *_node)
28  {
29  ts_debug_only(assert(_node->next.node == nullptr););
30  atomic_node_ptr copy, newNode;
31  do
32  {
33  copy = head;
34  newNode.data.access.as_atomic = copy.data.access.as_atomic + 1;
35  newNode.data.points_to.node = _node;
36  _node->next.node = head.data.points_to.node;
37  } while (!head.compare_exchange_weak(copy, newNode));
38  ts_debug_only(atomics::increment(debug.counter););
39  }
40 
41  node_type *pop_front()
42  {
43  atomic_node_ptr copy, inc_copy;
44  do
45  {
46  copy = head;
47  if (copy.data.points_to.node == nullptr)
48  {
49  return nullptr;
50  }
51 
52  inc_copy.data.access.as_atomic = copy.data.access.as_atomic + 1;
53  inc_copy.data.points_to.node = copy.data.points_to.node->next.node;
54 
55  } while (!head.compare_exchange_weak(copy, inc_copy));
56 
57  ts_debug_only(atomics::decrement(debug.counter););
58  ts_debug_only(copy.data.points_to.node->next.node = nullptr;);
59  return copy.data.points_to.node;
60  }
61 
62  bool empty() { return head.data.points_to.node == nullptr; }
63 
65  {}
66 
67  private:
68  atomic_node_ptr head;
69 
70  struct debug_container
71  {
72  int32_t counter;
73  };
74  ts_debug_only(debug_container debug;);
75  };
76 }
int64_t increment(volatile int64_t &_data)
Increments the specified data.
Definition: atomics.h:32
Class stl_allocator.
Definition: allocator.h:16
Definition: lockfreenodestack.h:15
int64_t decrement(volatile int64_t &_data)
Decrements the specified data.
Definition: atomics.h:39
Definition: lockfreenode.h:24
Definition: lockfreenode.h:55