You.i Engine
YiThread.h
Go to the documentation of this file.
1 // © You i Labs Inc. 2000-2017. All rights reserved.
2 #ifndef _YI_THREAD_H_
3 #define _YI_THREAD_H_
4 
5 #include "framework/YiPredef.h"
7 #include "utility/YiString.h"
8 
14 typedef enum
15 {
21 
23 
25 
26 } YI_PRIORITY;
27 
28 const int32_t YI_MAX_THREADS = 100;
29 
30 class CYIThreadPriv;
31 
86 class CYIThread
87 {
88  friend class CYIThreadPriv;
89 
90 public:
91  static const uint32_t DEFAULT_STACK_SIZE = 128 * 1024; // 128KB
92 
94  {
96  NON_BLOCKING = false
97  };
98 
104  class Listener
105  {
106  public:
107  Listener() {}
108  virtual ~Listener() {}
109 
115  virtual void OnThreadStarted(CYIThread * /*pPublisher*/) {}
116 
122  virtual void OnThreadTerminated(CYIThread * /*pPublisher*/) {}
123 
129  virtual void OnThreadFinished(CYIThread * /*pPublisher*/) {}
130 
134  virtual void OnThreadDestroyed(CYIThread * /*pPublisher*/) {}
135  };
136 
137  CYIThread(const CYIString &name = CYIString(), YI_PRIORITY ePriority = YI_PRIORITY_DEFAULT, uint32_t uStackSize = DEFAULT_STACK_SIZE);
138 
144  virtual ~CYIThread();
145 
150 
158  static const CYIThreadHandle &GetUIThreadId();
159 
163  static int32_t GetOSIdealThreadCount();
164 
170  static void Sleep(uint32_t uMillis);
171 
175  static void YieldCurrentThread();
176 
180  static void ReportDefaults();
181 
191  bool StartThread(YI_START_TYPE eStartType = BLOCK_UNTIL_STARTED);
192 
213  void Terminate();
214 
215 
231  void EnableTermination();
232 
244  void DisableTermination();
245 
254  bool IsTerminationEnabled();
255 
263  bool Join();
264 
274  bool Join(uint32_t uTimeoutMs);
275 
281  bool IsRunning() const;
282 
290 
298  void SetName(const CYIString &name);
299 
305  const CYIString &GetName() const;
306 
312  void SetPriority(YI_PRIORITY priority);
313 
322  YI_PRIORITY GetPriority() const;
323 
329  bool AddThreadListener(Listener *pListener);
330 
336  bool RemoveThreadListener(Listener *pListener);
337 
345  bool SetStackSize(uint32_t uStackSize);
346 
352  uint32_t GetStackSize() const;
353 
354 protected:
355 
356  virtual void Run() = 0;
357 
371  bool IsTerminationRequested() const;
372 
373 private:
374 
375  // Hiding copy constructor
376  CYIThread(const CYIThread &);
377 
378  // Hiding assignment operator.
379  CYIThread &operator=(const CYIThread &);
380 
381  CYIThreadPriv *m_pPriv;
382 };
383 
386 #endif // _YI_THREAD_H_
YI_PRIORITY GetPriority() const
virtual void Run()=0
Definition: YiThread.h:95
void Terminate()
bool StartThread(YI_START_TYPE eStartType=BLOCK_UNTIL_STARTED)
void DisableTermination()
const int32_t YI_MAX_THREADS
Definition: YiThread.h:28
Container class for Unicode strings. Conceptually, a CYIString object is a sequence of Unicode charac...
Definition: YiString.h:35
virtual ~CYIThread()
virtual void OnThreadStarted(CYIThread *)
Definition: YiThread.h:115
Definition: YiThread.h:17
static const uint32_t DEFAULT_STACK_SIZE
Definition: YiThread.h:91
YI_PRIORITY
Definition: YiThread.h:14
CYIThread(const CYIString &name=CYIString(), YI_PRIORITY ePriority=YI_PRIORITY_DEFAULT, uint32_t uStackSize=DEFAULT_STACK_SIZE)
static void ReportDefaults()
virtual void OnThreadTerminated(CYIThread *)
Definition: YiThread.h:122
bool IsTerminationEnabled()
YI_START_TYPE
Definition: YiThread.h:93
Definition: YiThread.h:16
static const CYIThreadHandle & GetUIThreadId()
static void Sleep(uint32_t uMillis)
const CYIString & GetName() const
bool SetStackSize(uint32_t uStackSize)
bool IsTerminationRequested() const
Definition: YiThread.h:24
bool IsRunning() const
void EnableTermination()
void SetPriority(YI_PRIORITY priority)
virtual ~Listener()
Definition: YiThread.h:108
static CYIThreadHandle GetCurrentThreadId()
CYIThreadHandle GetThreadId() const
Definition: YiThread.h:96
static void YieldCurrentThread()
void SetName(const CYIString &name)
virtual void OnThreadFinished(CYIThread *)
Definition: YiThread.h:129
bool AddThreadListener(Listener *pListener)
Listener()
Definition: YiThread.h:107
Definition: YiThread.h:20
static int32_t GetOSIdealThreadCount()
Definition: YiThread.h:104
Definition: YiThread.h:86
friend class CYIThreadPriv
Definition: YiThread.h:88
bool RemoveThreadListener(Listener *pListener)
virtual void OnThreadDestroyed(CYIThread *)
Definition: YiThread.h:134
Definition: YiThread.h:22
Definition: YiThreadHandle.h:43
bool Join()
Definition: YiThread.h:18
Definition: YiThread.h:19
uint32_t GetStackSize() const