sgx_thread.c 3.4 KB

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