threading.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (c) 2013-2014 Hugh Bailey <[email protected]>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #pragma once
  17. /*
  18. * Allows posix thread usage on windows as well as other operating systems.
  19. * Use this header if you want to make your code more platform independent.
  20. *
  21. * Also provides a custom platform-independent "event" handler via
  22. * pthread conditional waits.
  23. */
  24. #include "c99defs.h"
  25. #ifdef _MSC_VER
  26. #include "../../deps/w32-pthreads/pthread.h"
  27. #else
  28. #include <errno.h>
  29. #include <pthread.h>
  30. #endif
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. #ifdef _WIN32
  35. #include "threading-windows.h"
  36. #else
  37. #include "threading-posix.h"
  38. #endif
  39. /* this may seem strange, but you can't use it unless it's an initializer */
  40. static inline void pthread_mutex_init_value(pthread_mutex_t *mutex)
  41. {
  42. pthread_mutex_t init_val = PTHREAD_MUTEX_INITIALIZER;
  43. if (!mutex)
  44. return;
  45. *mutex = init_val;
  46. }
  47. static inline int pthread_mutex_init_recursive(pthread_mutex_t *mutex)
  48. {
  49. pthread_mutexattr_t attr;
  50. int ret = pthread_mutexattr_init(&attr);
  51. if (ret == 0) {
  52. ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
  53. if (ret == 0) {
  54. ret = pthread_mutex_init(mutex, &attr);
  55. }
  56. pthread_mutexattr_destroy(&attr);
  57. }
  58. return ret;
  59. }
  60. enum os_event_type {
  61. OS_EVENT_TYPE_AUTO,
  62. OS_EVENT_TYPE_MANUAL,
  63. };
  64. struct os_event_data;
  65. struct os_sem_data;
  66. typedef struct os_event_data os_event_t;
  67. typedef struct os_sem_data os_sem_t;
  68. EXPORT int os_event_init(os_event_t **event, enum os_event_type type);
  69. EXPORT void os_event_destroy(os_event_t *event);
  70. EXPORT int os_event_wait(os_event_t *event);
  71. EXPORT int os_event_timedwait(os_event_t *event, unsigned long milliseconds);
  72. EXPORT int os_event_try(os_event_t *event);
  73. EXPORT int os_event_signal(os_event_t *event);
  74. EXPORT void os_event_reset(os_event_t *event);
  75. EXPORT int os_sem_init(os_sem_t **sem, int value);
  76. EXPORT void os_sem_destroy(os_sem_t *sem);
  77. EXPORT int os_sem_post(os_sem_t *sem);
  78. EXPORT int os_sem_wait(os_sem_t *sem);
  79. EXPORT void os_set_thread_name(const char *name);
  80. #ifdef _MSC_VER
  81. #define THREAD_LOCAL __declspec(thread)
  82. #else
  83. #define THREAD_LOCAL __thread
  84. #endif
  85. #ifdef __cplusplus
  86. }
  87. #endif