thread.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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 "freertos/FreeRTOS.h"
  21. #include "freertos/queue.h"
  22. #include "osi/semaphore.h"
  23. #include "osi/thread.h"
  24. #include "osi/mutex.h"
  25. struct work_item {
  26. osi_thread_func_t func;
  27. void *context;
  28. };
  29. struct work_queue {
  30. QueueHandle_t queue;
  31. size_t capacity;
  32. };
  33. struct osi_thread {
  34. TaskHandle_t thread_handle; /*!< Store the thread object */
  35. int thread_id; /*!< May for some OS, such as Linux */
  36. bool stop;
  37. uint8_t work_queue_num; /*!< Work queue number */
  38. struct work_queue **work_queues; /*!< Point to queue array, and the priority inverse array index */
  39. osi_sem_t work_sem;
  40. osi_sem_t stop_sem;
  41. };
  42. struct osi_thread_start_arg {
  43. osi_thread_t *thread;
  44. osi_sem_t start_sem;
  45. int error;
  46. };
  47. struct osi_event {
  48. struct work_item item;
  49. osi_mutex_t lock;
  50. uint16_t is_queued;
  51. uint16_t queue_idx;
  52. osi_thread_t *thread;
  53. };
  54. static const size_t DEFAULT_WORK_QUEUE_CAPACITY = 100;
  55. static struct work_queue *osi_work_queue_create(size_t capacity)
  56. {
  57. if (capacity == 0) {
  58. return NULL;
  59. }
  60. struct work_queue *wq = (struct work_queue *)osi_malloc(sizeof(struct work_queue));
  61. if (wq != NULL) {
  62. wq->queue = xQueueCreate(capacity, sizeof(struct work_item));
  63. if (wq->queue != 0) {
  64. wq->capacity = capacity;
  65. return wq;
  66. } else {
  67. osi_free(wq);
  68. }
  69. }
  70. return NULL;
  71. }
  72. static void osi_work_queue_delete(struct work_queue *wq)
  73. {
  74. if (wq != NULL) {
  75. if (wq->queue != 0) {
  76. vQueueDelete(wq->queue);
  77. }
  78. wq->queue = 0;
  79. wq->capacity = 0;
  80. osi_free(wq);
  81. }
  82. return;
  83. }
  84. static bool osi_thead_work_queue_get(struct work_queue *wq, struct work_item *item)
  85. {
  86. assert (wq != NULL);
  87. assert (wq->queue != 0);
  88. assert (item != NULL);
  89. if (pdTRUE == xQueueReceive(wq->queue, item, 0)) {
  90. return true;
  91. } else {
  92. return false;
  93. }
  94. }
  95. static bool osi_thead_work_queue_put(struct work_queue *wq, const struct work_item *item, uint32_t timeout)
  96. {
  97. assert (wq != NULL);
  98. assert (wq->queue != 0);
  99. assert (item != NULL);
  100. bool ret = true;
  101. if (timeout == OSI_SEM_MAX_TIMEOUT) {
  102. if (xQueueSend(wq->queue, item, portMAX_DELAY) != pdTRUE) {
  103. ret = false;
  104. }
  105. } else {
  106. if (xQueueSend(wq->queue, item, timeout / portTICK_PERIOD_MS) != pdTRUE) {
  107. ret = false;
  108. }
  109. }
  110. return ret;
  111. }
  112. static size_t osi_thead_work_queue_len(struct work_queue *wq)
  113. {
  114. assert (wq != NULL);
  115. assert (wq->queue != 0);
  116. assert (wq->capacity != 0);
  117. size_t available_spaces = (size_t)uxQueueSpacesAvailable(wq->queue);
  118. if (available_spaces <= wq->capacity) {
  119. return wq->capacity - available_spaces;
  120. } else {
  121. assert (0);
  122. }
  123. return 0;
  124. }
  125. static void osi_thread_run(void *arg)
  126. {
  127. struct osi_thread_start_arg *start = (struct osi_thread_start_arg *)arg;
  128. osi_thread_t *thread = start->thread;
  129. osi_sem_give(&start->start_sem);
  130. while (1) {
  131. int idx = 0;
  132. osi_sem_take(&thread->work_sem, OSI_SEM_MAX_TIMEOUT);
  133. if (thread->stop) {
  134. break;
  135. }
  136. struct work_item item;
  137. while (!thread->stop && idx < thread->work_queue_num) {
  138. if (osi_thead_work_queue_get(thread->work_queues[idx], &item) == true) {
  139. item.func(item.context);
  140. idx = 0;
  141. continue;
  142. } else {
  143. idx++;
  144. }
  145. }
  146. }
  147. thread->thread_handle = NULL;
  148. osi_sem_give(&thread->stop_sem);
  149. vTaskDelete(NULL);
  150. }
  151. static int osi_thread_join(osi_thread_t *thread, uint32_t wait_ms)
  152. {
  153. assert(thread != NULL);
  154. return osi_sem_take(&thread->stop_sem, wait_ms);
  155. }
  156. static void osi_thread_stop(osi_thread_t *thread)
  157. {
  158. int ret;
  159. assert(thread != NULL);
  160. //stop the thread
  161. thread->stop = true;
  162. osi_sem_give(&thread->work_sem);
  163. //join
  164. ret = osi_thread_join(thread, 1000); //wait 1000ms
  165. //if join failed, delete the task here
  166. if (ret != 0 && thread->thread_handle) {
  167. vTaskDelete(thread->thread_handle);
  168. }
  169. }
  170. //in linux, the stack_size, priority and core may not be set here, the code will be ignore the arguments
  171. 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, const size_t work_queue_len[])
  172. {
  173. int ret;
  174. struct osi_thread_start_arg start_arg = {0};
  175. if (stack_size <= 0 ||
  176. core < OSI_THREAD_CORE_0 || core > OSI_THREAD_CORE_AFFINITY ||
  177. work_queue_num <= 0 || work_queue_len == NULL) {
  178. return NULL;
  179. }
  180. osi_thread_t *thread = (osi_thread_t *)osi_calloc(sizeof(osi_thread_t));
  181. if (thread == NULL) {
  182. goto _err;
  183. }
  184. thread->stop = false;
  185. thread->work_queues = (struct work_queue **)osi_calloc(sizeof(struct work_queue *) * work_queue_num);
  186. if (thread->work_queues == NULL) {
  187. goto _err;
  188. }
  189. thread->work_queue_num = work_queue_num;
  190. for (int i = 0; i < thread->work_queue_num; i++) {
  191. size_t queue_len = work_queue_len[i] ? work_queue_len[i] : DEFAULT_WORK_QUEUE_CAPACITY;
  192. thread->work_queues[i] = osi_work_queue_create(queue_len);
  193. if (thread->work_queues[i] == NULL) {
  194. goto _err;
  195. }
  196. }
  197. ret = osi_sem_new(&thread->work_sem, 1, 0);
  198. if (ret != 0) {
  199. goto _err;
  200. }
  201. ret = osi_sem_new(&thread->stop_sem, 1, 0);
  202. if (ret != 0) {
  203. goto _err;
  204. }
  205. start_arg.thread = thread;
  206. ret = osi_sem_new(&start_arg.start_sem, 1, 0);
  207. if (ret != 0) {
  208. goto _err;
  209. }
  210. if (xTaskCreatePinnedToCore(osi_thread_run, name, stack_size, &start_arg, priority, &thread->thread_handle, core) != pdPASS) {
  211. goto _err;
  212. }
  213. osi_sem_take(&start_arg.start_sem, OSI_SEM_MAX_TIMEOUT);
  214. osi_sem_free(&start_arg.start_sem);
  215. return thread;
  216. _err:
  217. if (thread) {
  218. if (start_arg.start_sem) {
  219. osi_sem_free(&start_arg.start_sem);
  220. }
  221. if (thread->thread_handle) {
  222. vTaskDelete(thread->thread_handle);
  223. }
  224. for (int i = 0; i < thread->work_queue_num; i++) {
  225. if (thread->work_queues[i]) {
  226. osi_work_queue_delete(thread->work_queues[i]);
  227. }
  228. thread->work_queues[i] = NULL;
  229. }
  230. if (thread->work_queues) {
  231. osi_free(thread->work_queues);
  232. thread->work_queues = NULL;
  233. }
  234. if (thread->work_sem) {
  235. osi_sem_free(&thread->work_sem);
  236. }
  237. if (thread->stop_sem) {
  238. osi_sem_free(&thread->stop_sem);
  239. }
  240. osi_free(thread);
  241. }
  242. return NULL;
  243. }
  244. void osi_thread_free(osi_thread_t *thread)
  245. {
  246. if (!thread)
  247. return;
  248. osi_thread_stop(thread);
  249. for (int i = 0; i < thread->work_queue_num; i++) {
  250. if (thread->work_queues[i]) {
  251. osi_work_queue_delete(thread->work_queues[i]);
  252. thread->work_queues[i] = NULL;
  253. }
  254. }
  255. if (thread->work_queues) {
  256. osi_free(thread->work_queues);
  257. thread->work_queues = NULL;
  258. }
  259. if (thread->work_sem) {
  260. osi_sem_free(&thread->work_sem);
  261. }
  262. if (thread->stop_sem) {
  263. osi_sem_free(&thread->stop_sem);
  264. }
  265. osi_free(thread);
  266. }
  267. bool osi_thread_post(osi_thread_t *thread, osi_thread_func_t func, void *context, int queue_idx, uint32_t timeout)
  268. {
  269. assert(thread != NULL);
  270. assert(func != NULL);
  271. if (queue_idx >= thread->work_queue_num) {
  272. return false;
  273. }
  274. struct work_item item;
  275. item.func = func;
  276. item.context = context;
  277. if (osi_thead_work_queue_put(thread->work_queues[queue_idx], &item, timeout) == false) {
  278. return false;
  279. }
  280. osi_sem_give(&thread->work_sem);
  281. return true;
  282. }
  283. bool osi_thread_set_priority(osi_thread_t *thread, int priority)
  284. {
  285. assert(thread != NULL);
  286. vTaskPrioritySet(thread->thread_handle, priority);
  287. return true;
  288. }
  289. const char *osi_thread_name(osi_thread_t *thread)
  290. {
  291. assert(thread != NULL);
  292. return pcTaskGetName(thread->thread_handle);
  293. }
  294. int osi_thread_queue_wait_size(osi_thread_t *thread, int wq_idx)
  295. {
  296. if (wq_idx < 0 || wq_idx >= thread->work_queue_num) {
  297. return -1;
  298. }
  299. return (int)(osi_thead_work_queue_len(thread->work_queues[wq_idx]));
  300. }
  301. struct osi_event *osi_event_create(osi_thread_func_t func, void *context)
  302. {
  303. struct osi_event *event = osi_calloc(sizeof(struct osi_event));
  304. if (event != NULL) {
  305. if (osi_mutex_new(&event->lock) == 0) {
  306. event->item.func = func;
  307. event->item.context = context;
  308. return event;
  309. }
  310. osi_free(event);
  311. }
  312. return NULL;
  313. }
  314. void osi_event_delete(struct osi_event* event)
  315. {
  316. if (event != NULL) {
  317. osi_mutex_free(&event->lock);
  318. memset(event, 0, sizeof(struct osi_event));
  319. osi_free(event);
  320. }
  321. }
  322. bool osi_event_bind(struct osi_event* event, osi_thread_t *thread, int queue_idx)
  323. {
  324. if (event == NULL || event->thread != NULL) {
  325. return false;
  326. }
  327. if (thread == NULL || queue_idx >= thread->work_queue_num) {
  328. return false;
  329. }
  330. event->thread = thread;
  331. event->queue_idx = queue_idx;
  332. return true;
  333. }
  334. static void osi_thread_generic_event_handler(void *context)
  335. {
  336. struct osi_event *event = (struct osi_event *)context;
  337. if (event != NULL && event->item.func != NULL) {
  338. osi_mutex_lock(&event->lock, OSI_MUTEX_MAX_TIMEOUT);
  339. event->is_queued = 0;
  340. osi_mutex_unlock(&event->lock);
  341. event->item.func(event->item.context);
  342. }
  343. }
  344. bool osi_thread_post_event(struct osi_event *event, uint32_t timeout)
  345. {
  346. assert(event != NULL && event->thread != NULL);
  347. assert(event->queue_idx >= 0 && event->queue_idx < event->thread->work_queue_num);
  348. bool ret = false;
  349. if (event->is_queued == 0) {
  350. uint16_t acquire_cnt = 0;
  351. osi_mutex_lock(&event->lock, OSI_MUTEX_MAX_TIMEOUT);
  352. event->is_queued += 1;
  353. acquire_cnt = event->is_queued;
  354. osi_mutex_unlock(&event->lock);
  355. if (acquire_cnt == 1) {
  356. ret = osi_thread_post(event->thread, osi_thread_generic_event_handler, event, event->queue_idx, timeout);
  357. if (!ret) {
  358. // clear "is_queued" when post failure, to allow for following event posts
  359. osi_mutex_lock(&event->lock, OSI_MUTEX_MAX_TIMEOUT);
  360. event->is_queued = 0;
  361. osi_mutex_unlock(&event->lock);
  362. }
  363. }
  364. }
  365. return ret;
  366. }