rtx_thread.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933
  1. /*
  2. * Copyright (c) 2013-2021 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. * 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. // OS Runtime Object Memory Usage
  27. #ifdef RTX_OBJ_MEM_USAGE
  28. osRtxObjectMemUsage_t osRtxThreadMemUsage \
  29. __attribute__((section(".data.os.thread.obj"))) =
  30. { 0U, 0U, 0U };
  31. #endif
  32. // ==== Helper functions ====
  33. /// Set Thread Flags.
  34. /// \param[in] thread thread object.
  35. /// \param[in] flags specifies the flags to set.
  36. /// \return thread flags after setting.
  37. static uint32_t ThreadFlagsSet (os_thread_t *thread, uint32_t flags) {
  38. #if (EXCLUSIVE_ACCESS == 0)
  39. uint32_t primask = __get_PRIMASK();
  40. #endif
  41. uint32_t thread_flags;
  42. #if (EXCLUSIVE_ACCESS == 0)
  43. __disable_irq();
  44. thread->thread_flags |= flags;
  45. thread_flags = thread->thread_flags;
  46. if (primask == 0U) {
  47. __enable_irq();
  48. }
  49. #else
  50. thread_flags = atomic_set32(&thread->thread_flags, flags);
  51. #endif
  52. return thread_flags;
  53. }
  54. /// Clear Thread Flags.
  55. /// \param[in] thread thread object.
  56. /// \param[in] flags specifies the flags to clear.
  57. /// \return thread flags before clearing.
  58. static uint32_t ThreadFlagsClear (os_thread_t *thread, uint32_t flags) {
  59. #if (EXCLUSIVE_ACCESS == 0)
  60. uint32_t primask = __get_PRIMASK();
  61. #endif
  62. uint32_t thread_flags;
  63. #if (EXCLUSIVE_ACCESS == 0)
  64. __disable_irq();
  65. thread_flags = thread->thread_flags;
  66. thread->thread_flags &= ~flags;
  67. if (primask == 0U) {
  68. __enable_irq();
  69. }
  70. #else
  71. thread_flags = atomic_clr32(&thread->thread_flags, flags);
  72. #endif
  73. return thread_flags;
  74. }
  75. /// Check Thread Flags.
  76. /// \param[in] thread thread object.
  77. /// \param[in] flags specifies the flags to check.
  78. /// \param[in] options specifies flags options (osFlagsXxxx).
  79. /// \return thread flags before clearing or 0 if specified flags have not been set.
  80. static uint32_t ThreadFlagsCheck (os_thread_t *thread, uint32_t flags, uint32_t options) {
  81. #if (EXCLUSIVE_ACCESS == 0)
  82. uint32_t primask;
  83. #endif
  84. uint32_t thread_flags;
  85. if ((options & osFlagsNoClear) == 0U) {
  86. #if (EXCLUSIVE_ACCESS == 0)
  87. primask = __get_PRIMASK();
  88. __disable_irq();
  89. thread_flags = thread->thread_flags;
  90. if ((((options & osFlagsWaitAll) != 0U) && ((thread_flags & flags) != flags)) ||
  91. (((options & osFlagsWaitAll) == 0U) && ((thread_flags & flags) == 0U))) {
  92. thread_flags = 0U;
  93. } else {
  94. thread->thread_flags &= ~flags;
  95. }
  96. if (primask == 0U) {
  97. __enable_irq();
  98. }
  99. #else
  100. if ((options & osFlagsWaitAll) != 0U) {
  101. thread_flags = atomic_chk32_all(&thread->thread_flags, flags);
  102. } else {
  103. thread_flags = atomic_chk32_any(&thread->thread_flags, flags);
  104. }
  105. #endif
  106. } else {
  107. thread_flags = thread->thread_flags;
  108. if ((((options & osFlagsWaitAll) != 0U) && ((thread_flags & flags) != flags)) ||
  109. (((options & osFlagsWaitAll) == 0U) && ((thread_flags & flags) == 0U))) {
  110. thread_flags = 0U;
  111. }
  112. }
  113. return thread_flags;
  114. }
  115. // ==== Library functions ====
  116. /// Put a Thread into specified Object list sorted by Priority (Highest at Head).
  117. /// \param[in] object generic object.
  118. /// \param[in] thread thread object.
  119. void osRtxThreadListPut (os_object_t *object, os_thread_t *thread) {
  120. os_thread_t *prev, *next;
  121. int32_t priority;
  122. priority = thread->priority;
  123. prev = osRtxThreadObject(object);
  124. next = prev->thread_next;
  125. while ((next != NULL) && (next->priority >= priority)) {
  126. prev = next;
  127. next = next->thread_next;
  128. }
  129. thread->thread_prev = prev;
  130. thread->thread_next = next;
  131. prev->thread_next = thread;
  132. if (next != NULL) {
  133. next->thread_prev = thread;
  134. }
  135. }
  136. /// Get a Thread with Highest Priority from specified Object list and remove it.
  137. /// \param[in] object generic object.
  138. /// \return thread object.
  139. os_thread_t *osRtxThreadListGet (os_object_t *object) {
  140. os_thread_t *thread;
  141. thread = object->thread_list;
  142. object->thread_list = thread->thread_next;
  143. if (thread->thread_next != NULL) {
  144. thread->thread_next->thread_prev = osRtxThreadObject(object);
  145. }
  146. thread->thread_prev = NULL;
  147. return thread;
  148. }
  149. /// Retrieve Thread list root object.
  150. /// \param[in] thread thread object.
  151. /// \return root object.
  152. static void *osRtxThreadListRoot (os_thread_t *thread) {
  153. os_thread_t *thread0;
  154. thread0 = thread;
  155. while (thread0->id == osRtxIdThread) {
  156. thread0 = thread0->thread_prev;
  157. }
  158. return thread0;
  159. }
  160. /// Re-sort a Thread in linked Object list by Priority (Highest at Head).
  161. /// \param[in] thread thread object.
  162. void osRtxThreadListSort (os_thread_t *thread) {
  163. os_object_t *object;
  164. os_thread_t *thread0;
  165. // Search for object
  166. thread0 = thread;
  167. while ((thread0 != NULL) && (thread0->id == osRtxIdThread)) {
  168. thread0 = thread0->thread_prev;
  169. }
  170. object = osRtxObject(thread0);
  171. if (object != NULL) {
  172. osRtxThreadListRemove(thread);
  173. osRtxThreadListPut(object, thread);
  174. }
  175. }
  176. /// Remove a Thread from linked Object list.
  177. /// \param[in] thread thread object.
  178. void osRtxThreadListRemove (os_thread_t *thread) {
  179. if (thread->thread_prev != NULL) {
  180. thread->thread_prev->thread_next = thread->thread_next;
  181. if (thread->thread_next != NULL) {
  182. thread->thread_next->thread_prev = thread->thread_prev;
  183. }
  184. thread->thread_prev = NULL;
  185. }
  186. }
  187. /// Unlink a Thread from specified linked list.
  188. /// \param[in] thread thread object.
  189. static void osRtxThreadListUnlink (os_thread_t **thread_list, os_thread_t *thread) {
  190. if (thread->thread_next != NULL) {
  191. thread->thread_next->thread_prev = thread->thread_prev;
  192. }
  193. if (thread->thread_prev != NULL) {
  194. thread->thread_prev->thread_next = thread->thread_next;
  195. thread->thread_prev = NULL;
  196. } else {
  197. *thread_list = thread->thread_next;
  198. }
  199. }
  200. /// Mark a Thread as Ready and put it into Ready list (sorted by Priority).
  201. /// \param[in] thread thread object.
  202. void osRtxThreadReadyPut (os_thread_t *thread) {
  203. thread->state = osRtxThreadReady;
  204. osRtxThreadListPut(&osRtxInfo.thread.ready, thread);
  205. }
  206. /// Insert a Thread into the Delay list sorted by Delay (Lowest at Head).
  207. /// \param[in] thread thread object.
  208. /// \param[in] delay delay value.
  209. static void osRtxThreadDelayInsert (os_thread_t *thread, uint32_t delay) {
  210. os_thread_t *prev, *next;
  211. if (delay == osWaitForever) {
  212. prev = NULL;
  213. next = osRtxInfo.thread.wait_list;
  214. while (next != NULL) {
  215. prev = next;
  216. next = next->delay_next;
  217. }
  218. thread->delay = delay;
  219. thread->delay_prev = prev;
  220. thread->delay_next = NULL;
  221. if (prev != NULL) {
  222. prev->delay_next = thread;
  223. } else {
  224. osRtxInfo.thread.wait_list = thread;
  225. }
  226. } else {
  227. prev = NULL;
  228. next = osRtxInfo.thread.delay_list;
  229. while ((next != NULL) && (next->delay <= delay)) {
  230. delay -= next->delay;
  231. prev = next;
  232. next = next->delay_next;
  233. }
  234. thread->delay = delay;
  235. thread->delay_prev = prev;
  236. thread->delay_next = next;
  237. if (prev != NULL) {
  238. prev->delay_next = thread;
  239. } else {
  240. osRtxInfo.thread.delay_list = thread;
  241. }
  242. if (next != NULL) {
  243. next->delay -= delay;
  244. next->delay_prev = thread;
  245. }
  246. }
  247. }
  248. /// Remove a Thread from the Delay list.
  249. /// \param[in] thread thread object.
  250. static void osRtxThreadDelayRemove (os_thread_t *thread) {
  251. if (thread->delay == osWaitForever) {
  252. if (thread->delay_next != NULL) {
  253. thread->delay_next->delay_prev = thread->delay_prev;
  254. }
  255. if (thread->delay_prev != NULL) {
  256. thread->delay_prev->delay_next = thread->delay_next;
  257. thread->delay_prev = NULL;
  258. } else {
  259. osRtxInfo.thread.wait_list = thread->delay_next;
  260. }
  261. } else {
  262. if (thread->delay_next != NULL) {
  263. thread->delay_next->delay += thread->delay;
  264. thread->delay_next->delay_prev = thread->delay_prev;
  265. }
  266. if (thread->delay_prev != NULL) {
  267. thread->delay_prev->delay_next = thread->delay_next;
  268. thread->delay_prev = NULL;
  269. } else {
  270. osRtxInfo.thread.delay_list = thread->delay_next;
  271. }
  272. }
  273. thread->delay = 0U;
  274. }
  275. /// Process Thread Delay Tick (executed each System Tick).
  276. void osRtxThreadDelayTick (void) {
  277. os_thread_t *thread;
  278. os_object_t *object;
  279. thread = osRtxInfo.thread.delay_list;
  280. if (thread == NULL) {
  281. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  282. return;
  283. }
  284. thread->delay--;
  285. if (thread->delay == 0U) {
  286. do {
  287. switch (thread->state) {
  288. case osRtxThreadWaitingDelay:
  289. EvrRtxDelayCompleted(thread);
  290. break;
  291. case osRtxThreadWaitingThreadFlags:
  292. EvrRtxThreadFlagsWaitTimeout(thread);
  293. break;
  294. case osRtxThreadWaitingEventFlags:
  295. object = osRtxObject(osRtxThreadListRoot(thread));
  296. EvrRtxEventFlagsWaitTimeout(osRtxEventFlagsObject(object));
  297. break;
  298. case osRtxThreadWaitingMutex:
  299. object = osRtxObject(osRtxThreadListRoot(thread));
  300. osRtxMutexOwnerRestore(osRtxMutexObject(object), thread);
  301. EvrRtxMutexAcquireTimeout(osRtxMutexObject(object));
  302. break;
  303. case osRtxThreadWaitingSemaphore:
  304. object = osRtxObject(osRtxThreadListRoot(thread));
  305. EvrRtxSemaphoreAcquireTimeout(osRtxSemaphoreObject(object));
  306. break;
  307. case osRtxThreadWaitingMemoryPool:
  308. object = osRtxObject(osRtxThreadListRoot(thread));
  309. EvrRtxMemoryPoolAllocTimeout(osRtxMemoryPoolObject(object));
  310. break;
  311. case osRtxThreadWaitingMessageGet:
  312. object = osRtxObject(osRtxThreadListRoot(thread));
  313. EvrRtxMessageQueueGetTimeout(osRtxMessageQueueObject(object));
  314. break;
  315. case osRtxThreadWaitingMessagePut:
  316. object = osRtxObject(osRtxThreadListRoot(thread));
  317. EvrRtxMessageQueuePutTimeout(osRtxMessageQueueObject(object));
  318. break;
  319. default:
  320. // Invalid
  321. break;
  322. }
  323. EvrRtxThreadUnblocked(thread, (osRtxThreadRegPtr(thread))[0]);
  324. osRtxThreadListRemove(thread);
  325. osRtxThreadReadyPut(thread);
  326. thread = thread->delay_next;
  327. } while ((thread != NULL) && (thread->delay == 0U));
  328. if (thread != NULL) {
  329. thread->delay_prev = NULL;
  330. }
  331. osRtxInfo.thread.delay_list = thread;
  332. }
  333. }
  334. /// Get pointer to Thread registers (R0..R3)
  335. /// \param[in] thread thread object.
  336. /// \return pointer to registers R0-R3.
  337. uint32_t *osRtxThreadRegPtr (const os_thread_t *thread) {
  338. uint32_t addr = thread->sp + StackOffsetR0(thread->stack_frame);
  339. //lint -e{923} -e{9078} "cast from unsigned int to pointer"
  340. return ((uint32_t *)addr);
  341. }
  342. /// Block running Thread execution and register it as Ready to Run.
  343. /// \param[in] thread running thread object.
  344. static void osRtxThreadBlock (os_thread_t *thread) {
  345. os_thread_t *prev, *next;
  346. int32_t priority;
  347. thread->state = osRtxThreadReady;
  348. priority = thread->priority;
  349. prev = osRtxThreadObject(&osRtxInfo.thread.ready);
  350. next = prev->thread_next;
  351. while ((next != NULL) && (next->priority > priority)) {
  352. prev = next;
  353. next = next->thread_next;
  354. }
  355. thread->thread_prev = prev;
  356. thread->thread_next = next;
  357. prev->thread_next = thread;
  358. if (next != NULL) {
  359. next->thread_prev = thread;
  360. }
  361. EvrRtxThreadPreempted(thread);
  362. }
  363. /// Switch to specified Thread.
  364. /// \param[in] thread thread object.
  365. void osRtxThreadSwitch (os_thread_t *thread) {
  366. thread->state = osRtxThreadRunning;
  367. osRtxInfo.thread.run.next = thread;
  368. EvrRtxThreadSwitched(thread);
  369. }
  370. /// Dispatch specified Thread or Ready Thread with Highest Priority.
  371. /// \param[in] thread thread object or NULL.
  372. void osRtxThreadDispatch (os_thread_t *thread) {
  373. uint8_t kernel_state;
  374. os_thread_t *thread_running;
  375. os_thread_t *thread_ready;
  376. kernel_state = osRtxKernelGetState();
  377. thread_running = osRtxThreadGetRunning();
  378. if (thread == NULL) {
  379. thread_ready = osRtxInfo.thread.ready.thread_list;
  380. if ((kernel_state == osRtxKernelRunning) &&
  381. (thread_ready != NULL) &&
  382. (thread_ready->priority > thread_running->priority)) {
  383. // Preempt running Thread
  384. osRtxThreadListRemove(thread_ready);
  385. osRtxThreadBlock(thread_running);
  386. osRtxThreadSwitch(thread_ready);
  387. }
  388. } else {
  389. if ((kernel_state == osRtxKernelRunning) &&
  390. (thread->priority > thread_running->priority)) {
  391. // Preempt running Thread
  392. osRtxThreadBlock(thread_running);
  393. osRtxThreadSwitch(thread);
  394. } else {
  395. // Put Thread into Ready list
  396. osRtxThreadReadyPut(thread);
  397. }
  398. }
  399. }
  400. /// Exit Thread wait state.
  401. /// \param[in] thread thread object.
  402. /// \param[in] ret_val return value.
  403. /// \param[in] dispatch dispatch flag.
  404. void osRtxThreadWaitExit (os_thread_t *thread, uint32_t ret_val, bool_t dispatch) {
  405. uint32_t *reg;
  406. EvrRtxThreadUnblocked(thread, ret_val);
  407. reg = osRtxThreadRegPtr(thread);
  408. reg[0] = ret_val;
  409. osRtxThreadDelayRemove(thread);
  410. if (dispatch) {
  411. osRtxThreadDispatch(thread);
  412. } else {
  413. osRtxThreadReadyPut(thread);
  414. }
  415. }
  416. /// Enter Thread wait state.
  417. /// \param[in] state new thread state.
  418. /// \param[in] timeout timeout.
  419. /// \return true - success, false - failure.
  420. bool_t osRtxThreadWaitEnter (uint8_t state, uint32_t timeout) {
  421. os_thread_t *thread;
  422. // Check if Kernel is running
  423. if (osRtxKernelGetState() != osRtxKernelRunning) {
  424. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  425. return FALSE;
  426. }
  427. // Check if any thread is ready
  428. if (osRtxInfo.thread.ready.thread_list == NULL) {
  429. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  430. return FALSE;
  431. }
  432. // Get running thread
  433. thread = osRtxThreadGetRunning();
  434. EvrRtxThreadBlocked(thread, timeout);
  435. thread->state = state;
  436. osRtxThreadDelayInsert(thread, timeout);
  437. thread = osRtxThreadListGet(&osRtxInfo.thread.ready);
  438. osRtxThreadSwitch(thread);
  439. return TRUE;
  440. }
  441. #ifdef RTX_STACK_CHECK
  442. /// Check current running Thread Stack.
  443. /// \param[in] thread running thread.
  444. /// \return true - success, false - failure.
  445. //lint -esym(714,osRtxThreadStackCheck) "Referenced by Exception handlers"
  446. //lint -esym(759,osRtxThreadStackCheck) "Prototype in header"
  447. //lint -esym(765,osRtxThreadStackCheck) "Global scope"
  448. bool_t osRtxThreadStackCheck (const os_thread_t *thread) {
  449. //lint -e{923} "cast from pointer to unsigned int"
  450. //lint -e{9079} -e{9087} "cast between pointers to different object types"
  451. if ((thread->sp <= (uint32_t)thread->stack_mem) ||
  452. (*((uint32_t *)thread->stack_mem) != osRtxStackMagicWord)) {
  453. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  454. return FALSE;
  455. }
  456. return TRUE;
  457. }
  458. #endif
  459. #ifdef RTX_TF_M_EXTENSION
  460. /// Get TrustZone Module Identifier of running Thread.
  461. /// \return TrustZone Module Identifier.
  462. uint32_t osRtxTzGetModuleId (void) {
  463. os_thread_t *thread;
  464. uint32_t tz_module;
  465. thread = osRtxThreadGetRunning();
  466. if (thread != NULL) {
  467. tz_module = thread->tz_module;
  468. } else {
  469. tz_module = 0U;
  470. }
  471. return tz_module;
  472. }
  473. #endif
  474. // ==== Post ISR processing ====
  475. /// Thread post ISR processing.
  476. /// \param[in] thread thread object.
  477. static void osRtxThreadPostProcess (os_thread_t *thread) {
  478. uint32_t thread_flags;
  479. // Check if Thread is waiting for Thread Flags
  480. if (thread->state == osRtxThreadWaitingThreadFlags) {
  481. thread_flags = ThreadFlagsCheck(thread, thread->wait_flags, thread->flags_options);
  482. if (thread_flags != 0U) {
  483. osRtxThreadWaitExit(thread, thread_flags, FALSE);
  484. EvrRtxThreadFlagsWaitCompleted(thread->wait_flags, thread->flags_options, thread_flags, thread);
  485. }
  486. }
  487. }
  488. // ==== Service Calls ====
  489. /// Create a thread and add it to Active Threads.
  490. /// \note API identical to osThreadNew
  491. static osThreadId_t svcRtxThreadNew (osThreadFunc_t func, void *argument, const osThreadAttr_t *attr) {
  492. os_thread_t *thread;
  493. uint32_t attr_bits;
  494. void *stack_mem;
  495. uint32_t stack_size;
  496. osPriority_t priority;
  497. uint8_t flags;
  498. const char *name;
  499. uint32_t *ptr;
  500. uint32_t n;
  501. #if (DOMAIN_NS == 1)
  502. TZ_ModuleId_t tz_module;
  503. TZ_MemoryId_t tz_memory;
  504. #endif
  505. // Check parameters
  506. if (func == NULL) {
  507. EvrRtxThreadError(NULL, (int32_t)osErrorParameter);
  508. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  509. return NULL;
  510. }
  511. // Process attributes
  512. if (attr != NULL) {
  513. name = attr->name;
  514. attr_bits = attr->attr_bits;
  515. //lint -e{9079} "conversion from pointer to void to pointer to other type" [MISRA Note 6]
  516. thread = attr->cb_mem;
  517. //lint -e{9079} "conversion from pointer to void to pointer to other type" [MISRA Note 6]
  518. stack_mem = attr->stack_mem;
  519. stack_size = attr->stack_size;
  520. priority = attr->priority;
  521. #if (DOMAIN_NS == 1)
  522. tz_module = attr->tz_module;
  523. #endif
  524. if (thread != NULL) {
  525. //lint -e(923) -e(9078) "cast from pointer to unsigned int" [MISRA Note 7]
  526. if ((((uint32_t)thread & 3U) != 0U) || (attr->cb_size < sizeof(os_thread_t))) {
  527. EvrRtxThreadError(NULL, osRtxErrorInvalidControlBlock);
  528. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  529. return NULL;
  530. }
  531. } else {
  532. if (attr->cb_size != 0U) {
  533. EvrRtxThreadError(NULL, osRtxErrorInvalidControlBlock);
  534. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  535. return NULL;
  536. }
  537. }
  538. if (stack_mem != NULL) {
  539. //lint -e{923} "cast from pointer to unsigned int" [MISRA Note 7]
  540. if ((((uint32_t)stack_mem & 7U) != 0U) || (stack_size == 0U)) {
  541. EvrRtxThreadError(NULL, osRtxErrorInvalidThreadStack);
  542. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  543. return NULL;
  544. }
  545. }
  546. if (priority == osPriorityNone) {
  547. priority = osPriorityNormal;
  548. } else {
  549. if ((priority < osPriorityIdle) || (priority > osPriorityISR)) {
  550. EvrRtxThreadError(NULL, osRtxErrorInvalidPriority);
  551. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  552. return NULL;
  553. }
  554. }
  555. } else {
  556. name = NULL;
  557. attr_bits = 0U;
  558. thread = NULL;
  559. stack_mem = NULL;
  560. stack_size = 0U;
  561. priority = osPriorityNormal;
  562. #if (DOMAIN_NS == 1)
  563. tz_module = 0U;
  564. #endif
  565. }
  566. // Check stack size
  567. if (stack_size != 0U) {
  568. if (((stack_size & 7U) != 0U) || (stack_size < (64U + 8U)) || (stack_size > 0x7FFFFFFFU)) {
  569. EvrRtxThreadError(NULL, osRtxErrorInvalidThreadStack);
  570. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  571. return NULL;
  572. }
  573. }
  574. // Allocate object memory if not provided
  575. if (thread == NULL) {
  576. if (osRtxInfo.mpi.thread != NULL) {
  577. //lint -e{9079} "conversion from pointer to void to pointer to other type" [MISRA Note 5]
  578. thread = osRtxMemoryPoolAlloc(osRtxInfo.mpi.thread);
  579. } else {
  580. //lint -e{9079} "conversion from pointer to void to pointer to other type" [MISRA Note 5]
  581. thread = osRtxMemoryAlloc(osRtxInfo.mem.common, sizeof(os_thread_t), 1U);
  582. }
  583. #ifdef RTX_OBJ_MEM_USAGE
  584. if (thread != NULL) {
  585. uint32_t used;
  586. osRtxThreadMemUsage.cnt_alloc++;
  587. used = osRtxThreadMemUsage.cnt_alloc - osRtxThreadMemUsage.cnt_free;
  588. if (osRtxThreadMemUsage.max_used < used) {
  589. osRtxThreadMemUsage.max_used = used;
  590. }
  591. }
  592. #endif
  593. flags = osRtxFlagSystemObject;
  594. } else {
  595. flags = 0U;
  596. }
  597. // Allocate stack memory if not provided
  598. if ((thread != NULL) && (stack_mem == NULL)) {
  599. if (stack_size == 0U) {
  600. stack_size = osRtxConfig.thread_stack_size;
  601. if (osRtxInfo.mpi.stack != NULL) {
  602. //lint -e{9079} "conversion from pointer to void to pointer to other type" [MISRA Note 5]
  603. stack_mem = osRtxMemoryPoolAlloc(osRtxInfo.mpi.stack);
  604. if (stack_mem != NULL) {
  605. flags |= osRtxThreadFlagDefStack;
  606. }
  607. } else {
  608. //lint -e{9079} "conversion from pointer to void to pointer to other type" [MISRA Note 5]
  609. stack_mem = osRtxMemoryAlloc(osRtxInfo.mem.stack, stack_size, 0U);
  610. }
  611. } else {
  612. //lint -e{9079} "conversion from pointer to void to pointer to other type" [MISRA Note 5]
  613. stack_mem = osRtxMemoryAlloc(osRtxInfo.mem.stack, stack_size, 0U);
  614. }
  615. if (stack_mem == NULL) {
  616. if ((flags & osRtxFlagSystemObject) != 0U) {
  617. if (osRtxInfo.mpi.thread != NULL) {
  618. (void)osRtxMemoryPoolFree(osRtxInfo.mpi.thread, thread);
  619. } else {
  620. (void)osRtxMemoryFree(osRtxInfo.mem.common, thread);
  621. }
  622. #ifdef RTX_OBJ_MEM_USAGE
  623. osRtxThreadMemUsage.cnt_free++;
  624. #endif
  625. }
  626. thread = NULL;
  627. }
  628. flags |= osRtxFlagSystemMemory;
  629. }
  630. #if (DOMAIN_NS == 1)
  631. // Allocate secure process stack
  632. if ((thread != NULL) && (tz_module != 0U)) {
  633. tz_memory = TZ_AllocModuleContext_S(tz_module);
  634. if (tz_memory == 0U) {
  635. EvrRtxThreadError(NULL, osRtxErrorTZ_AllocContext_S);
  636. if ((flags & osRtxFlagSystemMemory) != 0U) {
  637. if ((flags & osRtxThreadFlagDefStack) != 0U) {
  638. (void)osRtxMemoryPoolFree(osRtxInfo.mpi.stack, thread->stack_mem);
  639. } else {
  640. (void)osRtxMemoryFree(osRtxInfo.mem.stack, thread->stack_mem);
  641. }
  642. }
  643. if ((flags & osRtxFlagSystemObject) != 0U) {
  644. if (osRtxInfo.mpi.thread != NULL) {
  645. (void)osRtxMemoryPoolFree(osRtxInfo.mpi.thread, thread);
  646. } else {
  647. (void)osRtxMemoryFree(osRtxInfo.mem.common, thread);
  648. }
  649. #ifdef RTX_OBJ_MEM_USAGE
  650. osRtxThreadMemUsage.cnt_free++;
  651. #endif
  652. }
  653. thread = NULL;
  654. }
  655. } else {
  656. tz_memory = 0U;
  657. }
  658. #endif
  659. if (thread != NULL) {
  660. // Initialize control block
  661. //lint --e{923} --e{9078} "cast between pointers and unsigned int"
  662. //lint --e{9079} --e{9087} "cast between pointers to different object types"
  663. //lint --e{9074} "conversion between a pointer to function and another type"
  664. thread->id = osRtxIdThread;
  665. thread->state = osRtxThreadReady;
  666. thread->flags = flags;
  667. thread->attr = (uint8_t)attr_bits;
  668. thread->name = name;
  669. thread->thread_next = NULL;
  670. thread->thread_prev = NULL;
  671. thread->delay_next = NULL;
  672. thread->delay_prev = NULL;
  673. thread->thread_join = NULL;
  674. thread->delay = 0U;
  675. thread->priority = (int8_t)priority;
  676. thread->priority_base = (int8_t)priority;
  677. thread->stack_frame = STACK_FRAME_INIT_VAL;
  678. thread->flags_options = 0U;
  679. thread->wait_flags = 0U;
  680. thread->thread_flags = 0U;
  681. thread->mutex_list = NULL;
  682. thread->stack_mem = stack_mem;
  683. thread->stack_size = stack_size;
  684. thread->sp = (uint32_t)stack_mem + stack_size - 64U;
  685. thread->thread_addr = (uint32_t)func;
  686. #if (DOMAIN_NS == 1)
  687. thread->tz_memory = tz_memory;
  688. #ifdef RTX_TF_M_EXTENSION
  689. thread->tz_module = tz_module;
  690. #endif
  691. #endif
  692. // Initialize stack
  693. //lint --e{613} false detection: "Possible use of null pointer"
  694. ptr = (uint32_t *)stack_mem;
  695. ptr[0] = osRtxStackMagicWord;
  696. if ((osRtxConfig.flags & osRtxConfigStackWatermark) != 0U) {
  697. for (n = (stack_size/4U) - (16U + 1U); n != 0U; n--) {
  698. ptr++;
  699. *ptr = osRtxStackFillPattern;
  700. }
  701. }
  702. ptr = (uint32_t *)thread->sp;
  703. for (n = 0U; n != 13U; n++) {
  704. ptr[n] = 0U; // R4..R11, R0..R3, R12
  705. }
  706. ptr[13] = (uint32_t)osThreadExit; // LR
  707. ptr[14] = (uint32_t)func; // PC
  708. ptr[15] = xPSR_InitVal(
  709. (bool_t)((osRtxConfig.flags & osRtxConfigPrivilegedMode) != 0U),
  710. (bool_t)(((uint32_t)func & 1U) != 0U)
  711. ); // xPSR
  712. ptr[8] = (uint32_t)argument; // R0
  713. // Register post ISR processing function
  714. osRtxInfo.post_process.thread = osRtxThreadPostProcess;
  715. EvrRtxThreadCreated(thread, thread->thread_addr, thread->name);
  716. } else {
  717. EvrRtxThreadError(NULL, (int32_t)osErrorNoMemory);
  718. }
  719. if (thread != NULL) {
  720. osRtxThreadDispatch(thread);
  721. }
  722. return thread;
  723. }
  724. /// Get name of a thread.
  725. /// \note API identical to osThreadGetName
  726. static const char *svcRtxThreadGetName (osThreadId_t thread_id) {
  727. os_thread_t *thread = osRtxThreadId(thread_id);
  728. // Check parameters
  729. if ((thread == NULL) || (thread->id != osRtxIdThread)) {
  730. EvrRtxThreadGetName(thread, NULL);
  731. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  732. return NULL;
  733. }
  734. EvrRtxThreadGetName(thread, thread->name);
  735. return thread->name;
  736. }
  737. /// Return the thread ID of the current running thread.
  738. /// \note API identical to osThreadGetId
  739. static osThreadId_t svcRtxThreadGetId (void) {
  740. os_thread_t *thread;
  741. thread = osRtxThreadGetRunning();
  742. EvrRtxThreadGetId(thread);
  743. return thread;
  744. }
  745. /// Get current thread state of a thread.
  746. /// \note API identical to osThreadGetState
  747. static osThreadState_t svcRtxThreadGetState (osThreadId_t thread_id) {
  748. os_thread_t *thread = osRtxThreadId(thread_id);
  749. osThreadState_t state;
  750. // Check parameters
  751. if ((thread == NULL) || (thread->id != osRtxIdThread)) {
  752. EvrRtxThreadGetState(thread, osThreadError);
  753. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  754. return osThreadError;
  755. }
  756. state = osRtxThreadState(thread);
  757. EvrRtxThreadGetState(thread, state);
  758. return state;
  759. }
  760. /// Get stack size of a thread.
  761. /// \note API identical to osThreadGetStackSize
  762. static uint32_t svcRtxThreadGetStackSize (osThreadId_t thread_id) {
  763. os_thread_t *thread = osRtxThreadId(thread_id);
  764. // Check parameters
  765. if ((thread == NULL) || (thread->id != osRtxIdThread)) {
  766. EvrRtxThreadGetStackSize(thread, 0U);
  767. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  768. return 0U;
  769. }
  770. EvrRtxThreadGetStackSize(thread, thread->stack_size);
  771. return thread->stack_size;
  772. }
  773. /// Get available stack space of a thread based on stack watermark recording during execution.
  774. /// \note API identical to osThreadGetStackSpace
  775. static uint32_t svcRtxThreadGetStackSpace (osThreadId_t thread_id) {
  776. os_thread_t *thread = osRtxThreadId(thread_id);
  777. const uint32_t *stack;
  778. uint32_t space;
  779. // Check parameters
  780. if ((thread == NULL) || (thread->id != osRtxIdThread)) {
  781. EvrRtxThreadGetStackSpace(thread, 0U);
  782. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  783. return 0U;
  784. }
  785. // Check if stack watermark is not enabled
  786. if ((osRtxConfig.flags & osRtxConfigStackWatermark) == 0U) {
  787. EvrRtxThreadGetStackSpace(thread, 0U);
  788. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  789. return 0U;
  790. }
  791. //lint -e{9079} "conversion from pointer to void to pointer to other type"
  792. stack = thread->stack_mem;
  793. if (*stack++ == osRtxStackMagicWord) {
  794. for (space = 4U; space < thread->stack_size; space += 4U) {
  795. if (*stack++ != osRtxStackFillPattern) {
  796. break;
  797. }
  798. }
  799. } else {
  800. space = 0U;
  801. }
  802. EvrRtxThreadGetStackSpace(thread, space);
  803. return space;
  804. }
  805. /// Change priority of a thread.
  806. /// \note API identical to osThreadSetPriority
  807. static osStatus_t svcRtxThreadSetPriority (osThreadId_t thread_id, osPriority_t priority) {
  808. os_thread_t *thread = osRtxThreadId(thread_id);
  809. // Check parameters
  810. if ((thread == NULL) || (thread->id != osRtxIdThread) ||
  811. (priority < osPriorityIdle) || (priority > osPriorityISR)) {
  812. EvrRtxThreadError(thread, (int32_t)osErrorParameter);
  813. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  814. return osErrorParameter;
  815. }
  816. // Check object state
  817. if (thread->state == osRtxThreadTerminated) {
  818. EvrRtxThreadError(thread, (int32_t)osErrorResource);
  819. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  820. return osErrorResource;
  821. }
  822. if (thread->priority != (int8_t)priority) {
  823. thread->priority = (int8_t)priority;
  824. thread->priority_base = (int8_t)priority;
  825. EvrRtxThreadPriorityUpdated(thread, priority);
  826. osRtxThreadListSort(thread);
  827. osRtxThreadDispatch(NULL);
  828. }
  829. return osOK;
  830. }
  831. /// Get current priority of a thread.
  832. /// \note API identical to osThreadGetPriority
  833. static osPriority_t svcRtxThreadGetPriority (osThreadId_t thread_id) {
  834. os_thread_t *thread = osRtxThreadId(thread_id);
  835. osPriority_t priority;
  836. // Check parameters
  837. if ((thread == NULL) || (thread->id != osRtxIdThread)) {
  838. EvrRtxThreadGetPriority(thread, osPriorityError);
  839. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  840. return osPriorityError;
  841. }
  842. // Check object state
  843. if (thread->state == osRtxThreadTerminated) {
  844. EvrRtxThreadGetPriority(thread, osPriorityError);
  845. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  846. return osPriorityError;
  847. }
  848. priority = osRtxThreadPriority(thread);
  849. EvrRtxThreadGetPriority(thread, priority);
  850. return priority;
  851. }
  852. /// Pass control to next thread that is in state READY.
  853. /// \note API identical to osThreadYield
  854. static osStatus_t svcRtxThreadYield (void) {
  855. os_thread_t *thread_running;
  856. os_thread_t *thread_ready;
  857. if (osRtxKernelGetState() == osRtxKernelRunning) {
  858. thread_running = osRtxThreadGetRunning();
  859. thread_ready = osRtxInfo.thread.ready.thread_list;
  860. if ((thread_ready != NULL) &&
  861. (thread_ready->priority == thread_running->priority)) {
  862. osRtxThreadListRemove(thread_ready);
  863. osRtxThreadReadyPut(thread_running);
  864. EvrRtxThreadPreempted(thread_running);
  865. osRtxThreadSwitch(thread_ready);
  866. }
  867. }
  868. return osOK;
  869. }
  870. /// Suspend execution of a thread.
  871. /// \note API identical to osThreadSuspend
  872. static osStatus_t svcRtxThreadSuspend (osThreadId_t thread_id) {
  873. os_thread_t *thread = osRtxThreadId(thread_id);
  874. osStatus_t status;
  875. // Check parameters
  876. if ((thread == NULL) || (thread->id != osRtxIdThread)) {
  877. EvrRtxThreadError(thread, (int32_t)osErrorParameter);
  878. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  879. return osErrorParameter;
  880. }
  881. // Check object state
  882. switch (thread->state & osRtxThreadStateMask) {
  883. case osRtxThreadRunning:
  884. if ((osRtxKernelGetState() != osRtxKernelRunning) ||
  885. (osRtxInfo.thread.ready.thread_list == NULL)) {
  886. EvrRtxThreadError(thread, (int32_t)osErrorResource);
  887. status = osErrorResource;
  888. } else {
  889. status = osOK;
  890. }
  891. break;
  892. case osRtxThreadReady:
  893. osRtxThreadListRemove(thread);
  894. status = osOK;
  895. break;
  896. case osRtxThreadBlocked:
  897. osRtxThreadListRemove(thread);
  898. osRtxThreadDelayRemove(thread);
  899. status = osOK;
  900. break;
  901. case osRtxThreadInactive:
  902. case osRtxThreadTerminated:
  903. default:
  904. EvrRtxThreadError(thread, (int32_t)osErrorResource);
  905. status = osErrorResource;
  906. break;
  907. }
  908. if (status == osOK) {
  909. EvrRtxThreadSuspended(thread);
  910. if (thread->state == osRtxThreadRunning) {
  911. osRtxThreadSwitch(osRtxThreadListGet(&osRtxInfo.thread.ready));
  912. }
  913. // Update Thread State and put it into Delay list
  914. thread->state = osRtxThreadBlocked;
  915. osRtxThreadDelayInsert(thread, osWaitForever);
  916. }
  917. return status;
  918. }
  919. /// Resume execution of a thread.
  920. /// \note API identical to osThreadResume
  921. static osStatus_t svcRtxThreadResume (osThreadId_t thread_id) {
  922. os_thread_t *thread = osRtxThreadId(thread_id);
  923. // Check parameters
  924. if ((thread == NULL) || (thread->id != osRtxIdThread)) {
  925. EvrRtxThreadError(thread, (int32_t)osErrorParameter);
  926. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  927. return osErrorParameter;
  928. }
  929. // Check object state
  930. if ((thread->state & osRtxThreadStateMask) != osRtxThreadBlocked) {
  931. EvrRtxThreadError(thread, (int32_t)osErrorResource);
  932. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  933. return osErrorResource;
  934. }
  935. EvrRtxThreadResumed(thread);
  936. // Wakeup Thread
  937. osRtxThreadListRemove(thread);
  938. osRtxThreadDelayRemove(thread);
  939. osRtxThreadDispatch(thread);
  940. return osOK;
  941. }
  942. /// Wakeup a thread waiting to join.
  943. /// \param[in] thread thread object.
  944. static void osRtxThreadJoinWakeup (os_thread_t *thread) {
  945. if (thread->thread_join != NULL) {
  946. osRtxThreadWaitExit(thread->thread_join, (uint32_t)osOK, FALSE);
  947. EvrRtxThreadJoined(thread->thread_join);
  948. }
  949. if (thread->state == osRtxThreadWaitingJoin) {
  950. thread->thread_next->thread_join = NULL;
  951. }
  952. }
  953. /// Free Thread resources.
  954. /// \param[in] thread thread object.
  955. static void osRtxThreadFree (os_thread_t *thread) {
  956. // Mark object as inactive and invalid
  957. thread->state = osRtxThreadInactive;
  958. thread->id = osRtxIdInvalid;
  959. #if (DOMAIN_NS == 1)
  960. // Free secure process stack
  961. if (thread->tz_memory != 0U) {
  962. (void)TZ_FreeModuleContext_S(thread->tz_memory);
  963. }
  964. #endif
  965. // Free stack memory
  966. if ((thread->flags & osRtxFlagSystemMemory) != 0U) {
  967. if ((thread->flags & osRtxThreadFlagDefStack) != 0U) {
  968. (void)osRtxMemoryPoolFree(osRtxInfo.mpi.stack, thread->stack_mem);
  969. } else {
  970. (void)osRtxMemoryFree(osRtxInfo.mem.stack, thread->stack_mem);
  971. }
  972. }
  973. // Free object memory
  974. if ((thread->flags & osRtxFlagSystemObject) != 0U) {
  975. if (osRtxInfo.mpi.thread != NULL) {
  976. (void)osRtxMemoryPoolFree(osRtxInfo.mpi.thread, thread);
  977. } else {
  978. (void)osRtxMemoryFree(osRtxInfo.mem.common, thread);
  979. }
  980. #ifdef RTX_OBJ_MEM_USAGE
  981. osRtxThreadMemUsage.cnt_free++;
  982. #endif
  983. }
  984. }
  985. /// Destroy a Thread.
  986. /// \param[in] thread thread object.
  987. static void osRtxThreadDestroy (os_thread_t *thread) {
  988. if ((thread->attr & osThreadJoinable) == 0U) {
  989. osRtxThreadFree(thread);
  990. } else {
  991. // Update Thread State and put it into Terminate Thread list
  992. thread->state = osRtxThreadTerminated;
  993. thread->thread_prev = NULL;
  994. thread->thread_next = osRtxInfo.thread.terminate_list;
  995. if (osRtxInfo.thread.terminate_list != NULL) {
  996. osRtxInfo.thread.terminate_list->thread_prev = thread;
  997. }
  998. osRtxInfo.thread.terminate_list = thread;
  999. }
  1000. EvrRtxThreadDestroyed(thread);
  1001. }
  1002. /// Detach a thread (thread storage can be reclaimed when thread terminates).
  1003. /// \note API identical to osThreadDetach
  1004. static osStatus_t svcRtxThreadDetach (osThreadId_t thread_id) {
  1005. os_thread_t *thread = osRtxThreadId(thread_id);
  1006. // Check parameters
  1007. if ((thread == NULL) || (thread->id != osRtxIdThread)) {
  1008. EvrRtxThreadError(thread, (int32_t)osErrorParameter);
  1009. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1010. return osErrorParameter;
  1011. }
  1012. // Check object attributes
  1013. if ((thread->attr & osThreadJoinable) == 0U) {
  1014. EvrRtxThreadError(thread, osRtxErrorThreadNotJoinable);
  1015. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1016. return osErrorResource;
  1017. }
  1018. if (thread->state == osRtxThreadTerminated) {
  1019. osRtxThreadListUnlink(&osRtxInfo.thread.terminate_list, thread);
  1020. osRtxThreadFree(thread);
  1021. } else {
  1022. thread->attr &= ~osThreadJoinable;
  1023. }
  1024. EvrRtxThreadDetached(thread);
  1025. return osOK;
  1026. }
  1027. /// Wait for specified thread to terminate.
  1028. /// \note API identical to osThreadJoin
  1029. static osStatus_t svcRtxThreadJoin (osThreadId_t thread_id) {
  1030. os_thread_t *thread = osRtxThreadId(thread_id);
  1031. os_thread_t *thread_running;
  1032. osStatus_t status;
  1033. // Check parameters
  1034. if ((thread == NULL) || (thread->id != osRtxIdThread)) {
  1035. EvrRtxThreadError(thread, (int32_t)osErrorParameter);
  1036. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1037. return osErrorParameter;
  1038. }
  1039. // Check object attributes
  1040. if ((thread->attr & osThreadJoinable) == 0U) {
  1041. EvrRtxThreadError(thread, osRtxErrorThreadNotJoinable);
  1042. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1043. return osErrorResource;
  1044. }
  1045. // Check object state
  1046. if (thread->state == osRtxThreadRunning) {
  1047. EvrRtxThreadError(thread, (int32_t)osErrorResource);
  1048. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1049. return osErrorResource;
  1050. }
  1051. if (thread->state == osRtxThreadTerminated) {
  1052. osRtxThreadListUnlink(&osRtxInfo.thread.terminate_list, thread);
  1053. osRtxThreadFree(thread);
  1054. EvrRtxThreadJoined(thread);
  1055. status = osOK;
  1056. } else {
  1057. // Suspend current Thread
  1058. if (osRtxThreadWaitEnter(osRtxThreadWaitingJoin, osWaitForever)) {
  1059. thread_running = osRtxThreadGetRunning();
  1060. thread_running->thread_next = thread;
  1061. thread->thread_join = thread_running;
  1062. thread->attr &= ~osThreadJoinable;
  1063. EvrRtxThreadJoinPending(thread);
  1064. } else {
  1065. EvrRtxThreadError(thread, (int32_t)osErrorResource);
  1066. }
  1067. status = osErrorResource;
  1068. }
  1069. return status;
  1070. }
  1071. /// Terminate execution of current running thread.
  1072. /// \note API identical to osThreadExit
  1073. static void svcRtxThreadExit (void) {
  1074. os_thread_t *thread;
  1075. // Check if switch to next Ready Thread is possible
  1076. if ((osRtxKernelGetState() != osRtxKernelRunning) ||
  1077. (osRtxInfo.thread.ready.thread_list == NULL)) {
  1078. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1079. return;
  1080. }
  1081. // Get running thread
  1082. thread = osRtxThreadGetRunning();
  1083. // Release owned Mutexes
  1084. osRtxMutexOwnerRelease(thread->mutex_list);
  1085. // Wakeup Thread waiting to Join
  1086. osRtxThreadJoinWakeup(thread);
  1087. // Switch to next Ready Thread
  1088. osRtxThreadSwitch(osRtxThreadListGet(&osRtxInfo.thread.ready));
  1089. // Update Stack Pointer
  1090. thread->sp = __get_PSP();
  1091. #ifdef RTX_STACK_CHECK
  1092. // Check Stack usage
  1093. if (!osRtxThreadStackCheck(thread)) {
  1094. osRtxThreadSetRunning(osRtxInfo.thread.run.next);
  1095. (void)osRtxKernelErrorNotify(osRtxErrorStackOverflow, thread);
  1096. }
  1097. #endif
  1098. // Mark running thread as deleted
  1099. osRtxThreadSetRunning(NULL);
  1100. // Destroy Thread
  1101. osRtxThreadDestroy(thread);
  1102. }
  1103. /// Terminate execution of a thread.
  1104. /// \note API identical to osThreadTerminate
  1105. static osStatus_t svcRtxThreadTerminate (osThreadId_t thread_id) {
  1106. os_thread_t *thread = osRtxThreadId(thread_id);
  1107. osStatus_t status;
  1108. // Check parameters
  1109. if ((thread == NULL) || (thread->id != osRtxIdThread)) {
  1110. EvrRtxThreadError(thread, (int32_t)osErrorParameter);
  1111. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1112. return osErrorParameter;
  1113. }
  1114. // Check object state
  1115. switch (thread->state & osRtxThreadStateMask) {
  1116. case osRtxThreadRunning:
  1117. if ((osRtxKernelGetState() != osRtxKernelRunning) ||
  1118. (osRtxInfo.thread.ready.thread_list == NULL)) {
  1119. EvrRtxThreadError(thread, (int32_t)osErrorResource);
  1120. status = osErrorResource;
  1121. } else {
  1122. status = osOK;
  1123. }
  1124. break;
  1125. case osRtxThreadReady:
  1126. osRtxThreadListRemove(thread);
  1127. status = osOK;
  1128. break;
  1129. case osRtxThreadBlocked:
  1130. osRtxThreadListRemove(thread);
  1131. osRtxThreadDelayRemove(thread);
  1132. status = osOK;
  1133. break;
  1134. case osRtxThreadInactive:
  1135. case osRtxThreadTerminated:
  1136. default:
  1137. EvrRtxThreadError(thread, (int32_t)osErrorResource);
  1138. status = osErrorResource;
  1139. break;
  1140. }
  1141. if (status == osOK) {
  1142. // Release owned Mutexes
  1143. osRtxMutexOwnerRelease(thread->mutex_list);
  1144. // Wakeup Thread waiting to Join
  1145. osRtxThreadJoinWakeup(thread);
  1146. // Switch to next Ready Thread when terminating running Thread
  1147. if (thread->state == osRtxThreadRunning) {
  1148. osRtxThreadSwitch(osRtxThreadListGet(&osRtxInfo.thread.ready));
  1149. // Update Stack Pointer
  1150. thread->sp = __get_PSP();
  1151. #ifdef RTX_STACK_CHECK
  1152. // Check Stack usage
  1153. if (!osRtxThreadStackCheck(thread)) {
  1154. osRtxThreadSetRunning(osRtxInfo.thread.run.next);
  1155. (void)osRtxKernelErrorNotify(osRtxErrorStackOverflow, thread);
  1156. }
  1157. #endif
  1158. // Mark running thread as deleted
  1159. osRtxThreadSetRunning(NULL);
  1160. } else {
  1161. osRtxThreadDispatch(NULL);
  1162. }
  1163. // Destroy Thread
  1164. osRtxThreadDestroy(thread);
  1165. }
  1166. return status;
  1167. }
  1168. /// Get number of active threads.
  1169. /// \note API identical to osThreadGetCount
  1170. static uint32_t svcRtxThreadGetCount (void) {
  1171. const os_thread_t *thread;
  1172. uint32_t count;
  1173. // Running Thread
  1174. count = 1U;
  1175. // Ready List
  1176. for (thread = osRtxInfo.thread.ready.thread_list;
  1177. thread != NULL; thread = thread->thread_next) {
  1178. count++;
  1179. }
  1180. // Delay List
  1181. for (thread = osRtxInfo.thread.delay_list;
  1182. thread != NULL; thread = thread->delay_next) {
  1183. count++;
  1184. }
  1185. // Wait List
  1186. for (thread = osRtxInfo.thread.wait_list;
  1187. thread != NULL; thread = thread->delay_next) {
  1188. count++;
  1189. }
  1190. EvrRtxThreadGetCount(count);
  1191. return count;
  1192. }
  1193. /// Enumerate active threads.
  1194. /// \note API identical to osThreadEnumerate
  1195. static uint32_t svcRtxThreadEnumerate (osThreadId_t *thread_array, uint32_t array_items) {
  1196. os_thread_t *thread;
  1197. uint32_t count;
  1198. // Check parameters
  1199. if ((thread_array == NULL) || (array_items == 0U)) {
  1200. EvrRtxThreadEnumerate(thread_array, array_items, 0U);
  1201. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1202. return 0U;
  1203. }
  1204. // Running Thread
  1205. *thread_array = osRtxThreadGetRunning();
  1206. thread_array++;
  1207. count = 1U;
  1208. // Ready List
  1209. for (thread = osRtxInfo.thread.ready.thread_list;
  1210. (thread != NULL) && (count < array_items); thread = thread->thread_next) {
  1211. *thread_array = thread;
  1212. thread_array++;
  1213. count++;
  1214. }
  1215. // Delay List
  1216. for (thread = osRtxInfo.thread.delay_list;
  1217. (thread != NULL) && (count < array_items); thread = thread->delay_next) {
  1218. *thread_array = thread;
  1219. thread_array++;
  1220. count++;
  1221. }
  1222. // Wait List
  1223. for (thread = osRtxInfo.thread.wait_list;
  1224. (thread != NULL) && (count < array_items); thread = thread->delay_next) {
  1225. *thread_array = thread;
  1226. thread_array++;
  1227. count++;
  1228. }
  1229. EvrRtxThreadEnumerate(thread_array - count, array_items, count);
  1230. return count;
  1231. }
  1232. /// Set the specified Thread Flags of a thread.
  1233. /// \note API identical to osThreadFlagsSet
  1234. static uint32_t svcRtxThreadFlagsSet (osThreadId_t thread_id, uint32_t flags) {
  1235. os_thread_t *thread = osRtxThreadId(thread_id);
  1236. uint32_t thread_flags;
  1237. uint32_t thread_flags0;
  1238. // Check parameters
  1239. if ((thread == NULL) || (thread->id != osRtxIdThread) ||
  1240. ((flags & ~(((uint32_t)1U << osRtxThreadFlagsLimit) - 1U)) != 0U)) {
  1241. EvrRtxThreadFlagsError(thread, (int32_t)osErrorParameter);
  1242. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1243. return ((uint32_t)osErrorParameter);
  1244. }
  1245. // Check object state
  1246. if (thread->state == osRtxThreadTerminated) {
  1247. EvrRtxThreadFlagsError(thread, (int32_t)osErrorResource);
  1248. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1249. return ((uint32_t)osErrorResource);
  1250. }
  1251. // Set Thread Flags
  1252. thread_flags = ThreadFlagsSet(thread, flags);
  1253. // Check if Thread is waiting for Thread Flags
  1254. if (thread->state == osRtxThreadWaitingThreadFlags) {
  1255. thread_flags0 = ThreadFlagsCheck(thread, thread->wait_flags, thread->flags_options);
  1256. if (thread_flags0 != 0U) {
  1257. if ((thread->flags_options & osFlagsNoClear) == 0U) {
  1258. thread_flags = thread_flags0 & ~thread->wait_flags;
  1259. } else {
  1260. thread_flags = thread_flags0;
  1261. }
  1262. osRtxThreadWaitExit(thread, thread_flags0, TRUE);
  1263. EvrRtxThreadFlagsWaitCompleted(thread->wait_flags, thread->flags_options, thread_flags0, thread);
  1264. }
  1265. }
  1266. EvrRtxThreadFlagsSetDone(thread, thread_flags);
  1267. return thread_flags;
  1268. }
  1269. /// Clear the specified Thread Flags of current running thread.
  1270. /// \note API identical to osThreadFlagsClear
  1271. static uint32_t svcRtxThreadFlagsClear (uint32_t flags) {
  1272. os_thread_t *thread;
  1273. uint32_t thread_flags;
  1274. // Check running thread
  1275. thread = osRtxThreadGetRunning();
  1276. if (thread == NULL) {
  1277. EvrRtxThreadFlagsError(NULL, osRtxErrorKernelNotRunning);
  1278. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1279. return ((uint32_t)osError);
  1280. }
  1281. // Check parameters
  1282. if ((flags & ~(((uint32_t)1U << osRtxThreadFlagsLimit) - 1U)) != 0U) {
  1283. EvrRtxThreadFlagsError(thread, (int32_t)osErrorParameter);
  1284. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1285. return ((uint32_t)osErrorParameter);
  1286. }
  1287. // Clear Thread Flags
  1288. thread_flags = ThreadFlagsClear(thread, flags);
  1289. EvrRtxThreadFlagsClearDone(thread_flags);
  1290. return thread_flags;
  1291. }
  1292. /// Get the current Thread Flags of current running thread.
  1293. /// \note API identical to osThreadFlagsGet
  1294. static uint32_t svcRtxThreadFlagsGet (void) {
  1295. const os_thread_t *thread;
  1296. // Check running thread
  1297. thread = osRtxThreadGetRunning();
  1298. if (thread == NULL) {
  1299. EvrRtxThreadFlagsGet(0U);
  1300. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1301. return 0U;
  1302. }
  1303. EvrRtxThreadFlagsGet(thread->thread_flags);
  1304. return thread->thread_flags;
  1305. }
  1306. /// Wait for one or more Thread Flags of the current running thread to become signaled.
  1307. /// \note API identical to osThreadFlagsWait
  1308. static uint32_t svcRtxThreadFlagsWait (uint32_t flags, uint32_t options, uint32_t timeout) {
  1309. os_thread_t *thread;
  1310. uint32_t thread_flags;
  1311. // Check running thread
  1312. thread = osRtxThreadGetRunning();
  1313. if (thread == NULL) {
  1314. EvrRtxThreadFlagsError(NULL, osRtxErrorKernelNotRunning);
  1315. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1316. return ((uint32_t)osError);
  1317. }
  1318. // Check parameters
  1319. if ((flags & ~(((uint32_t)1U << osRtxThreadFlagsLimit) - 1U)) != 0U) {
  1320. EvrRtxThreadFlagsError(thread, (int32_t)osErrorParameter);
  1321. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1322. return ((uint32_t)osErrorParameter);
  1323. }
  1324. // Check Thread Flags
  1325. thread_flags = ThreadFlagsCheck(thread, flags, options);
  1326. if (thread_flags != 0U) {
  1327. EvrRtxThreadFlagsWaitCompleted(flags, options, thread_flags, thread);
  1328. } else {
  1329. // Check if timeout is specified
  1330. if (timeout != 0U) {
  1331. // Store waiting flags and options
  1332. EvrRtxThreadFlagsWaitPending(flags, options, timeout);
  1333. thread->wait_flags = flags;
  1334. thread->flags_options = (uint8_t)options;
  1335. // Suspend current Thread
  1336. if (!osRtxThreadWaitEnter(osRtxThreadWaitingThreadFlags, timeout)) {
  1337. EvrRtxThreadFlagsWaitTimeout(thread);
  1338. }
  1339. thread_flags = (uint32_t)osErrorTimeout;
  1340. } else {
  1341. EvrRtxThreadFlagsWaitNotCompleted(flags, options);
  1342. thread_flags = (uint32_t)osErrorResource;
  1343. }
  1344. }
  1345. return thread_flags;
  1346. }
  1347. // Service Calls definitions
  1348. //lint ++flb "Library Begin" [MISRA Note 11]
  1349. SVC0_3 (ThreadNew, osThreadId_t, osThreadFunc_t, void *, const osThreadAttr_t *)
  1350. SVC0_1 (ThreadGetName, const char *, osThreadId_t)
  1351. SVC0_0 (ThreadGetId, osThreadId_t)
  1352. SVC0_1 (ThreadGetState, osThreadState_t, osThreadId_t)
  1353. SVC0_1 (ThreadGetStackSize, uint32_t, osThreadId_t)
  1354. SVC0_1 (ThreadGetStackSpace, uint32_t, osThreadId_t)
  1355. SVC0_2 (ThreadSetPriority, osStatus_t, osThreadId_t, osPriority_t)
  1356. SVC0_1 (ThreadGetPriority, osPriority_t, osThreadId_t)
  1357. SVC0_0 (ThreadYield, osStatus_t)
  1358. SVC0_1 (ThreadSuspend, osStatus_t, osThreadId_t)
  1359. SVC0_1 (ThreadResume, osStatus_t, osThreadId_t)
  1360. SVC0_1 (ThreadDetach, osStatus_t, osThreadId_t)
  1361. SVC0_1 (ThreadJoin, osStatus_t, osThreadId_t)
  1362. SVC0_0N(ThreadExit, void)
  1363. SVC0_1 (ThreadTerminate, osStatus_t, osThreadId_t)
  1364. SVC0_0 (ThreadGetCount, uint32_t)
  1365. SVC0_2 (ThreadEnumerate, uint32_t, osThreadId_t *, uint32_t)
  1366. SVC0_2 (ThreadFlagsSet, uint32_t, osThreadId_t, uint32_t)
  1367. SVC0_1 (ThreadFlagsClear, uint32_t, uint32_t)
  1368. SVC0_0 (ThreadFlagsGet, uint32_t)
  1369. SVC0_3 (ThreadFlagsWait, uint32_t, uint32_t, uint32_t, uint32_t)
  1370. //lint --flb "Library End"
  1371. // ==== ISR Calls ====
  1372. /// Set the specified Thread Flags of a thread.
  1373. /// \note API identical to osThreadFlagsSet
  1374. __STATIC_INLINE
  1375. uint32_t isrRtxThreadFlagsSet (osThreadId_t thread_id, uint32_t flags) {
  1376. os_thread_t *thread = osRtxThreadId(thread_id);
  1377. uint32_t thread_flags;
  1378. // Check parameters
  1379. if ((thread == NULL) || (thread->id != osRtxIdThread) ||
  1380. ((flags & ~(((uint32_t)1U << osRtxThreadFlagsLimit) - 1U)) != 0U)) {
  1381. EvrRtxThreadFlagsError(thread, (int32_t)osErrorParameter);
  1382. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1383. return ((uint32_t)osErrorParameter);
  1384. }
  1385. // Check object state
  1386. if (thread->state == osRtxThreadTerminated) {
  1387. EvrRtxThreadFlagsError(thread, (int32_t)osErrorResource);
  1388. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1389. return ((uint32_t)osErrorResource);
  1390. }
  1391. // Set Thread Flags
  1392. thread_flags = ThreadFlagsSet(thread, flags);
  1393. // Register post ISR processing
  1394. osRtxPostProcess(osRtxObject(thread));
  1395. EvrRtxThreadFlagsSetDone(thread, thread_flags);
  1396. return thread_flags;
  1397. }
  1398. // ==== Library functions ====
  1399. /// Thread startup (Idle and Timer Thread).
  1400. /// \return true - success, false - failure.
  1401. bool_t osRtxThreadStartup (void) {
  1402. bool_t ret = FALSE;
  1403. // Create Idle Thread
  1404. osRtxInfo.thread.idle = osRtxThreadId(
  1405. svcRtxThreadNew(osRtxIdleThread, NULL, osRtxConfig.idle_thread_attr)
  1406. );
  1407. // Create Timer Thread
  1408. if (osRtxConfig.timer_setup != NULL) {
  1409. if (osRtxConfig.timer_setup() == 0) {
  1410. osRtxInfo.timer.thread = osRtxThreadId(
  1411. svcRtxThreadNew(osRtxConfig.timer_thread, osRtxInfo.timer.mq, osRtxConfig.timer_thread_attr)
  1412. );
  1413. if (osRtxInfo.timer.thread != NULL) {
  1414. ret = TRUE;
  1415. }
  1416. }
  1417. } else {
  1418. ret = TRUE;
  1419. }
  1420. return ret;
  1421. }
  1422. // ==== Public API ====
  1423. /// Create a thread and add it to Active Threads.
  1424. osThreadId_t osThreadNew (osThreadFunc_t func, void *argument, const osThreadAttr_t *attr) {
  1425. osThreadId_t thread_id;
  1426. EvrRtxThreadNew(func, argument, attr);
  1427. if (IsException() || IsIrqMasked()) {
  1428. EvrRtxThreadError(NULL, (int32_t)osErrorISR);
  1429. thread_id = NULL;
  1430. } else {
  1431. thread_id = __svcThreadNew(func, argument, attr);
  1432. }
  1433. return thread_id;
  1434. }
  1435. /// Get name of a thread.
  1436. const char *osThreadGetName (osThreadId_t thread_id) {
  1437. const char *name;
  1438. if (IsException() || IsIrqMasked()) {
  1439. EvrRtxThreadGetName(thread_id, NULL);
  1440. name = NULL;
  1441. } else {
  1442. name = __svcThreadGetName(thread_id);
  1443. }
  1444. return name;
  1445. }
  1446. /// Return the thread ID of the current running thread.
  1447. osThreadId_t osThreadGetId (void) {
  1448. osThreadId_t thread_id;
  1449. if (IsException() || IsIrqMasked()) {
  1450. thread_id = svcRtxThreadGetId();
  1451. } else {
  1452. thread_id = __svcThreadGetId();
  1453. }
  1454. return thread_id;
  1455. }
  1456. /// Get current thread state of a thread.
  1457. osThreadState_t osThreadGetState (osThreadId_t thread_id) {
  1458. osThreadState_t state;
  1459. if (IsException() || IsIrqMasked()) {
  1460. EvrRtxThreadGetState(thread_id, osThreadError);
  1461. state = osThreadError;
  1462. } else {
  1463. state = __svcThreadGetState(thread_id);
  1464. }
  1465. return state;
  1466. }
  1467. /// Get stack size of a thread.
  1468. uint32_t osThreadGetStackSize (osThreadId_t thread_id) {
  1469. uint32_t stack_size;
  1470. if (IsException() || IsIrqMasked()) {
  1471. EvrRtxThreadGetStackSize(thread_id, 0U);
  1472. stack_size = 0U;
  1473. } else {
  1474. stack_size = __svcThreadGetStackSize(thread_id);
  1475. }
  1476. return stack_size;
  1477. }
  1478. /// Get available stack space of a thread based on stack watermark recording during execution.
  1479. uint32_t osThreadGetStackSpace (osThreadId_t thread_id) {
  1480. uint32_t stack_space;
  1481. if (IsException() || IsIrqMasked()) {
  1482. EvrRtxThreadGetStackSpace(thread_id, 0U);
  1483. stack_space = 0U;
  1484. } else {
  1485. stack_space = __svcThreadGetStackSpace(thread_id);
  1486. }
  1487. return stack_space;
  1488. }
  1489. /// Change priority of a thread.
  1490. osStatus_t osThreadSetPriority (osThreadId_t thread_id, osPriority_t priority) {
  1491. osStatus_t status;
  1492. EvrRtxThreadSetPriority(thread_id, priority);
  1493. if (IsException() || IsIrqMasked()) {
  1494. EvrRtxThreadError(thread_id, (int32_t)osErrorISR);
  1495. status = osErrorISR;
  1496. } else {
  1497. status = __svcThreadSetPriority(thread_id, priority);
  1498. }
  1499. return status;
  1500. }
  1501. /// Get current priority of a thread.
  1502. osPriority_t osThreadGetPriority (osThreadId_t thread_id) {
  1503. osPriority_t priority;
  1504. if (IsException() || IsIrqMasked()) {
  1505. EvrRtxThreadGetPriority(thread_id, osPriorityError);
  1506. priority = osPriorityError;
  1507. } else {
  1508. priority = __svcThreadGetPriority(thread_id);
  1509. }
  1510. return priority;
  1511. }
  1512. /// Pass control to next thread that is in state READY.
  1513. osStatus_t osThreadYield (void) {
  1514. osStatus_t status;
  1515. EvrRtxThreadYield();
  1516. if (IsException() || IsIrqMasked()) {
  1517. EvrRtxThreadError(NULL, (int32_t)osErrorISR);
  1518. status = osErrorISR;
  1519. } else {
  1520. status = __svcThreadYield();
  1521. }
  1522. return status;
  1523. }
  1524. /// Suspend execution of a thread.
  1525. osStatus_t osThreadSuspend (osThreadId_t thread_id) {
  1526. osStatus_t status;
  1527. EvrRtxThreadSuspend(thread_id);
  1528. if (IsException() || IsIrqMasked()) {
  1529. EvrRtxThreadError(thread_id, (int32_t)osErrorISR);
  1530. status = osErrorISR;
  1531. } else {
  1532. status = __svcThreadSuspend(thread_id);
  1533. }
  1534. return status;
  1535. }
  1536. /// Resume execution of a thread.
  1537. osStatus_t osThreadResume (osThreadId_t thread_id) {
  1538. osStatus_t status;
  1539. EvrRtxThreadResume(thread_id);
  1540. if (IsException() || IsIrqMasked()) {
  1541. EvrRtxThreadError(thread_id, (int32_t)osErrorISR);
  1542. status = osErrorISR;
  1543. } else {
  1544. status = __svcThreadResume(thread_id);
  1545. }
  1546. return status;
  1547. }
  1548. /// Detach a thread (thread storage can be reclaimed when thread terminates).
  1549. osStatus_t osThreadDetach (osThreadId_t thread_id) {
  1550. osStatus_t status;
  1551. EvrRtxThreadDetach(thread_id);
  1552. if (IsException() || IsIrqMasked()) {
  1553. EvrRtxThreadError(thread_id, (int32_t)osErrorISR);
  1554. status = osErrorISR;
  1555. } else {
  1556. status = __svcThreadDetach(thread_id);
  1557. }
  1558. return status;
  1559. }
  1560. /// Wait for specified thread to terminate.
  1561. osStatus_t osThreadJoin (osThreadId_t thread_id) {
  1562. osStatus_t status;
  1563. EvrRtxThreadJoin(thread_id);
  1564. if (IsException() || IsIrqMasked()) {
  1565. EvrRtxThreadError(thread_id, (int32_t)osErrorISR);
  1566. status = osErrorISR;
  1567. } else {
  1568. status = __svcThreadJoin(thread_id);
  1569. }
  1570. return status;
  1571. }
  1572. /// Terminate execution of current running thread.
  1573. __NO_RETURN void osThreadExit (void) {
  1574. EvrRtxThreadExit();
  1575. __svcThreadExit();
  1576. EvrRtxThreadError(NULL, (int32_t)osError);
  1577. for (;;) {}
  1578. }
  1579. /// Terminate execution of a thread.
  1580. osStatus_t osThreadTerminate (osThreadId_t thread_id) {
  1581. osStatus_t status;
  1582. EvrRtxThreadTerminate(thread_id);
  1583. if (IsException() || IsIrqMasked()) {
  1584. EvrRtxThreadError(thread_id, (int32_t)osErrorISR);
  1585. status = osErrorISR;
  1586. } else {
  1587. status = __svcThreadTerminate(thread_id);
  1588. }
  1589. return status;
  1590. }
  1591. /// Get number of active threads.
  1592. uint32_t osThreadGetCount (void) {
  1593. uint32_t count;
  1594. if (IsException() || IsIrqMasked()) {
  1595. EvrRtxThreadGetCount(0U);
  1596. count = 0U;
  1597. } else {
  1598. count = __svcThreadGetCount();
  1599. }
  1600. return count;
  1601. }
  1602. /// Enumerate active threads.
  1603. uint32_t osThreadEnumerate (osThreadId_t *thread_array, uint32_t array_items) {
  1604. uint32_t count;
  1605. if (IsException() || IsIrqMasked()) {
  1606. EvrRtxThreadEnumerate(thread_array, array_items, 0U);
  1607. count = 0U;
  1608. } else {
  1609. count = __svcThreadEnumerate(thread_array, array_items);
  1610. }
  1611. return count;
  1612. }
  1613. /// Set the specified Thread Flags of a thread.
  1614. uint32_t osThreadFlagsSet (osThreadId_t thread_id, uint32_t flags) {
  1615. uint32_t thread_flags;
  1616. EvrRtxThreadFlagsSet(thread_id, flags);
  1617. if (IsException() || IsIrqMasked()) {
  1618. thread_flags = isrRtxThreadFlagsSet(thread_id, flags);
  1619. } else {
  1620. thread_flags = __svcThreadFlagsSet(thread_id, flags);
  1621. }
  1622. return thread_flags;
  1623. }
  1624. /// Clear the specified Thread Flags of current running thread.
  1625. uint32_t osThreadFlagsClear (uint32_t flags) {
  1626. uint32_t thread_flags;
  1627. EvrRtxThreadFlagsClear(flags);
  1628. if (IsException() || IsIrqMasked()) {
  1629. EvrRtxThreadFlagsError(NULL, (int32_t)osErrorISR);
  1630. thread_flags = (uint32_t)osErrorISR;
  1631. } else {
  1632. thread_flags = __svcThreadFlagsClear(flags);
  1633. }
  1634. return thread_flags;
  1635. }
  1636. /// Get the current Thread Flags of current running thread.
  1637. uint32_t osThreadFlagsGet (void) {
  1638. uint32_t thread_flags;
  1639. if (IsException() || IsIrqMasked()) {
  1640. EvrRtxThreadFlagsGet(0U);
  1641. thread_flags = 0U;
  1642. } else {
  1643. thread_flags = __svcThreadFlagsGet();
  1644. }
  1645. return thread_flags;
  1646. }
  1647. /// Wait for one or more Thread Flags of the current running thread to become signaled.
  1648. uint32_t osThreadFlagsWait (uint32_t flags, uint32_t options, uint32_t timeout) {
  1649. uint32_t thread_flags;
  1650. EvrRtxThreadFlagsWait(flags, options, timeout);
  1651. if (IsException() || IsIrqMasked()) {
  1652. EvrRtxThreadFlagsError(NULL, (int32_t)osErrorISR);
  1653. thread_flags = (uint32_t)osErrorISR;
  1654. } else {
  1655. thread_flags = __svcThreadFlagsWait(flags, options, timeout);
  1656. }
  1657. return thread_flags;
  1658. }