sgx_thread.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "platform_api_vmcore.h"
  6. #include "platform_api_extension.h"
  7. #ifndef SGX_DISABLE_PTHREAD
  8. typedef struct {
  9. thread_start_routine_t start;
  10. void *arg;
  11. } thread_wrapper_arg;
  12. static void *
  13. os_thread_wrapper(void *arg)
  14. {
  15. thread_wrapper_arg *targ = arg;
  16. thread_start_routine_t start_func = targ->start;
  17. void *thread_arg = targ->arg;
  18. #if 0
  19. os_printf("THREAD CREATED %p\n", &targ);
  20. #endif
  21. BH_FREE(targ);
  22. start_func(thread_arg);
  23. return NULL;
  24. }
  25. int
  26. os_thread_create_with_prio(korp_tid *tid, thread_start_routine_t start,
  27. void *arg, unsigned int stack_size, int prio)
  28. {
  29. thread_wrapper_arg *targ;
  30. assert(tid);
  31. assert(start);
  32. targ = (thread_wrapper_arg *)BH_MALLOC(sizeof(*targ));
  33. if (!targ) {
  34. return BHT_ERROR;
  35. }
  36. targ->start = start;
  37. targ->arg = arg;
  38. if (pthread_create(tid, NULL, os_thread_wrapper, targ) != 0) {
  39. BH_FREE(targ);
  40. return BHT_ERROR;
  41. }
  42. return BHT_OK;
  43. }
  44. int
  45. os_thread_create(korp_tid *tid, thread_start_routine_t start, void *arg,
  46. unsigned int stack_size)
  47. {
  48. return os_thread_create_with_prio(tid, start, arg, stack_size,
  49. BH_THREAD_DEFAULT_PRIORITY);
  50. }
  51. #endif
  52. korp_tid
  53. os_self_thread()
  54. {
  55. #ifndef SGX_DISABLE_PTHREAD
  56. return pthread_self();
  57. #else
  58. return 0;
  59. #endif
  60. }
  61. int
  62. os_mutex_init(korp_mutex *mutex)
  63. {
  64. #ifndef SGX_DISABLE_PTHREAD
  65. pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
  66. *mutex = m;
  67. #endif
  68. return BHT_OK;
  69. }
  70. int
  71. os_mutex_destroy(korp_mutex *mutex)
  72. {
  73. #ifndef SGX_DISABLE_PTHREAD
  74. pthread_mutex_destroy(mutex);
  75. #endif
  76. return BHT_OK;
  77. }
  78. int
  79. os_mutex_lock(korp_mutex *mutex)
  80. {
  81. #ifndef SGX_DISABLE_PTHREAD
  82. return pthread_mutex_lock(mutex);
  83. #else
  84. return 0;
  85. #endif
  86. }
  87. int
  88. os_mutex_unlock(korp_mutex *mutex)
  89. {
  90. #ifndef SGX_DISABLE_PTHREAD
  91. return pthread_mutex_unlock(mutex);
  92. #else
  93. return 0;
  94. #endif
  95. }
  96. int
  97. os_cond_init(korp_cond *cond)
  98. {
  99. #ifndef SGX_DISABLE_PTHREAD
  100. pthread_cond_t c = PTHREAD_COND_INITIALIZER;
  101. *cond = c;
  102. #endif
  103. return BHT_OK;
  104. }
  105. int
  106. os_cond_destroy(korp_cond *cond)
  107. {
  108. #ifndef SGX_DISABLE_PTHREAD
  109. pthread_cond_destroy(cond);
  110. #endif
  111. return BHT_OK;
  112. }
  113. int
  114. os_cond_wait(korp_cond *cond, korp_mutex *mutex)
  115. {
  116. #ifndef SGX_DISABLE_PTHREAD
  117. assert(cond);
  118. assert(mutex);
  119. if (pthread_cond_wait(cond, mutex) != BHT_OK)
  120. return BHT_ERROR;
  121. #endif
  122. return BHT_OK;
  123. }
  124. int
  125. os_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, uint64 useconds)
  126. {
  127. os_printf("warning: SGX pthread_cond_timedwait isn't supported, "
  128. "calling pthread_cond_wait instead!\n");
  129. return BHT_ERROR;
  130. }
  131. int
  132. os_cond_signal(korp_cond *cond)
  133. {
  134. #ifndef SGX_DISABLE_PTHREAD
  135. assert(cond);
  136. if (pthread_cond_signal(cond) != BHT_OK)
  137. return BHT_ERROR;
  138. #endif
  139. return BHT_OK;
  140. }
  141. int
  142. os_cond_broadcast(korp_cond *cond)
  143. {
  144. #ifndef SGX_DISABLE_PTHREAD
  145. assert(cond);
  146. if (pthread_cond_broadcast(cond) != BHT_OK)
  147. return BHT_ERROR;
  148. #endif
  149. return BHT_OK;
  150. }
  151. int
  152. os_thread_join(korp_tid thread, void **value_ptr)
  153. {
  154. #ifndef SGX_DISABLE_PTHREAD
  155. return pthread_join(thread, value_ptr);
  156. #else
  157. return 0;
  158. #endif
  159. }
  160. int
  161. os_thread_detach(korp_tid thread)
  162. {
  163. /* SGX pthread_detach isn't provided, return directly. */
  164. return 0;
  165. }
  166. void
  167. os_thread_exit(void *retval)
  168. {
  169. #ifndef SGX_DISABLE_PTHREAD
  170. pthread_exit(retval);
  171. #else
  172. return;
  173. #endif
  174. }
  175. uint8 *
  176. os_thread_get_stack_boundary()
  177. {
  178. /* TODO: get sgx stack boundary */
  179. return NULL;
  180. }
  181. void
  182. os_thread_jit_write_protect_np(bool enabled)
  183. {}
  184. int
  185. os_rwlock_init(korp_rwlock *lock)
  186. {
  187. #ifndef SGX_DISABLE_PTHREAD
  188. assert(lock);
  189. if (pthread_rwlock_init(lock, NULL) != BHT_OK)
  190. return BHT_ERROR;
  191. #endif
  192. return BHT_OK;
  193. }
  194. int
  195. os_rwlock_rdlock(korp_rwlock *lock)
  196. {
  197. #ifndef SGX_DISABLE_PTHREAD
  198. assert(lock);
  199. if (pthread_rwlock_rdlock(lock) != BHT_OK)
  200. return BHT_ERROR;
  201. #endif
  202. return BHT_OK;
  203. }
  204. int
  205. os_rwlock_wrlock(korp_rwlock *lock)
  206. {
  207. #ifndef SGX_DISABLE_PTHREAD
  208. assert(lock);
  209. if (pthread_rwlock_wrlock(lock) != BHT_OK)
  210. return BHT_ERROR;
  211. #endif
  212. return BHT_OK;
  213. }
  214. int
  215. os_rwlock_unlock(korp_rwlock *lock)
  216. {
  217. #ifndef SGX_DISABLE_PTHREAD
  218. assert(lock);
  219. if (pthread_rwlock_unlock(lock) != BHT_OK)
  220. return BHT_ERROR;
  221. #endif
  222. return BHT_OK;
  223. }
  224. int
  225. os_rwlock_destroy(korp_rwlock *lock)
  226. {
  227. #ifndef SGX_DISABLE_PTHREAD
  228. assert(lock);
  229. if (pthread_rwlock_destroy(lock) != BHT_OK)
  230. return BHT_ERROR;
  231. #endif
  232. return BHT_OK;
  233. }