Tactics: Western Philosophers Vs. Musicians  0.12
A turn-based tactical game combining rules and gameplay elements inspired by Final Fantasy Tactics and the Mayfair Exponential Game System. Unlike most games of this type, motion is in full, grid-less 3D.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
UThread.h
Go to the documentation of this file.
1 // Copyright (C) 2006-2015 Dylan Blair
3 //
4 // email: dblair@alumni.cs.utexas.edu
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 
21 #ifndef UCRITICAL_SECTION_H
22 #define UCRITICAL_SECTION_H
23 
24 #ifdef _WIN32
25 #ifndef WIN32_LEAN_AND_MEAN
26 #define WIN32_LEAN_AND_MEAN
27 #endif
28 #include <windows.h>
29 #else
30 #include <pthread.h>
31 #include <semaphore.h>
32 #endif
33 
34 #include <assert.h>
35 
36 namespace OpenSkyNet {
37  namespace Utils {
38  class LockableCS {
39  public:
40 #ifdef _WIN32
41 
42  inline LockableCS() throw() { InitializeCriticalSection(&_cS); }
43  inline ~LockableCS() throw() { DeleteCriticalSection(&_cS); }
44 
45  inline void lock() throw() { EnterCriticalSection(&_cS); }
46  inline void unlock() throw() { LeaveCriticalSection(&_cS); }
47 #else
48 
49  inline LockableCS() throw() {
50  pthread_mutexattr_t mutexAttr;
51  pthread_mutexattr_settype(&mutexAttr,PTHREAD_MUTEX_RECURSIVE_NP);
52  pthread_mutex_init(&_cS,&mutexAttr);
53  pthread_mutexattr_destroy(&mutexAttr);
54  }
55  inline ~LockableCS() throw() { pthread_mutex_destroy(&_cS); }
56 
57  inline void lock() throw() { pthread_mutex_lock(&_cS); }
58  inline void unlock() throw() { pthread_mutex_unlock(&_cS); }
59 #endif
60  private:
61 #ifdef _WIN32
62  CRITICAL_SECTION _cS;
63 #else
64  pthread_mutex_t _cS;
65 #endif
66  };
67 
68  class SyncEvent {
69  public:
70 #ifdef _WIN32
71  inline SyncEvent() throw() { _event = CreateEvent(NULL,FALSE,FALSE,NULL); }
72  inline ~SyncEvent() throw() { CloseHandle(_event); }
73 
74  inline void set() throw() { SetEvent(_event); }
75  inline void wait() throw() { WaitForSingleObject(_event,INFINITE); }
76 #else
77 
78  inline SyncEvent() throw() {
79  int error = sem_init(&_event,0,0);
80  assert(!error);
81  }
82  inline ~SyncEvent() throw() {
83  int error = sem_destroy(&_event);
84  assert(!error);
85  }
86 
87  inline void set() throw() {
88  int val = 0;
89  int error = sem_getvalue(&_event,&val);
90  assert(!error);
93  if (!val) {
94  error = sem_post(&_event);
95  assert(!error);
96  }
97  }
98  inline void wait() throw() {
99  int error = sem_wait(&_event);
100  assert(!error);
101  }
102 #endif
103  private:
104 #ifdef _WIN32
105  HANDLE _event;
106 #else
107  sem_t _event;
108 #endif
109  };
110 
111  class Thread {
112  public:
113 #ifdef _WIN32
114  Thread() : _hThread(0) {}
115 
116  void create(LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter) {
117  DWORD dwThreadId;
118  _hThread = CreateThread(
119  NULL, // default security attributes
120  0, // use default stack size
121  lpStartAddress, // thread function
122  lpParameter, // argument to thread function
123  0, // use default creation flags
124  &dwThreadId); // returns the thread identifier
125  }
126 
127  void wait() {
128  if (_hThread)
129  WaitForSingleObject(_hThread, INFINITE);
130  }
131 
132  void close() {
133  if (_hThread)
134  if (CloseHandle(_hThread))
135  _hThread = 0;
136  }
137 #else
138  Thread() : _thread(0) {}
139 
140  void create(void* (*start_address)(void*), void* arg) {
141  // initialize the thread attribute
142  pthread_attr_init(&_threadAttr);
143  // set the stack size of the thread
144  pthread_attr_setstacksize(&_threadAttr, 1048576);
145  // create the thread
146  pthread_create(&_thread, &_threadAttr, start_address, arg);
147  }
148 
149  void wait() {
150  if (_thread)
151  pthread_join(_thread, NULL);
152  }
153 
154  void close() {
155  if (_thread)
156  if (!pthread_attr_destroy(&_threadAttr))
157  _thread = 0;
158  }
159 #endif
160  private:
161 #ifdef _WIN32
162  HANDLE _hThread;
163 #else
164  pthread_t _thread;
165  pthread_attr_t _threadAttr;
166 #endif
167  };
168  }
169 }
170 
171 #endif //UCRITICAL_SECTION_H
void set()
Definition: UThread.h:87
void unlock()
Definition: UThread.h:58
LockableCS()
Definition: UThread.h:49
SyncEvent()
Definition: UThread.h:78
void wait()
Definition: UThread.h:98
Definition: UThread.h:111
Thread()
Definition: UThread.h:138
void wait()
Definition: UThread.h:149
Definition: UThread.h:68
void lock()
Definition: UThread.h:57
Definition: UThread.h:38
void create(void *(*start_address)(void *), void *arg)
Definition: UThread.h:140
~LockableCS()
Definition: UThread.h:55
void close()
Definition: UThread.h:154
~SyncEvent()
Definition: UThread.h:82