hidapi_thread_sdl.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. /* Barrier implementation because Android/Bionic don't have pthread_barrier.
  19. This implementation came from Brent Priddy and was posted on
  20. StackOverflow. It is used with his permission. */
  21. typedef struct _SDL_ThreadBarrier
  22. {
  23. SDL_Mutex *mutex;
  24. SDL_Condition *cond;
  25. Uint32 count;
  26. Uint32 trip_count;
  27. } SDL_ThreadBarrier;
  28. static int SDL_CreateThreadBarrier(SDL_ThreadBarrier *barrier, Uint32 count)
  29. {
  30. SDL_assert(barrier != NULL);
  31. SDL_assert(count != 0);
  32. barrier->mutex = SDL_CreateMutex();
  33. if (barrier->mutex == NULL) {
  34. return -1; /* Error set by CreateMutex */
  35. }
  36. barrier->cond = SDL_CreateCondition();
  37. if (barrier->cond == NULL) {
  38. return -1; /* Error set by CreateCond */
  39. }
  40. barrier->trip_count = count;
  41. barrier->count = 0;
  42. return 0;
  43. }
  44. static void SDL_DestroyThreadBarrier(SDL_ThreadBarrier *barrier)
  45. {
  46. SDL_DestroyCondition(barrier->cond);
  47. SDL_DestroyMutex(barrier->mutex);
  48. }
  49. static int SDL_WaitThreadBarrier(SDL_ThreadBarrier *barrier)
  50. {
  51. SDL_LockMutex(barrier->mutex);
  52. barrier->count += 1;
  53. if (barrier->count >= barrier->trip_count) {
  54. barrier->count = 0;
  55. SDL_BroadcastCondition(barrier->cond);
  56. SDL_UnlockMutex(barrier->mutex);
  57. return 1;
  58. }
  59. SDL_WaitCondition(barrier->cond, barrier->mutex);
  60. SDL_UnlockMutex(barrier->mutex);
  61. return 0;
  62. }
  63. #include "../../thread/SDL_systhread.h"
  64. #define HIDAPI_THREAD_TIMED_OUT SDL_MUTEX_TIMEDOUT
  65. typedef Uint64 hidapi_timespec;
  66. typedef struct
  67. {
  68. SDL_Thread *thread;
  69. SDL_Mutex *mutex; /* Protects input_reports */
  70. SDL_Condition *condition;
  71. SDL_ThreadBarrier barrier; /* Ensures correct startup sequence */
  72. } hidapi_thread_state;
  73. static void hidapi_thread_state_init(hidapi_thread_state *state)
  74. {
  75. state->mutex = SDL_CreateMutex();
  76. state->condition = SDL_CreateCondition();
  77. SDL_CreateThreadBarrier(&state->barrier, 2);
  78. }
  79. static void hidapi_thread_state_destroy(hidapi_thread_state *state)
  80. {
  81. SDL_DestroyThreadBarrier(&state->barrier);
  82. SDL_DestroyCondition(state->condition);
  83. SDL_DestroyMutex(state->mutex);
  84. }
  85. static void hidapi_thread_cleanup_push(void (*routine)(void *), void *arg)
  86. {
  87. /* There isn't an equivalent in SDL, and it's only useful for threads calling hid_read_timeout() */
  88. }
  89. static void hidapi_thread_cleanup_pop(int execute)
  90. {
  91. }
  92. static void hidapi_thread_mutex_lock(hidapi_thread_state *state)
  93. {
  94. SDL_LockMutex(state->mutex);
  95. }
  96. static void hidapi_thread_mutex_unlock(hidapi_thread_state *state)
  97. {
  98. SDL_UnlockMutex(state->mutex);
  99. }
  100. static void hidapi_thread_cond_wait(hidapi_thread_state *state)
  101. {
  102. SDL_WaitCondition(state->condition, state->mutex);
  103. }
  104. static int hidapi_thread_cond_timedwait(hidapi_thread_state *state, hidapi_timespec *ts)
  105. {
  106. Sint64 timeout_ns;
  107. Sint32 timeout_ms;
  108. timeout_ns = (Sint64)(*ts - SDL_GetTicksNS());
  109. if (timeout_ns <= 0) {
  110. timeout_ms = 0;
  111. } else {
  112. timeout_ms = (Sint32)SDL_NS_TO_MS(timeout_ns);
  113. }
  114. return SDL_WaitConditionTimeout(state->condition, state->mutex, timeout_ms);
  115. }
  116. static void hidapi_thread_cond_signal(hidapi_thread_state *state)
  117. {
  118. SDL_SignalCondition(state->condition);
  119. }
  120. static void hidapi_thread_cond_broadcast(hidapi_thread_state *state)
  121. {
  122. SDL_BroadcastCondition(state->condition);
  123. }
  124. static void hidapi_thread_barrier_wait(hidapi_thread_state *state)
  125. {
  126. SDL_WaitThreadBarrier(&state->barrier);
  127. }
  128. typedef struct
  129. {
  130. void *(*func)(void*);
  131. void *func_arg;
  132. } RunInputThreadParam;
  133. static int RunInputThread(void *param)
  134. {
  135. RunInputThreadParam *data = (RunInputThreadParam *)param;
  136. void *(*func)(void*) = data->func;
  137. void *func_arg = data->func_arg;
  138. SDL_free(data);
  139. func(func_arg);
  140. return 0;
  141. }
  142. static void hidapi_thread_create(hidapi_thread_state *state, void *(*func)(void*), void *func_arg)
  143. {
  144. RunInputThreadParam *param = (RunInputThreadParam *)malloc(sizeof(*param));
  145. /* Note that the hidapi code didn't check for thread creation failure.
  146. * We'll crash if malloc() fails
  147. */
  148. param->func = func;
  149. param->func_arg = func_arg;
  150. state->thread = SDL_CreateThread(RunInputThread, "libusb", param);
  151. }
  152. static void hidapi_thread_join(hidapi_thread_state *state)
  153. {
  154. SDL_WaitThread(state->thread, NULL);
  155. }
  156. static void hidapi_thread_gettime(hidapi_timespec *ts)
  157. {
  158. *ts = SDL_GetTicksNS();
  159. }
  160. static void hidapi_thread_addtime(hidapi_timespec *ts, int milliseconds)
  161. {
  162. *ts += SDL_MS_TO_NS(milliseconds);
  163. }