sgx_thread.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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_trylock(korp_mutex *mutex)
  89. {
  90. #ifndef SGX_DISABLE_PTHREAD
  91. return pthread_mutex_lock(mutex);
  92. #else
  93. return 0;
  94. #endif
  95. }
  96. int
  97. os_mutex_unlock(korp_mutex *mutex)
  98. {
  99. #ifndef SGX_DISABLE_PTHREAD
  100. return pthread_mutex_unlock(mutex);
  101. #else
  102. return 0;
  103. #endif
  104. }
  105. int
  106. os_cond_init(korp_cond *cond)
  107. {
  108. #ifndef SGX_DISABLE_PTHREAD
  109. pthread_cond_t c = PTHREAD_COND_INITIALIZER;
  110. *cond = c;
  111. #endif
  112. return BHT_OK;
  113. }
  114. int
  115. os_cond_destroy(korp_cond *cond)
  116. {
  117. #ifndef SGX_DISABLE_PTHREAD
  118. pthread_cond_destroy(cond);
  119. #endif
  120. return BHT_OK;
  121. }
  122. int
  123. os_cond_wait(korp_cond *cond, korp_mutex *mutex)
  124. {
  125. #ifndef SGX_DISABLE_PTHREAD
  126. assert(cond);
  127. assert(mutex);
  128. if (pthread_cond_wait(cond, mutex) != BHT_OK)
  129. return BHT_ERROR;
  130. #endif
  131. return BHT_OK;
  132. }
  133. int
  134. os_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, uint64 useconds)
  135. {
  136. os_printf("warning: SGX pthread_cond_timedwait isn't supported, "
  137. "calling pthread_cond_wait instead!\n");
  138. return BHT_ERROR;
  139. }
  140. int
  141. os_cond_signal(korp_cond *cond)
  142. {
  143. #ifndef SGX_DISABLE_PTHREAD
  144. assert(cond);
  145. if (pthread_cond_signal(cond) != BHT_OK)
  146. return BHT_ERROR;
  147. #endif
  148. return BHT_OK;
  149. }
  150. int
  151. os_cond_broadcast(korp_cond *cond)
  152. {
  153. #ifndef SGX_DISABLE_PTHREAD
  154. assert(cond);
  155. if (pthread_cond_broadcast(cond) != BHT_OK)
  156. return BHT_ERROR;
  157. #endif
  158. return BHT_OK;
  159. }
  160. int
  161. os_thread_join(korp_tid thread, void **value_ptr)
  162. {
  163. #ifndef SGX_DISABLE_PTHREAD
  164. return pthread_join(thread, value_ptr);
  165. #else
  166. return 0;
  167. #endif
  168. }
  169. int
  170. os_thread_detach(korp_tid thread)
  171. {
  172. /* SGX pthread_detach isn't provided, return directly. */
  173. return 0;
  174. }
  175. void
  176. os_thread_exit(void *retval)
  177. {
  178. #ifndef SGX_DISABLE_PTHREAD
  179. pthread_exit(retval);
  180. #else
  181. return;
  182. #endif
  183. }
  184. uint8 *
  185. os_thread_get_stack_boundary()
  186. {
  187. /* TODO: get sgx stack boundary */
  188. return NULL;
  189. }
  190. void
  191. os_thread_jit_write_protect_np(bool enabled)
  192. {}
  193. int
  194. os_rwlock_init(korp_rwlock *lock)
  195. {
  196. #ifndef SGX_DISABLE_PTHREAD
  197. assert(lock);
  198. if (pthread_rwlock_init(lock, NULL) != BHT_OK)
  199. return BHT_ERROR;
  200. #endif
  201. return BHT_OK;
  202. }
  203. int
  204. os_rwlock_rdlock(korp_rwlock *lock)
  205. {
  206. #ifndef SGX_DISABLE_PTHREAD
  207. assert(lock);
  208. if (pthread_rwlock_rdlock(lock) != BHT_OK)
  209. return BHT_ERROR;
  210. #endif
  211. return BHT_OK;
  212. }
  213. int
  214. os_rwlock_wrlock(korp_rwlock *lock)
  215. {
  216. #ifndef SGX_DISABLE_PTHREAD
  217. assert(lock);
  218. if (pthread_rwlock_wrlock(lock) != BHT_OK)
  219. return BHT_ERROR;
  220. #endif
  221. return BHT_OK;
  222. }
  223. int
  224. os_rwlock_unlock(korp_rwlock *lock)
  225. {
  226. #ifndef SGX_DISABLE_PTHREAD
  227. assert(lock);
  228. if (pthread_rwlock_unlock(lock) != BHT_OK)
  229. return BHT_ERROR;
  230. #endif
  231. return BHT_OK;
  232. }
  233. int
  234. os_rwlock_destroy(korp_rwlock *lock)
  235. {
  236. #ifndef SGX_DISABLE_PTHREAD
  237. assert(lock);
  238. if (pthread_rwlock_destroy(lock) != BHT_OK)
  239. return BHT_ERROR;
  240. #endif
  241. return BHT_OK;
  242. }