rtx_thread.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696
  1. /*
  2. * Copyright (c) 2013-2017 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. // ==== 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 uint32_t ThreadFlagsSet (os_thread_t *thread, uint32_t flags) {
  32. #if (__EXCLUSIVE_ACCESS == 0U)
  33. uint32_t primask = __get_PRIMASK();
  34. #endif
  35. uint32_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 = atomic_set32(&thread->thread_flags, 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 uint32_t ThreadFlagsClear (os_thread_t *thread, uint32_t flags) {
  53. #if (__EXCLUSIVE_ACCESS == 0U)
  54. uint32_t primask = __get_PRIMASK();
  55. #endif
  56. uint32_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 = atomic_clr32(&thread->thread_flags, 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 uint32_t ThreadFlagsCheck (os_thread_t *thread, uint32_t flags, uint32_t options) {
  75. #if (__EXCLUSIVE_ACCESS == 0U)
  76. uint32_t primask;
  77. #endif
  78. uint32_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) == 0U))) {
  86. thread_flags = 0U;
  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 = atomic_chk32_all(&thread->thread_flags, flags);
  96. } else {
  97. thread_flags = atomic_chk32_any(&thread->thread_flags, 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) == 0U))) {
  104. thread_flags = 0U;
  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 osRtxThreadListPut (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 *osRtxThreadListGet (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. /// Retrieve Thread list root.
  149. /// \param[in] thread thread object.
  150. void *osRtxThreadListRoot (os_thread_t *thread) {
  151. while ((thread != NULL) && (thread->id == osRtxIdThread)) {
  152. thread = thread->thread_prev;
  153. }
  154. return ((void *)thread);
  155. }
  156. /// Re-sort a Thread in linked Object list by Priority (Highest at Head).
  157. /// \param[in] thread thread object.
  158. void osRtxThreadListSort (os_thread_t *thread) {
  159. os_object_t *object;
  160. os_thread_t *thread0;
  161. // Search for object
  162. thread0 = thread;
  163. while (thread0->id == osRtxIdThread) {
  164. thread0 = thread0->thread_prev;
  165. if (thread0 == NULL) {
  166. return;
  167. }
  168. }
  169. object = (os_object_t *)thread0;
  170. osRtxThreadListRemove(thread);
  171. osRtxThreadListPut(object, thread);
  172. }
  173. /// Remove a Thread from linked Object list.
  174. /// \param[in] thread thread object.
  175. void osRtxThreadListRemove (os_thread_t *thread) {
  176. if (thread->thread_prev != NULL) {
  177. thread->thread_prev->thread_next = thread->thread_next;
  178. if (thread->thread_next != NULL) {
  179. thread->thread_next->thread_prev = thread->thread_prev;
  180. }
  181. thread->thread_prev = NULL;
  182. }
  183. }
  184. /// Unlink a Thread from specified linked list.
  185. /// \param[in] thread thread object.
  186. void osRtxThreadListUnlink (os_thread_t **thread_list, os_thread_t *thread) {
  187. if (thread->thread_next != NULL) {
  188. thread->thread_next->thread_prev = thread->thread_prev;
  189. }
  190. if (thread->thread_prev != NULL) {
  191. thread->thread_prev->thread_next = thread->thread_next;
  192. thread->thread_prev = NULL;
  193. } else {
  194. *thread_list = thread->thread_next;
  195. }
  196. }
  197. /// Mark a Thread as Ready and put it into Ready list (sorted by Priority).
  198. /// \param[in] thread thread object.
  199. void osRtxThreadReadyPut (os_thread_t *thread) {
  200. thread->state = osRtxThreadReady;
  201. osRtxThreadListPut(&osRtxInfo.thread.ready, thread);
  202. }
  203. /// Insert a Thread into the Delay list sorted by Delay (Lowest at Head).
  204. /// \param[in] thread thread object.
  205. /// \param[in] delay delay value.
  206. void osRtxThreadDelayInsert (os_thread_t *thread, uint32_t delay) {
  207. os_thread_t *prev, *next;
  208. if (delay == osWaitForever) {
  209. prev = NULL;
  210. next = osRtxInfo.thread.wait_list;
  211. while (next != NULL) {
  212. prev = next;
  213. next = next->delay_next;
  214. }
  215. thread->delay = delay;
  216. thread->delay_prev = prev;
  217. thread->delay_next = next;
  218. if (prev != NULL) {
  219. prev->delay_next = thread;
  220. } else {
  221. osRtxInfo.thread.wait_list = thread;
  222. }
  223. if (next != NULL) {
  224. next->delay_prev = 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. void osRtxThreadDelayRemove (os_thread_t *thread) {
  251. if (thread->delay == osWaitForever) {
  252. if ((thread->delay_prev == NULL) && (osRtxInfo.thread.wait_list != thread)) {
  253. return;
  254. }
  255. if (thread->delay_next != NULL) {
  256. thread->delay_next->delay_prev = thread->delay_prev;
  257. }
  258. if (thread->delay_prev != NULL) {
  259. thread->delay_prev->delay_next = thread->delay_next;
  260. thread->delay_prev = NULL;
  261. } else {
  262. osRtxInfo.thread.wait_list = thread->delay_next;
  263. }
  264. } else {
  265. if ((thread->delay_prev == NULL) && (osRtxInfo.thread.delay_list != thread)) {
  266. return;
  267. }
  268. if (thread->delay_next != NULL) {
  269. thread->delay_next->delay += thread->delay;
  270. thread->delay_next->delay_prev = thread->delay_prev;
  271. }
  272. if (thread->delay_prev != NULL) {
  273. thread->delay_prev->delay_next = thread->delay_next;
  274. thread->delay_prev = NULL;
  275. } else {
  276. osRtxInfo.thread.delay_list = thread->delay_next;
  277. }
  278. }
  279. }
  280. /// Process Thread Delay Tick (executed each System Tick).
  281. void osRtxThreadDelayTick (void) {
  282. os_thread_t *thread;
  283. thread = osRtxInfo.thread.delay_list;
  284. if (thread == NULL) {
  285. return;
  286. }
  287. thread->delay--;
  288. if (thread->delay == 0U) {
  289. do {
  290. switch (thread->state) {
  291. case osRtxThreadWaitingDelay:
  292. EvrRtxThreadDelayCompleted();
  293. break;
  294. case osRtxThreadWaitingThreadFlags:
  295. EvrRtxThreadFlagsWaitTimeout();
  296. break;
  297. case osRtxThreadWaitingEventFlags:
  298. EvrRtxEventFlagsWaitTimeout((osEventFlagsId_t)osRtxThreadListRoot(thread));
  299. break;
  300. case osRtxThreadWaitingMutex:
  301. EvrRtxMutexAcquireTimeout((osMutexId_t)osRtxThreadListRoot(thread));
  302. break;
  303. case osRtxThreadWaitingSemaphore:
  304. EvrRtxSemaphoreAcquireTimeout((osSemaphoreId_t)osRtxThreadListRoot(thread));
  305. break;
  306. case osRtxThreadWaitingMemoryPool:
  307. EvrRtxMemoryPoolAllocTimeout((osMemoryPoolId_t)osRtxThreadListRoot(thread));
  308. break;
  309. case osRtxThreadWaitingMessageGet:
  310. EvrRtxMessageQueueGetTimeout((osMessageQueueId_t)osRtxThreadListRoot(thread));
  311. break;
  312. case osRtxThreadWaitingMessagePut:
  313. EvrRtxMessageQueuePutTimeout((osMessageQueueId_t)osRtxThreadListRoot(thread));
  314. break;
  315. default:
  316. break;
  317. }
  318. EvrRtxThreadUnblocked(thread, (osRtxThreadRegPtr(thread))[0]);
  319. osRtxThreadListRemove(thread);
  320. osRtxThreadReadyPut(thread);
  321. thread = thread->delay_next;
  322. } while ((thread != NULL) && (thread->delay == 0U));
  323. if (thread != NULL) {
  324. thread->delay_prev = NULL;
  325. }
  326. osRtxInfo.thread.delay_list = thread;
  327. }
  328. }
  329. /// Get pointer to Thread registers (R0..R3)
  330. /// \param[in] thread thread object.
  331. /// \return pointer to registers R0-R3.
  332. uint32_t *osRtxThreadRegPtr (os_thread_t *thread) {
  333. return ((uint32_t *)(thread->sp + STACK_OFFSET_R0(thread->stack_frame)));
  334. }
  335. /// Block running Thread execution and register it as Ready to Run.
  336. /// \param[in] thread running thread object.
  337. void osRtxThreadBlock (os_thread_t *thread) {
  338. os_thread_t *prev, *next;
  339. int32_t priority;
  340. thread->state = osRtxThreadReady;
  341. priority = thread->priority;
  342. prev = (os_thread_t *)(uint32_t)&osRtxInfo.thread.ready;
  343. next = prev->thread_next;
  344. while ((next != NULL) && (next->priority > priority)) {
  345. prev = next;
  346. next = next->thread_next;
  347. }
  348. thread->thread_prev = prev;
  349. thread->thread_next = next;
  350. prev->thread_next = thread;
  351. if (next != NULL) {
  352. next->thread_prev = thread;
  353. }
  354. }
  355. /// Switch to specified Thread.
  356. /// \param[in] thread thread object.
  357. void osRtxThreadSwitch (os_thread_t *thread) {
  358. thread->state = osRtxThreadRunning;
  359. osRtxInfo.thread.run.next = thread;
  360. osRtxThreadStackCheck();
  361. EvrRtxThreadSwitch(thread);
  362. }
  363. /// Dispatch specified Thread or Ready Thread with Highest Priority.
  364. /// \param[in] thread thread object or NULL.
  365. void osRtxThreadDispatch (os_thread_t *thread) {
  366. uint8_t kernel_state;
  367. os_thread_t *thread_running;
  368. kernel_state = osRtxKernelGetState();
  369. thread_running = osRtxThreadGetRunning();
  370. #if (__ARM_ARCH_7A__ != 0U)
  371. // On Cortex-A PendSV_Handler is executed before final context switch.
  372. if ((thread_running != NULL) && (thread_running->state != osRtxThreadRunning)) {
  373. thread_running = osRtxInfo.thread.run.next;
  374. }
  375. #endif
  376. if (thread == NULL) {
  377. thread = osRtxInfo.thread.ready.thread_list;
  378. if ((kernel_state == osRtxKernelRunning) &&
  379. (thread_running != NULL) && (thread != NULL) &&
  380. (thread->priority > thread_running->priority)) {
  381. // Preempt running Thread
  382. osRtxThreadListRemove(thread);
  383. osRtxThreadBlock(thread_running);
  384. osRtxThreadSwitch(thread);
  385. }
  386. } else {
  387. if ((kernel_state == osRtxKernelRunning) &&
  388. (thread_running != NULL) &&
  389. (thread->priority > thread_running->priority)) {
  390. // Preempt running Thread
  391. osRtxThreadBlock(thread_running);
  392. osRtxThreadSwitch(thread);
  393. } else {
  394. // Put Thread into Ready list
  395. osRtxThreadReadyPut(thread);
  396. }
  397. }
  398. }
  399. /// Exit Thread wait state.
  400. /// \param[in] thread thread object.
  401. /// \param[in] ret_val return value.
  402. /// \param[in] dispatch dispatch flag.
  403. void osRtxThreadWaitExit (os_thread_t *thread, uint32_t ret_val, bool dispatch) {
  404. uint32_t *reg;
  405. EvrRtxThreadUnblocked(thread, ret_val);
  406. reg = osRtxThreadRegPtr(thread);
  407. reg[0] = ret_val;
  408. osRtxThreadDelayRemove(thread);
  409. if (dispatch) {
  410. osRtxThreadDispatch(thread);
  411. } else {
  412. osRtxThreadReadyPut(thread);
  413. }
  414. }
  415. /// Enter Thread wait state.
  416. /// \param[in] state new thread state.
  417. /// \param[in] timeout timeout.
  418. /// \return true - success, false - failure.
  419. bool osRtxThreadWaitEnter (uint8_t state, uint32_t timeout) {
  420. os_thread_t *thread;
  421. thread = osRtxThreadGetRunning();
  422. if (thread == NULL) {
  423. return false;
  424. }
  425. if (osRtxKernelGetState() != osRtxKernelRunning) {
  426. osRtxThreadListRemove(thread);
  427. return false;
  428. }
  429. if (osRtxInfo.thread.ready.thread_list == NULL) {
  430. return false;
  431. }
  432. EvrRtxThreadBlocked(thread, timeout);
  433. thread->state = state;
  434. osRtxThreadDelayInsert(thread, timeout);
  435. thread = osRtxThreadListGet(&osRtxInfo.thread.ready);
  436. osRtxThreadSwitch(thread);
  437. return true;
  438. }
  439. /// Check current running Thread Stack.
  440. __WEAK void osRtxThreadStackCheck (void) {
  441. os_thread_t *thread;
  442. thread = osRtxThreadGetRunning();
  443. if (thread != NULL) {
  444. if ((thread->sp <= (uint32_t)thread->stack_mem) ||
  445. (*((uint32_t *)thread->stack_mem) != osRtxStackMagicWord)) {
  446. osRtxErrorNotify(osRtxErrorStackUnderflow, thread);
  447. }
  448. }
  449. }
  450. /// Thread post ISR processing.
  451. /// \param[in] thread thread object.
  452. void osRtxThreadPostProcess (os_thread_t *thread) {
  453. uint32_t thread_flags;
  454. if ((thread->state == osRtxThreadInactive) ||
  455. (thread->state == osRtxThreadTerminated)) {
  456. return;
  457. }
  458. // Check if Thread is waiting for Thread Flags
  459. if (thread->state == osRtxThreadWaitingThreadFlags) {
  460. thread_flags = ThreadFlagsCheck(thread, thread->wait_flags, thread->flags_options);
  461. if (thread_flags != 0U) {
  462. osRtxThreadWaitExit(thread, thread_flags, false);
  463. EvrRtxThreadFlagsWaitCompleted(thread->wait_flags, thread->flags_options, thread_flags);
  464. }
  465. }
  466. }
  467. // ==== Service Calls ====
  468. // Service Calls definitions
  469. SVC0_3M(ThreadNew, osThreadId_t, osThreadFunc_t, void *, const osThreadAttr_t *)
  470. SVC0_1 (ThreadGetName, const char *, osThreadId_t)
  471. SVC0_0 (ThreadGetId, osThreadId_t)
  472. SVC0_1 (ThreadGetState, osThreadState_t, osThreadId_t)
  473. SVC0_1 (ThreadGetStackSize, uint32_t, osThreadId_t)
  474. SVC0_1 (ThreadGetStackSpace, uint32_t, osThreadId_t)
  475. SVC0_2 (ThreadSetPriority, osStatus_t, osThreadId_t, osPriority_t)
  476. SVC0_1 (ThreadGetPriority, osPriority_t, osThreadId_t)
  477. SVC0_0 (ThreadYield, osStatus_t)
  478. SVC0_1 (ThreadSuspend, osStatus_t, osThreadId_t)
  479. SVC0_1 (ThreadResume, osStatus_t, osThreadId_t)
  480. SVC0_1 (ThreadDetach, osStatus_t, osThreadId_t)
  481. SVC0_1 (ThreadJoin, osStatus_t, osThreadId_t)
  482. SVC0_0N(ThreadExit, void)
  483. SVC0_1 (ThreadTerminate, osStatus_t, osThreadId_t)
  484. SVC0_0 (ThreadGetCount, uint32_t)
  485. SVC0_2 (ThreadEnumerate, uint32_t, osThreadId_t *, uint32_t)
  486. SVC0_2 (ThreadFlagsSet, uint32_t, osThreadId_t, uint32_t)
  487. SVC0_1 (ThreadFlagsClear, uint32_t, uint32_t)
  488. SVC0_0 (ThreadFlagsGet, uint32_t)
  489. SVC0_3 (ThreadFlagsWait, uint32_t, uint32_t, uint32_t, uint32_t)
  490. /// Create a thread and add it to Active Threads.
  491. /// \note API identical to osThreadNew
  492. osThreadId_t svcRtxThreadNew (osThreadFunc_t func, void *argument, const osThreadAttr_t *attr) {
  493. os_thread_t *thread;
  494. uint32_t attr_bits;
  495. void *stack_mem;
  496. uint32_t stack_size;
  497. osPriority_t priority;
  498. uint8_t flags;
  499. const char *name;
  500. uint32_t *ptr;
  501. uint32_t n;
  502. #if (__DOMAIN_NS == 1U)
  503. TZ_ModuleId_t tz_module;
  504. TZ_MemoryId_t tz_memory;
  505. #endif
  506. // Check parameters
  507. if (func == NULL) {
  508. EvrRtxThreadError(NULL, osErrorParameter);
  509. return NULL;
  510. }
  511. // Process attributes
  512. if (attr != NULL) {
  513. name = attr->name;
  514. attr_bits = attr->attr_bits;
  515. thread = attr->cb_mem;
  516. stack_mem = attr->stack_mem;
  517. stack_size = attr->stack_size;
  518. priority = attr->priority;
  519. #if (__DOMAIN_NS == 1U)
  520. tz_module = attr->tz_module;
  521. #endif
  522. if (thread != NULL) {
  523. if (((uint32_t)thread & 3U) || (attr->cb_size < sizeof(os_thread_t))) {
  524. EvrRtxThreadError(NULL, osRtxErrorInvalidControlBlock);
  525. return NULL;
  526. }
  527. } else {
  528. if (attr->cb_size != 0U) {
  529. EvrRtxThreadError(NULL, osRtxErrorInvalidControlBlock);
  530. return NULL;
  531. }
  532. }
  533. if (stack_mem != NULL) {
  534. if (((uint32_t)stack_mem & 7U) || (stack_size == 0U)) {
  535. EvrRtxThreadError(NULL, osRtxErrorInvalidThreadStack);
  536. return NULL;
  537. }
  538. }
  539. if (priority == osPriorityNone) {
  540. priority = osPriorityNormal;
  541. } else {
  542. if ((priority < osPriorityIdle) || (priority > osPriorityISR)) {
  543. EvrRtxThreadError(NULL, osRtxErrorInvalidPriority);
  544. return NULL;
  545. }
  546. }
  547. } else {
  548. name = NULL;
  549. attr_bits = 0U;
  550. thread = NULL;
  551. stack_mem = NULL;
  552. stack_size = 0U;
  553. priority = osPriorityNormal;
  554. #if (__DOMAIN_NS == 1U)
  555. tz_module = 0U;
  556. #endif
  557. }
  558. // Check stack size
  559. if ((stack_size != 0U) && ((stack_size & 7U) || (stack_size < (64U + 8U)))) {
  560. EvrRtxThreadError(NULL, osRtxErrorInvalidThreadStack);
  561. return NULL;
  562. }
  563. // Allocate object memory if not provided
  564. if (thread == NULL) {
  565. if (osRtxInfo.mpi.thread != NULL) {
  566. thread = osRtxMemoryPoolAlloc(osRtxInfo.mpi.thread);
  567. } else {
  568. thread = osRtxMemoryAlloc(osRtxInfo.mem.common, sizeof(os_thread_t), 1U);
  569. }
  570. if (thread == NULL) {
  571. EvrRtxThreadError(NULL, osErrorNoMemory);
  572. return NULL;
  573. }
  574. flags = osRtxFlagSystemObject;
  575. } else {
  576. flags = 0U;
  577. }
  578. // Allocate stack memory if not provided
  579. if (stack_mem == NULL) {
  580. if (stack_size == 0U) {
  581. stack_size = osRtxConfig.thread_stack_size;
  582. if (osRtxInfo.mpi.stack != NULL) {
  583. stack_mem = osRtxMemoryPoolAlloc(osRtxInfo.mpi.stack);
  584. if (stack_mem != NULL) {
  585. flags |= osRtxThreadFlagDefStack;
  586. }
  587. } else {
  588. stack_mem = osRtxMemoryAlloc(osRtxInfo.mem.stack, stack_size, 0U);
  589. }
  590. } else {
  591. stack_mem = osRtxMemoryAlloc(osRtxInfo.mem.stack, stack_size, 0U);
  592. }
  593. if (stack_mem == NULL) {
  594. EvrRtxThreadError(NULL, osErrorNoMemory);
  595. if (flags & osRtxFlagSystemObject) {
  596. if (osRtxInfo.mpi.thread != NULL) {
  597. osRtxMemoryPoolFree(osRtxInfo.mpi.thread, thread);
  598. } else {
  599. osRtxMemoryFree(osRtxInfo.mem.common, thread);
  600. }
  601. }
  602. return NULL;
  603. }
  604. flags |= osRtxFlagSystemMemory;
  605. }
  606. #if (__DOMAIN_NS == 1U)
  607. // Allocate secure process stack
  608. if (tz_module != 0U) {
  609. tz_memory = TZ_AllocModuleContext_S(tz_module);
  610. if (tz_memory == 0U) {
  611. EvrRtxThreadError(NULL, osRtxErrorTZ_AllocContext_S);
  612. if (flags & osRtxFlagSystemMemory) {
  613. if (flags & osRtxThreadFlagDefStack) {
  614. osRtxMemoryPoolFree(osRtxInfo.mpi.stack, thread->stack_mem);
  615. } else {
  616. osRtxMemoryFree(osRtxInfo.mem.stack, thread->stack_mem);
  617. }
  618. }
  619. if (flags & osRtxFlagSystemObject) {
  620. if (osRtxInfo.mpi.thread != NULL) {
  621. osRtxMemoryPoolFree(osRtxInfo.mpi.thread, thread);
  622. } else {
  623. osRtxMemoryFree(osRtxInfo.mem.common, thread);
  624. }
  625. }
  626. return NULL;
  627. }
  628. } else {
  629. tz_memory = 0U;
  630. }
  631. #endif
  632. // Initialize control block
  633. thread->id = osRtxIdThread;
  634. thread->state = osRtxThreadReady;
  635. thread->flags = flags;
  636. thread->attr = (uint8_t)attr_bits;
  637. thread->name = name;
  638. thread->thread_next = NULL;
  639. thread->thread_prev = NULL;
  640. thread->delay_next = NULL;
  641. thread->delay_prev = NULL;
  642. thread->thread_join = NULL;
  643. thread->delay = 0U;
  644. thread->priority = (int8_t)priority;
  645. thread->priority_base = (int8_t)priority;
  646. thread->stack_frame = STACK_FRAME_INIT;
  647. thread->flags_options = 0U;
  648. thread->wait_flags = 0U;
  649. thread->thread_flags = 0U;
  650. thread->mutex_list = NULL;
  651. thread->stack_mem = stack_mem;
  652. thread->stack_size = stack_size;
  653. thread->sp = (uint32_t)stack_mem + stack_size - 64U;
  654. thread->thread_addr = (uint32_t)func;
  655. #if (__DOMAIN_NS == 1U)
  656. thread->tz_memory = tz_memory;
  657. #endif
  658. // Initialize stack
  659. ptr = (uint32_t *)stack_mem;
  660. *ptr++ = osRtxStackMagicWord;
  661. if (osRtxConfig.flags & osRtxConfigStackWatermark) {
  662. for (n = (stack_size/4U) - (16U + 1U); n; n--) {
  663. *ptr++ = osRtxStackFillPattern;
  664. }
  665. } else {
  666. ptr = (uint32_t *)thread->sp;
  667. }
  668. for (n = 13U; n; n--) {
  669. *ptr++ = 0U; // R4..R11, R0..R3, R12
  670. }
  671. *ptr++ = (uint32_t)osThreadExit; // LR
  672. *ptr++ = (uint32_t)func; // PC
  673. *ptr++ = xPSR_INIT(
  674. (osRtxConfig.flags & osRtxConfigPrivilegedMode),
  675. ((uint32_t)func & 1U)
  676. ); // xPSR
  677. *(ptr-8) = (uint32_t)argument; // R0
  678. // Register post ISR processing function
  679. osRtxInfo.post_process.thread = osRtxThreadPostProcess;
  680. EvrRtxThreadCreated(thread);
  681. osRtxThreadDispatch(thread);
  682. return thread;
  683. }
  684. /// Get name of a thread.
  685. /// \note API identical to osThreadGetName
  686. const char *svcRtxThreadGetName (osThreadId_t thread_id) {
  687. os_thread_t *thread = (os_thread_t *)thread_id;
  688. // Check parameters
  689. if ((thread == NULL) || (thread->id != osRtxIdThread)) {
  690. EvrRtxThreadGetName(thread, NULL);
  691. return NULL;
  692. }
  693. // Check object state
  694. if (thread->state == osRtxObjectInactive) {
  695. EvrRtxThreadGetName(thread, NULL);
  696. return NULL;
  697. }
  698. EvrRtxThreadGetName(thread, thread->name);
  699. return thread->name;
  700. }
  701. /// Return the thread ID of the current running thread.
  702. /// \note API identical to osThreadGetId
  703. osThreadId_t svcRtxThreadGetId (void) {
  704. os_thread_t *thread;
  705. thread = osRtxThreadGetRunning();
  706. EvrRtxThreadGetId(thread);
  707. return thread;
  708. }
  709. /// Get current thread state of a thread.
  710. /// \note API identical to osThreadGetState
  711. osThreadState_t svcRtxThreadGetState (osThreadId_t thread_id) {
  712. os_thread_t *thread = (os_thread_t *)thread_id;
  713. // Check parameters
  714. if ((thread == NULL) || (thread->id != osRtxIdThread)) {
  715. EvrRtxThreadGetState(thread, osThreadError);
  716. return osThreadError;
  717. }
  718. EvrRtxThreadGetState(thread, (osThreadState_t)(thread->state & osRtxThreadStateMask));
  719. return ((osThreadState_t)(thread->state & osRtxThreadStateMask));
  720. }
  721. /// Get stack size of a thread.
  722. /// \note API identical to osThreadGetStackSize
  723. uint32_t svcRtxThreadGetStackSize (osThreadId_t thread_id) {
  724. os_thread_t *thread = (os_thread_t *)thread_id;
  725. // Check parameters
  726. if ((thread == NULL) || (thread->id != osRtxIdThread)) {
  727. EvrRtxThreadGetStackSize(thread, 0U);
  728. return 0U;
  729. }
  730. // Check object state
  731. if (thread->state == osRtxObjectInactive) {
  732. EvrRtxThreadGetStackSize(thread, 0U);
  733. return 0U;
  734. }
  735. EvrRtxThreadGetStackSize(thread, thread->stack_size);
  736. return thread->stack_size;
  737. }
  738. /// Get available stack space of a thread based on stack watermark recording during execution.
  739. /// \note API identical to osThreadGetStackSpace
  740. uint32_t svcRtxThreadGetStackSpace (osThreadId_t thread_id) {
  741. os_thread_t *thread = (os_thread_t *)thread_id;
  742. uint32_t *stack;
  743. uint32_t space;
  744. // Check parameters
  745. if ((thread == NULL) || (thread->id != osRtxIdThread)) {
  746. EvrRtxThreadGetStackSpace(thread, 0U);
  747. return 0U;
  748. }
  749. // Check object state
  750. if (thread->state == osRtxObjectInactive) {
  751. EvrRtxThreadGetStackSpace(thread, 0U);
  752. return 0U;
  753. }
  754. if ((osRtxConfig.flags & osRtxConfigStackWatermark) == 0U) {
  755. EvrRtxThreadGetStackSpace(thread, 0U);
  756. return 0U;
  757. }
  758. stack = thread->stack_mem;
  759. if (*stack++ != osRtxStackMagicWord) {
  760. EvrRtxThreadGetStackSpace(thread, 0U);
  761. return 0U;
  762. }
  763. for (space = 4U; space < thread->stack_size; space += 4U) {
  764. if (*stack++ != osRtxStackFillPattern) {
  765. break;
  766. }
  767. }
  768. EvrRtxThreadGetStackSpace(thread, space);
  769. return space;
  770. }
  771. /// Change priority of a thread.
  772. /// \note API identical to osThreadSetPriority
  773. osStatus_t svcRtxThreadSetPriority (osThreadId_t thread_id, osPriority_t priority) {
  774. os_thread_t *thread = (os_thread_t *)thread_id;
  775. // Check parameters
  776. if ((thread == NULL) || (thread->id != osRtxIdThread) ||
  777. (priority < osPriorityIdle) || (priority > osPriorityISR)) {
  778. EvrRtxThreadError(thread, osErrorParameter);
  779. return osErrorParameter;
  780. }
  781. // Check object state
  782. if ((thread->state == osRtxThreadInactive) ||
  783. (thread->state == osRtxThreadTerminated)) {
  784. EvrRtxThreadError(thread, osErrorResource);
  785. return osErrorResource;
  786. }
  787. if (thread->priority != (int8_t)priority) {
  788. thread->priority = (int8_t)priority;
  789. thread->priority_base = (int8_t)priority;
  790. osRtxThreadListSort(thread);
  791. osRtxThreadDispatch(NULL);
  792. }
  793. return osOK;
  794. }
  795. /// Get current priority of a thread.
  796. /// \note API identical to osThreadGetPriority
  797. osPriority_t svcRtxThreadGetPriority (osThreadId_t thread_id) {
  798. os_thread_t *thread = (os_thread_t *)thread_id;
  799. // Check parameters
  800. if ((thread == NULL) || (thread->id != osRtxIdThread)) {
  801. EvrRtxThreadGetPriority(thread, osPriorityError);
  802. return osPriorityError;
  803. }
  804. // Check object state
  805. if ((thread->state == osRtxThreadInactive) ||
  806. (thread->state == osRtxThreadTerminated)) {
  807. EvrRtxThreadGetPriority(thread, osPriorityError);
  808. return osPriorityError;
  809. }
  810. EvrRtxThreadGetPriority(thread, (osPriority_t)thread->priority);
  811. return ((osPriority_t)thread->priority);
  812. }
  813. /// Pass control to next thread that is in state READY.
  814. /// \note API identical to osThreadYield
  815. osStatus_t svcRtxThreadYield (void) {
  816. uint8_t kernel_state;
  817. os_thread_t *thread_running;
  818. os_thread_t *thread_ready;
  819. kernel_state = osRtxKernelGetState();
  820. thread_running = osRtxThreadGetRunning();
  821. thread_ready = osRtxInfo.thread.ready.thread_list;
  822. if ((kernel_state == osRtxKernelRunning) &&
  823. (thread_ready != NULL) && (thread_running != NULL) &&
  824. (thread_ready->priority == thread_running->priority)) {
  825. osRtxThreadListRemove(thread_ready);
  826. osRtxThreadReadyPut(thread_running);
  827. osRtxThreadSwitch(thread_ready);
  828. }
  829. return osOK;
  830. }
  831. /// Suspend execution of a thread.
  832. /// \note API identical to osThreadSuspend
  833. osStatus_t svcRtxThreadSuspend (osThreadId_t thread_id) {
  834. os_thread_t *thread = (os_thread_t *)thread_id;
  835. // Check parameters
  836. if ((thread == NULL) || (thread->id != osRtxIdThread)) {
  837. EvrRtxThreadError(thread, osErrorParameter);
  838. return osErrorParameter;
  839. }
  840. // Check object state
  841. switch (thread->state & osRtxThreadStateMask) {
  842. case osRtxThreadRunning:
  843. if ((osRtxKernelGetState() != osRtxKernelRunning) ||
  844. (osRtxInfo.thread.ready.thread_list == NULL)) {
  845. EvrRtxThreadError(thread, osErrorResource);
  846. return osErrorResource;
  847. }
  848. break;
  849. case osRtxThreadReady:
  850. osRtxThreadListRemove(thread);
  851. break;
  852. case osRtxThreadBlocked:
  853. osRtxThreadListRemove(thread);
  854. osRtxThreadDelayRemove(thread);
  855. break;
  856. case osRtxThreadInactive:
  857. case osRtxThreadTerminated:
  858. default:
  859. EvrRtxThreadError(thread, osErrorResource);
  860. return osErrorResource;
  861. }
  862. EvrRtxThreadSuspended(thread);
  863. if (thread->state == osRtxThreadRunning) {
  864. osRtxThreadSwitch(osRtxThreadListGet(&osRtxInfo.thread.ready));
  865. }
  866. // Update Thread State and put it into Delay list
  867. thread->state = osRtxThreadBlocked;
  868. thread->thread_prev = NULL;
  869. thread->thread_next = NULL;
  870. osRtxThreadDelayInsert(thread, osWaitForever);
  871. return osOK;
  872. }
  873. /// Resume execution of a thread.
  874. /// \note API identical to osThreadResume
  875. osStatus_t svcRtxThreadResume (osThreadId_t thread_id) {
  876. os_thread_t *thread = (os_thread_t *)thread_id;
  877. // Check parameters
  878. if ((thread == NULL) || (thread->id != osRtxIdThread)) {
  879. EvrRtxThreadError(thread, osErrorParameter);
  880. return osErrorParameter;
  881. }
  882. // Check object state
  883. if ((thread->state & osRtxThreadStateMask) != osRtxThreadBlocked) {
  884. EvrRtxThreadError(thread, osErrorResource);
  885. return osErrorResource;
  886. }
  887. EvrRtxThreadResumed(thread);
  888. // Wakeup Thread
  889. osRtxThreadListRemove(thread);
  890. osRtxThreadDelayRemove(thread);
  891. osRtxThreadDispatch(thread);
  892. return osOK;
  893. }
  894. /// Free Thread resources.
  895. /// \param[in] thread thread object.
  896. static void osRtxThreadFree (os_thread_t *thread) {
  897. // Mark object as inactive
  898. thread->state = osRtxThreadInactive;
  899. #if (__DOMAIN_NS == 1U)
  900. // Free secure process stack
  901. if (thread->tz_memory != 0U) {
  902. TZ_FreeModuleContext_S(thread->tz_memory);
  903. }
  904. #endif
  905. // Free stack memory
  906. if (thread->flags & osRtxFlagSystemMemory) {
  907. if (thread->flags & osRtxThreadFlagDefStack) {
  908. osRtxMemoryPoolFree(osRtxInfo.mpi.stack, thread->stack_mem);
  909. } else {
  910. osRtxMemoryFree(osRtxInfo.mem.stack, thread->stack_mem);
  911. }
  912. }
  913. // Free object memory
  914. if (thread->flags & osRtxFlagSystemObject) {
  915. if (osRtxInfo.mpi.thread != NULL) {
  916. osRtxMemoryPoolFree(osRtxInfo.mpi.thread, thread);
  917. } else {
  918. osRtxMemoryFree(osRtxInfo.mem.common, thread);
  919. }
  920. }
  921. }
  922. /// Detach a thread (thread storage can be reclaimed when thread terminates).
  923. /// \note API identical to osThreadDetach
  924. osStatus_t svcRtxThreadDetach (osThreadId_t thread_id) {
  925. os_thread_t *thread = (os_thread_t *)thread_id;
  926. // Check parameters
  927. if ((thread == NULL) || (thread->id != osRtxIdThread)) {
  928. EvrRtxThreadError(thread, osErrorParameter);
  929. return osErrorParameter;
  930. }
  931. // Check object attributes
  932. if ((thread->attr & osThreadJoinable) == 0U) {
  933. EvrRtxThreadError(thread, osRtxErrorThreadNotJoinable);
  934. return osErrorResource;
  935. }
  936. // Check object state
  937. if (thread->state == osRtxThreadInactive) {
  938. EvrRtxThreadError(thread, osErrorResource);
  939. return osErrorResource;
  940. }
  941. if (thread->state == osRtxThreadTerminated) {
  942. osRtxThreadListUnlink(&osRtxInfo.thread.terminate_list, thread);
  943. osRtxThreadFree(thread);
  944. } else {
  945. thread->attr &= ~osThreadJoinable;
  946. }
  947. EvrRtxThreadDetached(thread);
  948. return osOK;
  949. }
  950. /// Wait for specified thread to terminate.
  951. /// \note API identical to osThreadJoin
  952. osStatus_t svcRtxThreadJoin (osThreadId_t thread_id) {
  953. os_thread_t *thread = (os_thread_t *)thread_id;
  954. // Check parameters
  955. if ((thread == NULL) || (thread->id != osRtxIdThread)) {
  956. EvrRtxThreadError(thread, osErrorParameter);
  957. return osErrorParameter;
  958. }
  959. // Check object attributes
  960. if ((thread->attr & osThreadJoinable) == 0U) {
  961. EvrRtxThreadError(thread, osRtxErrorThreadNotJoinable);
  962. return osErrorResource;
  963. }
  964. // Check object state
  965. if ((thread->state == osRtxThreadInactive) ||
  966. (thread->state == osRtxThreadRunning)) {
  967. EvrRtxThreadError(thread, osErrorResource);
  968. return osErrorResource;
  969. }
  970. if (thread->state == osRtxThreadTerminated) {
  971. osRtxThreadListUnlink(&osRtxInfo.thread.terminate_list, thread);
  972. osRtxThreadFree(thread);
  973. } else {
  974. EvrRtxThreadJoinPending(thread);
  975. // Suspend current Thread
  976. if (osRtxThreadWaitEnter(osRtxThreadWaitingJoin, osWaitForever)) {
  977. thread->thread_join = osRtxThreadGetRunning();
  978. }
  979. return osErrorResource;
  980. }
  981. EvrRtxThreadJoined(thread);
  982. return osOK;
  983. }
  984. /// Terminate execution of current running thread.
  985. /// \note API identical to osThreadExit
  986. void svcRtxThreadExit (void) {
  987. os_thread_t *thread;
  988. thread = osRtxThreadGetRunning();
  989. if (thread == NULL) {
  990. return;
  991. }
  992. // Release owned Mutexes
  993. osRtxMutexOwnerRelease(thread->mutex_list);
  994. // Wakeup Thread waiting to Join
  995. if (thread->thread_join != NULL) {
  996. osRtxThreadWaitExit(thread->thread_join, (uint32_t)osOK, false);
  997. EvrRtxThreadJoined(thread->thread_join);
  998. }
  999. // Switch to next Ready Thread
  1000. if ((osRtxKernelGetState() != osRtxKernelRunning) ||
  1001. (osRtxInfo.thread.ready.thread_list == NULL)) {
  1002. return;
  1003. }
  1004. thread->sp = __get_PSP();
  1005. osRtxThreadSwitch(osRtxThreadListGet(&osRtxInfo.thread.ready));
  1006. osRtxThreadSetRunning(NULL);
  1007. if (((thread->attr & osThreadJoinable) == 0U) || (thread->thread_join != NULL)) {
  1008. osRtxThreadFree(thread);
  1009. } else {
  1010. // Update Thread State and put it into Terminate Thread list
  1011. thread->state = osRtxThreadTerminated;
  1012. thread->thread_prev = NULL;
  1013. thread->thread_next = osRtxInfo.thread.terminate_list;
  1014. if (osRtxInfo.thread.terminate_list != NULL) {
  1015. osRtxInfo.thread.terminate_list->thread_prev = thread;
  1016. }
  1017. osRtxInfo.thread.terminate_list = thread;
  1018. }
  1019. EvrRtxThreadDestroyed(thread);
  1020. }
  1021. /// Terminate execution of a thread.
  1022. /// \note API identical to osThreadTerminate
  1023. osStatus_t svcRtxThreadTerminate (osThreadId_t thread_id) {
  1024. os_thread_t *thread = (os_thread_t *)thread_id;
  1025. // Check parameters
  1026. if ((thread == NULL) || (thread->id != osRtxIdThread)) {
  1027. EvrRtxThreadError(thread, osErrorParameter);
  1028. return osErrorParameter;
  1029. }
  1030. // Check object state
  1031. switch (thread->state & osRtxThreadStateMask) {
  1032. case osRtxThreadRunning:
  1033. break;
  1034. case osRtxThreadReady:
  1035. osRtxThreadListRemove(thread);
  1036. break;
  1037. case osRtxThreadBlocked:
  1038. osRtxThreadListRemove(thread);
  1039. osRtxThreadDelayRemove(thread);
  1040. break;
  1041. case osRtxThreadInactive:
  1042. case osRtxThreadTerminated:
  1043. default:
  1044. EvrRtxThreadError(thread, osErrorResource);
  1045. return osErrorResource;
  1046. }
  1047. // Release owned Mutexes
  1048. osRtxMutexOwnerRelease(thread->mutex_list);
  1049. // Wakeup Thread waiting to Join
  1050. if (thread->thread_join != NULL) {
  1051. osRtxThreadWaitExit(thread->thread_join, (uint32_t)osOK, false);
  1052. EvrRtxThreadJoined(thread->thread_join);
  1053. }
  1054. // Switch to next Ready Thread when terminating running Thread
  1055. if (thread->state == osRtxThreadRunning) {
  1056. if ((osRtxKernelGetState() != osRtxKernelRunning) ||
  1057. (osRtxInfo.thread.ready.thread_list == NULL)) {
  1058. EvrRtxThreadError(thread, osErrorResource);
  1059. return osErrorResource;
  1060. }
  1061. thread->sp = __get_PSP();
  1062. osRtxThreadSwitch(osRtxThreadListGet(&osRtxInfo.thread.ready));
  1063. osRtxThreadSetRunning(NULL);
  1064. } else {
  1065. osRtxThreadDispatch(NULL);
  1066. }
  1067. if (((thread->attr & osThreadJoinable) == 0U) || (thread->thread_join != NULL)) {
  1068. osRtxThreadFree(thread);
  1069. } else {
  1070. // Update Thread State and put it into Terminate Thread list
  1071. thread->state = osRtxThreadTerminated;
  1072. thread->thread_prev = NULL;
  1073. thread->thread_next = osRtxInfo.thread.terminate_list;
  1074. if (osRtxInfo.thread.terminate_list != NULL) {
  1075. osRtxInfo.thread.terminate_list->thread_prev = thread;
  1076. }
  1077. osRtxInfo.thread.terminate_list = thread;
  1078. }
  1079. EvrRtxThreadDestroyed(thread);
  1080. return osOK;
  1081. }
  1082. /// Get number of active threads.
  1083. /// \note API identical to osThreadGetCount
  1084. uint32_t svcRtxThreadGetCount (void) {
  1085. os_thread_t *thread;
  1086. uint32_t count;
  1087. // Running Thread
  1088. count = 1U;
  1089. // Ready List
  1090. for (thread = osRtxInfo.thread.ready.thread_list;
  1091. (thread != NULL); thread = thread->thread_next, count++) {};
  1092. // Delay List
  1093. for (thread = osRtxInfo.thread.delay_list;
  1094. (thread != NULL); thread = thread->delay_next, count++) {};
  1095. // Wait List
  1096. for (thread = osRtxInfo.thread.wait_list;
  1097. (thread != NULL); thread = thread->delay_next, count++) {};
  1098. EvrRtxThreadGetCount(count);
  1099. return count;
  1100. }
  1101. /// Enumerate active threads.
  1102. /// \note API identical to osThreadEnumerate
  1103. uint32_t svcRtxThreadEnumerate (osThreadId_t *thread_array, uint32_t array_items) {
  1104. os_thread_t *thread;
  1105. uint32_t count;
  1106. // Check parameters
  1107. if ((thread_array == NULL) || (array_items == 0U)) {
  1108. EvrRtxThreadEnumerate(thread_array, array_items, 0U);
  1109. return 0U;
  1110. }
  1111. // Running Thread
  1112. *thread_array++ = osRtxThreadGetRunning();
  1113. count = 1U;
  1114. // Ready List
  1115. for (thread = osRtxInfo.thread.ready.thread_list;
  1116. (thread != NULL) && (count < array_items); thread = thread->thread_next, count++) {
  1117. *thread_array++ = thread;
  1118. }
  1119. // Delay List
  1120. for (thread = osRtxInfo.thread.delay_list;
  1121. (thread != NULL) && (count < array_items); thread = thread->delay_next, count++) {
  1122. *thread_array++ = thread;
  1123. }
  1124. // Wait List
  1125. for (thread = osRtxInfo.thread.wait_list;
  1126. (thread != NULL) && (count < array_items); thread = thread->delay_next, count++) {
  1127. *thread_array++ = thread;
  1128. }
  1129. EvrRtxThreadEnumerate(thread_array - count, array_items, count);
  1130. return count;
  1131. }
  1132. /// Set the specified Thread Flags of a thread.
  1133. /// \note API identical to osThreadFlagsSet
  1134. uint32_t svcRtxThreadFlagsSet (osThreadId_t thread_id, uint32_t flags) {
  1135. os_thread_t *thread = (os_thread_t *)thread_id;
  1136. uint32_t thread_flags;
  1137. uint32_t thread_flags0;
  1138. // Check parameters
  1139. if ((thread == NULL) || (thread->id != osRtxIdThread) ||
  1140. (flags & ~((1U << osRtxThreadFlagsLimit) - 1U))) {
  1141. EvrRtxThreadError(thread, osErrorParameter);
  1142. return ((uint32_t)osErrorParameter);
  1143. }
  1144. // Check object state
  1145. if ((thread->state == osRtxThreadInactive) ||
  1146. (thread->state == osRtxThreadTerminated)) {
  1147. EvrRtxThreadError(thread, osErrorResource);
  1148. return ((uint32_t)osErrorResource);
  1149. }
  1150. // Set Thread Flags
  1151. thread_flags = ThreadFlagsSet(thread, flags);
  1152. // Check if Thread is waiting for Thread Flags
  1153. if (thread->state == osRtxThreadWaitingThreadFlags) {
  1154. thread_flags0 = ThreadFlagsCheck(thread, thread->wait_flags, thread->flags_options);
  1155. if (thread_flags0 != 0U) {
  1156. if ((thread->flags_options & osFlagsNoClear) == 0U) {
  1157. thread_flags = thread_flags0 & ~thread->wait_flags;
  1158. } else {
  1159. thread_flags = thread_flags0;
  1160. }
  1161. osRtxThreadWaitExit(thread, thread_flags0, true);
  1162. EvrRtxThreadFlagsWaitCompleted(thread->wait_flags, thread->flags_options, thread_flags0);
  1163. }
  1164. }
  1165. EvrRtxThreadFlagsSetDone(thread, thread_flags);
  1166. return thread_flags;
  1167. }
  1168. /// Clear the specified Thread Flags of current running thread.
  1169. /// \note API identical to osThreadFlagsClear
  1170. uint32_t svcRtxThreadFlagsClear (uint32_t flags) {
  1171. os_thread_t *thread;
  1172. uint32_t thread_flags;
  1173. thread = osRtxThreadGetRunning();
  1174. if (thread == NULL) {
  1175. EvrRtxThreadError(NULL, osRtxErrorKernelNotRunning);
  1176. return ((uint32_t)osError);
  1177. }
  1178. // Check parameters
  1179. if (flags & ~((1U << osRtxThreadFlagsLimit) - 1U)) {
  1180. EvrRtxThreadError(thread, osErrorParameter);
  1181. return ((uint32_t)osErrorParameter);
  1182. }
  1183. // Check object state
  1184. if ((thread->state == osRtxThreadInactive) ||
  1185. (thread->state == osRtxThreadTerminated)) {
  1186. EvrRtxThreadError(thread, osErrorResource);
  1187. return ((uint32_t)osErrorResource);
  1188. }
  1189. // Clear Thread Flags
  1190. thread_flags = ThreadFlagsClear(thread, flags);
  1191. EvrRtxThreadFlagsClearDone(thread_flags);
  1192. return thread_flags;
  1193. }
  1194. /// Get the current Thread Flags of current running thread.
  1195. /// \note API identical to osThreadFlagsGet
  1196. uint32_t svcRtxThreadFlagsGet (void) {
  1197. os_thread_t *thread;
  1198. thread = osRtxThreadGetRunning();
  1199. if (thread == NULL) {
  1200. EvrRtxThreadFlagsGet(0U);
  1201. return 0U;
  1202. }
  1203. // Check object state
  1204. if ((thread->state == osRtxThreadInactive) ||
  1205. (thread->state == osRtxThreadTerminated)) {
  1206. EvrRtxThreadFlagsGet(0U);
  1207. return 0U;
  1208. }
  1209. EvrRtxThreadFlagsGet(thread->thread_flags);
  1210. return thread->thread_flags;
  1211. }
  1212. /// Wait for one or more Thread Flags of the current running thread to become signaled.
  1213. /// \note API identical to osThreadFlagsWait
  1214. uint32_t svcRtxThreadFlagsWait (uint32_t flags, uint32_t options, uint32_t timeout) {
  1215. os_thread_t *thread;
  1216. uint32_t thread_flags;
  1217. thread = osRtxThreadGetRunning();
  1218. if (thread == NULL) {
  1219. EvrRtxThreadError(NULL, osRtxErrorKernelNotRunning);
  1220. return ((uint32_t)osError);
  1221. }
  1222. // Check parameters
  1223. if (flags & ~((1U << osRtxThreadFlagsLimit) - 1U)) {
  1224. EvrRtxThreadError(thread, osErrorParameter);
  1225. return ((uint32_t)osErrorParameter);
  1226. }
  1227. // Check Thread Flags
  1228. thread_flags = ThreadFlagsCheck(thread, flags, options);
  1229. if (thread_flags != 0U) {
  1230. EvrRtxThreadFlagsWaitCompleted(flags, options, thread_flags);
  1231. return thread_flags;
  1232. }
  1233. // Check if timeout is specified
  1234. if (timeout != 0U) {
  1235. // Store waiting flags and options
  1236. EvrRtxThreadFlagsWaitPending(flags, options, timeout);
  1237. thread->wait_flags = flags;
  1238. thread->flags_options = (uint8_t)options;
  1239. // Suspend current Thread
  1240. osRtxThreadWaitEnter(osRtxThreadWaitingThreadFlags, timeout);
  1241. return ((uint32_t)osErrorTimeout);
  1242. }
  1243. EvrRtxThreadFlagsWaitNotCompleted(flags, options);
  1244. return ((uint32_t)osErrorResource);
  1245. }
  1246. // ==== ISR Calls ====
  1247. /// Set the specified Thread Flags of a thread.
  1248. /// \note API identical to osThreadFlagsSet
  1249. __STATIC_INLINE
  1250. uint32_t isrRtxThreadFlagsSet (osThreadId_t thread_id, uint32_t flags) {
  1251. os_thread_t *thread = (os_thread_t *)thread_id;
  1252. uint32_t thread_flags;
  1253. // Check parameters
  1254. if ((thread == NULL) || (thread->id != osRtxIdThread) ||
  1255. (flags & ~((1U << osRtxThreadFlagsLimit) - 1U))) {
  1256. EvrRtxThreadError(thread, osErrorParameter);
  1257. return ((uint32_t)osErrorParameter);
  1258. }
  1259. // Check object state
  1260. if ((thread->state == osRtxThreadInactive) ||
  1261. (thread->state == osRtxThreadTerminated)) {
  1262. EvrRtxThreadError(thread, osErrorResource);
  1263. return ((uint32_t)osErrorResource);
  1264. }
  1265. // Set Thread Flags
  1266. thread_flags = ThreadFlagsSet(thread, flags);
  1267. // Register post ISR processing
  1268. osRtxPostProcess((os_object_t *)thread);
  1269. EvrRtxThreadFlagsSetDone(thread, thread_flags);
  1270. return thread_flags;
  1271. }
  1272. // ==== Public API ====
  1273. /// Create a thread and add it to Active Threads.
  1274. osThreadId_t osThreadNew (osThreadFunc_t func, void *argument, const osThreadAttr_t *attr) {
  1275. EvrRtxThreadNew(func, argument, attr);
  1276. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  1277. EvrRtxThreadError(NULL, osErrorISR);
  1278. return NULL;
  1279. }
  1280. return __svcThreadNew(func, argument, attr);
  1281. }
  1282. /// Get name of a thread.
  1283. const char *osThreadGetName (osThreadId_t thread_id) {
  1284. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  1285. EvrRtxThreadGetName(thread_id, NULL);
  1286. return NULL;
  1287. }
  1288. return __svcThreadGetName(thread_id);
  1289. }
  1290. /// Return the thread ID of the current running thread.
  1291. osThreadId_t osThreadGetId (void) {
  1292. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  1293. EvrRtxThreadGetId(NULL);
  1294. return NULL;
  1295. }
  1296. return __svcThreadGetId();
  1297. }
  1298. /// Get current thread state of a thread.
  1299. osThreadState_t osThreadGetState (osThreadId_t thread_id) {
  1300. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  1301. EvrRtxThreadGetState(thread_id, osThreadError);
  1302. return osThreadError;
  1303. }
  1304. return __svcThreadGetState(thread_id);
  1305. }
  1306. /// Get stack size of a thread.
  1307. uint32_t osThreadGetStackSize (osThreadId_t thread_id) {
  1308. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  1309. EvrRtxThreadGetStackSize(thread_id, 0U);
  1310. return 0U;
  1311. }
  1312. return __svcThreadGetStackSize(thread_id);
  1313. }
  1314. /// Get available stack space of a thread based on stack watermark recording during execution.
  1315. uint32_t osThreadGetStackSpace (osThreadId_t thread_id) {
  1316. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  1317. EvrRtxThreadGetStackSpace(thread_id, 0U);
  1318. return 0U;
  1319. }
  1320. return __svcThreadGetStackSpace(thread_id);
  1321. }
  1322. /// Change priority of a thread.
  1323. osStatus_t osThreadSetPriority (osThreadId_t thread_id, osPriority_t priority) {
  1324. EvrRtxThreadSetPriority(thread_id, priority);
  1325. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  1326. EvrRtxThreadError(thread_id, osErrorISR);
  1327. return osErrorISR;
  1328. }
  1329. return __svcThreadSetPriority(thread_id, priority);
  1330. }
  1331. /// Get current priority of a thread.
  1332. osPriority_t osThreadGetPriority (osThreadId_t thread_id) {
  1333. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  1334. EvrRtxThreadGetPriority(thread_id, osPriorityError);
  1335. return osPriorityError;
  1336. }
  1337. return __svcThreadGetPriority(thread_id);
  1338. }
  1339. /// Pass control to next thread that is in state READY.
  1340. osStatus_t osThreadYield (void) {
  1341. EvrRtxThreadYield();
  1342. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  1343. EvrRtxThreadError(NULL, osErrorISR);
  1344. return osErrorISR;
  1345. }
  1346. return __svcThreadYield();
  1347. }
  1348. /// Suspend execution of a thread.
  1349. osStatus_t osThreadSuspend (osThreadId_t thread_id) {
  1350. EvrRtxThreadSuspend(thread_id);
  1351. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  1352. EvrRtxThreadError(thread_id, osErrorISR);
  1353. return osErrorISR;
  1354. }
  1355. return __svcThreadSuspend(thread_id);
  1356. }
  1357. /// Resume execution of a thread.
  1358. osStatus_t osThreadResume (osThreadId_t thread_id) {
  1359. EvrRtxThreadResume(thread_id);
  1360. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  1361. EvrRtxThreadError(thread_id, osErrorISR);
  1362. return osErrorISR;
  1363. }
  1364. return __svcThreadResume(thread_id);
  1365. }
  1366. /// Detach a thread (thread storage can be reclaimed when thread terminates).
  1367. osStatus_t osThreadDetach (osThreadId_t thread_id) {
  1368. EvrRtxThreadDetach(thread_id);
  1369. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  1370. EvrRtxThreadError(thread_id, osErrorISR);
  1371. return osErrorISR;
  1372. }
  1373. return __svcThreadDetach(thread_id);
  1374. }
  1375. /// Wait for specified thread to terminate.
  1376. osStatus_t osThreadJoin (osThreadId_t thread_id) {
  1377. EvrRtxThreadJoin(thread_id);
  1378. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  1379. EvrRtxThreadError(thread_id, osErrorISR);
  1380. return osErrorISR;
  1381. }
  1382. return __svcThreadJoin(thread_id);
  1383. }
  1384. /// Terminate execution of current running thread.
  1385. __NO_RETURN void osThreadExit (void) {
  1386. EvrRtxThreadExit();
  1387. __svcThreadExit();
  1388. EvrRtxThreadError(NULL, osError);
  1389. for (;;);
  1390. }
  1391. /// Terminate execution of a thread.
  1392. osStatus_t osThreadTerminate (osThreadId_t thread_id) {
  1393. EvrRtxThreadTerminate(thread_id);
  1394. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  1395. EvrRtxThreadError(thread_id, osErrorISR);
  1396. return osErrorISR;
  1397. }
  1398. return __svcThreadTerminate(thread_id);
  1399. }
  1400. /// Get number of active threads.
  1401. uint32_t osThreadGetCount (void) {
  1402. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  1403. EvrRtxThreadGetCount(0U);
  1404. return 0U;
  1405. }
  1406. return __svcThreadGetCount();
  1407. }
  1408. /// Enumerate active threads.
  1409. uint32_t osThreadEnumerate (osThreadId_t *thread_array, uint32_t array_items) {
  1410. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  1411. EvrRtxThreadEnumerate(thread_array, array_items, 0U);
  1412. return 0U;
  1413. }
  1414. return __svcThreadEnumerate(thread_array, array_items);
  1415. }
  1416. /// Set the specified Thread Flags of a thread.
  1417. uint32_t osThreadFlagsSet (osThreadId_t thread_id, uint32_t flags) {
  1418. EvrRtxThreadFlagsSet(thread_id, flags);
  1419. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  1420. return isrRtxThreadFlagsSet(thread_id, flags);
  1421. } else {
  1422. return __svcThreadFlagsSet(thread_id, flags);
  1423. }
  1424. }
  1425. /// Clear the specified Thread Flags of current running thread.
  1426. uint32_t osThreadFlagsClear (uint32_t flags) {
  1427. EvrRtxThreadFlagsClear(flags);
  1428. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  1429. EvrRtxThreadError(NULL, osErrorISR);
  1430. return ((uint32_t)osErrorISR);
  1431. }
  1432. return __svcThreadFlagsClear(flags);
  1433. }
  1434. /// Get the current Thread Flags of current running thread.
  1435. uint32_t osThreadFlagsGet (void) {
  1436. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  1437. EvrRtxThreadFlagsGet(0U);
  1438. return 0U;
  1439. }
  1440. return __svcThreadFlagsGet();
  1441. }
  1442. /// Wait for one or more Thread Flags of the current running thread to become signaled.
  1443. uint32_t osThreadFlagsWait (uint32_t flags, uint32_t options, uint32_t timeout) {
  1444. EvrRtxThreadFlagsWait(flags, options, timeout);
  1445. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  1446. EvrRtxThreadError(NULL, osErrorISR);
  1447. return ((uint32_t)osErrorISR);
  1448. }
  1449. return __svcThreadFlagsWait(flags, options, timeout);
  1450. }