thread.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 2014 Google, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at:
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. ******************************************************************************/
  18. #include <string.h>
  19. #include "osi/allocator.h"
  20. #include "osi/fixed_queue.h"
  21. #include "osi/semaphore.h"
  22. #include "osi/thread.h"
  23. struct osi_thread {
  24. void *thread_handle; /*!< Store the thread object */
  25. int thread_id; /*!< May for some OS, such as Linux */
  26. bool stop;
  27. uint8_t work_queue_num; /*!< Work queue number */
  28. fixed_queue_t **work_queues; /*!< Point to queue array, and the priority inverse array index */
  29. osi_sem_t work_sem;
  30. osi_sem_t stop_sem;
  31. };
  32. struct osi_thread_start_arg {
  33. osi_thread_t *thread;
  34. osi_sem_t start_sem;
  35. int error;
  36. };
  37. typedef struct {
  38. osi_thread_func_t func;
  39. void *context;
  40. } work_item_t;
  41. static const size_t DEFAULT_WORK_QUEUE_CAPACITY = 100;
  42. static void osi_thread_run(void *arg)
  43. {
  44. struct osi_thread_start_arg *start = (struct osi_thread_start_arg *)arg;
  45. osi_thread_t *thread = start->thread;
  46. osi_sem_give(&start->start_sem);
  47. while (1) {
  48. int idx = 0;
  49. osi_sem_take(&thread->work_sem, OSI_SEM_MAX_TIMEOUT);
  50. if (thread->stop) {
  51. break;
  52. }
  53. while (!thread->stop && idx < thread->work_queue_num) {
  54. work_item_t *item = fixed_queue_dequeue(thread->work_queues[idx], 0);
  55. if (item) {
  56. item->func(item->context);
  57. osi_free(item);
  58. idx = 0;
  59. continue;
  60. } else {
  61. idx++;
  62. }
  63. }
  64. }
  65. thread->thread_handle = NULL;
  66. osi_sem_give(&thread->stop_sem);
  67. vTaskDelete(NULL);
  68. }
  69. static int osi_thread_join(osi_thread_t *thread, uint32_t wait_ms)
  70. {
  71. assert(thread != NULL);
  72. return osi_sem_take(&thread->stop_sem, wait_ms);
  73. }
  74. static void osi_thread_stop(osi_thread_t *thread)
  75. {
  76. int ret;
  77. assert(thread != NULL);
  78. //stop the thread
  79. thread->stop = true;
  80. osi_sem_give(&thread->work_sem);
  81. //join
  82. ret = osi_thread_join(thread, 1000); //wait 1000ms
  83. //if join failed, delete the task here
  84. if (ret != 0 && thread->thread_handle) {
  85. vTaskDelete(thread->thread_handle);
  86. }
  87. }
  88. //in linux, the stack_size, priority and core may not be set here, the code will be ignore the arguments
  89. osi_thread_t *osi_thread_create(const char *name, size_t stack_size, int priority, osi_thread_core_t core, uint8_t work_queue_num)
  90. {
  91. int ret;
  92. osi_thread_t *thread;
  93. struct osi_thread_start_arg start_arg = {0};
  94. if (stack_size <= 0 ||
  95. core < OSI_THREAD_CORE_0 || core > OSI_THREAD_CORE_AFFINITY ||
  96. work_queue_num <= 0) {
  97. return NULL;
  98. }
  99. thread = (osi_thread_t *)osi_malloc(sizeof(osi_thread_t));
  100. if (thread == NULL) {
  101. goto _err;
  102. }
  103. thread->stop = false;
  104. thread->work_queue_num = work_queue_num;
  105. thread->work_queues = (fixed_queue_t **)osi_malloc(sizeof(fixed_queue_t *) * work_queue_num);
  106. if (thread->work_queues == NULL) {
  107. goto _err;
  108. }
  109. for (int i = 0; i < thread->work_queue_num; i++) {
  110. thread->work_queues[i] = fixed_queue_new(DEFAULT_WORK_QUEUE_CAPACITY);
  111. if (thread->work_queues[i] == NULL) {
  112. goto _err;
  113. }
  114. }
  115. ret = osi_sem_new(&thread->work_sem, 1, 0);
  116. if (ret != 0) {
  117. goto _err;
  118. }
  119. ret = osi_sem_new(&thread->stop_sem, 1, 0);
  120. if (ret != 0) {
  121. goto _err;
  122. }
  123. start_arg.thread = thread;
  124. ret = osi_sem_new(&start_arg.start_sem, 1, 0);
  125. if (ret != 0) {
  126. goto _err;
  127. }
  128. if (xTaskCreatePinnedToCore(osi_thread_run, name, stack_size, &start_arg, priority, &thread->thread_handle, core) != pdPASS) {
  129. goto _err;
  130. }
  131. osi_sem_take(&start_arg.start_sem, OSI_SEM_MAX_TIMEOUT);
  132. osi_sem_free(&start_arg.start_sem);
  133. return thread;
  134. _err:
  135. if (thread) {
  136. if (start_arg.start_sem) {
  137. osi_sem_free(&start_arg.start_sem);
  138. }
  139. if (thread->thread_handle) {
  140. vTaskDelete(thread->thread_handle);
  141. }
  142. for (int i = 0; i < thread->work_queue_num; i++) {
  143. if (thread->work_queues[i]) {
  144. fixed_queue_free(thread->work_queues[i], osi_free_func);
  145. }
  146. }
  147. if (thread->work_queues) {
  148. osi_free(thread->work_queues);
  149. }
  150. if (thread->work_sem) {
  151. osi_sem_free(&thread->work_sem);
  152. }
  153. if (thread->stop_sem) {
  154. osi_sem_free(&thread->stop_sem);
  155. }
  156. osi_free(thread);
  157. }
  158. return NULL;
  159. }
  160. void osi_thread_free(osi_thread_t *thread)
  161. {
  162. if (!thread)
  163. return;
  164. osi_thread_stop(thread);
  165. for (int i = 0; i < thread->work_queue_num; i++) {
  166. if (thread->work_queues[i]) {
  167. fixed_queue_free(thread->work_queues[i], osi_free_func);
  168. }
  169. }
  170. if (thread->work_queues) {
  171. osi_free(thread->work_queues);
  172. }
  173. if (thread->work_sem) {
  174. osi_sem_free(&thread->work_sem);
  175. }
  176. if (thread->stop_sem) {
  177. osi_sem_free(&thread->stop_sem);
  178. }
  179. osi_free(thread);
  180. }
  181. bool osi_thread_post(osi_thread_t *thread, osi_thread_func_t func, void *context, int queue_idx, uint32_t timeout)
  182. {
  183. assert(thread != NULL);
  184. assert(func != NULL);
  185. if (queue_idx >= thread->work_queue_num) {
  186. return false;
  187. }
  188. work_item_t *item = (work_item_t *)osi_malloc(sizeof(work_item_t));
  189. if (item == NULL) {
  190. return false;
  191. }
  192. item->func = func;
  193. item->context = context;
  194. if (fixed_queue_enqueue(thread->work_queues[queue_idx], item, timeout) == false) {
  195. osi_free(item);
  196. return false;
  197. }
  198. osi_sem_give(&thread->work_sem);
  199. return true;
  200. }
  201. bool osi_thread_set_priority(osi_thread_t *thread, int priority)
  202. {
  203. assert(thread != NULL);
  204. vTaskPrioritySet(thread->thread_handle, priority);
  205. return true;
  206. }
  207. const char *osi_thread_name(osi_thread_t *thread)
  208. {
  209. assert(thread != NULL);
  210. return pcTaskGetTaskName(thread->thread_handle);
  211. }
  212. int osi_thread_queue_wait_size(osi_thread_t *thread, int wq_idx)
  213. {
  214. if (wq_idx < 0 || wq_idx >= thread->work_queue_num) {
  215. return -1;
  216. }
  217. return fixed_queue_length(thread->work_queues[wq_idx]);
  218. }