gatelib  2.1
g_mthread_Thread.h
1 #pragma once
2 
3 #include "g_common.h"
4 
5 #define G_THREAD_DATA_SIZE ( sizeof(void*)+sizeof(unsigned int) )
6 #define G_THREAD_NAME_MAX_SIZE 32
7 #define G_THREAD_CURRENT_PROCESS 0
8 
9 namespace g
10 {
11 namespace mthread
12 {
13 
14 enum GSchedulePolicy_t
15 {
16  sched_unknown = -1 ,
17  sched_other ,
18 
19  sched_round_robin,//posix-like only
20  sched_fifo ,
21 };
22 
23 G_EXC_DEFINE(MultithreadException,Exception);
24 G_EXC_DEFINE(WrongThreadRecreateException,MultithreadException);
25 G_EXC_DEFINE(NotRunningException,MultithreadException);
26 G_EXC_DEFINE(ThreadAlreadyCreatedException,MultithreadException);
27 G_EXC_DEFINE(DestructingStillExistingThread,MultithreadException);
28 G_EXC_DEFINE(KillingNotAttachedThread,MultithreadException);
29 G_EXC_DEFINE(InvalidSchedulePolicy,MultithreadException);
30 G_EXC_DEFINE(JoiningANotAttachedThread,MultithreadException);
31 
32 typedef void* (*GthreadEntryPointPf_t) ( void* );
33 
34 class ThreadAbstract;
35 
36 class G_LIB_ITEM Thread
37 {
38  friend class ThreadSelf;
39  friend class ThreadAbstract;
40 
41 G_COPY_CONSTRUCTOR_PROHIBITED(Thread);
42 protected:
43  Thread ( ); //used by subclasses
44 
45 public:
46  virtual ~Thread ( );
47 
48  virtual void cancel ( );
49  virtual void* join ( );
50  virtual const void* detach ( );
51 
52  GInt64_t getId ( ) const { return mId; }
53  const char* getThreadName ( ) const { return mThreadName; }
54  bool isAttached ( ) const { return mIsAttached; }
55  void* getData ( ) { return mData; }
56 
57 #if defined(WIN32) && G_OPTION_HIDE_INCOMPATIBLE == 0
58  void* getWindowsHandle ( );
59  int winSuspend ( );
60  int winResume ( );
61  void winTerminate ( GInt32_t exit_code );
62 #endif
63 
64  static GInt32_t get_MaxPriority ( GSchedulePolicy_t = sched_other );
65  static GInt32_t get_MinPriority ( GSchedulePolicy_t = sched_other );
66 
67  static GSchedulePolicy_t get_ProcessSchedulePolicy ( GInt32_t pid = G_THREAD_CURRENT_PROCESS );
68  static void set_ProcessSchedulePolicy ( GSchedulePolicy_t , GInt32_t pid = G_THREAD_CURRENT_PROCESS );
69 
70  GInt32_t getPriority ( );
71  void setPriority ( GInt32_t priority );
72 
73  static void do_Sleep ( double sleep_secs );
74  static GInt64_t get_CurrentThreadId ( );
75 
76  static void do_CurrentThreadExit ( void* exit_value );
77 
78  static ThreadAbstract* get_AssociatedThreadAbstract ( );
79 
80 protected:
81  virtual void mAttach ( const void* aData );
82 
83  bool mIsAttached;
84  GInt64_t mId;
85 
86 private:
87  //must be used from calling thread only
88  void mSetThreadName ( const char* aName );
89  void mSetThreadNameAsCurrentThreadName ( );
90  static void m_SetAsThreadAbstract ( ThreadAbstract* );
91  static void m_ResetCurrentThreadName ( );
92 
93  char mThreadName [G_THREAD_NAME_MAX_SIZE+1];
94  char mData [G_THREAD_DATA_SIZE];
95 };
96 
97 }//namespace mthread
98 }//namespace g
99 
100 #include "g_mthread_ThreadSelf.h"
Definition: g_mthread_ThreadSelf.h:10
#define G_EXC_DEFINE(aexctypename, abaseexctypename)
Definition: g_exception_macros.h:39
Definition: g.mthread.ThreadSimpleEvent.h:5
Definition: g_mthread_Thread.h:36
Definition: g_mthread_ThreadAbstract.h:18