sgx_thread.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. os_printf("THREAD CREATED %p\n", &targ);
  19. BH_FREE(targ);
  20. start_func(thread_arg);
  21. return NULL;
  22. }
  23. int
  24. os_thread_create_with_prio(korp_tid *tid, thread_start_routine_t start,
  25. void *arg, unsigned int stack_size, int prio)
  26. {
  27. thread_wrapper_arg *targ;
  28. assert(tid);
  29. assert(start);
  30. targ = (thread_wrapper_arg *)BH_MALLOC(sizeof(*targ));
  31. if (!targ) {
  32. return BHT_ERROR;
  33. }
  34. targ->start = start;
  35. targ->arg = arg;
  36. if (pthread_create(tid, NULL, os_thread_wrapper, targ) != 0) {
  37. BH_FREE(targ);
  38. return BHT_ERROR;
  39. }
  40. return BHT_OK;
  41. }
  42. int
  43. os_thread_create(korp_tid *tid, thread_start_routine_t start, void *arg,
  44. unsigned int stack_size)
  45. {
  46. return os_thread_create_with_prio(tid, start, arg, stack_size,
  47. BH_THREAD_DEFAULT_PRIORITY);
  48. }
  49. #endif
  50. korp_tid
  51. os_self_thread()
  52. {
  53. #ifndef SGX_DISABLE_PTHREAD
  54. return pthread_self();
  55. #else
  56. return 0;
  57. #endif
  58. }
  59. int
  60. os_mutex_init(korp_mutex *mutex)
  61. {
  62. #ifndef SGX_DISABLE_PTHREAD
  63. pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
  64. *mutex = m;
  65. #endif
  66. return BHT_OK;
  67. }
  68. int
  69. os_mutex_destroy(korp_mutex *mutex)
  70. {
  71. #ifndef SGX_DISABLE_PTHREAD
  72. pthread_mutex_destroy(mutex);
  73. #endif
  74. return BHT_OK;
  75. }
  76. int
  77. os_mutex_lock(korp_mutex *mutex)
  78. {
  79. #ifndef SGX_DISABLE_PTHREAD
  80. return pthread_mutex_lock(mutex);
  81. #else
  82. return 0;
  83. #endif
  84. }
  85. int
  86. os_mutex_unlock(korp_mutex *mutex)
  87. {
  88. #ifndef SGX_DISABLE_PTHREAD
  89. return pthread_mutex_unlock(mutex);
  90. #else
  91. return 0;
  92. #endif
  93. }
  94. int
  95. os_cond_init(korp_cond *cond)
  96. {
  97. #ifndef SGX_DISABLE_PTHREAD
  98. pthread_cond_t c = PTHREAD_COND_INITIALIZER;
  99. *cond = c;
  100. #endif
  101. return BHT_OK;
  102. }
  103. int
  104. os_cond_destroy(korp_cond *cond)
  105. {
  106. #ifndef SGX_DISABLE_PTHREAD
  107. pthread_cond_destroy(cond);
  108. #endif
  109. return BHT_OK;
  110. }
  111. int
  112. os_cond_wait(korp_cond *cond, korp_mutex *mutex)
  113. {
  114. #ifndef SGX_DISABLE_PTHREAD
  115. assert(cond);
  116. assert(mutex);
  117. if (pthread_cond_wait(cond, mutex) != BHT_OK)
  118. return BHT_ERROR;
  119. #endif
  120. return BHT_OK;
  121. }
  122. int
  123. os_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, uint64 useconds)
  124. {
  125. os_printf("warning: SGX pthread_cond_timedwait isn't supported, "
  126. "calling pthread_cond_wait instead!\n");
  127. return BHT_ERROR;
  128. }
  129. int
  130. os_cond_signal(korp_cond *cond)
  131. {
  132. #ifndef SGX_DISABLE_PTHREAD
  133. assert(cond);
  134. if (pthread_cond_signal(cond) != BHT_OK)
  135. return BHT_ERROR;
  136. #endif
  137. return BHT_OK;
  138. }
  139. int
  140. os_thread_join(korp_tid thread, void **value_ptr)
  141. {
  142. #ifndef SGX_DISABLE_PTHREAD
  143. return pthread_join(thread, value_ptr);
  144. #else
  145. return 0;
  146. #endif
  147. }
  148. int
  149. os_thread_detach(korp_tid thread)
  150. {
  151. /* SGX pthread_detach isn't provided, return directly. */
  152. return 0;
  153. }
  154. void
  155. os_thread_exit(void *retval)
  156. {
  157. #ifndef SGX_DISABLE_PTHREAD
  158. pthread_exit(retval);
  159. #else
  160. return;
  161. #endif
  162. }
  163. uint8 *
  164. os_thread_get_stack_boundary()
  165. {
  166. /* TODO: get sgx stack boundary */
  167. return NULL;
  168. }