rtx_thread.c 56 KB

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