rtx_thread.c 48 KB

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