OnixS C++ CME Market Data Handler  5.4.0
API documentation
ThreadPool.h
Go to the documentation of this file.
1 // Copyright Onix Solutions Limited [OnixS]. All rights reserved.
2 //
3 // This software owned by Onix Solutions Limited [OnixS] and is
4 // protected by copyright law and international copyright treaties.
5 //
6 // Access to and use of the software is governed by the terms of the applicable
7 // OnixS Software Services Agreement (the Agreement) and Customer end user license
8 // agreements granting a non-assignable, non-transferable and non-exclusive license
9 // to use the software for it's own data processing purposes under the terms defined
10 // in the Agreement.
11 //
12 // Except as otherwise granted within the terms of the Agreement, copying or
13 // reproduction of any part of this source code or associated reference material
14 // to any other location for further reproduction or redistribution, and any
15 // amendments to this copyright notice, are expressly prohibited.
16 //
17 // Any reproduction or redistribution for sale or hiring of the Software not in
18 // accordance with the terms of the Agreement is a violation of copyright law.
19 //
20 
21 #pragma once
22 
23 #include <vector>
24 
25 #include <OnixS/CME/MDH/String.h>
26 #include <OnixS/CME/MDH/Integral.h>
27 #include <OnixS/CME/MDH/TinySet.h>
28 
30 
31 /// Defines an abstraction for a task.
33 {
34  /// Executes the given task.
35  virtual void run() = 0;
36 };
37 
38 /// Zero-based index of CPU.
39 typedef Int32 CpuIndex;
40 
41 /// Alias for collection of CPU indices.
42 typedef
45 
46 /// Represents a set of CPU indices.
48 {
49  CpuIndices indices_;
50 
51 public:
52  /// Initializes the empty set.
54  {
55  }
56 
57  /// Initializes as a copy of the other set.
59  const ThreadAffinity& other)
60  : indices_(other.indices_)
61  {
62  }
63 
64  /// Utilizes internal resources.
66  {
67  }
68 
69  /// Indicates whether affinity is defined.
70  operator bool() const
71  {
72  return indices_.empty();
73  }
74 
75  /// Read-only access to the indices.
76  const CpuIndices& cpus() const
77  {
78  return indices_;
79  }
80 
81  /// Collection of the CPU indices.
83  {
84  return indices_;
85  }
86 };
87 
88 /// Serializes thread affinity into a string.
89 ONIXS_CMEMDH_EXPORTED
90 void
91 toStr(
92  std::string&,
93  const ThreadAffinity&);
94 
95 /// Serializes thread affinity into a string.
96 inline
97 std::string
99  const ThreadAffinity& affinity)
100 {
101  std::string str;
102 
103  toStr(str, affinity);
104 
105  return str;
106 }
107 
108 //
109 
111 (
112  TaskRunner
113 );
114 
115 /// Manages group of threads executing user-defined tasks.
117 {
118  typedef
119  std::vector<TaskRunner*>
120  Tasks;
121 
122  Tasks tasks_;
123 
124  const size_t capacity_;
125 
126  ThreadPool(
127  const ThreadPool&);
128 
129  ThreadPool&
130  operator =(
131  const ThreadPool&);
132 
133 public:
134  /// Initializes the pool of the requested capacity.
135  explicit
137  size_t capacity)
138  : capacity_(capacity)
139  {
140  tasks_.reserve(capacity);
141  }
142 
143  /// Finalizes the instance.
144  ///
145  /// Waits until all threads accomplish their tasks.
147  {
148  joinAll();
149  }
150 
151  /// Number of active threads currently
152  /// busy executing user-defined tasks.
153  size_t size() const
154  {
155  return tasks_.size();
156  }
157 
158  /// Maximal number of threads maintained by the given pool.
159  size_t capacity() const
160  {
161  return capacity_;
162  }
163 
164  /// Spawns the new thread executing the given task.
165  ///
166  /// Raises exception if the number of active threads
167  /// exceeds the predefined capacity of the pool.
168  void
170  Task& task)
171  {
172  start(
173  task,
174  ThreadAffinity());
175  }
176 
177  /// Spawns the new thread executing the given task.
178  /// Binds the spawned thread to the defined set of
179  /// cores/processors.
180  ///
181  /// Raises exception if the number of active threads
182  /// exceeds the predefined capacity of the pool.
184  void
185  start(
186  Task&,
187  const ThreadAffinity&);
188 
189  /// Blocks until all threads accomplish their tasks.
191  void
192  joinAll();
193 };
194 
195 /// The collection of parameters affecting behavior
196 /// of a thread pool. The given class serves as a base for
197 /// defining and manipulating thread pool related parameters.
200 {
201  ThreadAffinity affinity_;
202  UInt32 size_;
203 
204 public:
205  /// Assigns the default values for the given
206  /// instance of the thread pool settings.
208  : affinity_()
209  , size_(1)
210  {
211  }
212 
213  /// Cleans everything up.
215  {
216  }
217 
218  /// Defines set of CPUs allowed for each working
219  /// thread to be executed on while running tasks.
220  ///
221  /// @note By default set is empty thus allowing threads
222  /// to be executed on any CPU available in the system.
223  const
225  affinity() const
226  {
227  return affinity_;
228  }
229 
230  /// Defines set of CPUs allowed for each working
231  /// thread to be executed on while running tasks.
232  ///
233  /// @note By default set is empty thus allowing threads
234  /// to be executed on any CPU available in the system.
236  {
237  return affinity_;
238  }
239 
240  /// Number of working threads in the pool.
241  ///
242  /// @note Default value is '1'.
243  UInt32
244  size() const
245  {
246  return size_;
247  }
248 
249  /// Defines the number of working threads in the pool.
250  void
252  UInt32 value)
253  {
254  size_ = value;
255  }
256 };
257 
258 /// Serializes thread pool settings into a string.
259 ONIXS_CMEMDH_EXPORTED
260 void
261 toStr(
262  std::string&,
263  const
265 
266 /// Serializes thread pool settings into a string.
267 inline
268 std::string
270  const
271  ThreadPoolSettings& settings)
272 {
273  std::string str;
274 
275  toStr(str, settings);
276 
277  return str;
278 }
279 
#define ONIXS_CMEMDH_LTWT_CLASS
Definition: Bootstrap.h:94
Int32 Int32
int32.
Definition: Fields.h:69
TinySet< CpuIndex > CpuIndices
Alias for collection of CPU indices.
Definition: ThreadPool.h:44
ThreadAffinity(const ThreadAffinity &other)
Initializes as a copy of the other set.
Definition: ThreadPool.h:58
UInt32 UInt32
uInt32.
Definition: Fields.h:247
const CpuIndices & cpus() const
Read-only access to the indices.
Definition: ThreadPool.h:76
size_t capacity() const
Maximal number of threads maintained by the given pool.
Definition: ThreadPool.h:159
std::string toStr(const ThreadPoolSettings &settings)
Serializes thread pool settings into a string.
Definition: ThreadPool.h:269
Int32 CpuIndex
Zero-based index of CPU.
Definition: ThreadPool.h:39
Represents a set of CPU indices.
Definition: ThreadPool.h:47
~ThreadPoolSettings()
Cleans everything up.
Definition: ThreadPool.h:214
Manages group of threads executing user-defined tasks.
Definition: ThreadPool.h:116
ThreadAffinity()
Initializes the empty set.
Definition: ThreadPool.h:53
bool value(Number &number, const MultiContainer &container, Tag tag)
void size(UInt32 value)
Defines the number of working threads in the pool.
Definition: ThreadPool.h:251
void start(Task &task)
Definition: ThreadPool.h:169
#define ONIXS_CMEMDH_NAMESPACE_BEGIN
Definition: Bootstrap.h:152
bool empty() const
Indicates whether the set is empty.
Definition: TinySet.h:82
const ThreadAffinity & affinity() const
Definition: ThreadPool.h:225
CpuIndices & cpus()
Collection of the CPU indices.
Definition: ThreadPool.h:82
Defines an abstraction for a task.
Definition: ThreadPool.h:32
#define ONIXS_CMEMDH_LTWT_EXPORTED
Definition: Bootstrap.h:102
ThreadPool(size_t capacity)
Initializes the pool of the requested capacity.
Definition: ThreadPool.h:136
#define ONIXS_CMEMDH_INTERNAL_CLASS_DECL(typeName)
Definition: Bootstrap.h:62
#define ONIXS_CMEMDH_EXPORTED_STRUCT
Definition: Bootstrap.h:58
~ThreadAffinity()
Utilizes internal resources.
Definition: ThreadPool.h:65
#define ONIXS_CMEMDH_NAMESPACE_END
Definition: Bootstrap.h:156