rtx_thread.c 56 KB

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