| 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 | bool maxPriority=false; |
| 111 | bool valid=false; |
| 112 | }; |
| 113 | } |
| 114 |
| 115 | #ifdef __APPLE__ |
| 116 | #include <dispatch/dispatch.h> |
| 117 | namespace tgvoip{ |
| 118 | class Semaphore{ |
| 119 | public: |
| 120 | Semaphore(unsigned int maxCount, unsigned int initValue){ |
| 121 | sem = dispatch_semaphore_create(initValue); |
| 122 | } |
| 123 |
| 124 | ~Semaphore(){ |
| 125 | #if ! __has_feature(objc_arc) |
| 126 | dispatch_release(sem); |
| 127 | #endif |
| 128 | } |
| 129 |
| 130 | void Acquire(){ |
| 131 | dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER); |
| 132 | } |
| 133 |
| 134 | void Release(){ |
| 135 | dispatch_semaphore_signal(sem); |
| 136 | } |
| 137 |
| 138 | void Acquire(int count){ |
| 139 | for(int i=0;i<count;i++) |
| 140 | Acquire(); |
| 141 | } |
| 142 |
| 143 | void Release(int count){ |
| 144 | for(int i=0;i<count;i++) |
| 145 | Release(); |
| 146 | } |
| 147 |
| 148 | private: |
| 149 | dispatch_semaphore_t sem; |
| 150 | }; |
| 151 | } |
| 152 | #else |
| 153 | namespace tgvoip{ |
| 154 | class Semaphore{ |
| 155 | public: |
| 156 | Semaphore(unsigned int maxCount, unsigned int initValue){ |
| 157 | sem_init(&sem, 0, initValue); |
| 158 | } |
| 159 |
| 160 | ~Semaphore(){ |
| 161 | sem_destroy(&sem); |
| 162 | } |
| 163 |
| 164 | void Acquire(){ |
| 165 | sem_wait(&sem); |
| 166 | } |
| 167 |
| 168 | void Release(){ |
| 169 | sem_post(&sem); |
| 170 | } |
| 171 |
| 172 | void Acquire(int count){ |
| 173 | for(int i=0;i<count;i++) |
| 174 | Acquire(); |
| 175 | } |
| 176 |
| 177 | void Release(int count){ |
| 178 | for(int i=0;i<count;i++) |
| 179 | Release(); |
| 180 | } |
| 181 |
| 182 | private: |
| 183 | sem_t sem; |
| 184 | }; |
| 185 | } |
| 186 | #endif |
| 187 |
| 188 | #elif defined(_WIN32) |
| 189 |
| 190 | #include <Windows.h> |
| 191 | #include <assert.h> |
| 192 |
| 193 | namespace tgvoip{ |
| 194 | class Mutex{ |
| 195 | public: |
| 196 | Mutex(){ |
| 197 | #if !defined(WINAPI_FAMILY) || WINAPI_FAMILY!=WINAPI_FAMILY_PHONE_APP |
| 198 | InitializeCriticalSection(§ion); |
| 199 | #else |
| 200 | InitializeCriticalSectionEx(§ion, 0, 0); |
| 201 | #endif |
| 202 | } |
| 203 |
| 204 | ~Mutex(){ |
| 205 | DeleteCriticalSection(§ion); |
| 206 | } |
| 207 |
| 208 | void Lock(){ |
| 209 | EnterCriticalSection(§ion); |
| 210 | } |
| 211 |
| 212 | void Unlock(){ |
| 213 | LeaveCriticalSection(§ion); |
| 214 | } |
| 215 |
| 216 | private: |
| 217 | Mutex(const Mutex& other); |
| 218 | CRITICAL_SECTION section; |
| 219 | }; |
| 220 |
| 221 | class Thread{ |
| 222 | public: |
| 223 | Thread(std::function<void()> entry) : entry(entry){ |
| 224 | name=NULL; |
| 225 | thread=NULL; |
| 226 | } |
| 227 |
| 228 | ~Thread(){ |
| 229 | } |
| 230 |
| 231 | void Start(){ |
| 232 | thread=CreateThread(NULL, 0, Thread::ActualEntryPoint, this, 0, &id); |
| 233 | } |
| 234 |
| 235 | void Join(){ |
| 236 | if(!thread) |
| 237 | return; |
| 238 | #if !defined(WINAPI_FAMILY) || WINAPI_FAMILY!=WINAPI_FAMILY_PHONE_APP |
| 239 | WaitForSingleObject(thread, INFINITE); |
| 240 | #else |
| 241 | WaitForSingleObjectEx(thread, INFINITE, false); |
| 242 | #endif |
| 243 | CloseHandle(thread); |
| 244 | } |
| 245 |
| 246 | void SetName(const char* name){ |
| 247 | this->name=name; |
| 248 | } |
| 249 |
| 250 | void SetMaxPriority(){ |
| 251 | SetThreadPriority(thread, THREAD_PRIORITY_HIGHEST); |
| 252 | } |
| 253 |
| 254 | static void Sleep(double seconds){ |
| 255 | ::Sleep((DWORD)(seconds*1000)); |
| 256 | } |
| 257 |
| 258 | bool IsCurrent(){ |
| 259 | return id==GetCurrentThreadId(); |
| 260 | } |
| 261 |
| 262 | private: |
| 263 | static const DWORD MS_VC_EXCEPTION=0x406D1388; |
| 264 |
| 265 | #pragma pack(push,8) |
| 266 | typedef struct tagTHREADNAME_INFO |
| 267 | { |
| 268 | DWORD dwType; // Must be 0x1000. |
| 269 | LPCSTR szName; // Pointer to name (in user addr space). |
| 270 | DWORD dwThreadID; // Thread ID (-1=caller thread). |
| 271 | DWORD dwFlags; // Reserved for future use, must be zero. |
| 272 | } THREADNAME_INFO; |
| 273 | #pragma pack(pop) |
| 274 |
| 275 | static DWORD WINAPI ActualEntryPoint(void* arg){ |
| 276 | Thread* self=reinterpret_cast<Thread*>(arg); |
| 277 | if(self->name){ |
| 278 | THREADNAME_INFO info; |
| 279 | info.dwType=0x1000; |
| 280 | info.szName=self->name; |
| 281 | info.dwThreadID=-1; |
| 282 | info.dwFlags=0; |
| 283 | __try{ |
| 284 | RaiseException(MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info); |
| 285 | }__except(EXCEPTION_EXECUTE_HANDLER){} |
| 286 | } |
| 287 | self->entry(); |
| 288 | return 0; |
| 289 | } |
| 290 | std::function<void()> entry; |
| 291 | HANDLE thread; |
| 292 | DWORD id; |
| 293 | const char* name; |
| 294 | }; |
| 295 |
| 296 | class Semaphore{ |
| 297 | public: |
| 298 | Semaphore(unsigned int maxCount, unsigned int initValue){ |
| 299 | #if !defined(WINAPI_FAMILY) || WINAPI_FAMILY!=WINAPI_FAMILY_PHONE_APP |
| 300 | h=CreateSemaphore(NULL, initValue, maxCount, NULL); |
| 301 | #else |
| 302 | h=CreateSemaphoreEx(NULL, initValue, maxCount, NULL, 0, SEMAPHORE_ALL_ACCESS); |
| 303 | assert(h); |
| 304 | #endif |
| 305 | } |
| 306 |
| 307 | ~Semaphore(){ |
| 308 | CloseHandle(h); |
| 309 | } |
| 310 |
| 311 | void Acquire(){ |
| 312 | #if !defined(WINAPI_FAMILY) || WINAPI_FAMILY!=WINAPI_FAMILY_PHONE_APP |
| 313 | WaitForSingleObject(h, INFINITE); |
| 314 | #else |
| 315 | WaitForSingleObjectEx(h, INFINITE, false); |
| 316 | #endif |
| 317 | } |
| 318 |
| 319 | void Release(){ |
| 320 | ReleaseSemaphore(h, 1, NULL); |
| 321 | } |
| 322 |
| 323 | void Acquire(int count){ |
| 324 | for(int i=0;i<count;i++) |
| 325 | Acquire(); |
| 326 | } |
| 327 |
| 328 | void Release(int count){ |
| 329 | ReleaseSemaphore(h, count, NULL); |
| 330 | } |
| 331 |
| 332 | private: |
| 333 | HANDLE h; |
| 334 | }; |
| 335 | } |
| 336 | #else |
| 337 | #error "No threading implementation for your operating system" |
| 338 | #endif |
| 339 |
| 340 | namespace tgvoip{ |
| 341 | class MutexGuard{ |
| 342 | public: |
| 343 | MutexGuard(Mutex &mutex) : mutex(mutex) { |
| 344 | mutex.Lock(); |
| 345 | } |
| 346 | ~MutexGuard(){ |
| 347 | mutex.Unlock(); |
| 348 | } |
| 349 | private: |
| 350 | Mutex &mutex; |
| 351 | }; |
| 352 | } |
| 353 |
| 354 | #endif //__THREADING_H |
| 355 |