espidf_thread.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #ifndef _GNU_SOURCE
  6. #define _GNU_SOURCE
  7. #endif
  8. #include "platform_api_vmcore.h"
  9. #include "platform_api_extension.h"
  10. typedef struct {
  11. thread_start_routine_t start;
  12. void *arg;
  13. } thread_wrapper_arg;
  14. static void *
  15. os_thread_wrapper(void *arg)
  16. {
  17. thread_wrapper_arg *targ = arg;
  18. thread_start_routine_t start_func = targ->start;
  19. void *thread_arg = targ->arg;
  20. #if 0
  21. os_printf("THREAD CREATED %jx\n", (uintmax_t)(uintptr_t)pthread_self());
  22. #endif
  23. BH_FREE(targ);
  24. start_func(thread_arg);
  25. return NULL;
  26. }
  27. korp_tid
  28. os_self_thread(void)
  29. {
  30. /* only allowed if this is a thread, xTaskCreate is not enough look at
  31. * product_mini for how to use this*/
  32. return pthread_self();
  33. }
  34. int
  35. os_mutex_init(korp_mutex *mutex)
  36. {
  37. return pthread_mutex_init(mutex, NULL);
  38. }
  39. int
  40. os_recursive_mutex_init(korp_mutex *mutex)
  41. {
  42. int ret;
  43. pthread_mutexattr_t mattr;
  44. assert(mutex);
  45. ret = pthread_mutexattr_init(&mattr);
  46. if (ret)
  47. return BHT_ERROR;
  48. pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_RECURSIVE);
  49. ret = pthread_mutex_init(mutex, &mattr);
  50. pthread_mutexattr_destroy(&mattr);
  51. return ret == 0 ? BHT_OK : BHT_ERROR;
  52. }
  53. int
  54. os_mutex_destroy(korp_mutex *mutex)
  55. {
  56. return pthread_mutex_destroy(mutex);
  57. }
  58. int
  59. os_mutex_lock(korp_mutex *mutex)
  60. {
  61. return pthread_mutex_lock(mutex);
  62. }
  63. int
  64. os_mutex_unlock(korp_mutex *mutex)
  65. {
  66. return pthread_mutex_unlock(mutex);
  67. }
  68. int
  69. os_thread_create_with_prio(korp_tid *tid, thread_start_routine_t start,
  70. void *arg, unsigned int stack_size, int prio)
  71. {
  72. pthread_attr_t tattr;
  73. thread_wrapper_arg *targ;
  74. assert(stack_size > 0);
  75. assert(tid);
  76. assert(start);
  77. pthread_attr_init(&tattr);
  78. pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_JOINABLE);
  79. if (pthread_attr_setstacksize(&tattr, stack_size) != 0) {
  80. os_printf("Invalid thread stack size %u. Min stack size = %u",
  81. stack_size, PTHREAD_STACK_MIN);
  82. pthread_attr_destroy(&tattr);
  83. return BHT_ERROR;
  84. }
  85. targ = (thread_wrapper_arg *)BH_MALLOC(sizeof(*targ));
  86. if (!targ) {
  87. pthread_attr_destroy(&tattr);
  88. return BHT_ERROR;
  89. }
  90. targ->start = start;
  91. targ->arg = arg;
  92. if (pthread_create(tid, &tattr, os_thread_wrapper, targ) != 0) {
  93. pthread_attr_destroy(&tattr);
  94. os_free(targ);
  95. return BHT_ERROR;
  96. }
  97. pthread_attr_destroy(&tattr);
  98. return BHT_OK;
  99. }
  100. int
  101. os_thread_create(korp_tid *tid, thread_start_routine_t start, void *arg,
  102. unsigned int stack_size)
  103. {
  104. return os_thread_create_with_prio(tid, start, arg, stack_size,
  105. BH_THREAD_DEFAULT_PRIORITY);
  106. }
  107. int
  108. os_thread_join(korp_tid thread, void **retval)
  109. {
  110. return pthread_join(thread, retval);
  111. }
  112. int
  113. os_thread_detach(korp_tid tid)
  114. {
  115. return pthread_detach(tid);
  116. }
  117. void
  118. os_thread_exit(void *retval)
  119. {
  120. pthread_exit(retval);
  121. }
  122. int
  123. os_cond_init(korp_cond *cond)
  124. {
  125. return pthread_cond_init(cond, NULL);
  126. }
  127. int
  128. os_cond_destroy(korp_cond *cond)
  129. {
  130. return pthread_cond_destroy(cond);
  131. }
  132. int
  133. os_cond_wait(korp_cond *cond, korp_mutex *mutex)
  134. {
  135. return pthread_cond_wait(cond, mutex);
  136. }
  137. static void
  138. msec_nsec_to_abstime(struct timespec *ts, uint64 usec)
  139. {
  140. struct timeval tv;
  141. time_t tv_sec_new;
  142. long int tv_nsec_new;
  143. gettimeofday(&tv, NULL);
  144. tv_sec_new = (time_t)(tv.tv_sec + usec / 1000000);
  145. if (tv_sec_new >= tv.tv_sec) {
  146. ts->tv_sec = tv_sec_new;
  147. }
  148. else {
  149. /* integer overflow */
  150. ts->tv_sec = BH_TIME_T_MAX;
  151. os_printf("Warning: os_cond_reltimedwait exceeds limit, "
  152. "set to max timeout instead\n");
  153. }
  154. tv_nsec_new = (long int)(tv.tv_usec * 1000 + (usec % 1000000) * 1000);
  155. if (tv.tv_usec * 1000 >= tv.tv_usec && tv_nsec_new >= tv.tv_usec * 1000) {
  156. ts->tv_nsec = tv_nsec_new;
  157. }
  158. else {
  159. /* integer overflow */
  160. ts->tv_nsec = LONG_MAX;
  161. os_printf("Warning: os_cond_reltimedwait exceeds limit, "
  162. "set to max timeout instead\n");
  163. }
  164. if (ts->tv_nsec >= 1000000000L && ts->tv_sec < BH_TIME_T_MAX) {
  165. ts->tv_sec++;
  166. ts->tv_nsec -= 1000000000L;
  167. }
  168. }
  169. int
  170. os_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, uint64 useconds)
  171. {
  172. int ret;
  173. struct timespec abstime;
  174. if (useconds == BHT_WAIT_FOREVER)
  175. ret = pthread_cond_wait(cond, mutex);
  176. else {
  177. msec_nsec_to_abstime(&abstime, useconds);
  178. ret = pthread_cond_timedwait(cond, mutex, &abstime);
  179. }
  180. if (ret != BHT_OK && ret != ETIMEDOUT)
  181. return BHT_ERROR;
  182. return ret;
  183. }
  184. int
  185. os_cond_signal(korp_cond *cond)
  186. {
  187. return pthread_cond_signal(cond);
  188. }
  189. int
  190. os_cond_broadcast(korp_cond *cond)
  191. {
  192. return pthread_cond_broadcast(cond);
  193. }