libUTL++
Thread.h
1 #pragma once
2 
4 
5 #include <libutl/host_thread.h>
6 #include <libutl/NamedObjectMI.h>
7 #include <libutl/RandUtils.h>
8 #include <libutl/Uint.h>
9 
11 
12 UTL_NS_BEGIN;
13 
15 
29 
31 class Thread : public Object, public NamedObjectMI, protected FlagsMI
32 {
35 
36 public:
37  virtual const Object& getKey() const;
38 
40  static Thread* get();
41 
43  static thread_handle_t handle();
44 
46  size_t
47  id() const
48  {
49  return _id;
50  }
51 
53  uint_t
54  node() const
55  {
56  return _node;
57  }
58 
60  uint_t&
61  node()
62  {
63  return _node;
64  }
65 
67  bool
68  isDetached() const
69  {
70  return getFlag(flg_detached);
71  }
72 
77  void
79  {
80  setFlag(flg_cancel, true);
81  }
82 
87  static void testCancel();
88 
96  void* join(bool deleteSelf = true);
97 
104  virtual void* run(void* arg = nullptr) = 0;
105 
112  void start(void* arg = nullptr, bool join = true);
113 
115  static void yield();
116 
118  virtual void pause();
119 
121  static ulong_t tlsNew();
122 
124  static void* tlsGet(ulong_t key);
125 
131  static void tlsSet(ulong_t key, void* value);
132 
134  randutils::mt19937_64_rng* rng();
135 
136 #if UTL_HOST_OS != UTL_OS_MINGW
137  void setAffinity(size_t cpusetsize, cpu_set_t* cpuset);
138 #endif
139 
140  static void utl_init();
141 
142  static void utl_deInit();
143 
144 protected:
145  virtual void onExit();
146 
147 #if UTL_HOST_OS == UTL_OS_MINGW
148  static ulong_t __stdcall runThread(void* vp_runThreadInfo);
149 #else
150  static void* runThread(void* vp_runThreadInfo);
151 #endif
152 private:
153  void init();
154  void deInit();
155 
156  void
157  setDetached(bool detached)
158  {
159  setFlag(flg_detached, detached);
160  }
161 
162 private:
163  enum flg_t
164  {
165  flg_detached,
166  flg_cancel
167  };
168 
169 private:
170  Uint _id;
171  uint_t _node;
172 
173  randutils::mt19937_64_rng* _rng;
174 #if UTL_HOST_OS == UTL_OS_MINGW
175  void* _threadHandle;
176  ulong_t _threadId;
177  static ulong_t _objectKey;
178 #else
179  pthread_t _threadHandle;
180  static __thread Thread* _thread;
181 #endif
182 };
183 
185 
191 
193 
194 UTL_NS_END;
void cancel()
Cancel the thread.
Definition: Thread.h:78
Named object mix-in.
Definition: NamedObjectMI.h:24
bool isDetached() const
Determine whether the thread is detached.
Definition: Thread.h:68
void deInit()
De-initialize UTL++.
Thread cancelled exception
Definition: Thread.h:190
Mix-in to provide 64-bits for space-efficient storage of up to 64 boolean flags.
Definition: FlagsMI.h:25
size_t id() const
Get the thread&#39;s ID.
Definition: Thread.h:47
#define UTL_EXCEPTION_DECL(exName, baseExName)
Declare a simple exception type.
Definition: Exception.h:41
Root of UTL++ exception class hierarchy.
Definition: Exception.h:109
Thread of execution.
Definition: Thread.h:31
64-bit unsigned integer.
Definition: Uint.h:27
#define UTL_CLASS_DECL_ABC(DC, BC)
Declaration of standard UTL++ functionality for an abstract base class (ABC).
Definition: macros.h:650
unsigned int uint_t
Unsigned integer.
Definition: types.h:59
unsigned long ulong_t
Unsigned long integer.
Definition: types.h:73
uint_t & node()
Get the thread&#39;s assigned node (NUMA).
Definition: Thread.h:61
#define UTL_CLASS_NO_COPY
Declare that a class cannot do copy().
Definition: macros.h:358
Root of UTL++ class hierarchy.
Definition: Object.h:52
uint_t node() const
Get the thread&#39;s assigned node (NUMA).
Definition: Thread.h:54
void init()
Initialize UTL++.