espidf_thread.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. os_printf("THREAD CREATED %jx\n", (uintmax_t)(uintptr_t)pthread_self());
  21. BH_FREE(targ);
  22. start_func(thread_arg);
  23. return NULL;
  24. }
  25. korp_tid
  26. os_self_thread(void)
  27. {
  28. /* only allowed if this is a thread, xTaskCreate is not enough look at
  29. * product_mini for how to use this*/
  30. return pthread_self();
  31. }
  32. int
  33. os_mutex_init(korp_mutex *mutex)
  34. {
  35. return pthread_mutex_init(mutex, NULL);
  36. }
  37. int
  38. os_mutex_destroy(korp_mutex *mutex)
  39. {
  40. return pthread_mutex_destroy(mutex);
  41. }
  42. int
  43. os_mutex_lock(korp_mutex *mutex)
  44. {
  45. return pthread_mutex_lock(mutex);
  46. }
  47. int
  48. os_mutex_unlock(korp_mutex *mutex)
  49. {
  50. return pthread_mutex_unlock(mutex);
  51. }
  52. int
  53. os_thread_create_with_prio(korp_tid *tid, thread_start_routine_t start,
  54. void *arg, unsigned int stack_size, int prio)
  55. {
  56. pthread_attr_t tattr;
  57. thread_wrapper_arg *targ;
  58. assert(stack_size > 0);
  59. assert(tid);
  60. assert(start);
  61. pthread_attr_init(&tattr);
  62. pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_JOINABLE);
  63. if (pthread_attr_setstacksize(&tattr, stack_size) != 0) {
  64. os_printf("Invalid thread stack size %u. Min stack size = %u",
  65. stack_size, PTHREAD_STACK_MIN);
  66. pthread_attr_destroy(&tattr);
  67. return BHT_ERROR;
  68. }
  69. targ = (thread_wrapper_arg *)BH_MALLOC(sizeof(*targ));
  70. if (!targ) {
  71. pthread_attr_destroy(&tattr);
  72. return BHT_ERROR;
  73. }
  74. targ->start = start;
  75. targ->arg = arg;
  76. if (pthread_create(tid, &tattr, os_thread_wrapper, targ) != 0) {
  77. pthread_attr_destroy(&tattr);
  78. os_free(targ);
  79. return BHT_ERROR;
  80. }
  81. pthread_attr_destroy(&tattr);
  82. return BHT_OK;
  83. }
  84. int
  85. os_thread_create(korp_tid *tid, thread_start_routine_t start, void *arg,
  86. unsigned int stack_size)
  87. {
  88. return os_thread_create_with_prio(tid, start, arg, stack_size,
  89. BH_THREAD_DEFAULT_PRIORITY);
  90. }
  91. int
  92. os_thread_join(korp_tid thread, void **retval)
  93. {
  94. return pthread_join(thread, retval);
  95. }
  96. int
  97. os_thread_detach(korp_tid tid)
  98. {
  99. return pthread_detach(tid);
  100. }
  101. void
  102. os_thread_exit(void *retval)
  103. {
  104. pthread_exit(retval);
  105. }
  106. int
  107. os_cond_init(korp_cond *cond)
  108. {
  109. return pthread_cond_init(cond, NULL);
  110. }
  111. int
  112. os_cond_destroy(korp_cond *cond)
  113. {
  114. return pthread_cond_destroy(cond);
  115. }
  116. int
  117. os_cond_wait(korp_cond *cond, korp_mutex *mutex)
  118. {
  119. return pthread_cond_wait(cond, mutex);
  120. }
  121. static void
  122. msec_nsec_to_abstime(struct timespec *ts, uint64 usec)
  123. {
  124. struct timeval tv;
  125. time_t tv_sec_new;
  126. long int tv_nsec_new;
  127. gettimeofday(&tv, NULL);
  128. tv_sec_new = (time_t)(tv.tv_sec + usec / 1000000);
  129. if (tv_sec_new >= tv.tv_sec) {
  130. ts->tv_sec = tv_sec_new;
  131. }
  132. else {
  133. /* integer overflow */
  134. ts->tv_sec = BH_TIME_T_MAX;
  135. os_printf("Warning: os_cond_reltimedwait exceeds limit, "
  136. "set to max timeout instead\n");
  137. }
  138. tv_nsec_new = (long int)(tv.tv_usec * 1000 + (usec % 1000000) * 1000);
  139. if (tv.tv_usec * 1000 >= tv.tv_usec && tv_nsec_new >= tv.tv_usec * 1000) {
  140. ts->tv_nsec = tv_nsec_new;
  141. }
  142. else {
  143. /* integer overflow */
  144. ts->tv_nsec = LONG_MAX;
  145. os_printf("Warning: os_cond_reltimedwait exceeds limit, "
  146. "set to max timeout instead\n");
  147. }
  148. if (ts->tv_nsec >= 1000000000L && ts->tv_sec < BH_TIME_T_MAX) {
  149. ts->tv_sec++;
  150. ts->tv_nsec -= 1000000000L;
  151. }
  152. }
  153. int
  154. os_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, uint64 useconds)
  155. {
  156. int ret;
  157. struct timespec abstime;
  158. if (useconds == BHT_WAIT_FOREVER)
  159. ret = pthread_cond_wait(cond, mutex);
  160. else {
  161. msec_nsec_to_abstime(&abstime, useconds);
  162. ret = pthread_cond_timedwait(cond, mutex, &abstime);
  163. }
  164. if (ret != BHT_OK && ret != ETIMEDOUT)
  165. return BHT_ERROR;
  166. return ret;
  167. }
  168. int
  169. os_cond_signal(korp_cond *cond)
  170. {
  171. return pthread_cond_signal(cond);
  172. }