The open source OpenXR runtime
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

a/os: Add "initialized" flags to threading primitives (in debug builds)

Failling to init these (other than zero-init) works fine on Linux, but fails on Windows.
So now we at least have an assert on Linux.

authored by

Ryan Pavlik and committed by
Jakob Bornecrantz
14aed901 1aba4348

+38
+38
src/xrt/auxiliary/os/os_threading.h
··· 54 54 struct os_mutex 55 55 { 56 56 pthread_mutex_t mutex; 57 + #ifndef NDEBUG 58 + bool initialized; 59 + #endif 57 60 }; 58 61 59 62 /*! ··· 64 67 static inline int 65 68 os_mutex_init(struct os_mutex *om) 66 69 { 70 + assert(!om->initialized); 71 + #ifndef NDEBUG 72 + om->initialized = true; 73 + #endif 67 74 return pthread_mutex_init(&om->mutex, NULL); 68 75 } 69 76 ··· 75 82 static inline void 76 83 os_mutex_lock(struct os_mutex *om) 77 84 { 85 + assert(om->initialized); 78 86 pthread_mutex_lock(&om->mutex); 79 87 } 80 88 ··· 86 94 static inline int 87 95 os_mutex_trylock(struct os_mutex *om) 88 96 { 97 + assert(om->initialized); 89 98 return pthread_mutex_trylock(&om->mutex); 90 99 } 91 100 ··· 97 106 static inline void 98 107 os_mutex_unlock(struct os_mutex *om) 99 108 { 109 + assert(om->initialized); 100 110 pthread_mutex_unlock(&om->mutex); 101 111 } 102 112 ··· 108 118 static inline void 109 119 os_mutex_destroy(struct os_mutex *om) 110 120 { 121 + assert(om->initialized); 111 122 pthread_mutex_destroy(&om->mutex); 123 + #ifndef NDEBUG 124 + om->initialized = false; 125 + #endif 112 126 } 113 127 114 128 ··· 120 134 struct os_cond 121 135 { 122 136 pthread_cond_t cond; 137 + #ifndef NDEBUG 138 + bool initialized; 139 + #endif 123 140 }; 124 141 125 142 /*! ··· 130 147 static inline int 131 148 os_cond_init(struct os_cond *oc) 132 149 { 150 + assert(!oc->initialized); 151 + #ifndef NDEBUG 152 + oc->initialized = true; 153 + #endif 133 154 return pthread_cond_init(&oc->cond, NULL); 134 155 } 135 156 ··· 141 162 static inline void 142 163 os_cond_signal(struct os_cond *oc) 143 164 { 165 + assert(oc->initialized); 144 166 pthread_cond_signal(&oc->cond); 145 167 } 146 168 ··· 152 174 static inline void 153 175 os_cond_wait(struct os_cond *oc, struct os_mutex *om) 154 176 { 177 + assert(oc->initialized); 155 178 pthread_cond_wait(&oc->cond, &om->mutex); 156 179 } 157 180 ··· 163 186 static inline void 164 187 os_cond_destroy(struct os_cond *oc) 165 188 { 189 + assert(oc->initialized); 166 190 pthread_cond_destroy(&oc->cond); 191 + #ifndef NDEBUG 192 + oc->initialized = false; 193 + #endif 167 194 } 168 195 169 196 ··· 364 391 pthread_mutex_t mutex; 365 392 pthread_cond_t cond; 366 393 394 + bool initialized; 367 395 bool running; 368 396 }; 369 397 ··· 387 415 pthread_mutex_destroy(&oth->mutex); 388 416 return ret; 389 417 } 418 + oth->initialized = true; 390 419 391 420 return 0; 392 421 } ··· 401 430 { 402 431 pthread_mutex_lock(&oth->mutex); 403 432 433 + assert(oth->initialized); 404 434 if (oth->running) { 405 435 pthread_mutex_unlock(&oth->mutex); 406 436 return -1; ··· 431 461 { 432 462 // The fields are protected. 433 463 pthread_mutex_lock(&oth->mutex); 464 + assert(oth->initialized); 434 465 435 466 // Report we're stopping the thread. 436 467 oth->running = false; ··· 458 489 459 490 // The fields are protected. 460 491 pthread_mutex_lock(&oth->mutex); 492 + assert(oth->initialized); 461 493 462 494 if (!oth->running) { 495 + // it already exited 463 496 pthread_mutex_unlock(&oth->mutex); 464 497 return 0; 465 498 } ··· 482 515 /*! 483 516 * Destroy the thread helper, externally synchronizable. 484 517 * 518 + * Integrates a call to @ref os_thread_helper_stop, so you may just call this for full cleanup 519 + * 485 520 * @public @memberof os_thread_helper 486 521 */ 487 522 static inline void 488 523 os_thread_helper_destroy(struct os_thread_helper *oth) 489 524 { 525 + assert(oth->initialized); 490 526 // Stop the thread. 491 527 os_thread_helper_stop(oth); 492 528 493 529 // Destroy resources. 494 530 pthread_mutex_destroy(&oth->mutex); 495 531 pthread_cond_destroy(&oth->cond); 532 + oth->initialized = false; 496 533 } 497 534 498 535 /*! ··· 529 566 os_thread_helper_is_running(struct os_thread_helper *oth) 530 567 { 531 568 os_thread_helper_lock(oth); 569 + assert(oth->initialized); 532 570 bool ret = oth->running; 533 571 os_thread_helper_unlock(oth); 534 572 return ret;