threading-windows.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (c) 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. #include "bmem.h"
  17. #include "threading.h"
  18. #define WIN32_LEAN_AND_MEAN
  19. #include <windows.h>
  20. struct os_event_data {
  21. HANDLE handle;
  22. };
  23. struct os_sem_data {
  24. HANDLE handle;
  25. };
  26. int os_event_init(os_event_t **event, enum os_event_type type)
  27. {
  28. HANDLE handle;
  29. struct os_event_data *data;
  30. handle = CreateEvent(NULL, (type == OS_EVENT_TYPE_MANUAL), FALSE, NULL);
  31. if (!handle)
  32. return -1;
  33. data = bmalloc(sizeof(struct os_event_data));
  34. data->handle = handle;
  35. *event = data;
  36. return 0;
  37. }
  38. void os_event_destroy(os_event_t *event)
  39. {
  40. if (event) {
  41. CloseHandle(event->handle);
  42. bfree(event);
  43. }
  44. }
  45. int os_event_wait(os_event_t *event)
  46. {
  47. DWORD code;
  48. if (!event)
  49. return EINVAL;
  50. code = WaitForSingleObject(event->handle, INFINITE);
  51. if (code != WAIT_OBJECT_0)
  52. return EINVAL;
  53. return 0;
  54. }
  55. int os_event_timedwait(os_event_t *event, unsigned long milliseconds)
  56. {
  57. DWORD code;
  58. if (!event)
  59. return EINVAL;
  60. code = WaitForSingleObject(event->handle, milliseconds);
  61. if (code == WAIT_TIMEOUT)
  62. return ETIMEDOUT;
  63. else if (code != WAIT_OBJECT_0)
  64. return EINVAL;
  65. return 0;
  66. }
  67. int os_event_try(os_event_t *event)
  68. {
  69. DWORD code;
  70. if (!event)
  71. return EINVAL;
  72. code = WaitForSingleObject(event->handle, 0);
  73. if (code == WAIT_TIMEOUT)
  74. return EAGAIN;
  75. else if (code != WAIT_OBJECT_0)
  76. return EINVAL;
  77. return 0;
  78. }
  79. int os_event_signal(os_event_t *event)
  80. {
  81. if (!event)
  82. return EINVAL;
  83. if (!SetEvent(event->handle))
  84. return EINVAL;
  85. return 0;
  86. }
  87. void os_event_reset(os_event_t *event)
  88. {
  89. if (!event)
  90. return;
  91. ResetEvent(event->handle);
  92. }
  93. int os_sem_init(os_sem_t **sem, int value)
  94. {
  95. HANDLE handle = CreateSemaphore(NULL, (LONG)value, 0x7FFFFFFF, NULL);
  96. if (!handle)
  97. return -1;
  98. *sem = bzalloc(sizeof(struct os_sem_data));
  99. (*sem)->handle = handle;
  100. return 0;
  101. }
  102. void os_sem_destroy(os_sem_t *sem)
  103. {
  104. if (sem) {
  105. CloseHandle(sem->handle);
  106. bfree(sem);
  107. }
  108. }
  109. int os_sem_post(os_sem_t *sem)
  110. {
  111. if (!sem) return -1;
  112. return ReleaseSemaphore(sem->handle, 1, NULL) ? 0 : -1;
  113. }
  114. int os_sem_wait(os_sem_t *sem)
  115. {
  116. DWORD ret;
  117. if (!sem) return -1;
  118. ret = WaitForSingleObject(sem->handle, INFINITE);
  119. return (ret == WAIT_OBJECT_0) ? 0 : -1;
  120. }
  121. long os_atomic_inc_long(volatile long *val)
  122. {
  123. return InterlockedIncrement(val);
  124. }
  125. long os_atomic_dec_long(volatile long *val)
  126. {
  127. return InterlockedDecrement(val);
  128. }