threading.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. /* this may seem strange, but you can't use it unless it's an initializer */
  35. static inline void pthread_mutex_init_value(pthread_mutex_t *mutex)
  36. {
  37. pthread_mutex_t init_val = PTHREAD_MUTEX_INITIALIZER;
  38. if (!mutex)
  39. return;
  40. *mutex = init_val;
  41. }
  42. enum os_event_type {
  43. OS_EVENT_TYPE_AUTO,
  44. OS_EVENT_TYPE_MANUAL
  45. };
  46. struct os_event_data;
  47. struct os_sem_data;
  48. typedef struct os_event_data os_event_t;
  49. typedef struct os_sem_data os_sem_t;
  50. EXPORT int os_event_init(os_event_t **event, enum os_event_type type);
  51. EXPORT void os_event_destroy(os_event_t *event);
  52. EXPORT int os_event_wait(os_event_t *event);
  53. EXPORT int os_event_timedwait(os_event_t *event, unsigned long milliseconds);
  54. EXPORT int os_event_try(os_event_t *event);
  55. EXPORT int os_event_signal(os_event_t *event);
  56. EXPORT void os_event_reset(os_event_t *event);
  57. EXPORT int os_sem_init(os_sem_t **sem, int value);
  58. EXPORT void os_sem_destroy(os_sem_t *sem);
  59. EXPORT int os_sem_post(os_sem_t *sem);
  60. EXPORT int os_sem_wait(os_sem_t *sem);
  61. EXPORT long os_atomic_inc_long(volatile long *val);
  62. EXPORT long os_atomic_dec_long(volatile long *val);
  63. #ifdef __cplusplus
  64. }
  65. #endif