thread.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. /*
  2. * Copyright (c) 2018 Intel Corporation
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <kernel.h>
  7. #include <ksched.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <sys/atomic.h>
  11. #include <debug/stack.h>
  12. #include "wrapper.h"
  13. static const osThreadAttr_t init_thread_attrs = {
  14. .name = "ZephyrThread",
  15. .attr_bits = osThreadDetached,
  16. .cb_mem = NULL,
  17. .cb_size = 0,
  18. .stack_mem = NULL,
  19. .stack_size = 0,
  20. .priority = osPriorityNormal,
  21. .tz_module = 0,
  22. .reserved = 0,
  23. };
  24. static sys_dlist_t thread_list;
  25. #if CONFIG_CMSIS_V2_THREAD_MAX_COUNT != 0
  26. static struct cv2_thread cv2_thread_pool[CONFIG_CMSIS_V2_THREAD_MAX_COUNT];
  27. static uint32_t thread_num;
  28. #endif
  29. #if CONFIG_CMSIS_V2_THREAD_DYNAMIC_MAX_COUNT != 0
  30. static uint32_t thread_num_dynamic;
  31. #endif
  32. #if CONFIG_CMSIS_V2_THREAD_DYNAMIC_MAX_COUNT != 0
  33. static K_THREAD_STACK_ARRAY_DEFINE(cv2_thread_stack_pool, \
  34. CONFIG_CMSIS_V2_THREAD_DYNAMIC_MAX_COUNT, \
  35. CONFIG_CMSIS_V2_THREAD_DYNAMIC_STACK_SIZE);
  36. #endif
  37. static inline int _is_thread_cmsis_inactive(struct k_thread *thread)
  38. {
  39. uint8_t state = thread->base.thread_state;
  40. return state & (_THREAD_PRESTART | _THREAD_DEAD);
  41. }
  42. static inline uint32_t zephyr_to_cmsis_priority(uint32_t z_prio)
  43. {
  44. return (osPriorityISR - z_prio);
  45. }
  46. static inline uint32_t cmsis_to_zephyr_priority(uint32_t c_prio)
  47. {
  48. return (osPriorityISR - c_prio);
  49. }
  50. static void zephyr_thread_wrapper(void *arg1, void *arg2, void *arg3)
  51. {
  52. struct cv2_thread *tid = arg2;
  53. void * (*fun_ptr)(void *) = arg3;
  54. fun_ptr(arg1);
  55. tid->has_joined = TRUE;
  56. k_sem_give(&tid->join_guard);
  57. }
  58. void *is_cmsis_rtos_v2_thread(void *thread_id)
  59. {
  60. sys_dnode_t *pnode;
  61. struct cv2_thread *itr;
  62. SYS_DLIST_FOR_EACH_NODE(&thread_list, pnode) {
  63. itr = CONTAINER_OF(pnode, struct cv2_thread, node);
  64. if ((void *)itr == thread_id) {
  65. return itr;
  66. }
  67. }
  68. return NULL;
  69. }
  70. osThreadId_t get_cmsis_thread_id(k_tid_t tid)
  71. {
  72. sys_dnode_t *pnode;
  73. struct cv2_thread *itr;
  74. if (tid != NULL) {
  75. SYS_DLIST_FOR_EACH_NODE(&thread_list, pnode) {
  76. itr = CONTAINER_OF(pnode, struct cv2_thread, node);
  77. if (&itr->z_thread == tid) {
  78. return (osThreadId_t)itr;
  79. }
  80. }
  81. }
  82. return NULL;
  83. }
  84. /**
  85. * @brief Create a thread and add it to Active Threads.
  86. */
  87. osThreadId_t osThreadNew(osThreadFunc_t threadfunc, void *arg,
  88. const osThreadAttr_t *attr)
  89. {
  90. int32_t prio;
  91. osPriority_t cv2_prio;
  92. struct cv2_thread *tid;
  93. static uint32_t one_time;
  94. void *stack;
  95. size_t stack_size;
  96. #if CONFIG_CMSIS_V2_THREAD_MAX_COUNT != 0
  97. uint32_t this_thread_num;
  98. #endif
  99. void *stack_in_heap = NULL;
  100. void *tid_in_heap = NULL;
  101. if (k_is_in_isr()) {
  102. return NULL;
  103. }
  104. #if CONFIG_CMSIS_V2_THREAD_MAX_COUNT != 0
  105. if (thread_num >= CONFIG_CMSIS_V2_THREAD_MAX_COUNT) {
  106. return NULL;
  107. }
  108. #endif
  109. if (attr == NULL) {
  110. attr = &init_thread_attrs;
  111. }
  112. if (attr->priority == osPriorityNone) {
  113. cv2_prio = osPriorityNormal;
  114. } else {
  115. cv2_prio = attr->priority;
  116. }
  117. #if CONFIG_CMSIS_V2_THREAD_DYNAMIC_MAX_COUNT != 0
  118. if ((attr->stack_mem == NULL) && (thread_num_dynamic >=
  119. CONFIG_CMSIS_V2_THREAD_DYNAMIC_MAX_COUNT)) {
  120. return NULL;
  121. }
  122. #endif
  123. BUILD_ASSERT(osPriorityISR <= CONFIG_NUM_PREEMPT_PRIORITIES,
  124. "Configure NUM_PREEMPT_PRIORITIES to at least osPriorityISR");
  125. BUILD_ASSERT(CONFIG_CMSIS_V2_THREAD_DYNAMIC_MAX_COUNT <=
  126. CONFIG_CMSIS_V2_THREAD_MAX_COUNT,
  127. "Number of dynamic threads cannot exceed max number of threads.");
  128. BUILD_ASSERT(CONFIG_CMSIS_V2_THREAD_DYNAMIC_STACK_SIZE <=
  129. CONFIG_CMSIS_V2_THREAD_MAX_STACK_SIZE,
  130. "Default dynamic thread stack size cannot exceed max stack size");
  131. __ASSERT(attr->stack_size <= CONFIG_CMSIS_V2_THREAD_MAX_STACK_SIZE,
  132. "invalid stack size\n");
  133. __ASSERT((cv2_prio >= osPriorityIdle) && (cv2_prio <= osPriorityISR),
  134. "invalid priority\n");
  135. if (attr->stack_mem != NULL) {
  136. if (attr->stack_size == 0) {
  137. return NULL;
  138. }
  139. }
  140. prio = cmsis_to_zephyr_priority(cv2_prio);
  141. #if CONFIG_CMSIS_V2_THREAD_MAX_COUNT != 0
  142. this_thread_num = atomic_inc((atomic_t *)&thread_num);
  143. tid = &cv2_thread_pool[this_thread_num];
  144. #else
  145. tid_in_heap = k_malloc(sizeof(struct cv2_thread));
  146. //printk("tid: %p %d\r\n", tid_in_heap, sizeof(struct cv2_thread));
  147. tid = (struct cv2_thread *)tid_in_heap;
  148. if (tid == NULL) {
  149. return NULL;
  150. }
  151. #endif
  152. tid->attr_bits = attr->attr_bits;
  153. #if CONFIG_CMSIS_V2_THREAD_DYNAMIC_MAX_COUNT != 0
  154. if (attr->stack_mem == NULL) {
  155. uint32_t this_thread_num_dynamic;
  156. __ASSERT(CONFIG_CMSIS_V2_THREAD_DYNAMIC_STACK_SIZE > 0,
  157. "dynamic stack size must be configured to be non-zero\n");
  158. this_thread_num_dynamic =
  159. atomic_inc((atomic_t *)&thread_num_dynamic);
  160. stack_size = CONFIG_CMSIS_V2_THREAD_DYNAMIC_STACK_SIZE;
  161. stack = cv2_thread_stack_pool[this_thread_num_dynamic];
  162. } else
  163. #else
  164. if ((attr->stack_mem == NULL) && (attr->stack_size > 0)) {
  165. stack_in_heap = k_aligned_alloc(ARCH_STACK_PTR_ALIGN, attr->stack_size);
  166. //printk("stack: %p %d\r\n", stack_in_heap, attr->stack_size);
  167. stack_size = attr->stack_size;
  168. stack = stack_in_heap;
  169. } else
  170. #endif
  171. {
  172. stack_size = attr->stack_size;
  173. stack = attr->stack_mem;
  174. }
  175. k_poll_signal_init(&tid->poll_signal);
  176. k_poll_event_init(&tid->poll_event, K_POLL_TYPE_SIGNAL,
  177. K_POLL_MODE_NOTIFY_ONLY, &tid->poll_signal);
  178. tid->signal_results = 0U;
  179. /* TODO: Do this somewhere only once */
  180. if (one_time == 0U) {
  181. sys_dlist_init(&thread_list);
  182. one_time = 1U;
  183. }
  184. sys_dlist_append(&thread_list, &tid->node);
  185. k_sem_init(&tid->join_guard, 0, 1);
  186. tid->has_joined = FALSE;
  187. tid->tid_in_heap = tid_in_heap;
  188. tid->stack_in_heap = stack_in_heap;
  189. (void)k_thread_create(&tid->z_thread,
  190. stack, stack_size,
  191. (k_thread_entry_t)zephyr_thread_wrapper,
  192. (void *)arg, tid, threadfunc,
  193. prio, 0, K_NO_WAIT);
  194. if (attr->name == NULL) {
  195. strncpy(tid->name, init_thread_attrs.name,
  196. sizeof(tid->name) - 1);
  197. } else {
  198. strncpy(tid->name, attr->name, sizeof(tid->name) - 1);
  199. }
  200. k_thread_name_set(&tid->z_thread, tid->name);
  201. return (osThreadId_t)tid;
  202. }
  203. /**
  204. * @brief Get name of a thread.
  205. */
  206. const char *osThreadGetName(osThreadId_t thread_id)
  207. {
  208. const char *name = NULL;
  209. if (k_is_in_isr() || (thread_id == NULL)) {
  210. name = NULL;
  211. } else {
  212. if (is_cmsis_rtos_v2_thread(thread_id) == NULL) {
  213. name = NULL;
  214. } else {
  215. struct cv2_thread *tid =
  216. (struct cv2_thread *)thread_id;
  217. name = k_thread_name_get(&tid->z_thread);
  218. }
  219. }
  220. return name;
  221. }
  222. /**
  223. * @brief Return the thread ID of the current running thread.
  224. */
  225. osThreadId_t osThreadGetId(void)
  226. {
  227. k_tid_t tid = k_current_get();
  228. return get_cmsis_thread_id(tid);
  229. }
  230. /**
  231. * @brief Get current priority of an active thread.
  232. */
  233. osPriority_t osThreadGetPriority(osThreadId_t thread_id)
  234. {
  235. struct cv2_thread *tid = (struct cv2_thread *)thread_id;
  236. uint32_t priority;
  237. if (k_is_in_isr() || (tid == NULL) ||
  238. (is_cmsis_rtos_v2_thread(tid) == NULL) ||
  239. (_is_thread_cmsis_inactive(&tid->z_thread))) {
  240. return osPriorityError;
  241. }
  242. priority = k_thread_priority_get(&tid->z_thread);
  243. return zephyr_to_cmsis_priority(priority);
  244. }
  245. /**
  246. * @brief Change priority of an active thread.
  247. */
  248. osStatus_t osThreadSetPriority(osThreadId_t thread_id, osPriority_t priority)
  249. {
  250. struct cv2_thread *tid = (struct cv2_thread *)thread_id;
  251. if ((tid == NULL) || (is_cmsis_rtos_v2_thread(tid) == NULL) ||
  252. (priority <= osPriorityNone) || (priority > osPriorityISR)) {
  253. return osErrorParameter;
  254. }
  255. if (k_is_in_isr()) {
  256. return osErrorISR;
  257. }
  258. if (_is_thread_cmsis_inactive(&tid->z_thread)) {
  259. return osErrorResource;
  260. }
  261. k_thread_priority_set((k_tid_t)&tid->z_thread,
  262. cmsis_to_zephyr_priority(priority));
  263. return osOK;
  264. }
  265. /**
  266. * @brief Get current thread state of a thread.
  267. */
  268. osThreadState_t osThreadGetState(osThreadId_t thread_id)
  269. {
  270. struct cv2_thread *tid = (struct cv2_thread *)thread_id;
  271. osThreadState_t state;
  272. if (k_is_in_isr() || (tid == NULL) ||
  273. (is_cmsis_rtos_v2_thread(tid) == NULL)) {
  274. return osThreadError;
  275. }
  276. switch (tid->z_thread.base.thread_state) {
  277. case _THREAD_DUMMY:
  278. state = osThreadError;
  279. break;
  280. case _THREAD_PRESTART:
  281. state = osThreadInactive;
  282. break;
  283. case _THREAD_DEAD:
  284. state = osThreadTerminated;
  285. break;
  286. case _THREAD_SUSPENDED:
  287. case _THREAD_PENDING:
  288. state = osThreadBlocked;
  289. break;
  290. case _THREAD_QUEUED:
  291. state = osThreadReady;
  292. break;
  293. default:
  294. state = osThreadError;
  295. break;
  296. }
  297. if (osThreadGetId() == thread_id) {
  298. state = osThreadRunning;
  299. }
  300. return state;
  301. }
  302. /**
  303. * @brief Pass control to next thread that is in READY state.
  304. */
  305. osStatus_t osThreadYield(void)
  306. {
  307. if (k_is_in_isr()) {
  308. return osErrorISR;
  309. }
  310. k_yield();
  311. return osOK;
  312. }
  313. /**
  314. * @brief Get stack size of a thread.
  315. */
  316. uint32_t osThreadGetStackSize(osThreadId_t thread_id)
  317. {
  318. struct cv2_thread *tid = (struct cv2_thread *)thread_id;
  319. __ASSERT(tid, "");
  320. __ASSERT(is_cmsis_rtos_v2_thread(tid), "");
  321. __ASSERT(!k_is_in_isr(), "");
  322. return tid->z_thread.stack_info.size;
  323. }
  324. /**
  325. * @brief Get available stack space of a thread based on stack watermark
  326. * recording during execution.
  327. */
  328. uint32_t osThreadGetStackSpace(osThreadId_t thread_id)
  329. {
  330. struct cv2_thread *tid = (struct cv2_thread *)thread_id;
  331. size_t unused;
  332. int ret;
  333. __ASSERT(tid, "");
  334. __ASSERT(is_cmsis_rtos_v2_thread(tid), "");
  335. __ASSERT(!k_is_in_isr(), "");
  336. ret = k_thread_stack_space_get(&tid->z_thread, &unused);
  337. if (ret != 0) {
  338. unused = 0;
  339. }
  340. return (uint32_t)unused;
  341. }
  342. /**
  343. * @brief Suspend execution of a thread.
  344. */
  345. osStatus_t osThreadSuspend(osThreadId_t thread_id)
  346. {
  347. struct cv2_thread *tid = (struct cv2_thread *)thread_id;
  348. if ((tid == NULL) || (is_cmsis_rtos_v2_thread(tid) == NULL)) {
  349. return osErrorParameter;
  350. }
  351. if (k_is_in_isr()) {
  352. return osErrorISR;
  353. }
  354. if (_is_thread_cmsis_inactive(&tid->z_thread)) {
  355. return osErrorResource;
  356. }
  357. k_thread_suspend(&tid->z_thread);
  358. return osOK;
  359. }
  360. /**
  361. * @brief Resume execution of a thread.
  362. */
  363. osStatus_t osThreadResume(osThreadId_t thread_id)
  364. {
  365. struct cv2_thread *tid = (struct cv2_thread *)thread_id;
  366. if ((tid == NULL) || (is_cmsis_rtos_v2_thread(tid) == NULL)) {
  367. return osErrorParameter;
  368. }
  369. if (k_is_in_isr()) {
  370. return osErrorISR;
  371. }
  372. if (_is_thread_cmsis_inactive(&tid->z_thread)) {
  373. return osErrorResource;
  374. }
  375. k_thread_resume(&tid->z_thread);
  376. return osOK;
  377. }
  378. /**
  379. * @brief Detach a thread (thread storage can be reclaimed when thread
  380. * terminates).
  381. */
  382. osStatus_t osThreadDetach(osThreadId_t thread_id)
  383. {
  384. struct cv2_thread *tid = (struct cv2_thread *)thread_id;
  385. if ((tid == NULL) || (is_cmsis_rtos_v2_thread(tid) == NULL)) {
  386. return osErrorParameter;
  387. }
  388. if (k_is_in_isr()) {
  389. return osErrorISR;
  390. }
  391. if (_is_thread_cmsis_inactive(&tid->z_thread)) {
  392. return osErrorResource;
  393. }
  394. __ASSERT(tid->attr_bits != osThreadDetached,
  395. "Thread already detached, behaviour undefined.");
  396. tid->attr_bits = osThreadDetached;
  397. k_sem_give(&tid->join_guard);
  398. return osOK;
  399. }
  400. /**
  401. * @brief Wait for specified thread to terminate.
  402. */
  403. osStatus_t osThreadJoin(osThreadId_t thread_id)
  404. {
  405. struct cv2_thread *tid = (struct cv2_thread *)thread_id;
  406. osStatus_t status = osError;
  407. if ((tid == NULL) || (is_cmsis_rtos_v2_thread(tid) == NULL)) {
  408. return osErrorParameter;
  409. }
  410. if (k_is_in_isr()) {
  411. return osErrorISR;
  412. }
  413. if (_is_thread_cmsis_inactive(&tid->z_thread)) {
  414. return osErrorResource;
  415. }
  416. if (tid->attr_bits != osThreadJoinable) {
  417. return osErrorResource;
  418. }
  419. if (!tid->has_joined) {
  420. if (k_sem_take(&tid->join_guard, K_FOREVER) != 0) {
  421. __ASSERT(0, "Failed to take from join guard.");
  422. }
  423. k_sem_give(&tid->join_guard);
  424. }
  425. if (tid->has_joined && (tid->attr_bits == osThreadJoinable)) {
  426. status = osOK;
  427. } else {
  428. status = osErrorResource;
  429. }
  430. return status;
  431. }
  432. /**
  433. * @brief Terminate execution of current running thread.
  434. */
  435. __NO_RETURN void osThreadExit(void)
  436. {
  437. struct cv2_thread *tid;
  438. __ASSERT(!k_is_in_isr(), "");
  439. tid = osThreadGetId();
  440. k_sem_give(&tid->join_guard);
  441. k_thread_abort((k_tid_t)&tid->z_thread);
  442. if (tid->stack_in_heap) {
  443. k_free(tid->stack_in_heap);
  444. }
  445. if (tid->tid_in_heap) {
  446. k_free(tid->tid_in_heap);
  447. }
  448. CODE_UNREACHABLE;
  449. }
  450. /**
  451. * @brief Terminate execution of a thread.
  452. */
  453. osStatus_t osThreadTerminate(osThreadId_t thread_id)
  454. {
  455. struct cv2_thread *tid = (struct cv2_thread *)thread_id;
  456. if ((tid == NULL) || (is_cmsis_rtos_v2_thread(tid) == NULL)) {
  457. return osErrorParameter;
  458. }
  459. if (k_is_in_isr()) {
  460. return osErrorISR;
  461. }
  462. if (_is_thread_cmsis_inactive(&tid->z_thread)) {
  463. return osErrorResource;
  464. }
  465. k_sem_give(&tid->join_guard);
  466. k_thread_abort((k_tid_t)&tid->z_thread);
  467. if (tid->stack_in_heap) {
  468. k_free(tid->stack_in_heap);
  469. }
  470. if (tid->tid_in_heap) {
  471. k_free(tid->tid_in_heap);
  472. }
  473. return osOK;
  474. }
  475. /**
  476. * @brief Get number of active threads.
  477. */
  478. uint32_t osThreadGetCount(void)
  479. {
  480. struct k_thread *thread;
  481. uint32_t count = 0U;
  482. __ASSERT(!k_is_in_isr(), "");
  483. for (thread = _kernel.threads; thread; thread = thread->next_thread) {
  484. if (get_cmsis_thread_id(thread) && z_is_thread_queued(thread)) {
  485. count++;
  486. }
  487. }
  488. return count;
  489. }
  490. /**
  491. * @brief Enumerate active threads.
  492. */
  493. uint32_t osThreadEnumerate(osThreadId_t *thread_array, uint32_t array_items)
  494. {
  495. struct k_thread *thread;
  496. uint32_t count = 0U;
  497. osThreadId_t tid;
  498. __ASSERT(!k_is_in_isr(), "");
  499. __ASSERT(thread_array != NULL, "");
  500. __ASSERT(array_items, "");
  501. for (thread = _kernel.threads; thread; thread = thread->next_thread) {
  502. if (count == array_items) {
  503. break;
  504. }
  505. tid = get_cmsis_thread_id(thread);
  506. if (tid != NULL) {
  507. thread_array[count] = tid;
  508. count++;
  509. }
  510. }
  511. return (count);
  512. }