rtx_thread.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901
  1. /*
  2. * Copyright (c) 2013-2019 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. #if ((defined(OS_OBJ_MEM_USAGE) && (OS_OBJ_MEM_USAGE != 0)))
  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. }
  274. /// Process Thread Delay Tick (executed each System Tick).
  275. void osRtxThreadDelayTick (void) {
  276. os_thread_t *thread;
  277. os_object_t *object;
  278. thread = osRtxInfo.thread.delay_list;
  279. if (thread == NULL) {
  280. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  281. return;
  282. }
  283. thread->delay--;
  284. if (thread->delay == 0U) {
  285. do {
  286. switch (thread->state) {
  287. case osRtxThreadWaitingDelay:
  288. EvrRtxDelayCompleted(thread);
  289. break;
  290. case osRtxThreadWaitingThreadFlags:
  291. EvrRtxThreadFlagsWaitTimeout(thread);
  292. break;
  293. case osRtxThreadWaitingEventFlags:
  294. object = osRtxObject(osRtxThreadListRoot(thread));
  295. EvrRtxEventFlagsWaitTimeout(osRtxEventFlagsObject(object));
  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. object = osRtxObject(osRtxThreadListRoot(thread));
  304. EvrRtxSemaphoreAcquireTimeout(osRtxSemaphoreObject(object));
  305. break;
  306. case osRtxThreadWaitingMemoryPool:
  307. object = osRtxObject(osRtxThreadListRoot(thread));
  308. EvrRtxMemoryPoolAllocTimeout(osRtxMemoryPoolObject(object));
  309. break;
  310. case osRtxThreadWaitingMessageGet:
  311. object = osRtxObject(osRtxThreadListRoot(thread));
  312. EvrRtxMessageQueueGetTimeout(osRtxMessageQueueObject(object));
  313. break;
  314. case osRtxThreadWaitingMessagePut:
  315. object = osRtxObject(osRtxThreadListRoot(thread));
  316. EvrRtxMessageQueuePutTimeout(osRtxMessageQueueObject(object));
  317. break;
  318. default:
  319. // Invalid
  320. break;
  321. }
  322. EvrRtxThreadUnblocked(thread, (osRtxThreadRegPtr(thread))[0]);
  323. osRtxThreadListRemove(thread);
  324. osRtxThreadReadyPut(thread);
  325. thread = thread->delay_next;
  326. } while ((thread != NULL) && (thread->delay == 0U));
  327. if (thread != NULL) {
  328. thread->delay_prev = NULL;
  329. }
  330. osRtxInfo.thread.delay_list = thread;
  331. }
  332. }
  333. /// Get pointer to Thread registers (R0..R3)
  334. /// \param[in] thread thread object.
  335. /// \return pointer to registers R0-R3.
  336. uint32_t *osRtxThreadRegPtr (const os_thread_t *thread) {
  337. uint32_t addr = thread->sp + StackOffsetR0(thread->stack_frame);
  338. //lint -e{923} -e{9078} "cast from unsigned int to pointer"
  339. return ((uint32_t *)addr);
  340. }
  341. /// Block running Thread execution and register it as Ready to Run.
  342. /// \param[in] thread running thread object.
  343. static void osRtxThreadBlock (os_thread_t *thread) {
  344. os_thread_t *prev, *next;
  345. int32_t priority;
  346. thread->state = osRtxThreadReady;
  347. priority = thread->priority;
  348. prev = osRtxThreadObject(&osRtxInfo.thread.ready);
  349. next = prev->thread_next;
  350. while ((next != NULL) && (next->priority > priority)) {
  351. prev = next;
  352. next = next->thread_next;
  353. }
  354. thread->thread_prev = prev;
  355. thread->thread_next = next;
  356. prev->thread_next = thread;
  357. if (next != NULL) {
  358. next->thread_prev = thread;
  359. }
  360. EvrRtxThreadPreempted(thread);
  361. }
  362. /// Switch to specified Thread.
  363. /// \param[in] thread thread object.
  364. void osRtxThreadSwitch (os_thread_t *thread) {
  365. thread->state = osRtxThreadRunning;
  366. osRtxInfo.thread.run.next = thread;
  367. osRtxThreadStackCheck();
  368. EvrRtxThreadSwitched(thread);
  369. }
  370. /// Dispatch specified Thread or Ready Thread with Highest Priority.
  371. /// \param[in] thread thread object or NULL.
  372. void osRtxThreadDispatch (os_thread_t *thread) {
  373. uint8_t kernel_state;
  374. os_thread_t *thread_running;
  375. os_thread_t *thread_ready;
  376. kernel_state = osRtxKernelGetState();
  377. thread_running = osRtxThreadGetRunning();
  378. if (thread == NULL) {
  379. thread_ready = osRtxInfo.thread.ready.thread_list;
  380. if ((kernel_state == osRtxKernelRunning) &&
  381. (thread_ready != NULL) &&
  382. (thread_ready->priority > thread_running->priority)) {
  383. // Preempt running Thread
  384. osRtxThreadListRemove(thread_ready);
  385. osRtxThreadBlock(thread_running);
  386. osRtxThreadSwitch(thread_ready);
  387. }
  388. } else {
  389. if ((kernel_state == osRtxKernelRunning) &&
  390. (thread->priority > thread_running->priority)) {
  391. // Preempt running Thread
  392. osRtxThreadBlock(thread_running);
  393. osRtxThreadSwitch(thread);
  394. } else {
  395. // Put Thread into Ready list
  396. osRtxThreadReadyPut(thread);
  397. }
  398. }
  399. }
  400. /// Exit Thread wait state.
  401. /// \param[in] thread thread object.
  402. /// \param[in] ret_val return value.
  403. /// \param[in] dispatch dispatch flag.
  404. void osRtxThreadWaitExit (os_thread_t *thread, uint32_t ret_val, bool_t dispatch) {
  405. uint32_t *reg;
  406. EvrRtxThreadUnblocked(thread, ret_val);
  407. reg = osRtxThreadRegPtr(thread);
  408. reg[0] = ret_val;
  409. osRtxThreadDelayRemove(thread);
  410. if (dispatch) {
  411. osRtxThreadDispatch(thread);
  412. } else {
  413. osRtxThreadReadyPut(thread);
  414. }
  415. }
  416. /// Enter Thread wait state.
  417. /// \param[in] state new thread state.
  418. /// \param[in] timeout timeout.
  419. /// \return true - success, false - failure.
  420. bool_t osRtxThreadWaitEnter (uint8_t state, uint32_t timeout) {
  421. os_thread_t *thread;
  422. // Check if Kernel is running
  423. if (osRtxKernelGetState() != osRtxKernelRunning) {
  424. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  425. return FALSE;
  426. }
  427. // Check if any thread is ready
  428. if (osRtxInfo.thread.ready.thread_list == NULL) {
  429. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  430. return FALSE;
  431. }
  432. // Get running thread
  433. thread = osRtxThreadGetRunning();
  434. EvrRtxThreadBlocked(thread, timeout);
  435. thread->state = state;
  436. osRtxThreadDelayInsert(thread, timeout);
  437. thread = osRtxThreadListGet(&osRtxInfo.thread.ready);
  438. osRtxThreadSwitch(thread);
  439. return TRUE;
  440. }
  441. /// Check current running Thread Stack.
  442. //lint -esym(759,osRtxThreadStackCheck) "Prototype in header"
  443. //lint -esym(765,osRtxThreadStackCheck) "Global scope (can be overridden)"
  444. __WEAK void osRtxThreadStackCheck (void) {
  445. os_thread_t *thread;
  446. thread = osRtxThreadGetRunning();
  447. if (thread != NULL) {
  448. //lint -e{923} "cast from pointer to unsigned int"
  449. //lint -e{9079} -e{9087} "cast between pointers to different object types"
  450. if ((thread->sp <= (uint32_t)thread->stack_mem) ||
  451. (*((uint32_t *)thread->stack_mem) != osRtxStackMagicWord)) {
  452. (void)osRtxErrorNotify(osRtxErrorStackUnderflow, thread);
  453. }
  454. }
  455. }
  456. #ifdef RTX_TF_M_EXTENSION
  457. /// Get TrustZone Module Identifier of running Thread.
  458. /// \return TrustZone Module Identifier.
  459. uint32_t osRtxTzGetModuleId (void) {
  460. os_thread_t *thread;
  461. uint32_t tz_module;
  462. thread = osRtxThreadGetRunning();
  463. if (thread != NULL) {
  464. tz_module = thread->tz_module;
  465. } else {
  466. tz_module = 0U;
  467. }
  468. return tz_module;
  469. }
  470. #endif
  471. // ==== Post ISR processing ====
  472. /// Thread post ISR processing.
  473. /// \param[in] thread thread object.
  474. static void osRtxThreadPostProcess (os_thread_t *thread) {
  475. uint32_t thread_flags;
  476. // Check if Thread is waiting for Thread Flags
  477. if (thread->state == osRtxThreadWaitingThreadFlags) {
  478. thread_flags = ThreadFlagsCheck(thread, thread->wait_flags, thread->flags_options);
  479. if (thread_flags != 0U) {
  480. osRtxThreadWaitExit(thread, thread_flags, FALSE);
  481. EvrRtxThreadFlagsWaitCompleted(thread->wait_flags, thread->flags_options, thread_flags, thread);
  482. }
  483. }
  484. }
  485. // ==== Service Calls ====
  486. /// Create a thread and add it to Active Threads.
  487. /// \note API identical to osThreadNew
  488. static osThreadId_t svcRtxThreadNew (osThreadFunc_t func, void *argument, const osThreadAttr_t *attr) {
  489. os_thread_t *thread;
  490. uint32_t attr_bits;
  491. void *stack_mem;
  492. uint32_t stack_size;
  493. osPriority_t priority;
  494. uint8_t flags;
  495. const char *name;
  496. uint32_t *ptr;
  497. uint32_t n;
  498. #if (DOMAIN_NS == 1)
  499. TZ_ModuleId_t tz_module;
  500. TZ_MemoryId_t tz_memory;
  501. #endif
  502. // Check parameters
  503. if (func == NULL) {
  504. EvrRtxThreadError(NULL, (int32_t)osErrorParameter);
  505. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  506. return NULL;
  507. }
  508. // Process attributes
  509. if (attr != NULL) {
  510. name = attr->name;
  511. attr_bits = attr->attr_bits;
  512. //lint -e{9079} "conversion from pointer to void to pointer to other type" [MISRA Note 6]
  513. thread = attr->cb_mem;
  514. //lint -e{9079} "conversion from pointer to void to pointer to other type" [MISRA Note 6]
  515. stack_mem = attr->stack_mem;
  516. stack_size = attr->stack_size;
  517. priority = attr->priority;
  518. #if (DOMAIN_NS == 1)
  519. tz_module = attr->tz_module;
  520. #endif
  521. if (thread != NULL) {
  522. //lint -e(923) -e(9078) "cast from pointer to unsigned int" [MISRA Note 7]
  523. if ((((uint32_t)thread & 3U) != 0U) || (attr->cb_size < sizeof(os_thread_t))) {
  524. EvrRtxThreadError(NULL, osRtxErrorInvalidControlBlock);
  525. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  526. return NULL;
  527. }
  528. } else {
  529. if (attr->cb_size != 0U) {
  530. EvrRtxThreadError(NULL, osRtxErrorInvalidControlBlock);
  531. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  532. return NULL;
  533. }
  534. }
  535. if (stack_mem != NULL) {
  536. //lint -e(923) -e(9078) "cast from pointer to unsigned int" [MISRA Note 7]
  537. if ((((uint32_t)stack_mem & 7U) != 0U) || (stack_size == 0U)) {
  538. EvrRtxThreadError(NULL, osRtxErrorInvalidThreadStack);
  539. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  540. return NULL;
  541. }
  542. }
  543. if (priority == osPriorityNone) {
  544. priority = osPriorityNormal;
  545. } else {
  546. if ((priority < osPriorityIdle) || (priority > osPriorityISR)) {
  547. EvrRtxThreadError(NULL, osRtxErrorInvalidPriority);
  548. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  549. return NULL;
  550. }
  551. }
  552. } else {
  553. name = NULL;
  554. attr_bits = 0U;
  555. thread = NULL;
  556. stack_mem = NULL;
  557. stack_size = 0U;
  558. priority = osPriorityNormal;
  559. #if (DOMAIN_NS == 1)
  560. tz_module = 0U;
  561. #endif
  562. }
  563. // Check stack size
  564. if ((stack_size != 0U) && (((stack_size & 7U) != 0U) || (stack_size < (64U + 8U)))) {
  565. EvrRtxThreadError(NULL, osRtxErrorInvalidThreadStack);
  566. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  567. return NULL;
  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. #if (defined(OS_OBJ_MEM_USAGE) && (OS_OBJ_MEM_USAGE != 0))
  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. #if (defined(OS_OBJ_MEM_USAGE) && (OS_OBJ_MEM_USAGE != 0))
  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. #if (defined(OS_OBJ_MEM_USAGE) && (OS_OBJ_MEM_USAGE != 0))
  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. thread->thread_prev = NULL;
  911. thread->thread_next = NULL;
  912. osRtxThreadDelayInsert(thread, osWaitForever);
  913. }
  914. return status;
  915. }
  916. /// Resume execution of a thread.
  917. /// \note API identical to osThreadResume
  918. static osStatus_t svcRtxThreadResume (osThreadId_t thread_id) {
  919. os_thread_t *thread = osRtxThreadId(thread_id);
  920. // Check parameters
  921. if ((thread == NULL) || (thread->id != osRtxIdThread)) {
  922. EvrRtxThreadError(thread, (int32_t)osErrorParameter);
  923. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  924. return osErrorParameter;
  925. }
  926. // Check object state
  927. if ((thread->state & osRtxThreadStateMask) != osRtxThreadBlocked) {
  928. EvrRtxThreadError(thread, (int32_t)osErrorResource);
  929. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  930. return osErrorResource;
  931. }
  932. EvrRtxThreadResumed(thread);
  933. // Wakeup Thread
  934. osRtxThreadListRemove(thread);
  935. osRtxThreadDelayRemove(thread);
  936. osRtxThreadDispatch(thread);
  937. return osOK;
  938. }
  939. /// Free Thread resources.
  940. /// \param[in] thread thread object.
  941. static void osRtxThreadFree (os_thread_t *thread) {
  942. // Mark object as inactive and invalid
  943. thread->state = osRtxThreadInactive;
  944. thread->id = osRtxIdInvalid;
  945. #if (DOMAIN_NS == 1)
  946. // Free secure process stack
  947. if (thread->tz_memory != 0U) {
  948. (void)TZ_FreeModuleContext_S(thread->tz_memory);
  949. }
  950. #endif
  951. // Free stack memory
  952. if ((thread->flags & osRtxFlagSystemMemory) != 0U) {
  953. if ((thread->flags & osRtxThreadFlagDefStack) != 0U) {
  954. (void)osRtxMemoryPoolFree(osRtxInfo.mpi.stack, thread->stack_mem);
  955. } else {
  956. (void)osRtxMemoryFree(osRtxInfo.mem.stack, thread->stack_mem);
  957. }
  958. }
  959. // Free object memory
  960. if ((thread->flags & osRtxFlagSystemObject) != 0U) {
  961. if (osRtxInfo.mpi.thread != NULL) {
  962. (void)osRtxMemoryPoolFree(osRtxInfo.mpi.thread, thread);
  963. } else {
  964. (void)osRtxMemoryFree(osRtxInfo.mem.common, thread);
  965. }
  966. #if (defined(OS_OBJ_MEM_USAGE) && (OS_OBJ_MEM_USAGE != 0))
  967. osRtxThreadMemUsage.cnt_free++;
  968. #endif
  969. }
  970. }
  971. /// Detach a thread (thread storage can be reclaimed when thread terminates).
  972. /// \note API identical to osThreadDetach
  973. static osStatus_t svcRtxThreadDetach (osThreadId_t thread_id) {
  974. os_thread_t *thread = osRtxThreadId(thread_id);
  975. // Check parameters
  976. if ((thread == NULL) || (thread->id != osRtxIdThread)) {
  977. EvrRtxThreadError(thread, (int32_t)osErrorParameter);
  978. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  979. return osErrorParameter;
  980. }
  981. // Check object attributes
  982. if ((thread->attr & osThreadJoinable) == 0U) {
  983. EvrRtxThreadError(thread, osRtxErrorThreadNotJoinable);
  984. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  985. return osErrorResource;
  986. }
  987. if (thread->state == osRtxThreadTerminated) {
  988. osRtxThreadListUnlink(&osRtxInfo.thread.terminate_list, thread);
  989. osRtxThreadFree(thread);
  990. } else {
  991. thread->attr &= ~osThreadJoinable;
  992. }
  993. EvrRtxThreadDetached(thread);
  994. return osOK;
  995. }
  996. /// Wait for specified thread to terminate.
  997. /// \note API identical to osThreadJoin
  998. static osStatus_t svcRtxThreadJoin (osThreadId_t thread_id) {
  999. os_thread_t *thread = osRtxThreadId(thread_id);
  1000. osStatus_t status;
  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. // Check object state
  1014. if (thread->state == osRtxThreadRunning) {
  1015. EvrRtxThreadError(thread, (int32_t)osErrorResource);
  1016. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1017. return osErrorResource;
  1018. }
  1019. if (thread->state == osRtxThreadTerminated) {
  1020. osRtxThreadListUnlink(&osRtxInfo.thread.terminate_list, thread);
  1021. osRtxThreadFree(thread);
  1022. EvrRtxThreadJoined(thread);
  1023. status = osOK;
  1024. } else {
  1025. // Suspend current Thread
  1026. if (osRtxThreadWaitEnter(osRtxThreadWaitingJoin, osWaitForever)) {
  1027. thread->thread_join = osRtxThreadGetRunning();
  1028. thread->attr &= ~osThreadJoinable;
  1029. EvrRtxThreadJoinPending(thread);
  1030. } else {
  1031. EvrRtxThreadError(thread, (int32_t)osErrorResource);
  1032. }
  1033. status = osErrorResource;
  1034. }
  1035. return status;
  1036. }
  1037. /// Terminate execution of current running thread.
  1038. /// \note API identical to osThreadExit
  1039. static void svcRtxThreadExit (void) {
  1040. os_thread_t *thread;
  1041. // Check if switch to next Ready Thread is possible
  1042. if ((osRtxKernelGetState() != osRtxKernelRunning) ||
  1043. (osRtxInfo.thread.ready.thread_list == NULL)) {
  1044. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1045. return;
  1046. }
  1047. // Get running thread
  1048. thread = osRtxThreadGetRunning();
  1049. // Release owned Mutexes
  1050. osRtxMutexOwnerRelease(thread->mutex_list);
  1051. // Wakeup Thread waiting to Join
  1052. if (thread->thread_join != NULL) {
  1053. osRtxThreadWaitExit(thread->thread_join, (uint32_t)osOK, FALSE);
  1054. EvrRtxThreadJoined(thread->thread_join);
  1055. }
  1056. // Switch to next Ready Thread
  1057. thread->sp = __get_PSP();
  1058. osRtxThreadSwitch(osRtxThreadListGet(&osRtxInfo.thread.ready));
  1059. osRtxThreadSetRunning(NULL);
  1060. if ((thread->attr & osThreadJoinable) == 0U) {
  1061. osRtxThreadFree(thread);
  1062. } else {
  1063. // Update Thread State and put it into Terminate Thread list
  1064. thread->state = osRtxThreadTerminated;
  1065. thread->thread_prev = NULL;
  1066. thread->thread_next = osRtxInfo.thread.terminate_list;
  1067. if (osRtxInfo.thread.terminate_list != NULL) {
  1068. osRtxInfo.thread.terminate_list->thread_prev = thread;
  1069. }
  1070. osRtxInfo.thread.terminate_list = thread;
  1071. }
  1072. EvrRtxThreadDestroyed(thread);
  1073. }
  1074. /// Terminate execution of a thread.
  1075. /// \note API identical to osThreadTerminate
  1076. static osStatus_t svcRtxThreadTerminate (osThreadId_t thread_id) {
  1077. os_thread_t *thread = osRtxThreadId(thread_id);
  1078. osStatus_t status;
  1079. // Check parameters
  1080. if ((thread == NULL) || (thread->id != osRtxIdThread)) {
  1081. EvrRtxThreadError(thread, (int32_t)osErrorParameter);
  1082. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1083. return osErrorParameter;
  1084. }
  1085. // Check object state
  1086. switch (thread->state & osRtxThreadStateMask) {
  1087. case osRtxThreadRunning:
  1088. if ((osRtxKernelGetState() != osRtxKernelRunning) ||
  1089. (osRtxInfo.thread.ready.thread_list == NULL)) {
  1090. EvrRtxThreadError(thread, (int32_t)osErrorResource);
  1091. status = osErrorResource;
  1092. } else {
  1093. status = osOK;
  1094. }
  1095. break;
  1096. case osRtxThreadReady:
  1097. osRtxThreadListRemove(thread);
  1098. status = osOK;
  1099. break;
  1100. case osRtxThreadBlocked:
  1101. osRtxThreadListRemove(thread);
  1102. osRtxThreadDelayRemove(thread);
  1103. status = osOK;
  1104. break;
  1105. case osRtxThreadInactive:
  1106. case osRtxThreadTerminated:
  1107. default:
  1108. EvrRtxThreadError(thread, (int32_t)osErrorResource);
  1109. status = osErrorResource;
  1110. break;
  1111. }
  1112. if (status == osOK) {
  1113. // Release owned Mutexes
  1114. osRtxMutexOwnerRelease(thread->mutex_list);
  1115. // Wakeup Thread waiting to Join
  1116. if (thread->thread_join != NULL) {
  1117. osRtxThreadWaitExit(thread->thread_join, (uint32_t)osOK, FALSE);
  1118. EvrRtxThreadJoined(thread->thread_join);
  1119. }
  1120. // Switch to next Ready Thread when terminating running Thread
  1121. if (thread->state == osRtxThreadRunning) {
  1122. thread->sp = __get_PSP();
  1123. osRtxThreadSwitch(osRtxThreadListGet(&osRtxInfo.thread.ready));
  1124. osRtxThreadSetRunning(NULL);
  1125. } else {
  1126. osRtxThreadDispatch(NULL);
  1127. }
  1128. if ((thread->attr & osThreadJoinable) == 0U) {
  1129. osRtxThreadFree(thread);
  1130. } else {
  1131. // Update Thread State and put it into Terminate Thread list
  1132. thread->state = osRtxThreadTerminated;
  1133. thread->thread_prev = NULL;
  1134. thread->thread_next = osRtxInfo.thread.terminate_list;
  1135. if (osRtxInfo.thread.terminate_list != NULL) {
  1136. osRtxInfo.thread.terminate_list->thread_prev = thread;
  1137. }
  1138. osRtxInfo.thread.terminate_list = thread;
  1139. }
  1140. EvrRtxThreadDestroyed(thread);
  1141. }
  1142. return status;
  1143. }
  1144. /// Get number of active threads.
  1145. /// \note API identical to osThreadGetCount
  1146. static uint32_t svcRtxThreadGetCount (void) {
  1147. const os_thread_t *thread;
  1148. uint32_t count;
  1149. // Running Thread
  1150. count = 1U;
  1151. // Ready List
  1152. for (thread = osRtxInfo.thread.ready.thread_list;
  1153. thread != NULL; thread = thread->thread_next) {
  1154. count++;
  1155. }
  1156. // Delay List
  1157. for (thread = osRtxInfo.thread.delay_list;
  1158. thread != NULL; thread = thread->delay_next) {
  1159. count++;
  1160. }
  1161. // Wait List
  1162. for (thread = osRtxInfo.thread.wait_list;
  1163. thread != NULL; thread = thread->delay_next) {
  1164. count++;
  1165. }
  1166. EvrRtxThreadGetCount(count);
  1167. return count;
  1168. }
  1169. /// Enumerate active threads.
  1170. /// \note API identical to osThreadEnumerate
  1171. static uint32_t svcRtxThreadEnumerate (osThreadId_t *thread_array, uint32_t array_items) {
  1172. os_thread_t *thread;
  1173. uint32_t count;
  1174. // Check parameters
  1175. if ((thread_array == NULL) || (array_items == 0U)) {
  1176. EvrRtxThreadEnumerate(thread_array, array_items, 0U);
  1177. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1178. return 0U;
  1179. }
  1180. // Running Thread
  1181. *thread_array = osRtxThreadGetRunning();
  1182. thread_array++;
  1183. count = 1U;
  1184. // Ready List
  1185. for (thread = osRtxInfo.thread.ready.thread_list;
  1186. (thread != NULL) && (count < array_items); thread = thread->thread_next) {
  1187. *thread_array = thread;
  1188. thread_array++;
  1189. count++;
  1190. }
  1191. // Delay List
  1192. for (thread = osRtxInfo.thread.delay_list;
  1193. (thread != NULL) && (count < array_items); thread = thread->delay_next) {
  1194. *thread_array = thread;
  1195. thread_array++;
  1196. count++;
  1197. }
  1198. // Wait List
  1199. for (thread = osRtxInfo.thread.wait_list;
  1200. (thread != NULL) && (count < array_items); thread = thread->delay_next) {
  1201. *thread_array = thread;
  1202. thread_array++;
  1203. count++;
  1204. }
  1205. EvrRtxThreadEnumerate(thread_array - count, array_items, count);
  1206. return count;
  1207. }
  1208. /// Set the specified Thread Flags of a thread.
  1209. /// \note API identical to osThreadFlagsSet
  1210. static uint32_t svcRtxThreadFlagsSet (osThreadId_t thread_id, uint32_t flags) {
  1211. os_thread_t *thread = osRtxThreadId(thread_id);
  1212. uint32_t thread_flags;
  1213. uint32_t thread_flags0;
  1214. // Check parameters
  1215. if ((thread == NULL) || (thread->id != osRtxIdThread) ||
  1216. ((flags & ~(((uint32_t)1U << osRtxThreadFlagsLimit) - 1U)) != 0U)) {
  1217. EvrRtxThreadFlagsError(thread, (int32_t)osErrorParameter);
  1218. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1219. return ((uint32_t)osErrorParameter);
  1220. }
  1221. // Check object state
  1222. if (thread->state == osRtxThreadTerminated) {
  1223. EvrRtxThreadFlagsError(thread, (int32_t)osErrorResource);
  1224. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1225. return ((uint32_t)osErrorResource);
  1226. }
  1227. // Set Thread Flags
  1228. thread_flags = ThreadFlagsSet(thread, flags);
  1229. // Check if Thread is waiting for Thread Flags
  1230. if (thread->state == osRtxThreadWaitingThreadFlags) {
  1231. thread_flags0 = ThreadFlagsCheck(thread, thread->wait_flags, thread->flags_options);
  1232. if (thread_flags0 != 0U) {
  1233. if ((thread->flags_options & osFlagsNoClear) == 0U) {
  1234. thread_flags = thread_flags0 & ~thread->wait_flags;
  1235. } else {
  1236. thread_flags = thread_flags0;
  1237. }
  1238. osRtxThreadWaitExit(thread, thread_flags0, TRUE);
  1239. EvrRtxThreadFlagsWaitCompleted(thread->wait_flags, thread->flags_options, thread_flags0, thread);
  1240. }
  1241. }
  1242. EvrRtxThreadFlagsSetDone(thread, thread_flags);
  1243. return thread_flags;
  1244. }
  1245. /// Clear the specified Thread Flags of current running thread.
  1246. /// \note API identical to osThreadFlagsClear
  1247. static uint32_t svcRtxThreadFlagsClear (uint32_t flags) {
  1248. os_thread_t *thread;
  1249. uint32_t thread_flags;
  1250. // Check running thread
  1251. thread = osRtxThreadGetRunning();
  1252. if (thread == NULL) {
  1253. EvrRtxThreadFlagsError(NULL, osRtxErrorKernelNotRunning);
  1254. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1255. return ((uint32_t)osError);
  1256. }
  1257. // Check parameters
  1258. if ((flags & ~(((uint32_t)1U << osRtxThreadFlagsLimit) - 1U)) != 0U) {
  1259. EvrRtxThreadFlagsError(thread, (int32_t)osErrorParameter);
  1260. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1261. return ((uint32_t)osErrorParameter);
  1262. }
  1263. // Clear Thread Flags
  1264. thread_flags = ThreadFlagsClear(thread, flags);
  1265. EvrRtxThreadFlagsClearDone(thread_flags);
  1266. return thread_flags;
  1267. }
  1268. /// Get the current Thread Flags of current running thread.
  1269. /// \note API identical to osThreadFlagsGet
  1270. static uint32_t svcRtxThreadFlagsGet (void) {
  1271. const os_thread_t *thread;
  1272. // Check running thread
  1273. thread = osRtxThreadGetRunning();
  1274. if (thread == NULL) {
  1275. EvrRtxThreadFlagsGet(0U);
  1276. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1277. return 0U;
  1278. }
  1279. EvrRtxThreadFlagsGet(thread->thread_flags);
  1280. return thread->thread_flags;
  1281. }
  1282. /// Wait for one or more Thread Flags of the current running thread to become signaled.
  1283. /// \note API identical to osThreadFlagsWait
  1284. static uint32_t svcRtxThreadFlagsWait (uint32_t flags, uint32_t options, uint32_t timeout) {
  1285. os_thread_t *thread;
  1286. uint32_t thread_flags;
  1287. // Check running thread
  1288. thread = osRtxThreadGetRunning();
  1289. if (thread == NULL) {
  1290. EvrRtxThreadFlagsError(NULL, osRtxErrorKernelNotRunning);
  1291. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1292. return ((uint32_t)osError);
  1293. }
  1294. // Check parameters
  1295. if ((flags & ~(((uint32_t)1U << osRtxThreadFlagsLimit) - 1U)) != 0U) {
  1296. EvrRtxThreadFlagsError(thread, (int32_t)osErrorParameter);
  1297. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1298. return ((uint32_t)osErrorParameter);
  1299. }
  1300. // Check Thread Flags
  1301. thread_flags = ThreadFlagsCheck(thread, flags, options);
  1302. if (thread_flags != 0U) {
  1303. EvrRtxThreadFlagsWaitCompleted(flags, options, thread_flags, thread);
  1304. } else {
  1305. // Check if timeout is specified
  1306. if (timeout != 0U) {
  1307. // Store waiting flags and options
  1308. EvrRtxThreadFlagsWaitPending(flags, options, timeout);
  1309. thread->wait_flags = flags;
  1310. thread->flags_options = (uint8_t)options;
  1311. // Suspend current Thread
  1312. if (!osRtxThreadWaitEnter(osRtxThreadWaitingThreadFlags, timeout)) {
  1313. EvrRtxThreadFlagsWaitTimeout(thread);
  1314. }
  1315. thread_flags = (uint32_t)osErrorTimeout;
  1316. } else {
  1317. EvrRtxThreadFlagsWaitNotCompleted(flags, options);
  1318. thread_flags = (uint32_t)osErrorResource;
  1319. }
  1320. }
  1321. return thread_flags;
  1322. }
  1323. // Service Calls definitions
  1324. //lint ++flb "Library Begin" [MISRA Note 11]
  1325. SVC0_3 (ThreadNew, osThreadId_t, osThreadFunc_t, void *, const osThreadAttr_t *)
  1326. SVC0_1 (ThreadGetName, const char *, osThreadId_t)
  1327. SVC0_0 (ThreadGetId, osThreadId_t)
  1328. SVC0_1 (ThreadGetState, osThreadState_t, osThreadId_t)
  1329. SVC0_1 (ThreadGetStackSize, uint32_t, osThreadId_t)
  1330. SVC0_1 (ThreadGetStackSpace, uint32_t, osThreadId_t)
  1331. SVC0_2 (ThreadSetPriority, osStatus_t, osThreadId_t, osPriority_t)
  1332. SVC0_1 (ThreadGetPriority, osPriority_t, osThreadId_t)
  1333. SVC0_0 (ThreadYield, osStatus_t)
  1334. SVC0_1 (ThreadSuspend, osStatus_t, osThreadId_t)
  1335. SVC0_1 (ThreadResume, osStatus_t, osThreadId_t)
  1336. SVC0_1 (ThreadDetach, osStatus_t, osThreadId_t)
  1337. SVC0_1 (ThreadJoin, osStatus_t, osThreadId_t)
  1338. SVC0_0N(ThreadExit, void)
  1339. SVC0_1 (ThreadTerminate, osStatus_t, osThreadId_t)
  1340. SVC0_0 (ThreadGetCount, uint32_t)
  1341. SVC0_2 (ThreadEnumerate, uint32_t, osThreadId_t *, uint32_t)
  1342. SVC0_2 (ThreadFlagsSet, uint32_t, osThreadId_t, uint32_t)
  1343. SVC0_1 (ThreadFlagsClear, uint32_t, uint32_t)
  1344. SVC0_0 (ThreadFlagsGet, uint32_t)
  1345. SVC0_3 (ThreadFlagsWait, uint32_t, uint32_t, uint32_t, uint32_t)
  1346. //lint --flb "Library End"
  1347. // ==== ISR Calls ====
  1348. /// Set the specified Thread Flags of a thread.
  1349. /// \note API identical to osThreadFlagsSet
  1350. __STATIC_INLINE
  1351. uint32_t isrRtxThreadFlagsSet (osThreadId_t thread_id, uint32_t flags) {
  1352. os_thread_t *thread = osRtxThreadId(thread_id);
  1353. uint32_t thread_flags;
  1354. // Check parameters
  1355. if ((thread == NULL) || (thread->id != osRtxIdThread) ||
  1356. ((flags & ~(((uint32_t)1U << osRtxThreadFlagsLimit) - 1U)) != 0U)) {
  1357. EvrRtxThreadFlagsError(thread, (int32_t)osErrorParameter);
  1358. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1359. return ((uint32_t)osErrorParameter);
  1360. }
  1361. // Check object state
  1362. if (thread->state == osRtxThreadTerminated) {
  1363. EvrRtxThreadFlagsError(thread, (int32_t)osErrorResource);
  1364. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1365. return ((uint32_t)osErrorResource);
  1366. }
  1367. // Set Thread Flags
  1368. thread_flags = ThreadFlagsSet(thread, flags);
  1369. // Register post ISR processing
  1370. osRtxPostProcess(osRtxObject(thread));
  1371. EvrRtxThreadFlagsSetDone(thread, thread_flags);
  1372. return thread_flags;
  1373. }
  1374. // ==== Library functions ====
  1375. /// Thread startup (Idle and Timer Thread).
  1376. /// \return true - success, false - failure.
  1377. bool_t osRtxThreadStartup (void) {
  1378. bool_t ret = TRUE;
  1379. // Create Idle Thread
  1380. osRtxInfo.thread.idle = osRtxThreadId(
  1381. svcRtxThreadNew(osRtxIdleThread, NULL, osRtxConfig.idle_thread_attr)
  1382. );
  1383. // Create Timer Thread
  1384. if (osRtxConfig.timer_mq_mcnt != 0U) {
  1385. osRtxInfo.timer.thread = osRtxThreadId(
  1386. svcRtxThreadNew(osRtxTimerThread, NULL, osRtxConfig.timer_thread_attr)
  1387. );
  1388. if (osRtxInfo.timer.thread == NULL) {
  1389. ret = FALSE;
  1390. }
  1391. }
  1392. return ret;
  1393. }
  1394. // ==== Public API ====
  1395. /// Create a thread and add it to Active Threads.
  1396. osThreadId_t osThreadNew (osThreadFunc_t func, void *argument, const osThreadAttr_t *attr) {
  1397. osThreadId_t thread_id;
  1398. EvrRtxThreadNew(func, argument, attr);
  1399. if (IsIrqMode() || IsIrqMasked()) {
  1400. EvrRtxThreadError(NULL, (int32_t)osErrorISR);
  1401. thread_id = NULL;
  1402. } else {
  1403. thread_id = __svcThreadNew(func, argument, attr);
  1404. }
  1405. return thread_id;
  1406. }
  1407. /// Get name of a thread.
  1408. const char *osThreadGetName (osThreadId_t thread_id) {
  1409. const char *name;
  1410. if (IsIrqMode() || IsIrqMasked()) {
  1411. EvrRtxThreadGetName(thread_id, NULL);
  1412. name = NULL;
  1413. } else {
  1414. name = __svcThreadGetName(thread_id);
  1415. }
  1416. return name;
  1417. }
  1418. /// Return the thread ID of the current running thread.
  1419. osThreadId_t osThreadGetId (void) {
  1420. osThreadId_t thread_id;
  1421. if (IsIrqMode() || IsIrqMasked()) {
  1422. thread_id = svcRtxThreadGetId();
  1423. } else {
  1424. thread_id = __svcThreadGetId();
  1425. }
  1426. return thread_id;
  1427. }
  1428. /// Get current thread state of a thread.
  1429. osThreadState_t osThreadGetState (osThreadId_t thread_id) {
  1430. osThreadState_t state;
  1431. if (IsIrqMode() || IsIrqMasked()) {
  1432. EvrRtxThreadGetState(thread_id, osThreadError);
  1433. state = osThreadError;
  1434. } else {
  1435. state = __svcThreadGetState(thread_id);
  1436. }
  1437. return state;
  1438. }
  1439. /// Get stack size of a thread.
  1440. uint32_t osThreadGetStackSize (osThreadId_t thread_id) {
  1441. uint32_t stack_size;
  1442. if (IsIrqMode() || IsIrqMasked()) {
  1443. EvrRtxThreadGetStackSize(thread_id, 0U);
  1444. stack_size = 0U;
  1445. } else {
  1446. stack_size = __svcThreadGetStackSize(thread_id);
  1447. }
  1448. return stack_size;
  1449. }
  1450. /// Get available stack space of a thread based on stack watermark recording during execution.
  1451. uint32_t osThreadGetStackSpace (osThreadId_t thread_id) {
  1452. uint32_t stack_space;
  1453. if (IsIrqMode() || IsIrqMasked()) {
  1454. EvrRtxThreadGetStackSpace(thread_id, 0U);
  1455. stack_space = 0U;
  1456. } else {
  1457. stack_space = __svcThreadGetStackSpace(thread_id);
  1458. }
  1459. return stack_space;
  1460. }
  1461. /// Change priority of a thread.
  1462. osStatus_t osThreadSetPriority (osThreadId_t thread_id, osPriority_t priority) {
  1463. osStatus_t status;
  1464. EvrRtxThreadSetPriority(thread_id, priority);
  1465. if (IsIrqMode() || IsIrqMasked()) {
  1466. EvrRtxThreadError(thread_id, (int32_t)osErrorISR);
  1467. status = osErrorISR;
  1468. } else {
  1469. status = __svcThreadSetPriority(thread_id, priority);
  1470. }
  1471. return status;
  1472. }
  1473. /// Get current priority of a thread.
  1474. osPriority_t osThreadGetPriority (osThreadId_t thread_id) {
  1475. osPriority_t priority;
  1476. if (IsIrqMode() || IsIrqMasked()) {
  1477. EvrRtxThreadGetPriority(thread_id, osPriorityError);
  1478. priority = osPriorityError;
  1479. } else {
  1480. priority = __svcThreadGetPriority(thread_id);
  1481. }
  1482. return priority;
  1483. }
  1484. /// Pass control to next thread that is in state READY.
  1485. osStatus_t osThreadYield (void) {
  1486. osStatus_t status;
  1487. EvrRtxThreadYield();
  1488. if (IsIrqMode() || IsIrqMasked()) {
  1489. EvrRtxThreadError(NULL, (int32_t)osErrorISR);
  1490. status = osErrorISR;
  1491. } else {
  1492. status = __svcThreadYield();
  1493. }
  1494. return status;
  1495. }
  1496. /// Suspend execution of a thread.
  1497. osStatus_t osThreadSuspend (osThreadId_t thread_id) {
  1498. osStatus_t status;
  1499. EvrRtxThreadSuspend(thread_id);
  1500. if (IsIrqMode() || IsIrqMasked()) {
  1501. EvrRtxThreadError(thread_id, (int32_t)osErrorISR);
  1502. status = osErrorISR;
  1503. } else {
  1504. status = __svcThreadSuspend(thread_id);
  1505. }
  1506. return status;
  1507. }
  1508. /// Resume execution of a thread.
  1509. osStatus_t osThreadResume (osThreadId_t thread_id) {
  1510. osStatus_t status;
  1511. EvrRtxThreadResume(thread_id);
  1512. if (IsIrqMode() || IsIrqMasked()) {
  1513. EvrRtxThreadError(thread_id, (int32_t)osErrorISR);
  1514. status = osErrorISR;
  1515. } else {
  1516. status = __svcThreadResume(thread_id);
  1517. }
  1518. return status;
  1519. }
  1520. /// Detach a thread (thread storage can be reclaimed when thread terminates).
  1521. osStatus_t osThreadDetach (osThreadId_t thread_id) {
  1522. osStatus_t status;
  1523. EvrRtxThreadDetach(thread_id);
  1524. if (IsIrqMode() || IsIrqMasked()) {
  1525. EvrRtxThreadError(thread_id, (int32_t)osErrorISR);
  1526. status = osErrorISR;
  1527. } else {
  1528. status = __svcThreadDetach(thread_id);
  1529. }
  1530. return status;
  1531. }
  1532. /// Wait for specified thread to terminate.
  1533. osStatus_t osThreadJoin (osThreadId_t thread_id) {
  1534. osStatus_t status;
  1535. EvrRtxThreadJoin(thread_id);
  1536. if (IsIrqMode() || IsIrqMasked()) {
  1537. EvrRtxThreadError(thread_id, (int32_t)osErrorISR);
  1538. status = osErrorISR;
  1539. } else {
  1540. status = __svcThreadJoin(thread_id);
  1541. }
  1542. return status;
  1543. }
  1544. /// Terminate execution of current running thread.
  1545. __NO_RETURN void osThreadExit (void) {
  1546. EvrRtxThreadExit();
  1547. __svcThreadExit();
  1548. EvrRtxThreadError(NULL, (int32_t)osError);
  1549. for (;;) {}
  1550. }
  1551. /// Terminate execution of a thread.
  1552. osStatus_t osThreadTerminate (osThreadId_t thread_id) {
  1553. osStatus_t status;
  1554. EvrRtxThreadTerminate(thread_id);
  1555. if (IsIrqMode() || IsIrqMasked()) {
  1556. EvrRtxThreadError(thread_id, (int32_t)osErrorISR);
  1557. status = osErrorISR;
  1558. } else {
  1559. status = __svcThreadTerminate(thread_id);
  1560. }
  1561. return status;
  1562. }
  1563. /// Get number of active threads.
  1564. uint32_t osThreadGetCount (void) {
  1565. uint32_t count;
  1566. if (IsIrqMode() || IsIrqMasked()) {
  1567. EvrRtxThreadGetCount(0U);
  1568. count = 0U;
  1569. } else {
  1570. count = __svcThreadGetCount();
  1571. }
  1572. return count;
  1573. }
  1574. /// Enumerate active threads.
  1575. uint32_t osThreadEnumerate (osThreadId_t *thread_array, uint32_t array_items) {
  1576. uint32_t count;
  1577. if (IsIrqMode() || IsIrqMasked()) {
  1578. EvrRtxThreadEnumerate(thread_array, array_items, 0U);
  1579. count = 0U;
  1580. } else {
  1581. count = __svcThreadEnumerate(thread_array, array_items);
  1582. }
  1583. return count;
  1584. }
  1585. /// Set the specified Thread Flags of a thread.
  1586. uint32_t osThreadFlagsSet (osThreadId_t thread_id, uint32_t flags) {
  1587. uint32_t thread_flags;
  1588. EvrRtxThreadFlagsSet(thread_id, flags);
  1589. if (IsIrqMode() || IsIrqMasked()) {
  1590. thread_flags = isrRtxThreadFlagsSet(thread_id, flags);
  1591. } else {
  1592. thread_flags = __svcThreadFlagsSet(thread_id, flags);
  1593. }
  1594. return thread_flags;
  1595. }
  1596. /// Clear the specified Thread Flags of current running thread.
  1597. uint32_t osThreadFlagsClear (uint32_t flags) {
  1598. uint32_t thread_flags;
  1599. EvrRtxThreadFlagsClear(flags);
  1600. if (IsIrqMode() || IsIrqMasked()) {
  1601. EvrRtxThreadFlagsError(NULL, (int32_t)osErrorISR);
  1602. thread_flags = (uint32_t)osErrorISR;
  1603. } else {
  1604. thread_flags = __svcThreadFlagsClear(flags);
  1605. }
  1606. return thread_flags;
  1607. }
  1608. /// Get the current Thread Flags of current running thread.
  1609. uint32_t osThreadFlagsGet (void) {
  1610. uint32_t thread_flags;
  1611. if (IsIrqMode() || IsIrqMasked()) {
  1612. EvrRtxThreadFlagsGet(0U);
  1613. thread_flags = 0U;
  1614. } else {
  1615. thread_flags = __svcThreadFlagsGet();
  1616. }
  1617. return thread_flags;
  1618. }
  1619. /// Wait for one or more Thread Flags of the current running thread to become signaled.
  1620. uint32_t osThreadFlagsWait (uint32_t flags, uint32_t options, uint32_t timeout) {
  1621. uint32_t thread_flags;
  1622. EvrRtxThreadFlagsWait(flags, options, timeout);
  1623. if (IsIrqMode() || IsIrqMasked()) {
  1624. EvrRtxThreadFlagsError(NULL, (int32_t)osErrorISR);
  1625. thread_flags = (uint32_t)osErrorISR;
  1626. } else {
  1627. thread_flags = __svcThreadFlagsWait(flags, options, timeout);
  1628. }
  1629. return thread_flags;
  1630. }