rtx_thread.c 56 KB

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