OnixS C++ FIX Engine  4.2.0
API Documentation
Thread.h
Go to the documentation of this file.
1 /*
2 * Copyright Onix Solutions Limited [OnixS]. All rights reserved.
3 *
4 * This software owned by Onix Solutions Limited [OnixS] and is protected by copyright law
5 * and international copyright treaties.
6 *
7 * Access to and use of the software is governed by the terms of the applicable OnixS Software
8 * Services Agreement (the Agreement) and Customer end user license agreements granting
9 * a non-assignable, non-transferable and non-exclusive license to use the software
10 * for it's own data processing purposes under the terms defined in the Agreement.
11 *
12 * Except as otherwise granted within the terms of the Agreement, copying or reproduction of any part
13 * of this source code or associated reference material to any other location for further reproduction
14 * or redistribution, and any amendments to this copyright notice, are expressly prohibited.
15 *
16 * Any reproduction or redistribution for sale or hiring of the Software not in accordance with
17 * the terms of the Agreement is a violation of copyright law.
18 */
19 
20 #pragma once
21 
22 #include <OnixS/FIXEngine/ABI.h>
24 
25 #include <set>
26 #include <string>
27 
28 namespace OnixS {
29 namespace Threading {
30 /// Logical processors that a thread is allowed to run on (first logical CPU has index 0).
31 typedef size_t CpuIndex;
32 typedef std::set < CpuIndex > CpuIndexes;
33 
34 /// Represents set of CPU indices.
36 {
37 public:
38 
39  /// Constructs thread affinity from the string presentation.
40  /// Symbol ',' is used as the CPU index delimiter.
41  explicit ThreadAffinity(const std::string & str);
42 
43  /// Destructs the instance.
44  ~ThreadAffinity();
45 
46  /// Read-only access to index collection.
47  const CpuIndexes & cpuIndexes() const;
48 
49  /// Collection of CPU indices.
50  CpuIndexes & cpuIndexes();
51 
52  /// Serializes thread affinity to the string presentation.
53  std::string toString();
54 
55 private:
56 
57  // Copying & assignment is prohibited.
59  ThreadAffinity & operator = (const ThreadAffinity &);
60 
61  CpuIndexes * indexes_;
62 };
63 
64 /// Exposes manipulations over current thread.
66 {
67 public:
68  /// Suspends execution of current thread for
69  /// a given amount of time in milliseconds.
70  static void sleep(unsigned int milliseconds);
71 
72  /// Executes singular instruction during the given number of microseconds.
73  static void spinWait(int microseconds);
74 
75  /// Sets the processor affinity mask for the current thread.
76  static void affinity(const CpuIndexes & cpuIndexes);
77 
78  /// Sets the processor affinity mask for the current thread.
79  static void affinity(const CpuIndex cpuIndex);
80 
81  /// Sets the priority for the current thread.
82  static void priority(int priority);
83 
84  /// Sets the scheduling policy for the current thread.
85  ///
86  /// @note This method also sets the priority to the minimal value for the new policy,
87  /// therefore, the priority should be set to a necessary value afterwards.
88  static void policy(int policy);
89 
90  /// Gets platform identifier for the current thread.
91  static ThreadId id();
92 
93 private:
94  // No instances of current thread are available.
95  ThisThread(const ThisThread &);
96  ThisThread & operator = (const ThisThread &);
97 };
98 
99 /// Manipulates a thread.
100 /// Custom thread classes should derive from this class and implement 'run' method.
102 {
103 public:
104 
105  /// Creates the `Thread` object without running a real thread.
106  explicit Thread(const std::string & name);
107 
108  /// Cleans up internal resources.
109  ///
110  /// @note Calls `std::terminate` if the thread is not stopped at the moment.
111  virtual ~Thread();
112 
113  /// Creates and runs an actual thread.
114  /// Can be called only once for the current 'Thread' instance.
115  ///
116  /// @throw std::exception if the thread is started or joined already.
117  void start();
118 
119  /// Sends a stop signal to the thread.
120  void stopAsync();
121 
122  /// Returns 'true' if a stop signal is sent to the thread
123  /// by the `stopAsync` method or in a different way, otherwise 'false'.
124  bool stopRequested() const;
125 
126  /// Joins the thread completion.
127  void join() const;
128 
129  /// Returns platform identifier.
130  ThreadId id() const;
131 
132 private:
133 
134  /// Thread procedure.
135  virtual void run() = 0;
136 
137  /// Implementation details.
138  struct Impl;
139  Impl * impl_;
140 
141  /// Copying & assignment is prohibited.
142  Thread(const Thread &);
143  Thread & operator = (const Thread &);
144 };
145 
146 }
147 }
Manipulates a thread.
Definition: Thread.h:101
Represents set of CPU indices.
Definition: Thread.h:35
pthread_t ThreadId
Type alias for thread identifier.
Definition: Definitions.h:50
#define ONIXS_FIXENGINE_API
Definition: ABI.h:45
size_t CpuIndex
Logical processors that a thread is allowed to run on (first logical CPU has index 0)...
Definition: Thread.h:31
Exposes manipulations over current thread.
Definition: Thread.h:65
std::set< CpuIndex > CpuIndexes
Definition: Thread.h:32