ABY Framework  1.0
Arithmetic Bool Yao Framework
 All Classes Files Functions Variables Enumerations Enumerator Macros
thread.h
Go to the documentation of this file.
1 
19 #ifndef __THREAD_H__BY_SGCHOI
20 #define __THREAD_H__BY_SGCHOI
21 
22 #include "typedefs.h"
23 
24 #ifdef WIN32
25 
26 #include <process.h>
27 
28 class CEvent
29 {
30 // Constructor
31 public:
32  CEvent(BOOL bManualReset = FALSE, BOOL bInitialSet = FALSE)
33  {
34  m_hHandle = ::CreateEvent(0, bManualReset, bInitialSet, 0);
35  }
36 
37  ~CEvent()
38  {
39  CloseHandle(m_hHandle);
40  }
41 
42 // Operations
43 public:
44  BOOL Set() {return SetEvent(m_hHandle);}
45  BOOL Reset() {return ResetEvent(m_hHandle);}
46  BOOL Wait() {return WaitForSingleObject(m_hHandle, INFINITE) == WAIT_OBJECT_0;}
47 
48 private:
49  HANDLE m_hHandle;
50 };
51 
53 // CLock
54 
55 // Operations
56 class CLock
57 {
58 // Constructor
59 public:
60  CLock() {InitializeCriticalSection(&m_cs);}
61  ~CLock() {DeleteCriticalSection(&m_cs);}
62 
63 public:
64  void Lock() {EnterCriticalSection(&m_cs);}
65  void Unlock() {LeaveCriticalSection(&m_cs);}
66 
67 private:
68  CRITICAL_SECTION m_cs;
69 };
70 
71 class CThread
72 {
73 public:
74  CThread() {m_bRunning = FALSE; m_hHandle = NULL;}
75  virtual ~CThread() {if(m_hHandle != NULL) CloseHandle(m_hHandle);}
76 
77 public:
78  BOOL Start()
79  {
80  m_bRunning = TRUE;
81  m_hHandle = CreateThread(0, 0, ThreadMainHandler, this,0,0);
82  if( m_hHandle == NULL )
83  m_bRunning = FALSE;
84 
85  return m_bRunning;
86  }
87 
88  BOOL Wait()
89  {
90  if( !m_bRunning ) return TRUE;
91  return WaitForSingleObject(m_hHandle, INFINITE) == WAIT_OBJECT_0;
92  }
93 
94  BOOL Kill()
95  {
96  if( !m_bRunning) return TRUE;
97 
98  m_bRunning = !(TerminateThread(m_hHandle, 0));
99  return !m_bRunning;
100  }
101 
102  BOOL IsRunning()
103  {
104  return m_bRunning;
105  }
106 
107 protected:
108  virtual void ThreadMain() = 0;
109 
110  static DWORD __stdcall ThreadMainHandler( void* p )
111  {
112  CThread* pThis = (CThread*) p;
113  pThis->ThreadMain();
114  pThis->m_bRunning = FALSE;
115  return 0;
116  }
117 
118 protected:
119  BOOL m_bRunning;
120  HANDLE m_hHandle;
121 };
122 
123 #else // NOT WIN32
124 #include <pthread.h>
125 class CThread {
126 public:
127  CThread() {
128  m_bRunning = FALSE;
129  }
130  virtual ~CThread() {
131  }
132 
133 public:
134  BOOL Start() {
135  m_bRunning = !pthread_create(&m_pThread, NULL, ThreadMainHandler, (void*) this);
136  return m_bRunning;
137  }
138 
139  BOOL Wait() {
140  if (!m_bRunning)
141  return TRUE;
142  return pthread_join(m_pThread, NULL) == 0;
143  }
144 
145  BOOL Kill() {
146  if (!m_bRunning)
147  return TRUE;
148  pthread_exit(NULL);
149  return TRUE;
150  }
151 
152  BOOL IsRunning() {
153  return m_bRunning;
154  }
155 
156 protected:
157  virtual void ThreadMain() = 0;
158  static void* ThreadMainHandler(void* p) {
159  CThread* pThis = (CThread*) p;
160  pThis->ThreadMain();
161  pThis->m_bRunning = FALSE;
162  return 0;
163  }
164 
165 protected:
166  BOOL m_bRunning;
167  pthread_t m_pThread;
168 };
169 
170 class CLock {
171 // Constructor
172 public:
173  CLock() {
174  pthread_mutex_init(&m_mtx, NULL);
175  }
176  ~CLock() {
177  pthread_mutex_destroy(&m_mtx);
178  }
179 
180 public:
181  void Lock() {
182  pthread_mutex_lock(&m_mtx);
183  }
184  void Unlock() {
185  pthread_mutex_unlock(&m_mtx);
186  }
187 
188 private:
189  pthread_mutex_t m_mtx;
190 };
191 
192 class CEvent {
193 // Constructor
194 public:
195  CEvent(BOOL bManualReset = FALSE, BOOL bInitialSet = FALSE) {
196  pthread_mutex_init(&m_mtx, NULL);
197  pthread_cond_init(&m_cnd, NULL);
198  m_bManual = bManualReset;
199  m_bSet = bInitialSet;
200  }
201 
202  ~CEvent() {
203  pthread_mutex_destroy(&m_mtx);
204  pthread_cond_destroy(&m_cnd);
205  }
206 
207 // Operations
208 public:
209  BOOL Set() {
210  pthread_mutex_lock(&m_mtx);
211  if (!m_bSet) {
212  m_bSet = TRUE;
213 
214  pthread_cond_signal(&m_cnd);
215  }
216 
217  pthread_mutex_unlock(&m_mtx);
218  return TRUE;
219  }
220 
221  BOOL Wait() {
222  pthread_mutex_lock(&m_mtx);
223 
224  while (!m_bSet) {
225  pthread_cond_wait(&m_cnd, &m_mtx);
226  }
227 
228  if (!m_bManual)
229  m_bSet = FALSE;
230  pthread_mutex_unlock(&m_mtx);
231  return TRUE;
232  }
233 
234  BOOL Reset() {
235  pthread_mutex_lock(&m_mtx);
236  m_bSet = FALSE;
237  pthread_mutex_unlock(&m_mtx);
238  return TRUE;
239  }
240 private:
241  pthread_cond_t m_cnd;
242  pthread_mutex_t m_mtx;
243  BOOL m_bManual;
244  BOOL m_bSet;
245 };
246 
247 #endif //WIN32
248 
249 class CGrabLock {
250 public:
251  CGrabLock(CLock& l) :
252  lock(l) {
253  lock.Lock();
254  }
255  ~CGrabLock() {
256  lock.Unlock();
257  }
258 public:
259  CLock& lock;
260 };
261 
262 #endif //__THREAD_H__BY_SGCHOI
263 
Definition: thread.h:125
Typedefs Implementation.
Definition: thread.h:192
Definition: thread.h:170
Definition: thread.h:249