Lockless Task Scheduler  v1.0a
A lockless task scheduler
types.h
1 // ***********************************************************************
2 // Assembly : task_scheduler
3 // Author : viknash
4 // ***********************************************************************
5 // <copyright file="types.h" >
6 // Copyright (c) viknash. All rights reserved.
7 // </copyright>
8 // <summary></summary>
9 // ***********************************************************************
10 #pragma once
11 
12 #include <cstdint>
13 
17 namespace task_scheduler
18 {
19 
23  template < typename ValueType, uint32_t MinValue, uint32_t MaxValue, bool MinInclusive = false,
24  bool MaxInclusive = true >
26  {
27  typedef ValueType value_type;
28 
33  operator const value_type &() const { return value(); }
34 
39  void operator=(const ValueType &_value)
40  {
41  assert((MinInclusive ? _value >= (ValueType)MinValue : _value > (ValueType)MinValue) &&
42  (maxInclusive ? _value <= (ValueType)MaxValue : _value < (ValueType)MaxValue));
43  value = _value;
44  }
45 
50  operator const ValueType() { return value; }
51 
52  private:
56  ValueType value;
57  };
58 
60 
61  template < class TMemInterface > class base_thread_pool;
62 
63  typedef uint8_t thread_num_t;
64  typedef uint64_t thread_mask_int_t;
65 
66  template < typename ValueType, class TMemInterface > class base_thread_index
67  {
69 
70  public:
76  base_thread_index(thread_pool *_pool, ValueType _value)
77  : value(_value)
78  , pool(_pool)
79  {
80  }
81 
87  {
88  *this += 1;
89  return *this;
90  }
91 
97  {
98  *this -= 1;
99  return *this;
100  }
101 
108  {
109  value = (value + rhs) % pool->num_threads;
110  return *this;
111  }
112 
113  base_thread_index &operator+=(int32_t rhs)
114  {
115  value = (value + rhs) % pool->num_threads;
116  return *this;
117  }
118 
126  {
127  lhs += rhs;
128  return lhs;
129  }
130 
137  {
138  value = (value + pool->num_threads - rhs) % pool->num_threads;
139  return *this;
140  }
141 
148  {
149  value = (value + pool->num_threads - rhs) % pool->num_threads;
150  return *this;
151  }
152 
158  {
159  pool = other.pool;
160  value = other.value;
161  }
162 
168  base_thread_index &operator=(const base_thread_index &other) // copy assignment
169  {
170  if (this != &other)
171  { // self-assignment check expected
172  pool = other.pool;
173  value = other.value;
174  }
175  return *this;
176  }
177 
182  operator const ValueType() { return value; }
183 
188  thread_mask_int_t get_mask() { return 1ull << value; }
189 
195  bool is_set(thread_mask_int_t _other_mask)
196  {
197  thread_mask_int_t this_mask = get_mask();
198  return ((this_mask & _other_mask) == this_mask);
199  }
200 
201  private:
205  ValueType value;
209  thread_pool *pool;
210  };
211 
212  template < typename ValueType, class TMemInterface >
216  {
217  lhs += rhs;
218  return lhs;
219  }
220 
221  template < typename ValueType, class TMemInterface >
223  int32_t rhs)
224  {
225  lhs += rhs;
226  return lhs;
227  }
228 
229  template < typename ValueType, class TMemInterface >
232  {
233  return rhs + lhs;
234  }
235 
236  template < typename ValueType, class TMemInterface >
240  {
241  lhs -= rhs;
242  return lhs;
243  }
244 
245  template < typename ValueType, class TMemInterface >
247  int32_t rhs)
248  {
249  lhs -= rhs;
250  return lhs;
251  }
252 
253  template < typename ValueType, class TMemInterface >
255  operator-(int32_t lhs, const base_thread_index< ValueType, TMemInterface > &rhs)
256  {
257  return rhs - lhs;
258  }
259 
260  template < class TMemInterface > using thread_index_t = base_thread_index< thread_num_t, TMemInterface >;
261 }
Definition: types.h:66
base_thread_index< ValueType, TMemInterface > operator+(base_thread_index< ValueType, TMemInterface > lhs, const base_thread_index< ValueType, TMemInterface > &rhs)
Definition: types.h:214
base_thread_index & operator-=(const base_thread_index &rhs)
Operator-=s the specified RHS.
Definition: types.h:136
Class stl_allocator.
Definition: allocator.h:16
thread_mask_int_t get_mask()
Gets the mask.
Definition: types.h:188
friend base_thread_index operator+(base_thread_index lhs, const base_thread_index &rhs)
Operator+s the specified LHS.
Definition: types.h:125
Class constrained.
Definition: types.h:25
bool is_set(thread_mask_int_t _other_mask)
Determines whether the specified other mask is set.
Definition: types.h:195
base_thread_index & operator-=(int32_t rhs)
Operator-=s the specified RHS.
Definition: types.h:147
base_thread_index(const base_thread_index &other)
Initializes a new instance of the base_thread_index class.
Definition: types.h:157
base_thread_index(thread_pool *_pool, ValueType _value)
Initializes a new instance of the base_thread_index class.
Definition: types.h:76
base_thread_index & operator--()
Operator–s this instance.
Definition: types.h:96
base_thread_index & operator=(const base_thread_index &other)
Operator=s the specified other.
Definition: types.h:168
Class base_thread_pool.
Definition: task.h:36
base_thread_index & operator+=(const base_thread_index &rhs)
Operator+=s the specified RHS.
Definition: types.h:107
base_thread_index & operator++()
Operator++s this instance.
Definition: types.h:86