sgx_thread.c 2.9 KB

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