_thread.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include "_thread.h"
  2. #include "PikaVM.h"
  3. #include "TinyObj.h"
  4. #include "dataMemory.h"
  5. static volatile int g_thread_stack_size = PIKA_THREAD_STACK_SIZE;
  6. #if !PIKA_THREAD_MALLOC_STACK_ENABLE
  7. extern volatile PikaMemInfo g_PikaMemInfo;
  8. #endif
  9. #if PIKA_FREERTOS_ENABLE
  10. static pika_platform_thread_t* g_thread_to_free = NULL;
  11. static pika_platform_thread_mutex_t g_thread_idle_hook_mutex = {0};
  12. static int g_thread_idle_hook_mutex_inited = 0;
  13. #if PIKA_THREAD_MALLOC_STACK_ENABLE
  14. void pika_thread_idle_hook(void) {
  15. if (0 == g_thread_idle_hook_mutex_inited) {
  16. pika_platform_thread_mutex_init(&g_thread_idle_hook_mutex);
  17. g_thread_idle_hook_mutex_inited = 1;
  18. }
  19. pika_platform_thread_mutex_lock(&g_thread_idle_hook_mutex);
  20. if (NULL != g_thread_to_free) {
  21. pika_platform_thread_exit(g_thread_to_free);
  22. pikaFree(g_thread_to_free->thread_stack,
  23. g_thread_to_free->thread_stack_size);
  24. pikaFree(g_thread_to_free, sizeof(pika_platform_thread_t));
  25. g_thread_to_free = NULL;
  26. }
  27. pika_platform_thread_mutex_unlock(&g_thread_idle_hook_mutex);
  28. return;
  29. }
  30. #endif
  31. #endif
  32. typedef struct pika_thread_info {
  33. Arg* function;
  34. Arg* args;
  35. pika_platform_thread_t* thread;
  36. int stack_size;
  37. } pika_thread_info;
  38. static void _thread_func(void* arg) {
  39. pika_debug("thread start");
  40. pika_GIL_ENTER();
  41. PikaObj* ctx = New_TinyObj(NULL);
  42. pika_thread_info* info = (pika_thread_info*)arg;
  43. if (NULL != info->args) {
  44. obj_setArg(ctx, "args", info->args);
  45. }
  46. obj_setArg(ctx, "thread", info->function);
  47. if (NULL == info->args) {
  48. /* clang-format off */
  49. PIKA_PYTHON(
  50. thread()
  51. )
  52. /* clang-format on */
  53. const uint8_t bytes[] = {
  54. 0x04, 0x00, 0x00, 0x00, /* instruct array size */
  55. 0x00, 0x82, 0x01, 0x00, /* instruct array */
  56. 0x08, 0x00, 0x00, 0x00, /* const pool size */
  57. 0x00, 0x74, 0x68, 0x72, 0x65, 0x61, 0x64, 0x00, /* const pool */
  58. };
  59. pikaVM_runByteCode(ctx, (uint8_t*)bytes);
  60. } else {
  61. /* clang-format off */
  62. PIKA_PYTHON(
  63. thread(*args)
  64. )
  65. /* clang-format on */
  66. const uint8_t bytes[] = {
  67. 0x0c, 0x00, 0x00, 0x00, /* instruct array size */
  68. 0x20, 0x81, 0x01, 0x00, 0x10, 0x08, 0x06, 0x00, 0x00, 0x02, 0x08,
  69. 0x00,
  70. /* instruct array */
  71. 0x0f, 0x00, 0x00, 0x00, /* const pool size */
  72. 0x00, 0x61, 0x72, 0x67, 0x73, 0x00, 0x2a, 0x00, 0x74, 0x68, 0x72,
  73. 0x65, 0x61, 0x64, 0x00, /* const pool */
  74. };
  75. pikaVM_runByteCode(ctx, (uint8_t*)bytes);
  76. }
  77. obj_deinit(ctx);
  78. arg_deinit(info->function);
  79. if (NULL != info->args) {
  80. arg_deinit(info->args);
  81. }
  82. #if !PIKA_THREAD_MALLOC_STACK_ENABLE
  83. g_PikaMemInfo.heapUsed -= info->stack_size;
  84. #endif
  85. pika_debug("thread exiting");
  86. pika_platform_thread_t* thread = info->thread;
  87. pikaFree(info, sizeof(pika_thread_info));
  88. pika_GIL_EXIT();
  89. #if PIKA_FREERTOS_ENABLE
  90. #if PIKA_THREAD_MALLOC_STACK_ENABLE
  91. if (0 == g_thread_idle_hook_mutex_inited) {
  92. pika_platform_thread_mutex_init(&g_thread_idle_hook_mutex);
  93. g_thread_idle_hook_mutex_inited = 1;
  94. }
  95. pika_platform_thread_mutex_lock(&g_thread_idle_hook_mutex);
  96. while (NULL != g_thread_to_free) {
  97. pika_platform_thread_mutex_unlock(&g_thread_idle_hook_mutex);
  98. vTaskDelay(10);
  99. pika_platform_thread_mutex_lock(&g_thread_idle_hook_mutex);
  100. }
  101. g_thread_to_free = thread;
  102. pika_platform_thread_mutex_unlock(&g_thread_idle_hook_mutex);
  103. while (1) {
  104. vTaskDelay(1);
  105. }
  106. #else
  107. pikaFree(thread, sizeof(pika_platform_thread_t));
  108. pika_platform_thread_exit(NULL);
  109. #endif
  110. #else
  111. pika_platform_thread_exit(thread);
  112. #endif
  113. }
  114. void _thread_start_new_thread(PikaObj* self, Arg* function, Arg* args_) {
  115. pika_thread_info* info =
  116. (pika_thread_info*)pikaMalloc(sizeof(pika_thread_info));
  117. pika_platform_memset(info, 0, sizeof(pika_thread_info));
  118. info->function = arg_copy(function);
  119. if (arg_isObject(args_)) {
  120. PikaObj* tuple = arg_getPtr(args_);
  121. size_t tuple_size = objTuple_getSize(tuple);
  122. pika_debug("type of args is %d", arg_getType(args_));
  123. pika_debug("new_thread: args tuple size %d", tuple_size);
  124. if (tuple_size > 0) {
  125. info->args = arg_copy(args_);
  126. }
  127. } else {
  128. info->args = arg_copy(args_);
  129. }
  130. _VM_lock_init();
  131. if (!_VM_is_first_lock()) {
  132. pika_debug("first lock for main thread");
  133. pika_GIL_ENTER();
  134. pika_debug("VM num %d", _VMEvent_getVMCnt());
  135. }
  136. info->stack_size = g_thread_stack_size;
  137. pika_debug("thread stack size %d", info->stack_size);
  138. info->thread = pika_platform_thread_init("pika_thread", _thread_func, info,
  139. info->stack_size, PIKA_THREAD_PRIO,
  140. PIKA_THREAD_TICK);
  141. if (NULL == info->thread) {
  142. pikaFree(info, sizeof(pika_thread_info));
  143. obj_setErrorCode(self, PIKA_RES_ERR_RUNTIME_ERROR);
  144. obj_setSysOut(self, "thread create failed");
  145. return;
  146. }
  147. #if !PIKA_THREAD_MALLOC_STACK_ENABLE
  148. g_PikaMemInfo.heapUsed += info->stack_size;
  149. #endif
  150. }
  151. int _thread_stack_size(PikaObj* self, PikaTuple* size) {
  152. if (pikaTuple_getSize(size) == 1) {
  153. int stack_size = pikaTuple_getInt(size, 0);
  154. if (stack_size == 0) {
  155. stack_size = PIKA_THREAD_STACK_SIZE;
  156. }
  157. g_thread_stack_size = stack_size;
  158. }
  159. return g_thread_stack_size;
  160. }