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