rtx_thread.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947
  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 (defined(__ARM_ARCH_7A__) && (__ARM_ARCH_7A__ != 0))
  388. // On Cortex-A PendSV_Handler is executed before final context switch.
  389. if ((thread_running != NULL) && (thread_running->state != osRtxThreadRunning)) {
  390. thread_running = osRtxInfo.thread.run.next;
  391. }
  392. #endif
  393. if (thread == NULL) {
  394. thread_ready = osRtxInfo.thread.ready.thread_list;
  395. if ((kernel_state == osRtxKernelRunning) &&
  396. (thread_running != NULL) && (thread_ready != NULL) &&
  397. (thread_ready->priority > thread_running->priority)) {
  398. // Preempt running Thread
  399. osRtxThreadListRemove(thread_ready);
  400. osRtxThreadBlock(thread_running);
  401. osRtxThreadSwitch(thread_ready);
  402. }
  403. } else {
  404. if ((kernel_state == osRtxKernelRunning) &&
  405. (thread_running != NULL) &&
  406. (thread->priority > thread_running->priority)) {
  407. // Preempt running Thread
  408. osRtxThreadBlock(thread_running);
  409. osRtxThreadSwitch(thread);
  410. } else {
  411. // Put Thread into Ready list
  412. osRtxThreadReadyPut(thread);
  413. }
  414. }
  415. }
  416. /// Exit Thread wait state.
  417. /// \param[in] thread thread object.
  418. /// \param[in] ret_val return value.
  419. /// \param[in] dispatch dispatch flag.
  420. void osRtxThreadWaitExit (os_thread_t *thread, uint32_t ret_val, bool_t dispatch) {
  421. uint32_t *reg;
  422. EvrRtxThreadUnblocked(thread, ret_val);
  423. reg = osRtxThreadRegPtr(thread);
  424. reg[0] = ret_val;
  425. osRtxThreadDelayRemove(thread);
  426. if (dispatch) {
  427. osRtxThreadDispatch(thread);
  428. } else {
  429. osRtxThreadReadyPut(thread);
  430. }
  431. }
  432. /// Enter Thread wait state.
  433. /// \param[in] state new thread state.
  434. /// \param[in] timeout timeout.
  435. /// \return true - success, false - failure.
  436. bool_t osRtxThreadWaitEnter (uint8_t state, uint32_t timeout) {
  437. os_thread_t *thread;
  438. // Check if Kernel is running
  439. if (osRtxKernelGetState() != osRtxKernelRunning) {
  440. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  441. return FALSE;
  442. }
  443. // Check running thread
  444. thread = osRtxThreadGetRunning();
  445. if (thread == NULL) {
  446. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  447. return FALSE;
  448. }
  449. // Check if any thread is ready
  450. if (osRtxInfo.thread.ready.thread_list == NULL) {
  451. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  452. return FALSE;
  453. }
  454. EvrRtxThreadBlocked(thread, timeout);
  455. thread->state = state;
  456. osRtxThreadDelayInsert(thread, timeout);
  457. thread = osRtxThreadListGet(&osRtxInfo.thread.ready);
  458. osRtxThreadSwitch(thread);
  459. return TRUE;
  460. }
  461. /// Check current running Thread Stack.
  462. //lint -esym(759,osRtxThreadStackCheck) "Prototype in header"
  463. //lint -esym(765,osRtxThreadStackCheck) "Global scope (can be overridden)"
  464. __WEAK void osRtxThreadStackCheck (void) {
  465. os_thread_t *thread;
  466. thread = osRtxThreadGetRunning();
  467. if (thread != NULL) {
  468. //lint -e{923} "cast from pointer to unsigned int"
  469. //lint -e{9079} -e{9087} "cast between pointers to different object types"
  470. if ((thread->sp <= (uint32_t)thread->stack_mem) ||
  471. (*((uint32_t *)thread->stack_mem) != osRtxStackMagicWord)) {
  472. (void)osRtxErrorNotify(osRtxErrorStackUnderflow, thread);
  473. }
  474. }
  475. }
  476. // ==== Post ISR processing ====
  477. /// Thread post ISR processing.
  478. /// \param[in] thread thread object.
  479. static void osRtxThreadPostProcess (os_thread_t *thread) {
  480. uint32_t thread_flags;
  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 and invalid
  965. thread->state = osRtxThreadInactive;
  966. thread->id = osRtxIdInvalid;
  967. #if (DOMAIN_NS == 1)
  968. // Free secure process stack
  969. if (thread->tz_memory != 0U) {
  970. (void)TZ_FreeModuleContext_S(thread->tz_memory);
  971. }
  972. #endif
  973. // Free stack memory
  974. if ((thread->flags & osRtxFlagSystemMemory) != 0U) {
  975. if ((thread->flags & osRtxThreadFlagDefStack) != 0U) {
  976. (void)osRtxMemoryPoolFree(osRtxInfo.mpi.stack, thread->stack_mem);
  977. } else {
  978. (void)osRtxMemoryFree(osRtxInfo.mem.stack, thread->stack_mem);
  979. }
  980. }
  981. // Free object memory
  982. if ((thread->flags & osRtxFlagSystemObject) != 0U) {
  983. if (osRtxInfo.mpi.thread != NULL) {
  984. (void)osRtxMemoryPoolFree(osRtxInfo.mpi.thread, thread);
  985. } else {
  986. (void)osRtxMemoryFree(osRtxInfo.mem.common, thread);
  987. }
  988. #if (defined(OS_OBJ_MEM_USAGE) && (OS_OBJ_MEM_USAGE != 0))
  989. osRtxThreadMemUsage.cnt_free++;
  990. #endif
  991. }
  992. }
  993. /// Detach a thread (thread storage can be reclaimed when thread terminates).
  994. /// \note API identical to osThreadDetach
  995. static osStatus_t svcRtxThreadDetach (osThreadId_t thread_id) {
  996. os_thread_t *thread = osRtxThreadId(thread_id);
  997. // Check parameters
  998. if ((thread == NULL) || (thread->id != osRtxIdThread)) {
  999. EvrRtxThreadError(thread, (int32_t)osErrorParameter);
  1000. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1001. return osErrorParameter;
  1002. }
  1003. // Check object state
  1004. if (thread->state == osRtxThreadInactive) {
  1005. EvrRtxThreadError(thread, (int32_t)osErrorResource);
  1006. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1007. return osErrorResource;
  1008. }
  1009. // Check object attributes
  1010. if ((thread->attr & osThreadJoinable) == 0U) {
  1011. EvrRtxThreadError(thread, osRtxErrorThreadNotJoinable);
  1012. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1013. return osErrorResource;
  1014. }
  1015. if (thread->state == osRtxThreadTerminated) {
  1016. osRtxThreadListUnlink(&osRtxInfo.thread.terminate_list, thread);
  1017. osRtxThreadFree(thread);
  1018. } else {
  1019. thread->attr &= ~osThreadJoinable;
  1020. }
  1021. EvrRtxThreadDetached(thread);
  1022. return osOK;
  1023. }
  1024. /// Wait for specified thread to terminate.
  1025. /// \note API identical to osThreadJoin
  1026. static osStatus_t svcRtxThreadJoin (osThreadId_t thread_id) {
  1027. os_thread_t *thread = osRtxThreadId(thread_id);
  1028. osStatus_t status;
  1029. // Check parameters
  1030. if ((thread == NULL) || (thread->id != osRtxIdThread)) {
  1031. EvrRtxThreadError(thread, (int32_t)osErrorParameter);
  1032. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1033. return osErrorParameter;
  1034. }
  1035. // Check object state
  1036. if ((thread->state == osRtxThreadInactive) ||
  1037. (thread->state == osRtxThreadRunning)) {
  1038. EvrRtxThreadError(thread, (int32_t)osErrorResource);
  1039. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1040. return osErrorResource;
  1041. }
  1042. // Check object attributes
  1043. if ((thread->attr & osThreadJoinable) == 0U) {
  1044. EvrRtxThreadError(thread, osRtxErrorThreadNotJoinable);
  1045. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1046. return osErrorResource;
  1047. }
  1048. if (thread->state == osRtxThreadTerminated) {
  1049. osRtxThreadListUnlink(&osRtxInfo.thread.terminate_list, thread);
  1050. osRtxThreadFree(thread);
  1051. EvrRtxThreadJoined(thread);
  1052. status = osOK;
  1053. } else {
  1054. // Suspend current Thread
  1055. if (osRtxThreadWaitEnter(osRtxThreadWaitingJoin, osWaitForever)) {
  1056. thread->thread_join = osRtxThreadGetRunning();
  1057. EvrRtxThreadJoinPending(thread);
  1058. } else {
  1059. EvrRtxThreadError(thread, (int32_t)osErrorResource);
  1060. }
  1061. status = osErrorResource;
  1062. }
  1063. return status;
  1064. }
  1065. /// Terminate execution of current running thread.
  1066. /// \note API identical to osThreadExit
  1067. static void svcRtxThreadExit (void) {
  1068. os_thread_t *thread;
  1069. // Check running thread
  1070. thread = osRtxThreadGetRunning();
  1071. if (thread == NULL) {
  1072. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1073. return;
  1074. }
  1075. // Check if switch to next Ready Thread is possible
  1076. if ((osRtxKernelGetState() != osRtxKernelRunning) ||
  1077. (osRtxInfo.thread.ready.thread_list == NULL)) {
  1078. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1079. return;
  1080. }
  1081. // Release owned Mutexes
  1082. osRtxMutexOwnerRelease(thread->mutex_list);
  1083. // Wakeup Thread waiting to Join
  1084. if (thread->thread_join != NULL) {
  1085. osRtxThreadWaitExit(thread->thread_join, (uint32_t)osOK, FALSE);
  1086. EvrRtxThreadJoined(thread->thread_join);
  1087. }
  1088. // Switch to next Ready Thread
  1089. thread->sp = __get_PSP();
  1090. osRtxThreadSwitch(osRtxThreadListGet(&osRtxInfo.thread.ready));
  1091. osRtxThreadSetRunning(NULL);
  1092. if (((thread->attr & osThreadJoinable) == 0U) || (thread->thread_join != NULL)) {
  1093. osRtxThreadFree(thread);
  1094. } else {
  1095. // Update Thread State and put it into Terminate Thread list
  1096. thread->state = osRtxThreadTerminated;
  1097. thread->thread_prev = NULL;
  1098. thread->thread_next = osRtxInfo.thread.terminate_list;
  1099. if (osRtxInfo.thread.terminate_list != NULL) {
  1100. osRtxInfo.thread.terminate_list->thread_prev = thread;
  1101. }
  1102. osRtxInfo.thread.terminate_list = thread;
  1103. }
  1104. EvrRtxThreadDestroyed(thread);
  1105. }
  1106. /// Terminate execution of a thread.
  1107. /// \note API identical to osThreadTerminate
  1108. static osStatus_t svcRtxThreadTerminate (osThreadId_t thread_id) {
  1109. os_thread_t *thread = osRtxThreadId(thread_id);
  1110. osStatus_t status;
  1111. // Check parameters
  1112. if ((thread == NULL) || (thread->id != osRtxIdThread)) {
  1113. EvrRtxThreadError(thread, (int32_t)osErrorParameter);
  1114. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1115. return osErrorParameter;
  1116. }
  1117. // Check object state
  1118. switch (thread->state & osRtxThreadStateMask) {
  1119. case osRtxThreadRunning:
  1120. if ((osRtxKernelGetState() != osRtxKernelRunning) ||
  1121. (osRtxInfo.thread.ready.thread_list == NULL)) {
  1122. EvrRtxThreadError(thread, (int32_t)osErrorResource);
  1123. status = osErrorResource;
  1124. } else {
  1125. status = osOK;
  1126. }
  1127. break;
  1128. case osRtxThreadReady:
  1129. osRtxThreadListRemove(thread);
  1130. status = osOK;
  1131. break;
  1132. case osRtxThreadBlocked:
  1133. osRtxThreadListRemove(thread);
  1134. osRtxThreadDelayRemove(thread);
  1135. status = osOK;
  1136. break;
  1137. case osRtxThreadInactive:
  1138. case osRtxThreadTerminated:
  1139. default:
  1140. EvrRtxThreadError(thread, (int32_t)osErrorResource);
  1141. status = osErrorResource;
  1142. break;
  1143. }
  1144. if (status == osOK) {
  1145. // Release owned Mutexes
  1146. osRtxMutexOwnerRelease(thread->mutex_list);
  1147. // Wakeup Thread waiting to Join
  1148. if (thread->thread_join != NULL) {
  1149. osRtxThreadWaitExit(thread->thread_join, (uint32_t)osOK, FALSE);
  1150. EvrRtxThreadJoined(thread->thread_join);
  1151. }
  1152. // Switch to next Ready Thread when terminating running Thread
  1153. if (thread->state == osRtxThreadRunning) {
  1154. thread->sp = __get_PSP();
  1155. osRtxThreadSwitch(osRtxThreadListGet(&osRtxInfo.thread.ready));
  1156. osRtxThreadSetRunning(NULL);
  1157. } else {
  1158. osRtxThreadDispatch(NULL);
  1159. }
  1160. if (((thread->attr & osThreadJoinable) == 0U) || (thread->thread_join != NULL)) {
  1161. osRtxThreadFree(thread);
  1162. } else {
  1163. // Update Thread State and put it into Terminate Thread list
  1164. thread->state = osRtxThreadTerminated;
  1165. thread->thread_prev = NULL;
  1166. thread->thread_next = osRtxInfo.thread.terminate_list;
  1167. if (osRtxInfo.thread.terminate_list != NULL) {
  1168. osRtxInfo.thread.terminate_list->thread_prev = thread;
  1169. }
  1170. osRtxInfo.thread.terminate_list = thread;
  1171. }
  1172. EvrRtxThreadDestroyed(thread);
  1173. }
  1174. return status;
  1175. }
  1176. /// Get number of active threads.
  1177. /// \note API identical to osThreadGetCount
  1178. static uint32_t svcRtxThreadGetCount (void) {
  1179. const os_thread_t *thread;
  1180. uint32_t count;
  1181. // Running Thread
  1182. count = 1U;
  1183. // Ready List
  1184. for (thread = osRtxInfo.thread.ready.thread_list;
  1185. thread != NULL; thread = thread->thread_next) {
  1186. count++;
  1187. }
  1188. // Delay List
  1189. for (thread = osRtxInfo.thread.delay_list;
  1190. thread != NULL; thread = thread->delay_next) {
  1191. count++;
  1192. }
  1193. // Wait List
  1194. for (thread = osRtxInfo.thread.wait_list;
  1195. thread != NULL; thread = thread->delay_next) {
  1196. count++;
  1197. }
  1198. EvrRtxThreadGetCount(count);
  1199. return count;
  1200. }
  1201. /// Enumerate active threads.
  1202. /// \note API identical to osThreadEnumerate
  1203. static uint32_t svcRtxThreadEnumerate (osThreadId_t *thread_array, uint32_t array_items) {
  1204. os_thread_t *thread;
  1205. uint32_t count;
  1206. // Check parameters
  1207. if ((thread_array == NULL) || (array_items == 0U)) {
  1208. EvrRtxThreadEnumerate(thread_array, array_items, 0U);
  1209. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1210. return 0U;
  1211. }
  1212. // Running Thread
  1213. *thread_array = osRtxThreadGetRunning();
  1214. thread_array++;
  1215. count = 1U;
  1216. // Ready List
  1217. for (thread = osRtxInfo.thread.ready.thread_list;
  1218. (thread != NULL) && (count < array_items); thread = thread->thread_next) {
  1219. *thread_array = thread;
  1220. thread_array++;
  1221. count++;
  1222. }
  1223. // Delay List
  1224. for (thread = osRtxInfo.thread.delay_list;
  1225. (thread != NULL) && (count < array_items); thread = thread->delay_next) {
  1226. *thread_array = thread;
  1227. thread_array++;
  1228. count++;
  1229. }
  1230. // Wait List
  1231. for (thread = osRtxInfo.thread.wait_list;
  1232. (thread != NULL) && (count < array_items); thread = thread->delay_next) {
  1233. *thread_array = thread;
  1234. thread_array++;
  1235. count++;
  1236. }
  1237. EvrRtxThreadEnumerate(thread_array - count, array_items, count);
  1238. return count;
  1239. }
  1240. /// Set the specified Thread Flags of a thread.
  1241. /// \note API identical to osThreadFlagsSet
  1242. static uint32_t svcRtxThreadFlagsSet (osThreadId_t thread_id, uint32_t flags) {
  1243. os_thread_t *thread = osRtxThreadId(thread_id);
  1244. uint32_t thread_flags;
  1245. uint32_t thread_flags0;
  1246. // Check parameters
  1247. if ((thread == NULL) || (thread->id != osRtxIdThread) ||
  1248. ((flags & ~(((uint32_t)1U << osRtxThreadFlagsLimit) - 1U)) != 0U)) {
  1249. EvrRtxThreadError(thread, (int32_t)osErrorParameter);
  1250. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1251. return ((uint32_t)osErrorParameter);
  1252. }
  1253. // Check object state
  1254. if ((thread->state == osRtxThreadInactive) ||
  1255. (thread->state == osRtxThreadTerminated)) {
  1256. EvrRtxThreadError(thread, (int32_t)osErrorResource);
  1257. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1258. return ((uint32_t)osErrorResource);
  1259. }
  1260. // Set Thread Flags
  1261. thread_flags = ThreadFlagsSet(thread, flags);
  1262. // Check if Thread is waiting for Thread Flags
  1263. if (thread->state == osRtxThreadWaitingThreadFlags) {
  1264. thread_flags0 = ThreadFlagsCheck(thread, thread->wait_flags, thread->flags_options);
  1265. if (thread_flags0 != 0U) {
  1266. if ((thread->flags_options & osFlagsNoClear) == 0U) {
  1267. thread_flags = thread_flags0 & ~thread->wait_flags;
  1268. } else {
  1269. thread_flags = thread_flags0;
  1270. }
  1271. osRtxThreadWaitExit(thread, thread_flags0, TRUE);
  1272. EvrRtxThreadFlagsWaitCompleted(thread->wait_flags, thread->flags_options, thread_flags0);
  1273. }
  1274. }
  1275. EvrRtxThreadFlagsSetDone(thread, thread_flags);
  1276. return thread_flags;
  1277. }
  1278. /// Clear the specified Thread Flags of current running thread.
  1279. /// \note API identical to osThreadFlagsClear
  1280. static uint32_t svcRtxThreadFlagsClear (uint32_t flags) {
  1281. os_thread_t *thread;
  1282. uint32_t thread_flags;
  1283. // Check running thread
  1284. thread = osRtxThreadGetRunning();
  1285. if (thread == NULL) {
  1286. EvrRtxThreadError(NULL, osRtxErrorKernelNotRunning);
  1287. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1288. return ((uint32_t)osError);
  1289. }
  1290. // Check parameters
  1291. if ((flags & ~(((uint32_t)1U << osRtxThreadFlagsLimit) - 1U)) != 0U) {
  1292. EvrRtxThreadError(thread, (int32_t)osErrorParameter);
  1293. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1294. return ((uint32_t)osErrorParameter);
  1295. }
  1296. // Clear Thread Flags
  1297. thread_flags = ThreadFlagsClear(thread, flags);
  1298. EvrRtxThreadFlagsClearDone(thread_flags);
  1299. return thread_flags;
  1300. }
  1301. /// Get the current Thread Flags of current running thread.
  1302. /// \note API identical to osThreadFlagsGet
  1303. static uint32_t svcRtxThreadFlagsGet (void) {
  1304. const os_thread_t *thread;
  1305. // Check running thread
  1306. thread = osRtxThreadGetRunning();
  1307. if (thread == NULL) {
  1308. EvrRtxThreadFlagsGet(0U);
  1309. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1310. return 0U;
  1311. }
  1312. EvrRtxThreadFlagsGet(thread->thread_flags);
  1313. return thread->thread_flags;
  1314. }
  1315. /// Wait for one or more Thread Flags of the current running thread to become signaled.
  1316. /// \note API identical to osThreadFlagsWait
  1317. static uint32_t svcRtxThreadFlagsWait (uint32_t flags, uint32_t options, uint32_t timeout) {
  1318. os_thread_t *thread;
  1319. uint32_t thread_flags;
  1320. // Check running thread
  1321. thread = osRtxThreadGetRunning();
  1322. if (thread == NULL) {
  1323. EvrRtxThreadError(NULL, osRtxErrorKernelNotRunning);
  1324. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1325. return ((uint32_t)osError);
  1326. }
  1327. // Check parameters
  1328. if ((flags & ~(((uint32_t)1U << osRtxThreadFlagsLimit) - 1U)) != 0U) {
  1329. EvrRtxThreadError(thread, (int32_t)osErrorParameter);
  1330. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1331. return ((uint32_t)osErrorParameter);
  1332. }
  1333. // Check Thread Flags
  1334. thread_flags = ThreadFlagsCheck(thread, flags, options);
  1335. if (thread_flags != 0U) {
  1336. EvrRtxThreadFlagsWaitCompleted(flags, options, thread_flags);
  1337. } else {
  1338. // Check if timeout is specified
  1339. if (timeout != 0U) {
  1340. // Store waiting flags and options
  1341. EvrRtxThreadFlagsWaitPending(flags, options, timeout);
  1342. thread->wait_flags = flags;
  1343. thread->flags_options = (uint8_t)options;
  1344. // Suspend current Thread
  1345. if (!osRtxThreadWaitEnter(osRtxThreadWaitingThreadFlags, timeout)) {
  1346. EvrRtxThreadFlagsWaitTimeout();
  1347. }
  1348. thread_flags = (uint32_t)osErrorTimeout;
  1349. } else {
  1350. EvrRtxThreadFlagsWaitNotCompleted(flags, options);
  1351. thread_flags = (uint32_t)osErrorResource;
  1352. }
  1353. }
  1354. return thread_flags;
  1355. }
  1356. // Service Calls definitions
  1357. //lint ++flb "Library Begin" [MISRA Note 11]
  1358. SVC0_3 (ThreadNew, osThreadId_t, osThreadFunc_t, void *, const osThreadAttr_t *)
  1359. SVC0_1 (ThreadGetName, const char *, osThreadId_t)
  1360. SVC0_0 (ThreadGetId, osThreadId_t)
  1361. SVC0_1 (ThreadGetState, osThreadState_t, osThreadId_t)
  1362. SVC0_1 (ThreadGetStackSize, uint32_t, osThreadId_t)
  1363. SVC0_1 (ThreadGetStackSpace, uint32_t, osThreadId_t)
  1364. SVC0_2 (ThreadSetPriority, osStatus_t, osThreadId_t, osPriority_t)
  1365. SVC0_1 (ThreadGetPriority, osPriority_t, osThreadId_t)
  1366. SVC0_0 (ThreadYield, osStatus_t)
  1367. SVC0_1 (ThreadSuspend, osStatus_t, osThreadId_t)
  1368. SVC0_1 (ThreadResume, osStatus_t, osThreadId_t)
  1369. SVC0_1 (ThreadDetach, osStatus_t, osThreadId_t)
  1370. SVC0_1 (ThreadJoin, osStatus_t, osThreadId_t)
  1371. SVC0_0N(ThreadExit, void)
  1372. SVC0_1 (ThreadTerminate, osStatus_t, osThreadId_t)
  1373. SVC0_0 (ThreadGetCount, uint32_t)
  1374. SVC0_2 (ThreadEnumerate, uint32_t, osThreadId_t *, uint32_t)
  1375. SVC0_2 (ThreadFlagsSet, uint32_t, osThreadId_t, uint32_t)
  1376. SVC0_1 (ThreadFlagsClear, uint32_t, uint32_t)
  1377. SVC0_0 (ThreadFlagsGet, uint32_t)
  1378. SVC0_3 (ThreadFlagsWait, uint32_t, uint32_t, uint32_t, uint32_t)
  1379. //lint --flb "Library End"
  1380. // ==== ISR Calls ====
  1381. /// Set the specified Thread Flags of a thread.
  1382. /// \note API identical to osThreadFlagsSet
  1383. __STATIC_INLINE
  1384. uint32_t isrRtxThreadFlagsSet (osThreadId_t thread_id, uint32_t flags) {
  1385. os_thread_t *thread = osRtxThreadId(thread_id);
  1386. uint32_t thread_flags;
  1387. // Check parameters
  1388. if ((thread == NULL) || (thread->id != osRtxIdThread) ||
  1389. ((flags & ~(((uint32_t)1U << osRtxThreadFlagsLimit) - 1U)) != 0U)) {
  1390. EvrRtxThreadError(thread, (int32_t)osErrorParameter);
  1391. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1392. return ((uint32_t)osErrorParameter);
  1393. }
  1394. // Check object state
  1395. if ((thread->state == osRtxThreadInactive) ||
  1396. (thread->state == osRtxThreadTerminated)) {
  1397. EvrRtxThreadError(thread, (int32_t)osErrorResource);
  1398. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1399. return ((uint32_t)osErrorResource);
  1400. }
  1401. // Set Thread Flags
  1402. thread_flags = ThreadFlagsSet(thread, flags);
  1403. // Register post ISR processing
  1404. osRtxPostProcess(osRtxObject(thread));
  1405. EvrRtxThreadFlagsSetDone(thread, thread_flags);
  1406. return thread_flags;
  1407. }
  1408. // ==== Library functions ====
  1409. /// Thread startup (Idle and Timer Thread).
  1410. /// \return true - success, false - failure.
  1411. bool_t osRtxThreadStartup (void) {
  1412. bool_t ret = TRUE;
  1413. // Create Idle Thread
  1414. if (osRtxInfo.thread.idle == NULL) {
  1415. osRtxInfo.thread.idle = osRtxThreadId(
  1416. svcRtxThreadNew(osRtxIdleThread, NULL, osRtxConfig.idle_thread_attr)
  1417. );
  1418. if (osRtxInfo.thread.idle == NULL) {
  1419. ret = FALSE;
  1420. }
  1421. }
  1422. // Create Timer Thread
  1423. if (osRtxConfig.timer_mq_mcnt != 0U) {
  1424. if (osRtxInfo.timer.thread == NULL) {
  1425. osRtxInfo.timer.thread = osRtxThreadId(
  1426. svcRtxThreadNew(osRtxTimerThread, NULL, osRtxConfig.timer_thread_attr)
  1427. );
  1428. if (osRtxInfo.timer.thread == NULL) {
  1429. ret = FALSE;
  1430. }
  1431. }
  1432. }
  1433. return ret;
  1434. }
  1435. // ==== Public API ====
  1436. /// Create a thread and add it to Active Threads.
  1437. osThreadId_t osThreadNew (osThreadFunc_t func, void *argument, const osThreadAttr_t *attr) {
  1438. osThreadId_t thread_id;
  1439. EvrRtxThreadNew(func, argument, attr);
  1440. if (IsIrqMode() || IsIrqMasked()) {
  1441. EvrRtxThreadError(NULL, (int32_t)osErrorISR);
  1442. thread_id = NULL;
  1443. } else {
  1444. thread_id = __svcThreadNew(func, argument, attr);
  1445. }
  1446. return thread_id;
  1447. }
  1448. /// Get name of a thread.
  1449. const char *osThreadGetName (osThreadId_t thread_id) {
  1450. const char *name;
  1451. if (IsIrqMode() || IsIrqMasked()) {
  1452. EvrRtxThreadGetName(thread_id, NULL);
  1453. name = NULL;
  1454. } else {
  1455. name = __svcThreadGetName(thread_id);
  1456. }
  1457. return name;
  1458. }
  1459. /// Return the thread ID of the current running thread.
  1460. osThreadId_t osThreadGetId (void) {
  1461. osThreadId_t thread_id;
  1462. if (IsIrqMode() || IsIrqMasked()) {
  1463. EvrRtxThreadGetId(NULL);
  1464. thread_id = NULL;
  1465. } else {
  1466. thread_id = __svcThreadGetId();
  1467. }
  1468. return thread_id;
  1469. }
  1470. /// Get current thread state of a thread.
  1471. osThreadState_t osThreadGetState (osThreadId_t thread_id) {
  1472. osThreadState_t state;
  1473. if (IsIrqMode() || IsIrqMasked()) {
  1474. EvrRtxThreadGetState(thread_id, osThreadError);
  1475. state = osThreadError;
  1476. } else {
  1477. state = __svcThreadGetState(thread_id);
  1478. }
  1479. return state;
  1480. }
  1481. /// Get stack size of a thread.
  1482. uint32_t osThreadGetStackSize (osThreadId_t thread_id) {
  1483. uint32_t stack_size;
  1484. if (IsIrqMode() || IsIrqMasked()) {
  1485. EvrRtxThreadGetStackSize(thread_id, 0U);
  1486. stack_size = 0U;
  1487. } else {
  1488. stack_size = __svcThreadGetStackSize(thread_id);
  1489. }
  1490. return stack_size;
  1491. }
  1492. /// Get available stack space of a thread based on stack watermark recording during execution.
  1493. uint32_t osThreadGetStackSpace (osThreadId_t thread_id) {
  1494. uint32_t stack_space;
  1495. if (IsIrqMode() || IsIrqMasked()) {
  1496. EvrRtxThreadGetStackSpace(thread_id, 0U);
  1497. stack_space = 0U;
  1498. } else {
  1499. stack_space = __svcThreadGetStackSpace(thread_id);
  1500. }
  1501. return stack_space;
  1502. }
  1503. /// Change priority of a thread.
  1504. osStatus_t osThreadSetPriority (osThreadId_t thread_id, osPriority_t priority) {
  1505. osStatus_t status;
  1506. EvrRtxThreadSetPriority(thread_id, priority);
  1507. if (IsIrqMode() || IsIrqMasked()) {
  1508. EvrRtxThreadError(thread_id, (int32_t)osErrorISR);
  1509. status = osErrorISR;
  1510. } else {
  1511. status = __svcThreadSetPriority(thread_id, priority);
  1512. }
  1513. return status;
  1514. }
  1515. /// Get current priority of a thread.
  1516. osPriority_t osThreadGetPriority (osThreadId_t thread_id) {
  1517. osPriority_t priority;
  1518. if (IsIrqMode() || IsIrqMasked()) {
  1519. EvrRtxThreadGetPriority(thread_id, osPriorityError);
  1520. priority = osPriorityError;
  1521. } else {
  1522. priority = __svcThreadGetPriority(thread_id);
  1523. }
  1524. return priority;
  1525. }
  1526. /// Pass control to next thread that is in state READY.
  1527. osStatus_t osThreadYield (void) {
  1528. osStatus_t status;
  1529. EvrRtxThreadYield();
  1530. if (IsIrqMode() || IsIrqMasked()) {
  1531. EvrRtxThreadError(NULL, (int32_t)osErrorISR);
  1532. status = osErrorISR;
  1533. } else {
  1534. status = __svcThreadYield();
  1535. }
  1536. return status;
  1537. }
  1538. /// Suspend execution of a thread.
  1539. osStatus_t osThreadSuspend (osThreadId_t thread_id) {
  1540. osStatus_t status;
  1541. EvrRtxThreadSuspend(thread_id);
  1542. if (IsIrqMode() || IsIrqMasked()) {
  1543. EvrRtxThreadError(thread_id, (int32_t)osErrorISR);
  1544. status = osErrorISR;
  1545. } else {
  1546. status = __svcThreadSuspend(thread_id);
  1547. }
  1548. return status;
  1549. }
  1550. /// Resume execution of a thread.
  1551. osStatus_t osThreadResume (osThreadId_t thread_id) {
  1552. osStatus_t status;
  1553. EvrRtxThreadResume(thread_id);
  1554. if (IsIrqMode() || IsIrqMasked()) {
  1555. EvrRtxThreadError(thread_id, (int32_t)osErrorISR);
  1556. status = osErrorISR;
  1557. } else {
  1558. status = __svcThreadResume(thread_id);
  1559. }
  1560. return status;
  1561. }
  1562. /// Detach a thread (thread storage can be reclaimed when thread terminates).
  1563. osStatus_t osThreadDetach (osThreadId_t thread_id) {
  1564. osStatus_t status;
  1565. EvrRtxThreadDetach(thread_id);
  1566. if (IsIrqMode() || IsIrqMasked()) {
  1567. EvrRtxThreadError(thread_id, (int32_t)osErrorISR);
  1568. status = osErrorISR;
  1569. } else {
  1570. status = __svcThreadDetach(thread_id);
  1571. }
  1572. return status;
  1573. }
  1574. /// Wait for specified thread to terminate.
  1575. osStatus_t osThreadJoin (osThreadId_t thread_id) {
  1576. osStatus_t status;
  1577. EvrRtxThreadJoin(thread_id);
  1578. if (IsIrqMode() || IsIrqMasked()) {
  1579. EvrRtxThreadError(thread_id, (int32_t)osErrorISR);
  1580. status = osErrorISR;
  1581. } else {
  1582. status = __svcThreadJoin(thread_id);
  1583. }
  1584. return status;
  1585. }
  1586. /// Terminate execution of current running thread.
  1587. __NO_RETURN void osThreadExit (void) {
  1588. EvrRtxThreadExit();
  1589. __svcThreadExit();
  1590. EvrRtxThreadError(NULL, (int32_t)osError);
  1591. for (;;) {}
  1592. }
  1593. /// Terminate execution of a thread.
  1594. osStatus_t osThreadTerminate (osThreadId_t thread_id) {
  1595. osStatus_t status;
  1596. EvrRtxThreadTerminate(thread_id);
  1597. if (IsIrqMode() || IsIrqMasked()) {
  1598. EvrRtxThreadError(thread_id, (int32_t)osErrorISR);
  1599. status = osErrorISR;
  1600. } else {
  1601. status = __svcThreadTerminate(thread_id);
  1602. }
  1603. return status;
  1604. }
  1605. /// Get number of active threads.
  1606. uint32_t osThreadGetCount (void) {
  1607. uint32_t count;
  1608. if (IsIrqMode() || IsIrqMasked()) {
  1609. EvrRtxThreadGetCount(0U);
  1610. count = 0U;
  1611. } else {
  1612. count = __svcThreadGetCount();
  1613. }
  1614. return count;
  1615. }
  1616. /// Enumerate active threads.
  1617. uint32_t osThreadEnumerate (osThreadId_t *thread_array, uint32_t array_items) {
  1618. uint32_t count;
  1619. if (IsIrqMode() || IsIrqMasked()) {
  1620. EvrRtxThreadEnumerate(thread_array, array_items, 0U);
  1621. count = 0U;
  1622. } else {
  1623. count = __svcThreadEnumerate(thread_array, array_items);
  1624. }
  1625. return count;
  1626. }
  1627. /// Set the specified Thread Flags of a thread.
  1628. uint32_t osThreadFlagsSet (osThreadId_t thread_id, uint32_t flags) {
  1629. uint32_t thread_flags;
  1630. EvrRtxThreadFlagsSet(thread_id, flags);
  1631. if (IsIrqMode() || IsIrqMasked()) {
  1632. thread_flags = isrRtxThreadFlagsSet(thread_id, flags);
  1633. } else {
  1634. thread_flags = __svcThreadFlagsSet(thread_id, flags);
  1635. }
  1636. return thread_flags;
  1637. }
  1638. /// Clear the specified Thread Flags of current running thread.
  1639. uint32_t osThreadFlagsClear (uint32_t flags) {
  1640. uint32_t thread_flags;
  1641. EvrRtxThreadFlagsClear(flags);
  1642. if (IsIrqMode() || IsIrqMasked()) {
  1643. EvrRtxThreadError(NULL, (int32_t)osErrorISR);
  1644. thread_flags = (uint32_t)osErrorISR;
  1645. } else {
  1646. thread_flags = __svcThreadFlagsClear(flags);
  1647. }
  1648. return thread_flags;
  1649. }
  1650. /// Get the current Thread Flags of current running thread.
  1651. uint32_t osThreadFlagsGet (void) {
  1652. uint32_t thread_flags;
  1653. if (IsIrqMode() || IsIrqMasked()) {
  1654. EvrRtxThreadFlagsGet(0U);
  1655. thread_flags = 0U;
  1656. } else {
  1657. thread_flags = __svcThreadFlagsGet();
  1658. }
  1659. return thread_flags;
  1660. }
  1661. /// Wait for one or more Thread Flags of the current running thread to become signaled.
  1662. uint32_t osThreadFlagsWait (uint32_t flags, uint32_t options, uint32_t timeout) {
  1663. uint32_t thread_flags;
  1664. EvrRtxThreadFlagsWait(flags, options, timeout);
  1665. if (IsIrqMode() || IsIrqMasked()) {
  1666. EvrRtxThreadError(NULL, (int32_t)osErrorISR);
  1667. thread_flags = (uint32_t)osErrorISR;
  1668. } else {
  1669. thread_flags = __svcThreadFlagsWait(flags, options, timeout);
  1670. }
  1671. return thread_flags;
  1672. }