threading.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. enum os_event_type {
  48. OS_EVENT_TYPE_AUTO,
  49. OS_EVENT_TYPE_MANUAL,
  50. };
  51. struct os_event_data;
  52. struct os_sem_data;
  53. typedef struct os_event_data os_event_t;
  54. typedef struct os_sem_data os_sem_t;
  55. EXPORT int os_event_init(os_event_t **event, enum os_event_type type);
  56. EXPORT void os_event_destroy(os_event_t *event);
  57. EXPORT int os_event_wait(os_event_t *event);
  58. EXPORT int os_event_timedwait(os_event_t *event, unsigned long milliseconds);
  59. EXPORT int os_event_try(os_event_t *event);
  60. EXPORT int os_event_signal(os_event_t *event);
  61. EXPORT void os_event_reset(os_event_t *event);
  62. EXPORT int os_sem_init(os_sem_t **sem, int value);
  63. EXPORT void os_sem_destroy(os_sem_t *sem);
  64. EXPORT int os_sem_post(os_sem_t *sem);
  65. EXPORT int os_sem_wait(os_sem_t *sem);
  66. EXPORT void os_set_thread_name(const char *name);
  67. #ifdef _MSC_VER
  68. #define THREAD_LOCAL __declspec(thread)
  69. #else
  70. #define THREAD_LOCAL __thread
  71. #endif
  72. #ifdef __cplusplus
  73. }
  74. #endif