threading.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  1. #if defined(GB_SYSTEM_LINUX)
  2. #include <signal.h>
  3. #if __has_include(<valgrind/helgrind.h>)
  4. #include <valgrind/helgrind.h>
  5. #define HAS_VALGRIND
  6. #endif
  7. #endif
  8. #if defined(GB_SYSTEM_WINDOWS)
  9. #pragma warning(push)
  10. #pragma warning(disable: 4505)
  11. #endif
  12. #if defined(HAS_VALGRIND)
  13. #define ANNOTATE_LOCK_PRE(m, t) VALGRIND_HG_MUTEX_LOCK_PRE(m, t)
  14. #define ANNOTATE_LOCK_POST(m) VALGRIND_HG_MUTEX_LOCK_POST(m)
  15. #define ANNOTATE_UNLOCK_PRE(m) VALGRIND_HG_MUTEX_UNLOCK_PRE(m)
  16. #define ANNOTATE_UNLOCK_POST(m) VALGRIND_HG_MUTEX_UNLOCK_POST(m)
  17. #define ANNOTATE_SEM_WAIT_POST(s) VALGRIND_HG_SEM_WAIT_POST(s)
  18. #define ANNOTATE_SEM_POST_PRE(s) VALGRIND_HG_SEM_POST_PRE(s)
  19. #else
  20. #define ANNOTATE_LOCK_PRE(m, t)
  21. #define ANNOTATE_LOCK_POST(m)
  22. #define ANNOTATE_UNLOCK_PRE(m)
  23. #define ANNOTATE_UNLOCK_POST(m)
  24. #define ANNOTATE_SEM_WAIT_POST(s)
  25. #define ANNOTATE_SEM_POST_PRE(s)
  26. #endif
  27. struct BlockingMutex;
  28. struct RecursiveMutex;
  29. struct RwMutex;
  30. struct Semaphore;
  31. struct Condition;
  32. struct Thread;
  33. struct ThreadPool;
  34. struct Parker;
  35. #define THREAD_PROC(name) isize name(struct Thread *thread)
  36. gb_internal THREAD_PROC(thread_pool_thread_proc);
  37. #define WORKER_TASK_PROC(name) isize name(void *data)
  38. typedef WORKER_TASK_PROC(WorkerTaskProc);
  39. typedef struct WorkerTask {
  40. WorkerTaskProc *do_work;
  41. void *data;
  42. } WorkerTask;
  43. struct Thread {
  44. #if defined(GB_SYSTEM_WINDOWS)
  45. void *win32_handle;
  46. #else
  47. pthread_t posix_handle;
  48. #endif
  49. isize idx;
  50. WorkerTask *queue;
  51. size_t capacity;
  52. std::atomic<uint64_t> head_and_tail;
  53. isize stack_size;
  54. struct ThreadPool *pool;
  55. };
  56. typedef std::atomic<i32> Futex;
  57. typedef volatile i32 Footex;
  58. gb_internal void futex_wait(Futex *addr, Footex val);
  59. gb_internal void futex_signal(Futex *addr);
  60. gb_internal void futex_broadcast(Futex *addr);
  61. gb_internal void mutex_lock (BlockingMutex *m);
  62. gb_internal bool mutex_try_lock(BlockingMutex *m);
  63. gb_internal void mutex_unlock (BlockingMutex *m);
  64. gb_internal void mutex_lock (RecursiveMutex *m);
  65. gb_internal bool mutex_try_lock(RecursiveMutex *m);
  66. gb_internal void mutex_unlock (RecursiveMutex *m);
  67. gb_internal void rw_mutex_lock (RwMutex *m);
  68. gb_internal bool rw_mutex_try_lock (RwMutex *m);
  69. gb_internal void rw_mutex_unlock (RwMutex *m);
  70. gb_internal void rw_mutex_shared_lock (RwMutex *m);
  71. gb_internal bool rw_mutex_try_shared_lock(RwMutex *m);
  72. gb_internal void rw_mutex_shared_unlock (RwMutex *m);
  73. gb_internal void semaphore_post(Semaphore *s, i32 count);
  74. gb_internal void semaphore_wait(Semaphore *s);
  75. gb_internal void condition_broadcast(Condition *c);
  76. gb_internal void condition_signal(Condition *c);
  77. gb_internal void condition_wait(Condition *c, BlockingMutex *m);
  78. gb_internal void park(Parker *p);
  79. gb_internal void unpark_one(Parker *p);
  80. gb_internal void unpark_all(Parker *p);
  81. gb_internal u32 thread_current_id(void);
  82. gb_internal void thread_init (ThreadPool *pool, Thread *t, isize idx);
  83. gb_internal void thread_init_and_start (ThreadPool *pool, Thread *t, isize idx);
  84. gb_internal void thread_join_and_destroy(Thread *t);
  85. gb_internal void thread_set_name (Thread *t, char const *name);
  86. gb_internal void yield_thread(void);
  87. gb_internal void yield_process(void);
  88. struct Wait_Signal {
  89. Futex futex;
  90. };
  91. gb_internal void wait_signal_until_available(Wait_Signal *ws) {
  92. if (ws->futex.load() == 0) {
  93. futex_wait(&ws->futex, 1);
  94. }
  95. }
  96. gb_internal void wait_signal_set(Wait_Signal *ws) {
  97. ws->futex.store(1);
  98. futex_broadcast(&ws->futex);
  99. }
  100. struct MutexGuard {
  101. MutexGuard() = delete;
  102. MutexGuard(MutexGuard const &) = delete;
  103. MutexGuard(MutexGuard &&) = delete;
  104. explicit MutexGuard(BlockingMutex *bm) noexcept : bm{bm} {
  105. mutex_lock(this->bm);
  106. }
  107. explicit MutexGuard(RecursiveMutex *rm) noexcept : rm{rm} {
  108. mutex_lock(this->rm);
  109. }
  110. explicit MutexGuard(RwMutex *rwm) noexcept : rwm{rwm} {
  111. rw_mutex_lock(this->rwm);
  112. }
  113. explicit MutexGuard(BlockingMutex &bm) noexcept : bm{&bm} {
  114. mutex_lock(this->bm);
  115. }
  116. explicit MutexGuard(RecursiveMutex &rm) noexcept : rm{&rm} {
  117. mutex_lock(this->rm);
  118. }
  119. explicit MutexGuard(RwMutex &rwm) noexcept : rwm{&rwm} {
  120. rw_mutex_lock(this->rwm);
  121. }
  122. ~MutexGuard() noexcept {
  123. if (this->bm) {
  124. mutex_unlock(this->bm);
  125. } else if (this->rm) {
  126. mutex_unlock(this->rm);
  127. } else if (this->rwm) {
  128. rw_mutex_unlock(this->rwm);
  129. }
  130. }
  131. operator bool() const noexcept { return true; }
  132. BlockingMutex *bm;
  133. RecursiveMutex *rm;
  134. RwMutex *rwm;
  135. };
  136. #define MUTEX_GUARD_BLOCK(m) if (MutexGuard GB_DEFER_3(_mutex_guard_){m})
  137. #define MUTEX_GUARD(m) mutex_lock(m); defer (mutex_unlock(m))
  138. #define RW_MUTEX_GUARD(m) rw_mutex_lock(m); defer (rw_mutex_unlock(m))
  139. struct RecursiveMutex {
  140. Futex owner;
  141. i32 recursion;
  142. };
  143. gb_internal void mutex_lock(RecursiveMutex *m) {
  144. Futex tid;
  145. tid.store(cast(i32)thread_current_id());
  146. for (;;) {
  147. i32 prev_owner = 0;
  148. m->owner.compare_exchange_strong(prev_owner, tid, std::memory_order_acquire, std::memory_order_acquire);
  149. if (prev_owner == 0 || prev_owner == tid) {
  150. m->recursion++;
  151. // inside the lock
  152. return;
  153. }
  154. futex_wait(&m->owner, prev_owner);
  155. }
  156. }
  157. gb_internal bool mutex_try_lock(RecursiveMutex *m) {
  158. Futex tid;
  159. tid.store(cast(i32)thread_current_id());
  160. i32 prev_owner = 0;
  161. m->owner.compare_exchange_strong(prev_owner, tid, std::memory_order_acquire, std::memory_order_acquire);
  162. if (prev_owner == 0 || prev_owner == tid) {
  163. m->recursion++;
  164. // inside the lock
  165. return true;
  166. }
  167. return false;
  168. }
  169. gb_internal void mutex_unlock(RecursiveMutex *m) {
  170. m->recursion--;
  171. if (m->recursion != 0) {
  172. return;
  173. }
  174. m->owner.exchange(0, std::memory_order_release);
  175. futex_signal(&m->owner);
  176. // outside the lock
  177. }
  178. struct Semaphore {
  179. Footex count_;
  180. Futex &count() noexcept {
  181. return *(Futex *)&this->count_;
  182. }
  183. Futex const &count() const noexcept {
  184. return *(Futex *)&this->count_;
  185. }
  186. };
  187. gb_internal void semaphore_post(Semaphore *s, i32 count) {
  188. s->count().fetch_add(count, std::memory_order_release);
  189. if (s->count().load() == 1) {
  190. futex_signal(&s->count());
  191. } else {
  192. futex_broadcast(&s->count());
  193. }
  194. }
  195. gb_internal void semaphore_wait(Semaphore *s) {
  196. for (;;) {
  197. i32 original_count = s->count().load(std::memory_order_relaxed);
  198. while (original_count == 0) {
  199. futex_wait(&s->count(), original_count);
  200. original_count = s->count().load(std::memory_order_relaxed);
  201. }
  202. if (s->count().compare_exchange_strong(original_count, original_count-1, std::memory_order_acquire, std::memory_order_acquire)) {
  203. return;
  204. }
  205. }
  206. }
  207. #if defined(GB_SYSTEM_WINDOWS)
  208. struct BlockingMutex {
  209. SRWLOCK srwlock;
  210. };
  211. gb_internal void mutex_lock(BlockingMutex *m) {
  212. AcquireSRWLockExclusive(&m->srwlock);
  213. }
  214. gb_internal bool mutex_try_lock(BlockingMutex *m) {
  215. return !!TryAcquireSRWLockExclusive(&m->srwlock);
  216. }
  217. gb_internal void mutex_unlock(BlockingMutex *m) {
  218. ReleaseSRWLockExclusive(&m->srwlock);
  219. }
  220. struct Condition {
  221. CONDITION_VARIABLE cond;
  222. };
  223. gb_internal void condition_broadcast(Condition *c) {
  224. WakeAllConditionVariable(&c->cond);
  225. }
  226. gb_internal void condition_signal(Condition *c) {
  227. WakeConditionVariable(&c->cond);
  228. }
  229. gb_internal void condition_wait(Condition *c, BlockingMutex *m) {
  230. SleepConditionVariableSRW(&c->cond, &m->srwlock, INFINITE, 0);
  231. }
  232. struct RwMutex {
  233. SRWLOCK srwlock;
  234. };
  235. gb_internal void rw_mutex_lock(RwMutex *m) {
  236. AcquireSRWLockExclusive(&m->srwlock);
  237. }
  238. gb_internal bool rw_mutex_try_lock(RwMutex *m) {
  239. return !!TryAcquireSRWLockExclusive(&m->srwlock);
  240. }
  241. gb_internal void rw_mutex_unlock(RwMutex *m) {
  242. ReleaseSRWLockExclusive(&m->srwlock);
  243. }
  244. gb_internal void rw_mutex_shared_lock(RwMutex *m) {
  245. AcquireSRWLockShared(&m->srwlock);
  246. }
  247. gb_internal bool rw_mutex_try_shared_lock(RwMutex *m) {
  248. return !!TryAcquireSRWLockShared(&m->srwlock);
  249. }
  250. gb_internal void rw_mutex_shared_unlock(RwMutex *m) {
  251. ReleaseSRWLockShared(&m->srwlock);
  252. }
  253. #else
  254. enum Internal_Mutex_State : i32 {
  255. Internal_Mutex_State_Unlocked = 0,
  256. Internal_Mutex_State_Locked = 1,
  257. Internal_Mutex_State_Waiting = 2,
  258. };
  259. struct BlockingMutex {
  260. #if defined(HAS_VALGRIND)
  261. // BlockingMutex() {
  262. // VALGRIND_HG_MUTEX_INIT_POST(this, 0);
  263. // }
  264. // ~BlockingMutex() {
  265. // VALGRIND_HG_MUTEX_DESTROY_PRE(this);
  266. // }
  267. #endif
  268. i32 state_;
  269. Futex &state() {
  270. return *(Futex *)&this->state_;
  271. }
  272. Futex const &state() const {
  273. return *(Futex const *)&this->state_;
  274. }
  275. };
  276. gb_no_inline gb_internal void mutex_lock_slow(BlockingMutex *m, i32 curr_state) {
  277. i32 new_state = curr_state;
  278. for (i32 spin = 0; spin < 100; spin++) {
  279. i32 state = Internal_Mutex_State_Unlocked;
  280. bool ok = m->state().compare_exchange_weak(state, new_state, std::memory_order_acquire, std::memory_order_consume);
  281. if (ok) {
  282. return;
  283. }
  284. if (state == Internal_Mutex_State_Waiting) {
  285. break;
  286. }
  287. for (i32 i = gb_min(spin+1, 32); i > 0; i--) {
  288. yield_thread();
  289. }
  290. }
  291. // Set just in case 100 iterations did not do it
  292. new_state = Internal_Mutex_State_Waiting;
  293. for (;;) {
  294. if (m->state().exchange(Internal_Mutex_State_Waiting, std::memory_order_acquire) == Internal_Mutex_State_Unlocked) {
  295. return;
  296. }
  297. futex_wait(&m->state(), new_state);
  298. yield_thread();
  299. }
  300. }
  301. gb_internal void mutex_lock(BlockingMutex *m) {
  302. ANNOTATE_LOCK_PRE(m, 0);
  303. i32 v = m->state().exchange(Internal_Mutex_State_Locked, std::memory_order_acquire);
  304. if (v != Internal_Mutex_State_Unlocked) {
  305. mutex_lock_slow(m, v);
  306. }
  307. ANNOTATE_LOCK_POST(m);
  308. }
  309. gb_internal bool mutex_try_lock(BlockingMutex *m) {
  310. ANNOTATE_LOCK_PRE(m, 1);
  311. i32 v = m->state().exchange(Internal_Mutex_State_Locked, std::memory_order_acquire);
  312. if (v == Internal_Mutex_State_Unlocked) {
  313. ANNOTATE_LOCK_POST(m);
  314. return true;
  315. }
  316. return false;
  317. }
  318. gb_no_inline gb_internal void mutex_unlock_slow(BlockingMutex *m) {
  319. futex_signal(&m->state());
  320. }
  321. gb_internal void mutex_unlock(BlockingMutex *m) {
  322. ANNOTATE_UNLOCK_PRE(m);
  323. i32 v = m->state().exchange(Internal_Mutex_State_Unlocked, std::memory_order_release);
  324. switch (v) {
  325. case Internal_Mutex_State_Unlocked:
  326. GB_PANIC("Unreachable");
  327. break;
  328. case Internal_Mutex_State_Locked:
  329. // Okay
  330. break;
  331. case Internal_Mutex_State_Waiting:
  332. mutex_unlock_slow(m);
  333. break;
  334. }
  335. ANNOTATE_UNLOCK_POST(m);
  336. }
  337. struct Condition {
  338. i32 state_;
  339. Futex &state() {
  340. return *(Futex *)&this->state_;
  341. }
  342. Futex const &state() const {
  343. return *(Futex const *)&this->state_;
  344. }
  345. };
  346. gb_internal void condition_broadcast(Condition *c) {
  347. c->state().fetch_add(1, std::memory_order_release);
  348. futex_broadcast(&c->state());
  349. }
  350. gb_internal void condition_signal(Condition *c) {
  351. c->state().fetch_add(1, std::memory_order_release);
  352. futex_signal(&c->state());
  353. }
  354. gb_internal void condition_wait(Condition *c, BlockingMutex *m) {
  355. i32 state = c->state().load(std::memory_order_relaxed);
  356. mutex_unlock(m);
  357. futex_wait(&c->state(), state);
  358. mutex_lock(m);
  359. }
  360. struct RwMutex {
  361. // TODO(bill): make this a proper RW mutex
  362. BlockingMutex mutex;
  363. };
  364. gb_internal void rw_mutex_lock(RwMutex *m) {
  365. mutex_lock(&m->mutex);
  366. }
  367. gb_internal bool rw_mutex_try_lock(RwMutex *m) {
  368. return mutex_try_lock(&m->mutex);
  369. }
  370. gb_internal void rw_mutex_unlock(RwMutex *m) {
  371. mutex_unlock(&m->mutex);
  372. }
  373. gb_internal void rw_mutex_shared_lock(RwMutex *m) {
  374. mutex_lock(&m->mutex);
  375. }
  376. gb_internal bool rw_mutex_try_shared_lock(RwMutex *m) {
  377. return mutex_try_lock(&m->mutex);
  378. }
  379. gb_internal void rw_mutex_shared_unlock(RwMutex *m) {
  380. mutex_unlock(&m->mutex);
  381. }
  382. #endif
  383. struct Parker {
  384. Futex state;
  385. };
  386. enum ParkerState : u32 {
  387. ParkerState_Empty = 0,
  388. ParkerState_Notified = 1,
  389. ParkerState_Parked = UINT32_MAX,
  390. };
  391. gb_internal void park(Parker *p) {
  392. if (p->state.fetch_sub(1, std::memory_order_acquire) == ParkerState_Notified) {
  393. return;
  394. }
  395. for (;;) {
  396. futex_wait(&p->state, ParkerState_Parked);
  397. i32 notified = ParkerState_Empty;
  398. if (p->state.compare_exchange_strong(notified, ParkerState_Empty, std::memory_order_acquire, std::memory_order_acquire)) {
  399. return;
  400. }
  401. }
  402. }
  403. gb_internal void unpark_one(Parker *p) {
  404. if (p->state.exchange(ParkerState_Notified, std::memory_order_release) == ParkerState_Parked) {
  405. futex_signal(&p->state);
  406. }
  407. }
  408. gb_internal void unpark_all(Parker *p) {
  409. if (p->state.exchange(ParkerState_Notified, std::memory_order_release) == ParkerState_Parked) {
  410. futex_broadcast(&p->state);
  411. }
  412. }
  413. gb_internal u32 thread_current_id(void) {
  414. u32 thread_id;
  415. #if defined(GB_SYSTEM_WINDOWS)
  416. #if defined(GB_ARCH_32_BIT) && defined(GB_CPU_X86)
  417. thread_id = (cast(u32 *)__readfsdword(24))[9];
  418. #elif defined(GB_ARCH_64_BIT) && defined(GB_CPU_X86)
  419. thread_id = (cast(u32 *)__readgsqword(48))[18];
  420. #else
  421. thread_id = GetCurrentThreadId();
  422. #endif
  423. #elif defined(GB_SYSTEM_OSX) && defined(GB_ARCH_64_BIT)
  424. thread_id = pthread_mach_thread_np(pthread_self());
  425. #elif defined(GB_ARCH_32_BIT) && defined(GB_CPU_X86)
  426. __asm__("mov %%gs:0x08,%0" : "=r"(thread_id));
  427. #elif defined(GB_ARCH_64_BIT) && defined(GB_CPU_X86)
  428. __asm__("mov %%fs:0x10,%0" : "=r"(thread_id));
  429. #elif defined(GB_SYSTEM_LINUX)
  430. thread_id = gettid();
  431. #else
  432. #error Unsupported architecture for thread_current_id()
  433. #endif
  434. return thread_id;
  435. }
  436. gb_internal gb_inline void yield_thread(void) {
  437. #if defined(GB_SYSTEM_WINDOWS)
  438. _mm_pause();
  439. #elif defined(GB_SYSTEM_OSX)
  440. #if defined(GB_CPU_X86)
  441. __asm__ volatile ("" : : : "memory");
  442. #elif defined(GB_CPU_ARM)
  443. __asm__ volatile ("yield" : : : "memory");
  444. #endif
  445. #elif defined(GB_CPU_X86)
  446. _mm_pause();
  447. #elif defined(GB_CPU_ARM)
  448. __asm__ volatile ("yield" : : : "memory");
  449. #else
  450. #error Unknown architecture
  451. #endif
  452. }
  453. gb_internal gb_inline void yield(void) {
  454. #if defined(GB_SYSTEM_WINDOWS)
  455. YieldProcessor();
  456. #else
  457. sched_yield();
  458. #endif
  459. }
  460. #if defined(GB_SYSTEM_WINDOWS)
  461. gb_internal DWORD __stdcall internal_thread_proc(void *arg) {
  462. Thread *t = cast(Thread *)arg;
  463. thread_pool_thread_proc(t);
  464. return 0;
  465. }
  466. #else
  467. gb_internal void *internal_thread_proc(void *arg) {
  468. #if (GB_SYSTEM_LINUX)
  469. // NOTE: Don't permit any signal delivery to threads on Linux.
  470. sigset_t mask = {};
  471. sigfillset(&mask);
  472. GB_ASSERT_MSG(pthread_sigmask(SIG_BLOCK, &mask, nullptr) == 0, "failed to block signals");
  473. #endif
  474. Thread *t = cast(Thread *)arg;
  475. thread_pool_thread_proc(t);
  476. return NULL;
  477. }
  478. #endif
  479. gb_internal void thread_init(ThreadPool *pool, Thread *t, isize idx) {
  480. gb_zero_item(t);
  481. #if defined(GB_SYSTEM_WINDOWS)
  482. t->win32_handle = INVALID_HANDLE_VALUE;
  483. #else
  484. t->posix_handle = 0;
  485. #endif
  486. t->capacity = 1 << 14; // must be a power of 2
  487. t->queue = gb_alloc_array(heap_allocator(), WorkerTask, t->capacity);
  488. t->head_and_tail = 0;
  489. t->pool = pool;
  490. t->idx = idx;
  491. }
  492. gb_internal void thread_init_and_start(ThreadPool *pool, Thread *t, isize idx) {
  493. thread_init(pool, t, idx);
  494. isize stack_size = 0;
  495. #if defined(GB_SYSTEM_WINDOWS)
  496. t->win32_handle = CreateThread(NULL, stack_size, internal_thread_proc, t, 0, NULL);
  497. GB_ASSERT_MSG(t->win32_handle != NULL, "CreateThread: GetLastError");
  498. #else
  499. {
  500. pthread_attr_t attr;
  501. pthread_attr_init(&attr);
  502. defer (pthread_attr_destroy(&attr));
  503. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
  504. if (stack_size != 0) {
  505. pthread_attr_setstacksize(&attr, stack_size);
  506. }
  507. pthread_create(&t->posix_handle, &attr, internal_thread_proc, t);
  508. }
  509. #endif
  510. }
  511. gb_internal void thread_join_and_destroy(Thread *t) {
  512. #if defined(GB_SYSTEM_WINDOWS)
  513. WaitForSingleObject(t->win32_handle, INFINITE);
  514. CloseHandle(t->win32_handle);
  515. t->win32_handle = INVALID_HANDLE_VALUE;
  516. #else
  517. pthread_join(t->posix_handle, NULL);
  518. t->posix_handle = 0;
  519. #endif
  520. gb_free(heap_allocator(), t->queue);
  521. }
  522. gb_internal void thread_set_name(Thread *t, char const *name) {
  523. #if defined(GB_COMPILER_MSVC)
  524. #pragma pack(push, 8)
  525. typedef struct {
  526. DWORD type;
  527. char const *name;
  528. DWORD id;
  529. DWORD flags;
  530. } gbprivThreadName;
  531. #pragma pack(pop)
  532. gbprivThreadName tn;
  533. tn.type = 0x1000;
  534. tn.name = name;
  535. tn.id = GetThreadId(cast(HANDLE)t->win32_handle);
  536. tn.flags = 0;
  537. __try {
  538. RaiseException(0x406d1388, 0, gb_size_of(tn)/4, cast(ULONG_PTR *)&tn);
  539. } __except(1 /*EXCEPTION_EXECUTE_HANDLER*/) {
  540. }
  541. #elif defined(GB_SYSTEM_WINDOWS) && !defined(GB_COMPILER_MSVC)
  542. // IMPORTANT TODO(bill): Set thread name for GCC/Clang on windows
  543. return;
  544. #elif defined(GB_SYSTEM_OSX)
  545. // TODO(bill): Test if this works
  546. pthread_setname_np(name);
  547. #elif defined(GB_SYSTEM_FREEBSD) || defined(GB_SYSTEM_OPENBSD)
  548. pthread_set_name_np(t->posix_handle, name);
  549. #else
  550. // TODO(bill): Test if this works
  551. pthread_setname_np(t->posix_handle, name);
  552. #endif
  553. }
  554. #if defined(GB_SYSTEM_LINUX)
  555. #include <linux/futex.h>
  556. #include <sys/syscall.h>
  557. gb_internal void futex_signal(Futex *addr) {
  558. int ret = syscall(SYS_futex, addr, FUTEX_WAKE | FUTEX_PRIVATE_FLAG, 1, NULL, NULL, 0);
  559. if (ret == -1) {
  560. perror("Futex wake");
  561. GB_PANIC("Failed in futex wake!\n");
  562. }
  563. }
  564. gb_internal void futex_broadcast(Futex *addr) {
  565. int ret = syscall(SYS_futex, addr, FUTEX_WAKE | FUTEX_PRIVATE_FLAG, INT32_MAX, NULL, NULL, 0);
  566. if (ret == -1) {
  567. perror("Futex wake");
  568. GB_PANIC("Failed in futex wake!\n");
  569. }
  570. }
  571. gb_internal void futex_wait(Futex *addr, Footex val) {
  572. for (;;) {
  573. int ret = syscall(SYS_futex, addr, FUTEX_WAIT | FUTEX_PRIVATE_FLAG, val, NULL, NULL, 0);
  574. if (ret == -1) {
  575. if (errno != EAGAIN && errno != EINTR) {
  576. perror("Futex wait");
  577. GB_PANIC("Failed in futex wait!\n");
  578. } else {
  579. return;
  580. }
  581. } else if (ret == 0) {
  582. if (*addr != val) {
  583. return;
  584. }
  585. }
  586. }
  587. }
  588. #elif defined(GB_SYSTEM_FREEBSD)
  589. #include <sys/types.h>
  590. #include <sys/umtx.h>
  591. gb_internal void futex_signal(Futex *addr) {
  592. _umtx_op(addr, UMTX_OP_WAKE, 1, 0, 0);
  593. }
  594. gb_internal void futex_broadcast(Futex *addr) {
  595. _umtx_op(addr, UMTX_OP_WAKE, INT32_MAX, 0, 0);
  596. }
  597. gb_internal void futex_wait(Futex *addr, Footex val) {
  598. for (;;) {
  599. int ret = _umtx_op(addr, UMTX_OP_WAIT_UINT, val, 0, NULL);
  600. if (ret == -1) {
  601. if (errno == ETIMEDOUT || errno == EINTR) {
  602. continue;
  603. }
  604. perror("Futex wait");
  605. GB_PANIC("Failed in futex wait!\n");
  606. } else if (ret == 0) {
  607. if (*addr != val) {
  608. return;
  609. }
  610. }
  611. }
  612. }
  613. #elif defined(GB_SYSTEM_OPENBSD)
  614. #include <sys/futex.h>
  615. gb_internal void futex_signal(Futex *f) {
  616. for (;;) {
  617. int ret = futex((volatile uint32_t *)f, FUTEX_WAKE | FUTEX_PRIVATE_FLAG, 1, NULL, NULL);
  618. if (ret == -1) {
  619. if (errno == ETIMEDOUT || errno == EINTR) {
  620. continue;
  621. }
  622. perror("Futex wake");
  623. GB_PANIC("futex wake fail");
  624. } else if (ret == 1) {
  625. return;
  626. }
  627. }
  628. }
  629. gb_internal void futex_broadcast(Futex *f) {
  630. for (;;) {
  631. int ret = futex((volatile uint32_t *)f, FUTEX_WAKE | FUTEX_PRIVATE_FLAG, INT32_MAX, NULL, NULL);
  632. if (ret == -1) {
  633. if (errno == ETIMEDOUT || errno == EINTR) {
  634. continue;
  635. }
  636. perror("Futex wake");
  637. GB_PANIC("futex wake fail");
  638. } else if (ret == 1) {
  639. return;
  640. }
  641. }
  642. }
  643. gb_internal void futex_wait(Futex *f, Footex val) {
  644. for (;;) {
  645. int ret = futex((volatile uint32_t *)f, FUTEX_WAIT | FUTEX_PRIVATE_FLAG, val, NULL, NULL);
  646. if (ret == -1) {
  647. if (*f != val) {
  648. return;
  649. }
  650. if (errno == ETIMEDOUT || errno == EINTR) {
  651. continue;
  652. }
  653. perror("Futex wait");
  654. GB_PANIC("Failed in futex wait!\n");
  655. }
  656. }
  657. }
  658. #elif defined(GB_SYSTEM_OSX)
  659. #define UL_COMPARE_AND_WAIT 0x00000001
  660. #define ULF_NO_ERRNO 0x01000000
  661. extern "C" int __ulock_wait(uint32_t operation, void *addr, uint64_t value, uint32_t timeout); /* timeout is specified in microseconds */
  662. extern "C" int __ulock_wake(uint32_t operation, void *addr, uint64_t wake_value);
  663. gb_internal void futex_signal(Futex *f) {
  664. for (;;) {
  665. int ret = __ulock_wake(UL_COMPARE_AND_WAIT | ULF_NO_ERRNO, f, 0);
  666. if (ret >= 0) {
  667. return;
  668. }
  669. if (ret == -EINTR || ret == -EFAULT) {
  670. continue;
  671. }
  672. if (ret == -ENOENT) {
  673. return;
  674. }
  675. GB_PANIC("Failed in futex wake!\n");
  676. }
  677. }
  678. gb_internal void futex_broadcast(Futex *f) {
  679. for (;;) {
  680. enum { ULF_WAKE_ALL = 0x00000100 };
  681. int ret = __ulock_wake(UL_COMPARE_AND_WAIT | ULF_NO_ERRNO | ULF_WAKE_ALL, f, 0);
  682. if (ret == 0) {
  683. return;
  684. }
  685. if (ret == -EINTR || ret == -EFAULT) {
  686. continue;
  687. }
  688. if (ret == -ENOENT) {
  689. return;
  690. }
  691. GB_PANIC("Failed in futex wake!\n");
  692. }
  693. }
  694. gb_internal void futex_wait(Futex *f, Footex val) {
  695. for (;;) {
  696. int ret = __ulock_wait(UL_COMPARE_AND_WAIT | ULF_NO_ERRNO, f, val, 0);
  697. if (ret >= 0) {
  698. if (*f != val) {
  699. return;
  700. }
  701. continue;
  702. }
  703. if (ret == -EINTR || ret == -EFAULT) {continue;
  704. ret = -ret;
  705. }
  706. if (ret == -ENOENT) {
  707. return;
  708. }
  709. GB_PANIC("Failed in futex wait!\n");
  710. }
  711. }
  712. #elif defined(GB_SYSTEM_WINDOWS)
  713. gb_internal void futex_signal(Futex *f) {
  714. WakeByAddressSingle(f);
  715. }
  716. gb_internal void futex_broadcast(Futex *f) {
  717. WakeByAddressAll(f);
  718. }
  719. gb_internal void futex_wait(Futex *f, Footex val) {
  720. do {
  721. WaitOnAddress(f, (void *)&val, sizeof(val), INFINITE);
  722. } while (f->load() == val);
  723. }
  724. #endif
  725. #if defined(GB_SYSTEM_WINDOWS)
  726. #pragma warning(pop)
  727. #endif