rtx_thread.c 57 KB

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