| 6 |
| 7 | #ifndef __THREADING_H |
| 8 | #define __THREADING_H |
| 9 |
| 10 | #include <functional> |
| 11 |
| 12 | #if defined(_POSIX_THREADS) || defined(_POSIX_VERSION) || defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)) |
| 13 |
| 14 | #include <pthread.h> |
| 15 | #include <semaphore.h> |
| 16 | #include <sched.h> |
| 17 | #include <unistd.h> |
| 18 | #ifdef __APPLE__ |
| 19 | #include "os/darwin/DarwinSpecific.h" |
| 20 | #endif |
| 21 |
| 22 | namespace tgvoip{ |
| 23 | class Mutex{ |
| 24 | public: |
| 25 | Mutex(){ |
| 26 | pthread_mutex_init(&mtx, NULL); |
| 27 | } |
| 28 |
| 29 | ~Mutex(){ |
| 30 | pthread_mutex_destroy(&mtx); |
| 31 | } |
| 32 |
| 33 | void Lock(){ |
| 34 | pthread_mutex_lock(&mtx); |
| 35 | } |
| 36 |
| 37 | void Unlock(){ |
| 38 | pthread_mutex_unlock(&mtx); |
| 39 | } |
| 40 |
| 41 | pthread_mutex_t* NativeHandle(){ |
| 42 | return &mtx; |
| 43 | } |
| 44 |
| 45 | private: |
| 46 | Mutex(const Mutex& other); |
| 47 | pthread_mutex_t mtx; |
| 48 | }; |
| 49 |
| 50 | class Thread{ |
| 51 | public: |
| 52 | Thread(std::function<void()> entry) : entry(entry){ |
| 53 | name=NULL; |
| 54 | thread=0; |
| 55 | } |
| 56 |
| 57 | virtual ~Thread(){ |
| 58 |
| 59 | } |
| 60 |
| 61 | void Start(){ |
| 62 | if(pthread_create(&thread, NULL, Thread::ActualEntryPoint, this)==0){ |
| 63 | valid=true; |
| 64 | } |
| 65 | } |
| 66 |
| 67 | void Join(){ |
| 68 | if(valid) |
| 69 | pthread_join(thread, NULL); |
| 70 | } |
| 71 |
| 72 | void SetName(const char* name){ |
| 73 | this->name=name; |
| 74 | } |
| 75 |
| 76 |
| 77 | void SetMaxPriority(){ |
| 78 | #ifdef __APPLE__ |
| 79 | maxPriority=true; |
| 80 | #endif |
| 81 | } |
| 82 |
| 83 | static void Sleep(double seconds){ |
| 84 | usleep((useconds_t)(seconds*1000000.0)); |
| 85 | } |
| 86 |
| 87 | bool IsCurrent(){ |
| 88 | return pthread_equal(thread, pthread_self())!=0; |
| 89 | } |
| 90 |
| 91 | private: |
| 92 | static void* ActualEntryPoint(void* arg){ |
| 93 | Thread* self=reinterpret_cast<Thread*>(arg); |
| 94 | if(self->name){ |
| 95 | #if !defined(__APPLE__) && !defined(__gnu_hurd__) |
| 96 | pthread_setname_np(self->thread, self->name); |
| 97 | #elif !defined(__gnu_hurd__) |
| 98 | pthread_setname_np(self->name); |
| 99 | if(self->maxPriority){ |
| 100 | DarwinSpecific::SetCurrentThreadPriority(DarwinSpecific::THREAD_PRIO_USER_INTERACTIVE); |
| 101 | } |
| 102 | #endif |
| 103 | } |
| 104 | self->entry(); |
| 105 | return NULL; |
| 106 | } |
| 107 | std::function<void()> entry; |
| 108 | pthread_t thread; |
| 109 | const char* name; |
| 110 | #ifdef __APPLE__ |
| 111 | bool maxPriority=false; |
| 112 | #endif |
| 113 | bool valid=false; |
| 114 | }; |
| 115 | } |
| 116 |
| 117 | #ifdef __APPLE__ |
| 118 | #include <dispatch/dispatch.h> |
| 119 | namespace tgvoip{ |
| 120 | class Semaphore{ |
| 121 | public: |
| 122 | Semaphore(unsigned int maxCount, unsigned int initValue){ |
| 123 | sem = dispatch_semaphore_create(initValue); |
| 124 | } |
| 125 |
| 126 | ~Semaphore(){ |
| 127 | #if ! __has_feature(objc_arc) |
| 128 | dispatch_release(sem); |
| 129 | #endif |
| 130 | } |
| 131 |
| 132 | void Acquire(){ |
| 133 | dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER); |
| 134 | } |
| 135 |
| 136 | void Release(){ |
| 137 | dispatch_semaphore_signal(sem); |
| 138 | } |
| 139 |
| 140 | void Acquire(int count){ |
| 141 | for(int i=0;i<count;i++) |
| 142 | Acquire(); |
| 143 | } |
| 144 |
| 145 | void Release(int count){ |
| 146 | for(int i=0;i<count;i++) |
| 147 | Release(); |
| 148 | } |
| 149 |
| 150 | private: |
| 151 | dispatch_semaphore_t sem; |
| 152 | }; |
| 153 | } |
| 154 | #else |
| 155 | namespace tgvoip{ |
| 156 | class Semaphore{ |
| 157 | public: |
| 158 | Semaphore(unsigned int maxCount, unsigned int initValue){ |
| 159 | sem_init(&sem, 0, initValue); |
| 160 | } |
| 161 |
| 162 | ~Semaphore(){ |
| 163 | sem_destroy(&sem); |
| 164 | } |
| 165 |
| 166 | void Acquire(){ |
| 167 | sem_wait(&sem); |
| 168 | } |
| 169 |
| 170 | void Release(){ |
| 171 | sem_post(&sem); |
| 172 | } |
| 173 |
| 174 | void Acquire(int count){ |
| 175 | for(int i=0;i<count;i++) |
| 176 | Acquire(); |
| 177 | } |
| 178 |
| 179 | void Release(int count){ |
| 180 | for(int i=0;i<count;i++) |
| 181 | Release(); |
| 182 | } |
| 183 |
| 184 | private: |
| 185 | sem_t sem; |
| 186 | }; |
| 187 | } |
| 188 | #endif |
| 189 |
| 190 | #elif defined(_WIN32) |
| 191 |
| 192 | #include <Windows.h> |
| 193 | #include <assert.h> |
| 194 |
| 195 | namespace tgvoip{ |
| 196 | class Mutex{ |
| 197 | public: |
| 198 | Mutex(){ |
| 199 | #if !defined(WINAPI_FAMILY) || WINAPI_FAMILY!=WINAPI_FAMILY_PHONE_APP |
| 200 | InitializeCriticalSection(§ion); |
| 201 | #else |
| 202 | InitializeCriticalSectionEx(§ion, 0, 0); |
| 203 | #endif |
| 204 | } |
| 205 |
| 206 | ~Mutex(){ |
| 207 | DeleteCriticalSection(§ion); |
| 208 | } |
| 209 |
| 210 | void Lock(){ |
| 211 | EnterCriticalSection(§ion); |
| 212 | } |
| 213 |
| 214 | void Unlock(){ |
| 215 | LeaveCriticalSection(§ion); |
| 216 | } |
| 217 |
| 218 | private: |
| 219 | Mutex(const Mutex& other); |
| 220 | CRITICAL_SECTION section; |
| 221 | }; |
| 222 |
| 223 | class Thread{ |
| 224 | public: |
| 225 | Thread(std::function<void()> entry) : entry(entry){ |
| 226 | name=NULL; |
| 227 | thread=NULL; |
| 228 | } |
| 229 |
| 230 | ~Thread(){ |
| 231 | } |
| 232 |
| 233 | void Start(){ |
| 234 | thread=CreateThread(NULL, 0, Thread::ActualEntryPoint, this, 0, &id); |
| 235 | } |
| 236 |
| 237 | void Join(){ |
| 238 | if(!thread) |
| 239 | return; |
| 240 | #if !defined(WINAPI_FAMILY) || WINAPI_FAMILY!=WINAPI_FAMILY_PHONE_APP |
| 241 | WaitForSingleObject(thread, INFINITE); |
| 242 | #else |
| 243 | WaitForSingleObjectEx(thread, INFINITE, false); |
| 244 | #endif |
| 245 | CloseHandle(thread); |
| 246 | } |
| 247 |
| 248 | void SetName(const char* name){ |
| 249 | this->name=name; |
| 250 | } |
| 251 |
| 252 | void SetMaxPriority(){ |
| 253 | SetThreadPriority(thread, THREAD_PRIORITY_HIGHEST); |
| 254 | } |
| 255 |
| 256 | static void Sleep(double seconds){ |
| 257 | ::Sleep((DWORD)(seconds*1000)); |
| 258 | } |
| 259 |
| 260 | bool IsCurrent(){ |
| 261 | return id==GetCurrentThreadId(); |
| 262 | } |
| 263 |
| 264 | private: |
| 265 | static const DWORD MS_VC_EXCEPTION=0x406D1388; |
| 266 |
| 267 | #pragma pack(push,8) |
| 268 | typedef struct tagTHREADNAME_INFO |
| 269 | { |
| 270 | DWORD dwType; // Must be 0x1000. |
| 271 | LPCSTR szName; // Pointer to name (in user addr space). |
| 272 | DWORD dwThreadID; // Thread ID (-1=caller thread). |
| 273 | DWORD dwFlags; // Reserved for future use, must be zero. |
| 274 | } THREADNAME_INFO; |
| 275 | #pragma pack(pop) |
| 276 |
| 277 | static DWORD WINAPI ActualEntryPoint(void* arg){ |
| 278 | Thread* self=reinterpret_cast<Thread*>(arg); |
| 279 | if(self->name){ |
| 280 | THREADNAME_INFO info; |
| 281 | info.dwType=0x1000; |
| 282 | info.szName=self->name; |
| 283 | info.dwThreadID=-1; |
| 284 | info.dwFlags=0; |
| 285 | __try{ |
| 286 | RaiseException(MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info); |
| 287 | }__except(EXCEPTION_EXECUTE_HANDLER){} |
| 288 | } |
| 289 | self->entry(); |
| 290 | return 0; |
| 291 | } |
| 292 | std::function<void()> entry; |
| 293 | HANDLE thread; |
| 294 | DWORD id; |
| 295 | const char* name; |
| 296 | }; |
| 297 |
| 298 | class Semaphore{ |
| 299 | public: |
| 300 | Semaphore(unsigned int maxCount, unsigned int initValue){ |
| 301 | #if !defined(WINAPI_FAMILY) || WINAPI_FAMILY!=WINAPI_FAMILY_PHONE_APP |
| 302 | h=CreateSemaphore(NULL, initValue, maxCount, NULL); |
| 303 | #else |
| 304 | h=CreateSemaphoreEx(NULL, initValue, maxCount, NULL, 0, SEMAPHORE_ALL_ACCESS); |
| 305 | assert(h); |
| 306 | #endif |
| 307 | } |
| 308 |
| 309 | ~Semaphore(){ |
| 310 | CloseHandle(h); |
| 311 | } |
| 312 |
| 313 | void Acquire(){ |
| 314 | #if !defined(WINAPI_FAMILY) || WINAPI_FAMILY!=WINAPI_FAMILY_PHONE_APP |
| 315 | WaitForSingleObject(h, INFINITE); |
| 316 | #else |
| 317 | WaitForSingleObjectEx(h, INFINITE, false); |
| 318 | #endif |
| 319 | } |
| 320 |
| 321 | void Release(){ |
| 322 | ReleaseSemaphore(h, 1, NULL); |
| 323 | } |
| 324 |
| 325 | void Acquire(int count){ |
| 326 | for(int i=0;i<count;i++) |
| 327 | Acquire(); |
| 328 | } |
| 329 |
| 330 | void Release(int count){ |
| 331 | ReleaseSemaphore(h, count, NULL); |
| 332 | } |
| 333 |
| 334 | private: |
| 335 | HANDLE h; |
| 336 | }; |
| 337 | } |
| 338 | #else |
| 339 | #error "No threading implementation for your operating system" |
| 340 | #endif |
| 341 |
| 342 | namespace tgvoip{ |
| 343 | class MutexGuard{ |
| 344 | public: |
| 345 | MutexGuard(Mutex &mutex) : mutex(mutex) { |
| 346 | mutex.Lock(); |
| 347 | } |
| 348 | ~MutexGuard(){ |
| 349 | mutex.Unlock(); |
| 350 | } |
| 351 | private: |
| 352 | Mutex &mutex; |
| 353 | }; |
| 354 | } |
| 355 |
| 356 | #endif //__THREADING_H |
| 357 |