rtx_thread.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537
  1. /*
  2. * Copyright (c) 2013-2016 ARM Limited. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the License); you may
  7. * not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  14. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * -----------------------------------------------------------------------------
  19. *
  20. * Project: CMSIS-RTOS RTX
  21. * Title: Thread functions
  22. *
  23. * -----------------------------------------------------------------------------
  24. */
  25. #include "rtx_lib.h"
  26. // ==== Helper functions ====
  27. /// Set Thread Flags.
  28. /// \param[in] thread thread object.
  29. /// \param[in] flags specifies the flags to set.
  30. /// \return thread flags after setting.
  31. static int32_t os_ThreadFlagsSet (os_thread_t *thread, int32_t flags) {
  32. #if (__EXCLUSIVE_ACCESS == 0U)
  33. uint32_t primask = __get_PRIMASK();
  34. #endif
  35. int32_t thread_flags;
  36. #if (__EXCLUSIVE_ACCESS == 0U)
  37. __disable_irq();
  38. thread->thread_flags |= flags;
  39. thread_flags = thread->thread_flags;
  40. if (primask == 0U) {
  41. __enable_irq();
  42. }
  43. #else
  44. thread_flags = (int32_t)os_exc_set32((uint32_t *)&thread->thread_flags, (uint32_t)flags);
  45. #endif
  46. return thread_flags;
  47. }
  48. /// Clear Thread Flags.
  49. /// \param[in] thread thread object.
  50. /// \param[in] flags specifies the flags to clear.
  51. /// \return thread flags before clearing.
  52. static int32_t os_ThreadFlagsClear (os_thread_t *thread, int32_t flags) {
  53. #if (__EXCLUSIVE_ACCESS == 0U)
  54. uint32_t primask = __get_PRIMASK();
  55. #endif
  56. int32_t thread_flags;
  57. #if (__EXCLUSIVE_ACCESS == 0U)
  58. __disable_irq();
  59. thread_flags = thread->thread_flags;
  60. thread->thread_flags &= ~flags;
  61. if (primask == 0U) {
  62. __enable_irq();
  63. }
  64. #else
  65. thread_flags = (int32_t)os_exc_clr32((uint32_t *)&thread->thread_flags, (uint32_t)flags);
  66. #endif
  67. return thread_flags;
  68. }
  69. /// Check Thread Flags.
  70. /// \param[in] thread thread object.
  71. /// \param[in] flags specifies the flags to check.
  72. /// \param[in] options specifies flags options (osFlagsXxxx).
  73. /// \return thread flags before clearing or 0 if specified flags have not been set.
  74. static int32_t os_ThreadFlagsCheck (os_thread_t *thread, int32_t flags, uint32_t options) {
  75. #if (__EXCLUSIVE_ACCESS == 0U)
  76. uint32_t primask;
  77. #endif
  78. int32_t thread_flags;
  79. if ((options & osFlagsNoClear) == 0U) {
  80. #if (__EXCLUSIVE_ACCESS == 0U)
  81. primask = __get_PRIMASK();
  82. __disable_irq();
  83. thread_flags = thread->thread_flags;
  84. if ((((options & osFlagsWaitAll) != 0U) && ((thread_flags & flags) != flags)) ||
  85. (((options & osFlagsWaitAll) == 0U) && ((thread_flags & flags) == 0))) {
  86. thread_flags = 0;
  87. } else {
  88. thread->thread_flags &= ~flags;
  89. }
  90. if (primask == 0U) {
  91. __enable_irq();
  92. }
  93. #else
  94. if ((options & osFlagsWaitAll) != 0U) {
  95. thread_flags = (int32_t)os_exc_chk32_all((uint32_t *)&thread->thread_flags, (uint32_t)flags);
  96. } else {
  97. thread_flags = (int32_t)os_exc_chk32_any((uint32_t *)&thread->thread_flags, (uint32_t)flags);
  98. }
  99. #endif
  100. } else {
  101. thread_flags = thread->thread_flags;
  102. if ((((options & osFlagsWaitAll) != 0U) && ((thread_flags & flags) != flags)) ||
  103. (((options & osFlagsWaitAll) == 0U) && ((thread_flags & flags) == 0))) {
  104. thread_flags = 0;
  105. }
  106. }
  107. return thread_flags;
  108. }
  109. // ==== Library functions ====
  110. /// Put a Thread into specified Object list sorted by Priority (Highest at Head).
  111. /// \param[in] object generic object.
  112. /// \param[in] thread thread object.
  113. void os_ThreadListPut (volatile os_object_t *object, os_thread_t *thread) {
  114. os_thread_t *prev, *next;
  115. int32_t priority;
  116. if (thread == NULL) {
  117. return;
  118. }
  119. priority = thread->priority;
  120. prev = (os_thread_t *)(uint32_t)object;
  121. next = object->thread_list;
  122. while ((next != NULL) && (next->priority >= priority)) {
  123. prev = next;
  124. next = next->thread_next;
  125. }
  126. thread->thread_prev = prev;
  127. thread->thread_next = next;
  128. prev->thread_next = thread;
  129. if (next != NULL) {
  130. next->thread_prev = thread;
  131. }
  132. }
  133. /// Get a Thread with Highest Priority from specified Object list and remove it.
  134. /// \param[in] object generic object.
  135. /// \return thread object.
  136. os_thread_t *os_ThreadListGet (volatile os_object_t *object) {
  137. os_thread_t *thread;
  138. thread = object->thread_list;
  139. if (thread != NULL) {
  140. object->thread_list = thread->thread_next;
  141. if (thread->thread_next != NULL) {
  142. thread->thread_next->thread_prev = (os_thread_t *)(uint32_t)object;
  143. }
  144. thread->thread_prev = NULL;
  145. }
  146. return thread;
  147. }
  148. /// Re-sort a Thread in linked Object list by Priority (Highest at Head).
  149. /// \param[in] thread thread object.
  150. void os_ThreadListSort (os_thread_t *thread) {
  151. os_object_t *object;
  152. os_thread_t *thread0;
  153. // Search for object
  154. thread0 = thread;
  155. while (thread0->id == os_IdThread) {
  156. thread0 = thread0->thread_prev;
  157. if (thread0 == NULL) {
  158. return;
  159. }
  160. }
  161. object = (os_object_t *)thread0;
  162. os_ThreadListRemove(thread);
  163. os_ThreadListPut(object, thread);
  164. }
  165. /// Remove a Thread from linked Object list.
  166. /// \param[in] thread thread object.
  167. void os_ThreadListRemove (os_thread_t *thread) {
  168. if (thread->thread_prev != NULL) {
  169. thread->thread_prev->thread_next = thread->thread_next;
  170. if (thread->thread_next != NULL) {
  171. thread->thread_next->thread_prev = thread->thread_prev;
  172. }
  173. thread->thread_prev = NULL;
  174. }
  175. }
  176. /// Unlink a Thread from specified linked list.
  177. /// \param[in] thread thread object.
  178. void os_ThreadListUnlink (os_thread_t **thread_list, os_thread_t *thread) {
  179. if (thread->thread_next != NULL) {
  180. thread->thread_next->thread_prev = thread->thread_prev;
  181. }
  182. if (thread->thread_prev != NULL) {
  183. thread->thread_prev->thread_next = thread->thread_next;
  184. thread->thread_prev = NULL;
  185. } else {
  186. *thread_list = thread->thread_next;
  187. }
  188. }
  189. /// Mark a Thread as Ready and put it into Ready list (sorted by Priority).
  190. /// \param[in] thread thread object.
  191. void os_ThreadReadyPut (os_thread_t *thread) {
  192. thread->state = os_ThreadReady;
  193. os_ThreadListPut(&os_Info.thread.ready, thread);
  194. }
  195. /// Insert a Thread into the Delay list sorted by Delay (Lowest at Head).
  196. /// \param[in] thread thread object.
  197. /// \param[in] delay delay value.
  198. void os_ThreadDelayInsert (os_thread_t *thread, uint32_t delay) {
  199. os_thread_t *prev, *next;
  200. if (delay == osWaitForever) {
  201. prev = NULL;
  202. next = os_Info.thread.wait_list;
  203. while (next != NULL) {
  204. prev = next;
  205. next = next->delay_next;
  206. }
  207. thread->delay = delay;
  208. thread->delay_prev = prev;
  209. thread->delay_next = next;
  210. if (prev != NULL) {
  211. prev->delay_next = thread;
  212. } else {
  213. os_Info.thread.wait_list = thread;
  214. }
  215. if (next != NULL) {
  216. next->delay_prev = thread;
  217. }
  218. } else {
  219. prev = NULL;
  220. next = os_Info.thread.delay_list;
  221. while ((next != NULL) && (next->delay <= delay)) {
  222. delay -= next->delay;
  223. prev = next;
  224. next = next->delay_next;
  225. }
  226. thread->delay = delay;
  227. thread->delay_prev = prev;
  228. thread->delay_next = next;
  229. if (prev != NULL) {
  230. prev->delay_next = thread;
  231. } else {
  232. os_Info.thread.delay_list = thread;
  233. }
  234. if (next != NULL) {
  235. next->delay -= delay;
  236. next->delay_prev = thread;
  237. }
  238. }
  239. }
  240. /// Remove a Thread from the Delay list.
  241. /// \param[in] thread thread object.
  242. void os_ThreadDelayRemove (os_thread_t *thread) {
  243. if (thread->delay == osWaitForever) {
  244. if ((thread->delay_prev == NULL) && (os_Info.thread.wait_list != thread)) {
  245. return;
  246. }
  247. if (thread->delay_next != NULL) {
  248. thread->delay_next->delay_prev = thread->delay_prev;
  249. }
  250. if (thread->delay_prev != NULL) {
  251. thread->delay_prev->delay_next = thread->delay_next;
  252. thread->delay_prev = NULL;
  253. } else {
  254. os_Info.thread.wait_list = thread->delay_next;
  255. }
  256. } else {
  257. if ((thread->delay_prev == NULL) && (os_Info.thread.delay_list != thread)) {
  258. return;
  259. }
  260. if (thread->delay_next != NULL) {
  261. thread->delay_next->delay += thread->delay;
  262. thread->delay_next->delay_prev = thread->delay_prev;
  263. }
  264. if (thread->delay_prev != NULL) {
  265. thread->delay_prev->delay_next = thread->delay_next;
  266. thread->delay_prev = NULL;
  267. } else {
  268. os_Info.thread.delay_list = thread->delay_next;
  269. }
  270. }
  271. }
  272. /// Process Thread Delay Tick (executed each System Tick).
  273. void os_ThreadDelayTick (void) {
  274. os_thread_t *thread;
  275. thread = os_Info.thread.delay_list;
  276. if (thread == NULL) {
  277. return;
  278. }
  279. thread->delay--;
  280. if (thread->delay == 0U) {
  281. do {
  282. os_ThreadListRemove(thread);
  283. os_ThreadReadyPut(thread);
  284. thread = thread->delay_next;
  285. } while ((thread != NULL) && (thread->delay == 0U));
  286. if (thread != NULL) {
  287. thread->delay_prev = NULL;
  288. }
  289. os_Info.thread.delay_list = thread;
  290. }
  291. }
  292. /// Get pointer to Thread registers (R0..R3)
  293. /// \param[in] thread thread object.
  294. /// \return pointer to registers R0-R3.
  295. uint32_t *os_ThreadRegPtr (os_thread_t *thread) {
  296. #if (__FPU_USED == 1U)
  297. if (IS_EXTENDED_STACK_FRAME(thread->stack_frame)) {
  298. // Extended Stack Frame: S16-S31, R4-R11, R0-R3, R12, LR, PC, xPSR, S0-S15, FPSCR
  299. return ((uint32_t *)(thread->sp + (16U+8U)*4U));
  300. } else {
  301. // Basic Stack Frame: R4-R11, R0-R3, R12, LR, PC, xPSR
  302. return ((uint32_t *)(thread->sp + 8U *4U));
  303. }
  304. #else
  305. // Stack Frame: R4-R11, R0-R3, R12, LR, PC, xPSR
  306. return ((uint32_t *)(thread->sp + 8U*4U));
  307. #endif
  308. }
  309. /// Block running Thread execution and register it as Ready to Run.
  310. /// \param[in] thread running thread object.
  311. void os_ThreadBlock (os_thread_t *thread) {
  312. os_thread_t *prev, *next;
  313. int32_t priority;
  314. thread->state = os_ThreadReady;
  315. priority = thread->priority;
  316. prev = (os_thread_t *)(uint32_t)&os_Info.thread.ready;
  317. next = prev->thread_next;
  318. while ((next != NULL) && (next->priority > priority)) {
  319. prev = next;
  320. next = next->thread_next;
  321. }
  322. thread->thread_prev = prev;
  323. thread->thread_next = next;
  324. prev->thread_next = thread;
  325. if (next != NULL) {
  326. next->thread_prev = thread;
  327. }
  328. }
  329. /// Switch to specified Thread.
  330. /// \param[in] thread thread object.
  331. void os_ThreadSwitch (os_thread_t *thread) {
  332. thread->state = os_ThreadRunning;
  333. os_Info.thread.run.next = thread;
  334. os_ThreadStackCheck();
  335. }
  336. /// Dispatch specified Thread or Ready Thread with Highest Priority.
  337. /// \param[in] thread thread object or NULL.
  338. void os_ThreadDispatch (os_thread_t *thread) {
  339. uint8_t kernel_state;
  340. os_thread_t *thread_running;
  341. kernel_state = os_KernelGetState();
  342. thread_running = os_ThreadGetRunning();
  343. if (thread == NULL) {
  344. thread = os_Info.thread.ready.thread_list;
  345. if ((kernel_state == os_KernelRunning) &&
  346. (thread_running != NULL) && (thread != NULL) &&
  347. (thread->priority > thread_running->priority)) {
  348. // Preempt running Thread
  349. os_ThreadListRemove(thread);
  350. os_ThreadBlock(thread_running);
  351. os_ThreadSwitch(thread);
  352. }
  353. } else {
  354. if ((kernel_state == os_KernelRunning) &&
  355. (thread_running != NULL) &&
  356. (thread->priority > thread_running->priority)) {
  357. // Preempt running Thread
  358. os_ThreadBlock(thread_running);
  359. os_ThreadSwitch(thread);
  360. } else {
  361. // Put Thread into Ready list
  362. os_ThreadReadyPut(thread);
  363. }
  364. }
  365. }
  366. /// Exit Thread wait state.
  367. /// \param[in] thread thread object.
  368. /// \param[in] ret_val return value.
  369. /// \param[in] dispatch dispatch flag.
  370. void os_ThreadWaitExit (os_thread_t *thread, uint32_t ret_val, bool dispatch) {
  371. uint32_t *reg;
  372. reg = os_ThreadRegPtr(thread);
  373. reg[0] = ret_val;
  374. os_ThreadDelayRemove(thread);
  375. if (dispatch) {
  376. os_ThreadDispatch(thread);
  377. } else {
  378. os_ThreadReadyPut(thread);
  379. }
  380. }
  381. /// Enter Thread wait state.
  382. /// \param[in] state new thread state.
  383. /// \param[in] timeout timeout.
  384. /// \return true - success, false - failure.
  385. bool os_ThreadWaitEnter (uint8_t state, uint32_t timeout) {
  386. os_thread_t *thread;
  387. thread = os_ThreadGetRunning();
  388. if (thread == NULL) {
  389. return false;
  390. }
  391. if (os_KernelGetState() != os_KernelRunning) {
  392. os_ThreadListRemove(thread);
  393. return false;
  394. }
  395. if (os_Info.thread.ready.thread_list == NULL) {
  396. return false;
  397. }
  398. thread->state = state;
  399. os_ThreadDelayInsert(thread, timeout);
  400. thread = os_ThreadListGet(&os_Info.thread.ready);
  401. os_ThreadSwitch(thread);
  402. return true;
  403. }
  404. /// Check current running Thread Stack.
  405. __WEAK void os_ThreadStackCheck (void) {
  406. os_thread_t *thread;
  407. thread = os_ThreadGetRunning();
  408. if (thread != NULL) {
  409. if ((thread->sp <= (uint32_t)thread->stack_mem) ||
  410. (*((uint32_t *)thread->stack_mem) != os_StackMagicWord)) {
  411. os_Error(os_ErrorStackUnderflow, thread);
  412. }
  413. }
  414. }
  415. /// Thread post ISR processing.
  416. /// \param[in] thread thread object.
  417. void os_ThreadPostProcess (os_thread_t *thread) {
  418. int32_t thread_flags;
  419. if ((thread->state == os_ThreadInactive) ||
  420. (thread->state == os_ThreadTerminated)) {
  421. return;
  422. }
  423. // Check if Thread is waiting for Thread Flags
  424. if (thread->state == os_ThreadWaitingThreadFlags) {
  425. thread_flags = os_ThreadFlagsCheck(thread, thread->wait_flags, thread->flags_options);
  426. if (thread_flags > 0) {
  427. os_ThreadWaitExit(thread, (uint32_t)thread_flags, false);
  428. }
  429. }
  430. }
  431. // ==== Service Calls ====
  432. // Service Calls definitions
  433. SVC0_3 (ThreadNew, osThreadId_t, os_thread_func_t, void *, const osThreadAttr_t *)
  434. SVC0_1 (ThreadGetName, const char *, osThreadId_t)
  435. SVC0_0 (ThreadGetId, osThreadId_t)
  436. SVC0_1 (ThreadGetState, osThreadState_t, osThreadId_t)
  437. SVC0_1 (ThreadGetStackSize, uint32_t, osThreadId_t)
  438. SVC0_1 (ThreadGetStackSpace, uint32_t, osThreadId_t)
  439. SVC0_2 (ThreadSetPriority, osStatus_t, osThreadId_t, osPriority_t)
  440. SVC0_1 (ThreadGetPriority, osPriority_t, osThreadId_t)
  441. SVC0_0 (ThreadYield, osStatus_t)
  442. SVC0_1 (ThreadSuspend, osStatus_t, osThreadId_t)
  443. SVC0_1 (ThreadResume, osStatus_t, osThreadId_t)
  444. SVC0_1 (ThreadDetach, osStatus_t, osThreadId_t)
  445. SVC0_1 (ThreadJoin, osStatus_t, osThreadId_t)
  446. SVC0_0N(ThreadExit, void)
  447. SVC0_1 (ThreadTerminate, osStatus_t, osThreadId_t)
  448. SVC0_0 (ThreadGetCount, uint32_t)
  449. SVC0_2 (ThreadEnumerate, uint32_t, osThreadId_t *, uint32_t)
  450. SVC0_2 (ThreadFlagsSet, int32_t, osThreadId_t, int32_t)
  451. SVC0_1 (ThreadFlagsClear, int32_t, int32_t)
  452. SVC0_0 (ThreadFlagsGet, int32_t)
  453. SVC0_3 (ThreadFlagsWait, int32_t, int32_t, uint32_t, uint32_t)
  454. /// Create a thread and add it to Active Threads.
  455. /// \note API identical to osThreadNew
  456. osThreadId_t os_svcThreadNew (os_thread_func_t func, void *argument, const osThreadAttr_t *attr) {
  457. os_thread_t *thread;
  458. uint32_t attr_bits;
  459. void *stack_mem;
  460. uint32_t stack_size;
  461. osPriority_t priority;
  462. uint8_t flags;
  463. const char *name;
  464. uint32_t *ptr;
  465. uint32_t n;
  466. #if (__DOMAIN_NS == 1U)
  467. TZ_ModuleId_t tz_module;
  468. TZ_MemoryId_t tz_memory;
  469. #endif
  470. // Check parameters
  471. if (func == NULL) {
  472. return NULL;
  473. }
  474. // Process attributes
  475. if (attr != NULL) {
  476. name = attr->name;
  477. attr_bits = attr->attr_bits;
  478. thread = attr->cb_mem;
  479. stack_mem = attr->stack_mem;
  480. stack_size = attr->stack_size;
  481. priority = attr->priority;
  482. #if (__DOMAIN_NS == 1U)
  483. tz_module = attr->tz_module;
  484. #endif
  485. if (thread != NULL) {
  486. if (((uint32_t)thread & 3U) || (attr->cb_size < sizeof(os_thread_t))) {
  487. return NULL;
  488. }
  489. } else {
  490. if (attr->cb_size != 0U) {
  491. return NULL;
  492. }
  493. }
  494. if (stack_mem != NULL) {
  495. if (((uint32_t)stack_mem & 7U) || (stack_size == 0U)) {
  496. return NULL;
  497. }
  498. }
  499. if (priority == osPriorityNone) {
  500. priority = osPriorityNormal;
  501. } else {
  502. if ((priority < osPriorityIdle) || (priority > osPriorityISR)) {
  503. return NULL;
  504. }
  505. }
  506. } else {
  507. name = NULL;
  508. attr_bits = 0U;
  509. thread = NULL;
  510. stack_mem = NULL;
  511. stack_size = 0U;
  512. priority = osPriorityNormal;
  513. #if (__DOMAIN_NS == 1U)
  514. tz_module = 0U;
  515. #endif
  516. }
  517. // Check stack size
  518. if ((stack_size != 0U) && ((stack_size & 7U) || (stack_size < (64U + 8U)))) {
  519. return NULL;
  520. }
  521. // Allocate object memory if not provided
  522. if (thread == NULL) {
  523. if (os_Info.mpi.thread != NULL) {
  524. thread = os_MemoryPoolAlloc(os_Info.mpi.thread);
  525. } else {
  526. thread = os_MemoryAlloc(os_Info.mem.common, sizeof(os_thread_t), 1U);
  527. }
  528. if (thread == NULL) {
  529. return NULL;
  530. }
  531. flags = os_FlagSystemObject;
  532. } else {
  533. flags = 0U;
  534. }
  535. // Allocate stack memory if not provided
  536. if (stack_mem == NULL) {
  537. if (stack_size == 0U) {
  538. stack_size = os_Config.thread_stack_size;
  539. if (os_Info.mpi.stack != NULL) {
  540. stack_mem = os_MemoryPoolAlloc(os_Info.mpi.stack);
  541. if (stack_mem != NULL) {
  542. flags |= os_ThreadFlagDefStack;
  543. }
  544. }
  545. }
  546. if (stack_mem == NULL) {
  547. stack_mem = os_MemoryAlloc(os_Info.mem.stack, stack_size, 0U);
  548. }
  549. if (stack_mem == NULL) {
  550. if (flags & os_FlagSystemObject) {
  551. if (os_Info.mpi.thread != NULL) {
  552. os_MemoryPoolFree(os_Info.mpi.thread, thread);
  553. } else {
  554. os_MemoryFree(os_Info.mem.common, thread);
  555. }
  556. }
  557. return NULL;
  558. }
  559. flags |= os_FlagSystemMemory;
  560. }
  561. #if (__DOMAIN_NS == 1U)
  562. // Allocate secure process stack
  563. if (tz_module != 0U) {
  564. tz_memory = TZ_AllocModuleContext_S(tz_module);
  565. if (tz_memory == 0U) {
  566. if (flags & os_FlagSystemMemory) {
  567. if (flags & os_ThreadFlagDefStack) {
  568. os_MemoryPoolFree(os_Info.mpi.stack, thread->stack_mem);
  569. } else {
  570. os_MemoryFree(os_Info.mem.stack, thread->stack_mem);
  571. }
  572. }
  573. if (flags & os_FlagSystemObject) {
  574. if (os_Info.mpi.thread != NULL) {
  575. os_MemoryPoolFree(os_Info.mpi.thread, thread);
  576. } else {
  577. os_MemoryFree(os_Info.mem.common, thread);
  578. }
  579. }
  580. return NULL;
  581. }
  582. } else {
  583. tz_memory = 0U;
  584. }
  585. #endif
  586. // Initialize control block
  587. thread->id = os_IdThread;
  588. thread->state = os_ThreadReady;
  589. thread->flags = flags;
  590. thread->attr = (uint8_t)attr_bits;
  591. thread->name = name;
  592. thread->thread_next = NULL;
  593. thread->thread_prev = NULL;
  594. thread->delay_next = NULL;
  595. thread->delay_prev = NULL;
  596. thread->thread_join = NULL;
  597. thread->delay = 0U;
  598. thread->priority = (int8_t)priority;
  599. thread->priority_base = (int8_t)priority;
  600. thread->stack_frame = STACK_FRAME_INIT;
  601. thread->flags_options = 0U;
  602. thread->wait_flags = 0;
  603. thread->thread_flags = 0;
  604. thread->mutex_list = NULL;
  605. thread->stack_mem = stack_mem;
  606. thread->stack_size = stack_size;
  607. thread->sp = (uint32_t)stack_mem + stack_size - 64U;
  608. #if (__DOMAIN_NS == 1U)
  609. thread->tz_memory = tz_memory;
  610. #endif
  611. // Initialize stack
  612. ptr = (uint32_t *)stack_mem;
  613. *ptr++ = os_StackMagicWord;
  614. if (os_Config.flags & os_ConfigStackWatermark) {
  615. for (n = (stack_size/4U) - (16U + 1U); n; n--) {
  616. *ptr++ = os_StackFillPattern;
  617. }
  618. } else {
  619. ptr = (uint32_t *)thread->sp;
  620. }
  621. for (n = 13U; n; n--) {
  622. *ptr++ = 0U; // R4..R11, R0..R3, R12
  623. }
  624. *ptr++ = (uint32_t)osThreadExit; // LR
  625. *ptr++ = (uint32_t)func; // PC
  626. *ptr++ = XPSR_INITIAL_VALUE; // xPSR
  627. *(ptr-8) = (uint32_t)argument; // R0
  628. // Register post ISR processing function
  629. os_Info.post_process.thread = os_ThreadPostProcess;
  630. os_ThreadDispatch(thread);
  631. return thread;
  632. }
  633. /// Get name of a thread.
  634. /// \note API identical to osThreadGetName
  635. const char *os_svcThreadGetName (osThreadId_t thread_id) {
  636. os_thread_t *thread = (os_thread_t *)thread_id;
  637. // Check parameters
  638. if ((thread == NULL) ||
  639. (thread->id != os_IdThread)) {
  640. return NULL;
  641. }
  642. // Check object state
  643. if (thread->state == os_ObjectInactive) {
  644. return NULL;
  645. }
  646. return thread->name;
  647. }
  648. /// Return the thread ID of the current running thread.
  649. /// \note API identical to osThreadGetId
  650. osThreadId_t os_svcThreadGetId (void) {
  651. os_thread_t *thread;
  652. thread = os_ThreadGetRunning();
  653. return thread;
  654. }
  655. /// Get current thread state of a thread.
  656. /// \note API identical to osThreadGetState
  657. osThreadState_t os_svcThreadGetState (osThreadId_t thread_id) {
  658. os_thread_t *thread = (os_thread_t *)thread_id;
  659. // Check parameters
  660. if ((thread == NULL) ||
  661. (thread->id != os_IdThread)) {
  662. return osThreadError;
  663. }
  664. return ((osThreadState_t)(thread->state & os_ThreadStateMask));
  665. }
  666. /// Get stack size of a thread.
  667. /// \note API identical to osThreadGetStackSize
  668. uint32_t os_svcThreadGetStackSize (osThreadId_t thread_id) {
  669. os_thread_t *thread = (os_thread_t *)thread_id;
  670. // Check parameters
  671. if ((thread == NULL) ||
  672. (thread->id != os_IdThread)) {
  673. return 0U;
  674. }
  675. // Check object state
  676. if (thread->state == os_ObjectInactive) {
  677. return 0U;
  678. }
  679. return thread->stack_size;
  680. }
  681. /// Get available stack space of a thread based on stack watermark recording during execution.
  682. /// \note API identical to osThreadGetStackSpace
  683. uint32_t os_svcThreadGetStackSpace (osThreadId_t thread_id) {
  684. os_thread_t *thread = (os_thread_t *)thread_id;
  685. uint32_t *stack;
  686. uint32_t space;
  687. // Check parameters
  688. if ((thread == NULL) ||
  689. (thread->id != os_IdThread)) {
  690. return 0U;
  691. }
  692. // Check object state
  693. if (thread->state == os_ObjectInactive) {
  694. return 0U;
  695. }
  696. if ((os_Config.flags & os_ConfigStackWatermark) == 0U) {
  697. return 0U;
  698. }
  699. stack = thread->stack_mem;
  700. if (*stack++ != os_StackMagicWord) {
  701. return 0U;
  702. }
  703. for (space = 4U; space < thread->stack_size; space += 4U) {
  704. if (*stack++ != os_StackFillPattern) {
  705. break;
  706. }
  707. }
  708. return space;
  709. }
  710. /// Change priority of a thread.
  711. /// \note API identical to osThreadSetPriority
  712. osStatus_t os_svcThreadSetPriority (osThreadId_t thread_id, osPriority_t priority) {
  713. os_thread_t *thread = (os_thread_t *)thread_id;
  714. // Check parameters
  715. if ((thread == NULL) ||
  716. (thread->id != os_IdThread)) {
  717. return osErrorParameter;
  718. }
  719. if ((priority < osPriorityIdle) || (priority > osPriorityISR)) {
  720. return osErrorParameter;
  721. }
  722. // Check object state
  723. if ((thread->state == os_ThreadInactive) ||
  724. (thread->state == os_ThreadTerminated)) {
  725. return osErrorResource;
  726. }
  727. if (thread->priority != (int8_t)priority) {
  728. thread->priority = (int8_t)priority;
  729. thread->priority_base = (int8_t)priority;
  730. os_ThreadListSort(thread);
  731. os_ThreadDispatch(NULL);
  732. }
  733. return osOK;
  734. }
  735. /// Get current priority of a thread.
  736. /// \note API identical to osThreadGetPriority
  737. osPriority_t os_svcThreadGetPriority (osThreadId_t thread_id) {
  738. os_thread_t *thread = (os_thread_t *)thread_id;
  739. // Check parameters
  740. if ((thread == NULL) ||
  741. (thread->id != os_IdThread)) {
  742. return osPriorityError;
  743. }
  744. // Check object state
  745. if ((thread->state == os_ThreadInactive) ||
  746. (thread->state == os_ThreadTerminated)) {
  747. return osPriorityError;
  748. }
  749. return ((osPriority_t)thread->priority);
  750. }
  751. /// Pass control to next thread that is in state READY.
  752. /// \note API identical to osThreadYield
  753. osStatus_t os_svcThreadYield (void) {
  754. uint8_t kernel_state;
  755. os_thread_t *thread_running;
  756. os_thread_t *thread_ready;
  757. kernel_state = os_KernelGetState();
  758. thread_running = os_ThreadGetRunning();
  759. thread_ready = os_Info.thread.ready.thread_list;
  760. if ((kernel_state == os_KernelRunning) &&
  761. (thread_ready != NULL) && (thread_running != NULL) &&
  762. (thread_ready->priority == thread_running->priority)) {
  763. os_ThreadListRemove(thread_ready);
  764. os_ThreadReadyPut(thread_running);
  765. os_ThreadSwitch(thread_ready);
  766. }
  767. return osOK;
  768. }
  769. /// Suspend execution of a thread.
  770. /// \note API identical to osThreadSuspend
  771. osStatus_t os_svcThreadSuspend (osThreadId_t thread_id) {
  772. os_thread_t *thread = (os_thread_t *)thread_id;
  773. // Check parameters
  774. if ((thread == NULL) ||
  775. (thread->id != os_IdThread)) {
  776. return osErrorParameter;
  777. }
  778. // Check object state
  779. switch (thread->state & os_ThreadStateMask) {
  780. case os_ThreadRunning:
  781. if ((os_KernelGetState() != os_KernelRunning) ||
  782. (os_Info.thread.ready.thread_list == NULL)) {
  783. return osErrorResource;
  784. }
  785. os_ThreadSwitch(os_ThreadListGet(&os_Info.thread.ready));
  786. break;
  787. case os_ThreadReady:
  788. os_ThreadListRemove(thread);
  789. break;
  790. case os_ThreadBlocked:
  791. os_ThreadListRemove(thread);
  792. os_ThreadDelayRemove(thread);
  793. break;
  794. case os_ThreadInactive:
  795. case os_ThreadTerminated:
  796. default:
  797. return osErrorResource;
  798. }
  799. // Update Thread State and put it into Delay list
  800. thread->state = os_ThreadBlocked;
  801. thread->thread_prev = NULL;
  802. thread->thread_next = NULL;
  803. os_ThreadDelayInsert(thread, osWaitForever);
  804. return osOK;
  805. }
  806. /// Resume execution of a thread.
  807. /// \note API identical to osThreadResume
  808. osStatus_t os_svcThreadResume (osThreadId_t thread_id) {
  809. os_thread_t *thread = (os_thread_t *)thread_id;
  810. // Check parameters
  811. if ((thread == NULL) ||
  812. (thread->id != os_IdThread)) {
  813. return osErrorParameter;
  814. }
  815. // Check object state
  816. if ((thread->state & os_ThreadStateMask) != os_ThreadBlocked) {
  817. return osErrorResource;
  818. }
  819. // Wakeup Thread
  820. os_ThreadListRemove(thread);
  821. os_ThreadDelayRemove(thread);
  822. os_ThreadDispatch(thread);
  823. return osOK;
  824. }
  825. /// Free Thread resources.
  826. /// \param[in] thread thread object.
  827. static void os_ThreadFree (os_thread_t *thread) {
  828. // Mark object as inactive
  829. thread->state = os_ThreadInactive;
  830. #if (__DOMAIN_NS == 1U)
  831. // Free secure process stack
  832. if (thread->tz_memory != 0U) {
  833. TZ_FreeModuleContext_S(thread->tz_memory);
  834. }
  835. #endif
  836. // Free stack memory
  837. if (thread->flags & os_FlagSystemMemory) {
  838. if (thread->flags & os_ThreadFlagDefStack) {
  839. os_MemoryPoolFree(os_Info.mpi.stack, thread->stack_mem);
  840. } else {
  841. os_MemoryFree(os_Info.mem.stack, thread->stack_mem);
  842. }
  843. }
  844. // Free object memory
  845. if (thread->flags & os_FlagSystemObject) {
  846. if (os_Info.mpi.thread != NULL) {
  847. os_MemoryPoolFree(os_Info.mpi.thread, thread);
  848. } else {
  849. os_MemoryFree(os_Info.mem.common, thread);
  850. }
  851. }
  852. }
  853. /// Detach a thread (thread storage can be reclaimed when thread terminates).
  854. /// \note API identical to osThreadDetach
  855. osStatus_t os_svcThreadDetach (osThreadId_t thread_id) {
  856. os_thread_t *thread = (os_thread_t *)thread_id;
  857. // Check parameters
  858. if ((thread == NULL) ||
  859. (thread->id != os_IdThread)) {
  860. return osErrorParameter;
  861. }
  862. if ((thread->attr & osThreadJoinable) == 0U) {
  863. return osErrorResource;
  864. }
  865. // Check object state
  866. if (thread->state == os_ThreadInactive) {
  867. return osErrorResource;
  868. }
  869. if (thread->state == os_ThreadTerminated) {
  870. os_ThreadListUnlink(&os_Info.thread.terminate_list, thread);
  871. os_ThreadFree(thread);
  872. } else {
  873. thread->attr &= ~osThreadJoinable;
  874. }
  875. return osOK;
  876. }
  877. /// Wait for specified thread to terminate.
  878. /// \note API identical to osThreadJoin
  879. osStatus_t os_svcThreadJoin (osThreadId_t thread_id) {
  880. os_thread_t *thread = (os_thread_t *)thread_id;
  881. // Check parameters
  882. if ((thread == NULL) ||
  883. (thread->id != os_IdThread)) {
  884. return osErrorParameter;
  885. }
  886. if ((thread->attr & osThreadJoinable) == 0U) {
  887. return osErrorResource;
  888. }
  889. // Check object state
  890. if ((thread->state == os_ThreadInactive) ||
  891. (thread->state == os_ThreadRunning)) {
  892. return osErrorResource;
  893. }
  894. if (thread->state == os_ThreadTerminated) {
  895. os_ThreadListUnlink(&os_Info.thread.terminate_list, thread);
  896. os_ThreadFree(thread);
  897. } else {
  898. // Suspend current Thread
  899. if (os_ThreadWaitEnter(os_ThreadWaitingJoin, osWaitForever)) {
  900. thread->thread_join = os_ThreadGetRunning();
  901. }
  902. return osErrorResource;
  903. }
  904. return osOK;
  905. }
  906. /// Terminate execution of current running thread.
  907. /// \note API identical to osThreadExit
  908. void os_svcThreadExit (void) {
  909. os_thread_t *thread;
  910. thread = os_ThreadGetRunning();
  911. if (thread == NULL) {
  912. return;
  913. }
  914. // Release owned Mutexes
  915. os_MutexOwnerRelease(thread->mutex_list);
  916. // Wakeup Thread waiting to Join
  917. if (thread->thread_join != NULL) {
  918. os_ThreadWaitExit(thread->thread_join, (uint32_t)osOK, false);
  919. }
  920. // Switch to next Ready Thread
  921. if ((os_KernelGetState() != os_KernelRunning) ||
  922. (os_Info.thread.ready.thread_list == NULL)) {
  923. return;
  924. }
  925. thread->sp = __get_PSP();
  926. os_ThreadSwitch(os_ThreadListGet(&os_Info.thread.ready));
  927. os_ThreadSetRunning(NULL);
  928. if (((thread->attr & osThreadJoinable) == 0U) || (thread->thread_join != NULL)) {
  929. os_ThreadFree(thread);
  930. } else {
  931. // Update Thread State and put it into Terminate Thread list
  932. thread->state = os_ThreadTerminated;
  933. thread->thread_prev = NULL;
  934. thread->thread_next = os_Info.thread.terminate_list;
  935. os_Info.thread.terminate_list = thread;
  936. }
  937. }
  938. /// Terminate execution of a thread.
  939. /// \note API identical to osThreadTerminate
  940. osStatus_t os_svcThreadTerminate (osThreadId_t thread_id) {
  941. os_thread_t *thread = (os_thread_t *)thread_id;
  942. // Check parameters
  943. if ((thread == NULL) ||
  944. (thread->id != os_IdThread)) {
  945. return osErrorParameter;
  946. }
  947. // Check object state
  948. switch (thread->state & os_ThreadStateMask) {
  949. case os_ThreadRunning:
  950. break;
  951. case os_ThreadReady:
  952. os_ThreadListRemove(thread);
  953. break;
  954. case os_ThreadBlocked:
  955. os_ThreadListRemove(thread);
  956. os_ThreadDelayRemove(thread);
  957. break;
  958. case os_ThreadInactive:
  959. case os_ThreadTerminated:
  960. default:
  961. return osErrorResource;
  962. }
  963. // Release owned Mutexes
  964. os_MutexOwnerRelease(thread->mutex_list);
  965. // Wakeup Thread waiting to Join
  966. if (thread->thread_join != NULL) {
  967. os_ThreadWaitExit(thread->thread_join, (uint32_t)osOK, false);
  968. }
  969. // Switch to next Ready Thread when terminating running Thread
  970. if (thread->state == os_ThreadRunning) {
  971. if ((os_KernelGetState() != os_KernelRunning) ||
  972. (os_Info.thread.ready.thread_list == NULL)) {
  973. return osErrorResource;
  974. }
  975. thread->sp = __get_PSP();
  976. os_ThreadSwitch(os_ThreadListGet(&os_Info.thread.ready));
  977. os_ThreadSetRunning(NULL);
  978. } else {
  979. os_ThreadDispatch(NULL);
  980. }
  981. if (((thread->attr & osThreadJoinable) == 0U) || (thread->thread_join != NULL)) {
  982. os_ThreadFree(thread);
  983. } else {
  984. // Update Thread State and put it into Terminate Thread list
  985. thread->state = os_ThreadTerminated;
  986. thread->thread_prev = NULL;
  987. thread->thread_next = os_Info.thread.terminate_list;
  988. os_Info.thread.terminate_list = thread;
  989. }
  990. return osOK;
  991. }
  992. /// Get number of active threads.
  993. /// \note API identical to osThreadGetCount
  994. uint32_t os_svcThreadGetCount (void) {
  995. os_thread_t *thread;
  996. uint32_t count;
  997. // Running Thread
  998. count = 1U;
  999. // Ready List
  1000. for (thread = os_Info.thread.ready.thread_list;
  1001. (thread != NULL); thread = thread->thread_next, count++);
  1002. // Delay List
  1003. for (thread = os_Info.thread.delay_list;
  1004. (thread != NULL); thread = thread->delay_next, count++);
  1005. // Wait List
  1006. for (thread = os_Info.thread.wait_list;
  1007. (thread != NULL); thread = thread->delay_next, count++);
  1008. return count;
  1009. }
  1010. /// Enumerate active threads.
  1011. /// \note API identical to osThreadEnumerate
  1012. uint32_t os_svcThreadEnumerate (osThreadId_t *thread_array, uint32_t array_items) {
  1013. os_thread_t *thread;
  1014. uint32_t count;
  1015. // Check parameters
  1016. if ((thread_array == NULL) || (array_items == 0U)) {
  1017. return 0U;
  1018. }
  1019. // Running Thread
  1020. *thread_array++ = os_ThreadGetRunning();
  1021. count = 1U;
  1022. // Ready List
  1023. for (thread = os_Info.thread.ready.thread_list;
  1024. (thread != NULL) && (count < array_items); thread = thread->thread_next, count++) {
  1025. *thread_array++ = thread;
  1026. }
  1027. // Delay List
  1028. for (thread = os_Info.thread.delay_list;
  1029. (thread != NULL) && (count < array_items); thread = thread->delay_next, count++) {
  1030. *thread_array++ = thread;
  1031. }
  1032. // Wait List
  1033. for (thread = os_Info.thread.wait_list;
  1034. (thread != NULL) && (count < array_items); thread = thread->delay_next, count++) {
  1035. *thread_array++ = thread;
  1036. }
  1037. return count;
  1038. }
  1039. /// Set the specified Thread Flags of a thread.
  1040. /// \note API identical to osThreadFlagsSet
  1041. int32_t os_svcThreadFlagsSet (osThreadId_t thread_id, int32_t flags) {
  1042. os_thread_t *thread = (os_thread_t *)thread_id;
  1043. int32_t thread_flags;
  1044. int32_t thread_flags0;
  1045. // Check parameters
  1046. if ((thread == NULL) ||
  1047. (thread->id != os_IdThread)) {
  1048. return osErrorParameter;
  1049. }
  1050. if ((uint32_t)flags & ~((1U << os_ThreadFlagsLimit) - 1U)) {
  1051. return osErrorParameter;
  1052. }
  1053. // Check object state
  1054. if ((thread->state == os_ThreadInactive) ||
  1055. (thread->state == os_ThreadTerminated)) {
  1056. return osErrorResource;
  1057. }
  1058. // Set Thread Flags
  1059. thread_flags = os_ThreadFlagsSet(thread, flags);
  1060. // Check if Thread is waiting for Thread Flags
  1061. if (thread->state == os_ThreadWaitingThreadFlags) {
  1062. thread_flags0 = os_ThreadFlagsCheck(thread, thread->wait_flags, thread->flags_options);
  1063. if (thread_flags0 > 0) {
  1064. if ((thread->flags_options & osFlagsNoClear) == 0U) {
  1065. thread_flags = thread_flags0 & ~thread->wait_flags;
  1066. } else {
  1067. thread_flags = thread_flags0;
  1068. }
  1069. os_ThreadWaitExit(thread, (uint32_t)thread_flags0, true);
  1070. }
  1071. }
  1072. return thread_flags;
  1073. }
  1074. /// Clear the specified Thread Flags of current running thread.
  1075. /// \note API identical to osThreadFlagsClear
  1076. int32_t os_svcThreadFlagsClear (int32_t flags) {
  1077. os_thread_t *thread;
  1078. thread = os_ThreadGetRunning();
  1079. if (thread == NULL) {
  1080. return osError;
  1081. }
  1082. // Check parameters
  1083. if ((uint32_t)flags & ~((1U << os_ThreadFlagsLimit) - 1U)) {
  1084. return osErrorParameter;
  1085. }
  1086. // Check object state
  1087. if ((thread->state == os_ThreadInactive) ||
  1088. (thread->state == os_ThreadTerminated)) {
  1089. return osErrorResource;
  1090. }
  1091. // Clear Thread Flags
  1092. return os_ThreadFlagsClear(thread, flags);
  1093. }
  1094. /// Get the current Thread Flags of current running thread.
  1095. /// \note API identical to osThreadFlagsGet
  1096. int32_t os_svcThreadFlagsGet (void) {
  1097. os_thread_t *thread;
  1098. thread = os_ThreadGetRunning();
  1099. if (thread == NULL) {
  1100. return 0;
  1101. }
  1102. // Check object state
  1103. if ((thread->state == os_ThreadInactive) ||
  1104. (thread->state == os_ThreadTerminated)) {
  1105. return 0;
  1106. }
  1107. return thread->thread_flags;
  1108. }
  1109. /// Wait for one or more Thread Flags of the current running thread to become signaled.
  1110. /// \note API identical to osThreadFlagsWait
  1111. int32_t os_svcThreadFlagsWait (int32_t flags, uint32_t options, uint32_t timeout) {
  1112. os_thread_t *thread;
  1113. int32_t thread_flags;
  1114. thread = os_ThreadGetRunning();
  1115. if (thread == NULL) {
  1116. return osError;
  1117. }
  1118. // Check parameters
  1119. if ((uint32_t)flags & ~((1U << os_ThreadFlagsLimit) - 1U)) {
  1120. return osErrorParameter;
  1121. }
  1122. // Check Thread Flags
  1123. thread_flags = os_ThreadFlagsCheck(thread, flags, options);
  1124. if (thread_flags > 0) {
  1125. return thread_flags;
  1126. }
  1127. // Check if timeout is specified
  1128. if (timeout != 0U) {
  1129. // Store waiting flags and options
  1130. thread->wait_flags = flags;
  1131. thread->flags_options = (uint8_t)options;
  1132. // Suspend current Thread
  1133. os_ThreadWaitEnter(os_ThreadWaitingThreadFlags, timeout);
  1134. return osErrorTimeout;
  1135. }
  1136. return osErrorResource;
  1137. }
  1138. // ==== ISR Calls ====
  1139. /// Set the specified Thread Flags of a thread.
  1140. /// \note API identical to osThreadFlagsSet
  1141. __STATIC_INLINE
  1142. int32_t os_isrThreadFlagsSet (osThreadId_t thread_id, int32_t flags) {
  1143. os_thread_t *thread = (os_thread_t *)thread_id;
  1144. int32_t thread_flags;
  1145. // Check parameters
  1146. if ((thread == NULL) ||
  1147. (thread->id != os_IdThread)) {
  1148. return osErrorParameter;
  1149. }
  1150. if ((uint32_t)flags & ~((1U << os_ThreadFlagsLimit) - 1U)) {
  1151. return osErrorParameter;
  1152. }
  1153. // Check object state
  1154. if ((thread->state == os_ThreadInactive) ||
  1155. (thread->state == os_ThreadTerminated)) {
  1156. return osErrorResource;
  1157. }
  1158. // Set Thread Flags
  1159. thread_flags = os_ThreadFlagsSet(thread, flags);
  1160. // Register post ISR processing
  1161. os_PostProcess((os_object_t *)thread);
  1162. return thread_flags;
  1163. }
  1164. // ==== Public API ====
  1165. /// Create a thread and add it to Active Threads.
  1166. osThreadId_t osThreadNew (os_thread_func_t func, void *argument, const osThreadAttr_t *attr) {
  1167. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  1168. return NULL;
  1169. }
  1170. if ((os_KernelGetState() == os_KernelReady) && IS_PRIVILEGED()) {
  1171. // Kernel Ready (not running) and in Privileged mode
  1172. return os_svcThreadNew(func, argument, attr);
  1173. } else {
  1174. return __svcThreadNew(func, argument, attr);
  1175. }
  1176. }
  1177. /// Get name of a thread.
  1178. const char *osThreadGetName (osThreadId_t thread_id) {
  1179. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  1180. return NULL;
  1181. }
  1182. return __svcThreadGetName(thread_id);
  1183. }
  1184. /// Return the thread ID of the current running thread.
  1185. osThreadId_t osThreadGetId (void) {
  1186. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  1187. return NULL;
  1188. }
  1189. return __svcThreadGetId();
  1190. }
  1191. /// Get current thread state of a thread.
  1192. osThreadState_t osThreadGetState (osThreadId_t thread_id) {
  1193. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  1194. return osThreadError;
  1195. }
  1196. return __svcThreadGetState(thread_id);
  1197. }
  1198. /// Get stack size of a thread.
  1199. uint32_t osThreadGetStackSize (osThreadId_t thread_id) {
  1200. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  1201. return 0U;
  1202. }
  1203. return __svcThreadGetStackSize(thread_id);
  1204. }
  1205. /// Get available stack space of a thread based on stack watermark recording during execution.
  1206. uint32_t osThreadGetStackSpace (osThreadId_t thread_id) {
  1207. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  1208. return 0U;
  1209. }
  1210. return __svcThreadGetStackSpace(thread_id);
  1211. }
  1212. /// Change priority of a thread.
  1213. osStatus_t osThreadSetPriority (osThreadId_t thread_id, osPriority_t priority) {
  1214. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  1215. return osErrorISR;
  1216. }
  1217. return __svcThreadSetPriority(thread_id, priority);
  1218. }
  1219. /// Get current priority of a thread.
  1220. osPriority_t osThreadGetPriority (osThreadId_t thread_id) {
  1221. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  1222. return osPriorityError;
  1223. }
  1224. return __svcThreadGetPriority(thread_id);
  1225. }
  1226. /// Pass control to next thread that is in state READY.
  1227. osStatus_t osThreadYield (void) {
  1228. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  1229. return osErrorISR;
  1230. }
  1231. return __svcThreadYield();
  1232. }
  1233. /// Suspend execution of a thread.
  1234. osStatus_t osThreadSuspend (osThreadId_t thread_id) {
  1235. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  1236. return osErrorISR;
  1237. }
  1238. return __svcThreadSuspend(thread_id);
  1239. }
  1240. /// Resume execution of a thread.
  1241. osStatus_t osThreadResume (osThreadId_t thread_id) {
  1242. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  1243. return osErrorISR;
  1244. }
  1245. return __svcThreadResume(thread_id);
  1246. }
  1247. /// Detach a thread (thread storage can be reclaimed when thread terminates).
  1248. osStatus_t osThreadDetach (osThreadId_t thread_id) {
  1249. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  1250. return osErrorISR;
  1251. }
  1252. return __svcThreadDetach(thread_id);
  1253. }
  1254. /// Wait for specified thread to terminate.
  1255. osStatus_t osThreadJoin (osThreadId_t thread_id) {
  1256. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  1257. return osErrorISR;
  1258. }
  1259. return __svcThreadJoin(thread_id);
  1260. }
  1261. /// Terminate execution of current running thread.
  1262. __NO_RETURN void osThreadExit (void) {
  1263. __svcThreadExit();
  1264. for (;;);
  1265. }
  1266. /// Terminate execution of a thread.
  1267. osStatus_t osThreadTerminate (osThreadId_t thread_id) {
  1268. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  1269. return osErrorISR;
  1270. }
  1271. return __svcThreadTerminate(thread_id);
  1272. }
  1273. /// Get number of active threads.
  1274. uint32_t osThreadGetCount (void) {
  1275. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  1276. return 0U; // Not allowed in ISR
  1277. }
  1278. return __svcThreadGetCount();
  1279. }
  1280. /// Enumerate active threads.
  1281. uint32_t osThreadEnumerate (osThreadId_t *thread_array, uint32_t array_items) {
  1282. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  1283. return 0U; // Not allowed in ISR
  1284. }
  1285. return __svcThreadEnumerate(thread_array, array_items);
  1286. }
  1287. /// Set the specified Thread Flags of a thread.
  1288. int32_t osThreadFlagsSet (osThreadId_t thread_id, int32_t flags) {
  1289. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  1290. return os_isrThreadFlagsSet(thread_id, flags);
  1291. } else {
  1292. return __svcThreadFlagsSet(thread_id, flags);
  1293. }
  1294. }
  1295. /// Clear the specified Thread Flags of current running thread.
  1296. int32_t osThreadFlagsClear (int32_t flags) {
  1297. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  1298. return osErrorISR;
  1299. }
  1300. return __svcThreadFlagsClear(flags);
  1301. }
  1302. /// Get the current Thread Flags of current running thread.
  1303. int32_t osThreadFlagsGet (void) {
  1304. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  1305. return 0;
  1306. }
  1307. return __svcThreadFlagsGet();
  1308. }
  1309. /// Wait for one or more Thread Flags of the current running thread to become signaled.
  1310. int32_t osThreadFlagsWait (int32_t flags, uint32_t options, uint32_t timeout) {
  1311. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  1312. return osErrorISR;
  1313. }
  1314. return __svcThreadFlagsWait(flags, options, timeout);
  1315. }