rtx_thread.c 58 KB

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