ZeusBase-Library  2.0.4
Thread.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * Copyright (C) 2011 by Benjamin Hadorn (b_hadorn@bluewin.ch)
3  *****************************************************************************
4  * Project : Zeus Base Library
5  * Module : Thread
6  * Package : Zeus.ZeusBase.System
7  * Author : Benjamin Hadorn
8  * Date : 27.12.2011
9  * System : Zeus-Framework
10  *****************************************************************************
11  * Licence: *
12  * This library is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU Lesser General Public License as *
14  * published by the Free Software Foundation; either version *
15  * 2.1 of the License, or (at your option) any later version. *
16  * *
17  * This library is distributed in the hope that it will be useful, *
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
20  * GNU Lesser General Public License for more details. *
21  * *
22  * You should have received a copy of the GNU Lesser General Public *
23  * License along with this library; if not, write to the Free Software *
24  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA *
25  *****************************************************************************/
26 
27 /*****************************************************************************
28  * Changes:
29  * 27.12.2011 bha: created zeus 2.0
30  *****************************************************************************/
31 
32 #ifndef ThreadH
33 #define ThreadH
34 
38 
39 #if defined(ENABLE_LINUX_BINDING) || defined(ENABLE_OSX_BINDING)
40  #include <pthread.h>
41 #elif defined(ENABLE_WIN32_BINDING)
42  #if defined(USE_WINDOWS_H)
43  #include <windows.h>
44  #endif
45 #endif
46 
47 
49 
50 class TThreadMessageQueue;
51 class TCriticalSection;
52 class ISynchronizeObject;
53 
54 /***************************************************************************/
60 /***************************************************************************/
62 {
63  public:
64  /***********************************************************************/
67  /***********************************************************************/
68  enum EPriority
69  {
70  etNormal = 0,
71  etLow = 1,
72  etHigh = 2,
73  etLower = 3,
74  etHigher = 4,
75  etTimeCritical = 5
76  };
77 
78  TThread(EPriority ePriority = etNormal);
79  TThread(IRunnable& rObj, EPriority ePriority = etNormal);
80 
81 
82  EPriority getPriority() const;
83  void setPriority(EPriority ePriority);
84  static void sleep(const Float& rfDelay);
85 
86  virtual void kill(Float64 dTimeOut=0);
87  virtual bool isAlive() const;
88  virtual bool isDead() const;
89  virtual void resume();
90  virtual bool signalizeStop(Float64 dTimeOut=5.0);
91  virtual bool start();
92  virtual void suspend();
93  virtual bool yield();
94  virtual Retval postObject(ISynchronizeObject& rObject,
95  bool bWaitForCompletion);
96 
97  //Methods of IThread
98  virtual bool MQUALIFIER isInterrupted() const;
99  virtual Uint MQUALIFIER getThreadID() const;
100 
101  //Methods of IZUnknown
103 
104  protected:
109 
110  virtual ~TThread();
111  virtual void execute();
112  virtual void onTerminated() {}
113  virtual void onStarted() {}
114 
115  private:
116  void terminate_internal();
117  void release_internal();
118 
120  bool m_bCreated;
122  bool m_bTerminated;
124  bool m_bSuspended;
126  IRunnable* m_pRunObj;
128  EPriority m_ePriority;
130  Uint m_uiThreadID;
131  //Lock for ThreadID
132  TCriticalSection& m_rDataLock;
133 
134 
135  #if defined(ENABLE_LINUX_BINDING) || defined(ENABLE_OSX_BINDING)
136  pthread_t m_Thread;
138 
139  static void* createThread(void* pLife);
140  #elif defined(ENABLE_WIN32_BINDING)
141  //Thread handle
142  HANDLE m_hHandle;
143  static Uint32 __stdcall createThread(void* pLife);
144 
145  #if _WIN32_WINNT >= 0x0400
146  static void CALLBACK doUserAPCProc(DWORD dwParam);
147  #endif
148  #endif
149 };
150 
151 //INLINE
152 
153 /***************************************************************************/
156 /***************************************************************************/
157 inline bool TThread::isAlive() const
158 {
159  return !isDead();
160 }
161 
162 /***************************************************************************/
168 /***************************************************************************/
169 inline bool TThread::isDead() const
170 {
171  return (m_bTerminated || !m_bCreated);
172 }
173 
174 /***************************************************************************/
177 /***************************************************************************/
178 inline bool MQUALIFIER TThread::isInterrupted() const
179 {
180  return m_bInterrupted;
181 }
182 
183 /***************************************************************************/
186 /***************************************************************************/
187 inline TThread::EPriority TThread::getPriority() const
188 {
189  return m_ePriority;
190 }
191 
192 
193 
195 
196 #endif
Definition: ZObject.h:57
#define END_NAMESPACE_Zeus
Definition: PlatformDefines.hpp:96
#define zeusbase_class
Definition: PlatformDefines.hpp:165
TThreadMessageQueue & m_rQueue
Message queue of the thread.
Definition: Thread.h:108
#define MQUALIFIER
Definition: LinuxPlatform.hpp:64
Definition: IThread.hpp:47
bool m_bInterrupted
Flag to check if the thread should terminate.
Definition: Thread.h:106
virtual void onStarted()
Definition: Thread.h:113
#define BEGIN_NAMESPACE_Zeus
Definition: PlatformDefines.hpp:95
Definition: ISynchronizeObject.hpp:48
Definition: IRunnable.hpp:48
Definition: ThreadMessageQueue.h:49
EPriority
Definition: Thread.h:68
unsigned long Uint32
Definition: PlatformDefines.hpp:241
Definition: CriticalSection.h:52
virtual void onTerminated()
Definition: Thread.h:112
double Float64
Definition: PlatformDefines.hpp:274
Definition: Thread.h:61
#define MEMORY_MANAGER_DECL
Definition: IZUnknownImplHelper.hpp:44


Written by Benjamin Hadorn http://www.xatlantis.ch.
Last change made on Tue Sep 13 2016 22:30:51