threading.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright (c) 2023 Lain 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. #ifndef _MSC_VER
  26. #include <errno.h>
  27. #endif
  28. #include <pthread.h>
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. #ifdef _WIN32
  33. #include "threading-windows.h"
  34. #else
  35. #include "threading-posix.h"
  36. #endif
  37. /* this may seem strange, but you can't use it unless it's an initializer */
  38. static inline void pthread_mutex_init_value(pthread_mutex_t *mutex)
  39. {
  40. pthread_mutex_t init_val = PTHREAD_MUTEX_INITIALIZER;
  41. if (!mutex)
  42. return;
  43. *mutex = init_val;
  44. }
  45. static inline int pthread_mutex_init_recursive(pthread_mutex_t *mutex)
  46. {
  47. pthread_mutexattr_t attr;
  48. int ret = pthread_mutexattr_init(&attr);
  49. if (ret == 0) {
  50. ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
  51. if (ret == 0) {
  52. ret = pthread_mutex_init(mutex, &attr);
  53. }
  54. pthread_mutexattr_destroy(&attr);
  55. }
  56. return ret;
  57. }
  58. enum os_event_type {
  59. OS_EVENT_TYPE_AUTO,
  60. OS_EVENT_TYPE_MANUAL,
  61. };
  62. struct os_event_data;
  63. struct os_sem_data;
  64. typedef struct os_event_data os_event_t;
  65. typedef struct os_sem_data os_sem_t;
  66. EXPORT int os_event_init(os_event_t **event, enum os_event_type type);
  67. EXPORT void os_event_destroy(os_event_t *event);
  68. EXPORT int os_event_wait(os_event_t *event);
  69. EXPORT int os_event_timedwait(os_event_t *event, unsigned long milliseconds);
  70. EXPORT int os_event_try(os_event_t *event);
  71. EXPORT int os_event_signal(os_event_t *event);
  72. EXPORT void os_event_reset(os_event_t *event);
  73. EXPORT int os_sem_init(os_sem_t **sem, int value);
  74. EXPORT void os_sem_destroy(os_sem_t *sem);
  75. EXPORT int os_sem_post(os_sem_t *sem);
  76. EXPORT int os_sem_wait(os_sem_t *sem);
  77. EXPORT void os_set_thread_name(const char *name);
  78. #ifdef _MSC_VER
  79. #define THREAD_LOCAL __declspec(thread)
  80. #else
  81. #define THREAD_LOCAL __thread
  82. #endif
  83. #ifdef __cplusplus
  84. }
  85. #endif