threading.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /******************************************************************************
  2. Copyright (c) 2013 by Hugh Bailey <[email protected]>
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely, subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not
  10. claim that you wrote the original software. If you use this software
  11. in a product, an acknowledgment in the product documentation would be
  12. appreciated but is not required.
  13. 2. Altered source versions must be plainly marked as such, and must not be
  14. misrepresented as being the original software.
  15. 3. This notice may not be removed or altered from any source
  16. distribution.
  17. ******************************************************************************/
  18. #ifndef BASE_THREADING_H
  19. #define BASE_THREADING_H
  20. /*
  21. * Allows posix thread usage on windows as well as other operating systems.
  22. * Use this header if you want to make your code more platform independent.
  23. *
  24. * Also provides a custom platform-independent "event" handler via
  25. * pthread conditional waits.
  26. */
  27. #include "c99defs.h"
  28. #ifdef _MSC_VER
  29. #include "../../w32-pthreads/pthread.h"
  30. #include "../../w32-pthreads/semaphore.h"
  31. #else
  32. #include <errno.h>
  33. #include <pthread.h>
  34. #include <semaphore.h>
  35. #endif
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif
  39. struct event_data {
  40. pthread_mutex_t mutex;
  41. pthread_cond_t cond;
  42. bool signalled;
  43. bool manual;
  44. };
  45. typedef struct event_data event_t;
  46. static inline int event_init(event_t *event, bool manual)
  47. {
  48. int code = 0;
  49. if ((code = pthread_mutex_init(&event->mutex, NULL)) < 0)
  50. return code;
  51. if ((code = pthread_cond_init(&event->cond, NULL)) < 0)
  52. pthread_mutex_destroy(&event->mutex);
  53. event->manual = manual;
  54. event->signalled = false;
  55. return code;
  56. }
  57. static inline void event_destroy(event_t *event)
  58. {
  59. if (event) {
  60. pthread_mutex_destroy(&event->mutex);
  61. pthread_cond_destroy(&event->cond);
  62. }
  63. }
  64. static inline int event_wait(event_t *event)
  65. {
  66. int code = 0;
  67. pthread_mutex_lock(&event->mutex);
  68. if ((code = pthread_cond_wait(&event->cond, &event->mutex)) == 0) {
  69. if (!event->manual)
  70. event->signalled = false;
  71. pthread_mutex_unlock(&event->mutex);
  72. }
  73. return code;
  74. }
  75. static inline int event_try(event_t *event)
  76. {
  77. pthread_mutex_lock(&event->mutex);
  78. if (event->signalled) {
  79. if (!event->manual)
  80. event->signalled = false;
  81. pthread_mutex_unlock(&event->mutex);
  82. return 0;
  83. }
  84. pthread_mutex_unlock(&event->mutex);
  85. return EAGAIN;
  86. }
  87. static inline int event_signal(event_t *event)
  88. {
  89. int code = 0;
  90. pthread_mutex_lock(&event->mutex);
  91. code = pthread_cond_signal(&event->cond);
  92. event->signalled = true;
  93. pthread_mutex_unlock(&event->mutex);
  94. return code;
  95. }
  96. static inline void event_reset(event_t *event)
  97. {
  98. pthread_mutex_lock(&event->mutex);
  99. event->signalled = false;
  100. pthread_mutex_unlock(&event->mutex);
  101. }
  102. #ifdef __cplusplus
  103. }
  104. #endif
  105. #endif