rtx_thread.c 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795
  1. /*
  2. * Copyright (c) 2013-2023 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. #ifdef RTX_OBJ_MEM_USAGE
  28. osRtxObjectMemUsage_t osRtxThreadMemUsage \
  29. __attribute__((section(".data.os.thread.obj"))) =
  30. { 0U, 0U, 0U };
  31. #endif
  32. // Runtime Class/Zone assignment table
  33. #if defined(RTX_EXECUTION_ZONE) && defined(RTX_SAFETY_CLASS)
  34. static uint8_t ThreadClassTable[64] __attribute__((section(".data.os"))) = { 0U };
  35. #endif
  36. // Watchdog Alarm Flag
  37. #if defined(RTX_THREAD_WATCHDOG) && defined(RTX_EXECUTION_ZONE)
  38. static uint8_t WatchdogAlarmFlag __attribute__((section(".data.os"))) = 0U;
  39. #endif
  40. // ==== Helper functions ====
  41. /// Set Thread Flags.
  42. /// \param[in] thread thread object.
  43. /// \param[in] flags specifies the flags to set.
  44. /// \return thread flags after setting.
  45. static uint32_t ThreadFlagsSet (os_thread_t *thread, uint32_t flags) {
  46. #if (EXCLUSIVE_ACCESS == 0)
  47. uint32_t primask = __get_PRIMASK();
  48. #endif
  49. uint32_t thread_flags;
  50. #if (EXCLUSIVE_ACCESS == 0)
  51. __disable_irq();
  52. thread->thread_flags |= flags;
  53. thread_flags = thread->thread_flags;
  54. if (primask == 0U) {
  55. __enable_irq();
  56. }
  57. #else
  58. thread_flags = atomic_set32(&thread->thread_flags, flags);
  59. #endif
  60. return thread_flags;
  61. }
  62. /// Clear Thread Flags.
  63. /// \param[in] thread thread object.
  64. /// \param[in] flags specifies the flags to clear.
  65. /// \return thread flags before clearing.
  66. static uint32_t ThreadFlagsClear (os_thread_t *thread, uint32_t flags) {
  67. #if (EXCLUSIVE_ACCESS == 0)
  68. uint32_t primask = __get_PRIMASK();
  69. #endif
  70. uint32_t thread_flags;
  71. #if (EXCLUSIVE_ACCESS == 0)
  72. __disable_irq();
  73. thread_flags = thread->thread_flags;
  74. thread->thread_flags &= ~flags;
  75. if (primask == 0U) {
  76. __enable_irq();
  77. }
  78. #else
  79. thread_flags = atomic_clr32(&thread->thread_flags, flags);
  80. #endif
  81. return thread_flags;
  82. }
  83. /// Check Thread Flags.
  84. /// \param[in] thread thread object.
  85. /// \param[in] flags specifies the flags to check.
  86. /// \param[in] options specifies flags options (osFlagsXxxx).
  87. /// \return thread flags before clearing or 0 if specified flags have not been set.
  88. static uint32_t ThreadFlagsCheck (os_thread_t *thread, uint32_t flags, uint32_t options) {
  89. #if (EXCLUSIVE_ACCESS == 0)
  90. uint32_t primask;
  91. #endif
  92. uint32_t thread_flags;
  93. if ((options & osFlagsNoClear) == 0U) {
  94. #if (EXCLUSIVE_ACCESS == 0)
  95. primask = __get_PRIMASK();
  96. __disable_irq();
  97. thread_flags = thread->thread_flags;
  98. if ((((options & osFlagsWaitAll) != 0U) && ((thread_flags & flags) != flags)) ||
  99. (((options & osFlagsWaitAll) == 0U) && ((thread_flags & flags) == 0U))) {
  100. thread_flags = 0U;
  101. } else {
  102. thread->thread_flags &= ~flags;
  103. }
  104. if (primask == 0U) {
  105. __enable_irq();
  106. }
  107. #else
  108. if ((options & osFlagsWaitAll) != 0U) {
  109. thread_flags = atomic_chk32_all(&thread->thread_flags, flags);
  110. } else {
  111. thread_flags = atomic_chk32_any(&thread->thread_flags, flags);
  112. }
  113. #endif
  114. } else {
  115. thread_flags = thread->thread_flags;
  116. if ((((options & osFlagsWaitAll) != 0U) && ((thread_flags & flags) != flags)) ||
  117. (((options & osFlagsWaitAll) == 0U) && ((thread_flags & flags) == 0U))) {
  118. thread_flags = 0U;
  119. }
  120. }
  121. return thread_flags;
  122. }
  123. /// Verify that Thread object pointer is valid.
  124. /// \param[in] thread thread object.
  125. /// \return true - valid, false - invalid.
  126. static bool_t IsThreadPtrValid (const os_thread_t *thread) {
  127. #ifdef RTX_OBJ_PTR_CHECK
  128. //lint --e{923} --e{9078} "cast from pointer to unsigned int" [MISRA Note 7]
  129. uint32_t cb_start = (uint32_t)&__os_thread_cb_start__;
  130. uint32_t cb_length = (uint32_t)&__os_thread_cb_length__;
  131. // Check the section boundaries
  132. if (((uint32_t)thread - cb_start) >= cb_length) {
  133. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  134. return FALSE;
  135. }
  136. // Check the object alignment
  137. if ((((uint32_t)thread - cb_start) % sizeof(os_thread_t)) != 0U) {
  138. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  139. return FALSE;
  140. }
  141. #else
  142. // Check NULL pointer
  143. if (thread == NULL) {
  144. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  145. return FALSE;
  146. }
  147. #endif
  148. return TRUE;
  149. }
  150. #if defined(RTX_EXECUTION_ZONE) && defined(RTX_SAFETY_CLASS)
  151. /// Check if Thread Zone to Safety Class mapping is valid.
  152. /// \param[in] attr_bits thread attributes.
  153. /// \param[in] thread running thread.
  154. /// \return true - valid, false - not valid.
  155. static bool_t IsClassMappingValid (uint32_t attr_bits, const os_thread_t *thread) {
  156. uint32_t safety_class;
  157. uint32_t zone;
  158. if ((attr_bits & osThreadZone_Valid) != 0U) {
  159. zone = (attr_bits & osThreadZone_Msk) >> osThreadZone_Pos;
  160. } else if (thread != NULL) {
  161. zone = thread->zone;
  162. } else {
  163. zone = 0U;
  164. }
  165. if ((attr_bits & osSafetyClass_Valid) != 0U) {
  166. safety_class = (attr_bits & osSafetyClass_Msk) >> osSafetyClass_Pos;
  167. } else if (thread != NULL) {
  168. safety_class = (uint32_t)thread->attr >> osRtxAttrClass_Pos;
  169. } else {
  170. safety_class = 0U;
  171. }
  172. // Check if zone is free or assigned to class
  173. if ((ThreadClassTable[zone] == 0U) ||
  174. (ThreadClassTable[zone] == (0x80U | safety_class))) {
  175. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  176. return TRUE;
  177. }
  178. // Invalid class to zone mapping
  179. return FALSE;
  180. }
  181. #endif
  182. // ==== Library functions ====
  183. /// Put a Thread into specified Object list sorted by Priority (Highest at Head).
  184. /// \param[in] object generic object.
  185. /// \param[in] thread thread object.
  186. void osRtxThreadListPut (os_object_t *object, os_thread_t *thread) {
  187. os_thread_t *prev, *next;
  188. int32_t priority;
  189. priority = thread->priority;
  190. prev = osRtxThreadObject(object);
  191. next = prev->thread_next;
  192. while ((next != NULL) && (next->priority >= priority)) {
  193. prev = next;
  194. next = next->thread_next;
  195. }
  196. thread->thread_prev = prev;
  197. thread->thread_next = next;
  198. prev->thread_next = thread;
  199. if (next != NULL) {
  200. next->thread_prev = thread;
  201. }
  202. }
  203. /// Get a Thread with Highest Priority from specified Object list and remove it.
  204. /// \param[in] object generic object.
  205. /// \return thread object.
  206. os_thread_t *osRtxThreadListGet (os_object_t *object) {
  207. os_thread_t *thread;
  208. thread = object->thread_list;
  209. object->thread_list = thread->thread_next;
  210. if (thread->thread_next != NULL) {
  211. thread->thread_next->thread_prev = osRtxThreadObject(object);
  212. }
  213. thread->thread_prev = NULL;
  214. return thread;
  215. }
  216. /// Retrieve Thread list root object.
  217. /// \param[in] thread thread object.
  218. /// \return root object.
  219. static void *osRtxThreadListRoot (os_thread_t *thread) {
  220. os_thread_t *thread0;
  221. thread0 = thread;
  222. while (thread0->id == osRtxIdThread) {
  223. thread0 = thread0->thread_prev;
  224. }
  225. return thread0;
  226. }
  227. /// Re-sort a Thread in linked Object list by Priority (Highest at Head).
  228. /// \param[in] thread thread object.
  229. void osRtxThreadListSort (os_thread_t *thread) {
  230. os_object_t *object;
  231. os_thread_t *thread0;
  232. // Search for object
  233. thread0 = thread;
  234. while ((thread0 != NULL) && (thread0->id == osRtxIdThread)) {
  235. thread0 = thread0->thread_prev;
  236. }
  237. object = osRtxObject(thread0);
  238. if (object != NULL) {
  239. osRtxThreadListRemove(thread);
  240. osRtxThreadListPut(object, thread);
  241. }
  242. }
  243. /// Remove a Thread from linked Object list.
  244. /// \param[in] thread thread object.
  245. void osRtxThreadListRemove (os_thread_t *thread) {
  246. if (thread->thread_prev != NULL) {
  247. thread->thread_prev->thread_next = thread->thread_next;
  248. if (thread->thread_next != NULL) {
  249. thread->thread_next->thread_prev = thread->thread_prev;
  250. }
  251. thread->thread_prev = NULL;
  252. }
  253. }
  254. /// Unlink a Thread from specified linked list.
  255. /// \param[in] thread thread object.
  256. static void osRtxThreadListUnlink (os_thread_t **thread_list, os_thread_t *thread) {
  257. if (thread->thread_next != NULL) {
  258. thread->thread_next->thread_prev = thread->thread_prev;
  259. }
  260. if (thread->thread_prev != NULL) {
  261. thread->thread_prev->thread_next = thread->thread_next;
  262. thread->thread_prev = NULL;
  263. } else {
  264. *thread_list = thread->thread_next;
  265. }
  266. }
  267. /// Mark a Thread as Ready and put it into Ready list (sorted by Priority).
  268. /// \param[in] thread thread object.
  269. void osRtxThreadReadyPut (os_thread_t *thread) {
  270. thread->state = osRtxThreadReady;
  271. osRtxThreadListPut(&osRtxInfo.thread.ready, thread);
  272. }
  273. /// Insert a Thread into the Delay list sorted by Delay (Lowest at Head).
  274. /// \param[in] thread thread object.
  275. /// \param[in] delay delay value.
  276. static void osRtxThreadDelayInsert (os_thread_t *thread, uint32_t delay) {
  277. os_thread_t *prev, *next;
  278. if (delay == osWaitForever) {
  279. prev = NULL;
  280. next = osRtxInfo.thread.wait_list;
  281. while (next != NULL) {
  282. prev = next;
  283. next = next->delay_next;
  284. }
  285. thread->delay = delay;
  286. thread->delay_prev = prev;
  287. thread->delay_next = NULL;
  288. if (prev != NULL) {
  289. prev->delay_next = thread;
  290. } else {
  291. osRtxInfo.thread.wait_list = thread;
  292. }
  293. } else {
  294. prev = NULL;
  295. next = osRtxInfo.thread.delay_list;
  296. while ((next != NULL) && (next->delay <= delay)) {
  297. delay -= next->delay;
  298. prev = next;
  299. next = next->delay_next;
  300. }
  301. thread->delay = delay;
  302. thread->delay_prev = prev;
  303. thread->delay_next = next;
  304. if (prev != NULL) {
  305. prev->delay_next = thread;
  306. } else {
  307. osRtxInfo.thread.delay_list = thread;
  308. }
  309. if (next != NULL) {
  310. next->delay -= delay;
  311. next->delay_prev = thread;
  312. }
  313. }
  314. }
  315. /// Remove a Thread from the Delay list.
  316. /// \param[in] thread thread object.
  317. void osRtxThreadDelayRemove (os_thread_t *thread) {
  318. if (thread->delay == osWaitForever) {
  319. if (thread->delay_next != NULL) {
  320. thread->delay_next->delay_prev = thread->delay_prev;
  321. }
  322. if (thread->delay_prev != NULL) {
  323. thread->delay_prev->delay_next = thread->delay_next;
  324. thread->delay_prev = NULL;
  325. } else {
  326. osRtxInfo.thread.wait_list = thread->delay_next;
  327. }
  328. } else {
  329. if (thread->delay_next != NULL) {
  330. thread->delay_next->delay += thread->delay;
  331. thread->delay_next->delay_prev = thread->delay_prev;
  332. }
  333. if (thread->delay_prev != NULL) {
  334. thread->delay_prev->delay_next = thread->delay_next;
  335. thread->delay_prev = NULL;
  336. } else {
  337. osRtxInfo.thread.delay_list = thread->delay_next;
  338. }
  339. }
  340. thread->delay = 0U;
  341. }
  342. /// Process Thread Delay Tick (executed each System Tick).
  343. void osRtxThreadDelayTick (void) {
  344. os_thread_t *thread;
  345. os_object_t *object;
  346. thread = osRtxInfo.thread.delay_list;
  347. if (thread == NULL) {
  348. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  349. return;
  350. }
  351. thread->delay--;
  352. if (thread->delay == 0U) {
  353. do {
  354. switch (thread->state) {
  355. case osRtxThreadWaitingDelay:
  356. EvrRtxDelayCompleted(thread);
  357. break;
  358. case osRtxThreadWaitingThreadFlags:
  359. EvrRtxThreadFlagsWaitTimeout(thread);
  360. break;
  361. case osRtxThreadWaitingEventFlags:
  362. EvrRtxEventFlagsWaitTimeout((osEventFlagsId_t)osRtxThreadListRoot(thread));
  363. break;
  364. case osRtxThreadWaitingMutex:
  365. object = osRtxObject(osRtxThreadListRoot(thread));
  366. osRtxMutexOwnerRestore(osRtxMutexObject(object), thread);
  367. EvrRtxMutexAcquireTimeout(osRtxMutexObject(object));
  368. break;
  369. case osRtxThreadWaitingSemaphore:
  370. EvrRtxSemaphoreAcquireTimeout((osSemaphoreId_t)osRtxThreadListRoot(thread));
  371. break;
  372. case osRtxThreadWaitingMemoryPool:
  373. EvrRtxMemoryPoolAllocTimeout((osMemoryPoolId_t)osRtxThreadListRoot(thread));
  374. break;
  375. case osRtxThreadWaitingMessageGet:
  376. EvrRtxMessageQueueGetTimeout((osMessageQueueId_t)osRtxThreadListRoot(thread));
  377. break;
  378. case osRtxThreadWaitingMessagePut:
  379. EvrRtxMessageQueuePutTimeout((osMessageQueueId_t)osRtxThreadListRoot(thread));
  380. break;
  381. default:
  382. // Invalid
  383. break;
  384. }
  385. EvrRtxThreadUnblocked(thread, (osRtxThreadRegPtr(thread))[0]);
  386. osRtxThreadListRemove(thread);
  387. osRtxThreadReadyPut(thread);
  388. thread = thread->delay_next;
  389. } while ((thread != NULL) && (thread->delay == 0U));
  390. if (thread != NULL) {
  391. thread->delay_prev = NULL;
  392. }
  393. osRtxInfo.thread.delay_list = thread;
  394. }
  395. }
  396. /// Get pointer to Thread registers (R0..R3)
  397. /// \param[in] thread thread object.
  398. /// \return pointer to registers R0-R3.
  399. uint32_t *osRtxThreadRegPtr (const os_thread_t *thread) {
  400. uint32_t addr = thread->sp + StackOffsetR0(thread->stack_frame);
  401. //lint -e{923} -e{9078} "cast from unsigned int to pointer"
  402. return ((uint32_t *)addr);
  403. }
  404. /// Block running Thread execution and register it as Ready to Run.
  405. /// \param[in] thread running thread object.
  406. static void osRtxThreadBlock (os_thread_t *thread) {
  407. os_thread_t *prev, *next;
  408. int32_t priority;
  409. thread->state = osRtxThreadReady;
  410. priority = thread->priority;
  411. prev = osRtxThreadObject(&osRtxInfo.thread.ready);
  412. next = prev->thread_next;
  413. while ((next != NULL) && (next->priority > priority)) {
  414. prev = next;
  415. next = next->thread_next;
  416. }
  417. thread->thread_prev = prev;
  418. thread->thread_next = next;
  419. prev->thread_next = thread;
  420. if (next != NULL) {
  421. next->thread_prev = thread;
  422. }
  423. EvrRtxThreadPreempted(thread);
  424. }
  425. /// Switch to specified Thread.
  426. /// \param[in] thread thread object.
  427. void osRtxThreadSwitch (os_thread_t *thread) {
  428. thread->state = osRtxThreadRunning;
  429. SetPrivileged((bool_t)((thread->attr & osThreadPrivileged) != 0U));
  430. osRtxInfo.thread.run.next = thread;
  431. EvrRtxThreadSwitched(thread);
  432. }
  433. /// Dispatch specified Thread or Ready Thread with Highest Priority.
  434. /// \param[in] thread thread object or NULL.
  435. void osRtxThreadDispatch (os_thread_t *thread) {
  436. uint8_t kernel_state;
  437. os_thread_t *thread_running;
  438. os_thread_t *thread_ready;
  439. kernel_state = osRtxKernelGetState();
  440. thread_running = osRtxThreadGetRunning();
  441. if (thread == NULL) {
  442. thread_ready = osRtxInfo.thread.ready.thread_list;
  443. if ((kernel_state == osRtxKernelRunning) &&
  444. (thread_ready != NULL) &&
  445. (thread_ready->priority > thread_running->priority)) {
  446. // Preempt running Thread
  447. osRtxThreadListRemove(thread_ready);
  448. osRtxThreadBlock(thread_running);
  449. osRtxThreadSwitch(thread_ready);
  450. }
  451. } else {
  452. if ((kernel_state == osRtxKernelRunning) &&
  453. (thread->priority > thread_running->priority)) {
  454. // Preempt running Thread
  455. osRtxThreadBlock(thread_running);
  456. osRtxThreadSwitch(thread);
  457. } else {
  458. // Put Thread into Ready list
  459. osRtxThreadReadyPut(thread);
  460. }
  461. }
  462. }
  463. /// Exit Thread wait state.
  464. /// \param[in] thread thread object.
  465. /// \param[in] ret_val return value.
  466. /// \param[in] dispatch dispatch flag.
  467. void osRtxThreadWaitExit (os_thread_t *thread, uint32_t ret_val, bool_t dispatch) {
  468. uint32_t *reg;
  469. EvrRtxThreadUnblocked(thread, ret_val);
  470. reg = osRtxThreadRegPtr(thread);
  471. reg[0] = ret_val;
  472. osRtxThreadDelayRemove(thread);
  473. if (dispatch) {
  474. osRtxThreadDispatch(thread);
  475. } else {
  476. osRtxThreadReadyPut(thread);
  477. }
  478. }
  479. /// Enter Thread wait state.
  480. /// \param[in] state new thread state.
  481. /// \param[in] timeout timeout.
  482. /// \return true - success, false - failure.
  483. bool_t osRtxThreadWaitEnter (uint8_t state, uint32_t timeout) {
  484. os_thread_t *thread;
  485. // Check if Kernel is running
  486. if (osRtxKernelGetState() != osRtxKernelRunning) {
  487. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  488. return FALSE;
  489. }
  490. // Check if any thread is ready
  491. if (osRtxInfo.thread.ready.thread_list == NULL) {
  492. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  493. return FALSE;
  494. }
  495. // Get running thread
  496. thread = osRtxThreadGetRunning();
  497. EvrRtxThreadBlocked(thread, timeout);
  498. thread->state = state;
  499. osRtxThreadDelayInsert(thread, timeout);
  500. thread = osRtxThreadListGet(&osRtxInfo.thread.ready);
  501. osRtxThreadSwitch(thread);
  502. return TRUE;
  503. }
  504. #ifdef RTX_STACK_CHECK
  505. /// Check current running Thread Stack.
  506. /// \param[in] thread running thread.
  507. /// \return true - success, false - failure.
  508. //lint -esym(714,osRtxThreadStackCheck) "Referenced by Exception handlers"
  509. //lint -esym(759,osRtxThreadStackCheck) "Prototype in header"
  510. //lint -esym(765,osRtxThreadStackCheck) "Global scope"
  511. bool_t osRtxThreadStackCheck (const os_thread_t *thread) {
  512. //lint -e{923} "cast from pointer to unsigned int"
  513. //lint -e{9079} -e{9087} "cast between pointers to different object types"
  514. if ((thread->sp <= (uint32_t)thread->stack_mem) ||
  515. (*((uint32_t *)thread->stack_mem) != osRtxStackMagicWord)) {
  516. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  517. return FALSE;
  518. }
  519. return TRUE;
  520. }
  521. #endif
  522. #ifdef RTX_THREAD_WATCHDOG
  523. /// Insert a Thread into the Watchdog list, sorted by tick (lowest at Head).
  524. /// \param[in] thread thread object.
  525. /// \param[in] ticks watchdog timeout.
  526. static void osRtxThreadWatchdogInsert (os_thread_t *thread, uint32_t ticks) {
  527. os_thread_t *prev, *next;
  528. if (ticks == 0U) {
  529. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  530. return;
  531. }
  532. prev = NULL;
  533. next = osRtxInfo.thread.wdog_list;
  534. while ((next != NULL) && ((next->wdog_tick <= ticks))) {
  535. ticks -= next->wdog_tick;
  536. prev = next;
  537. next = next->wdog_next;
  538. }
  539. thread->wdog_tick = ticks;
  540. thread->wdog_next = next;
  541. if (next != NULL) {
  542. next->wdog_tick -= ticks;
  543. }
  544. if (prev != NULL) {
  545. prev->wdog_next = thread;
  546. } else {
  547. osRtxInfo.thread.wdog_list = thread;
  548. }
  549. }
  550. /// Remove a Thread from the Watchdog list.
  551. /// \param[in] thread thread object.
  552. void osRtxThreadWatchdogRemove (const os_thread_t *thread) {
  553. os_thread_t *prev, *next;
  554. prev = NULL;
  555. next = osRtxInfo.thread.wdog_list;
  556. while ((next != NULL) && (next != thread)) {
  557. prev = next;
  558. next = next->wdog_next;
  559. }
  560. if (next == NULL) {
  561. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  562. return;
  563. }
  564. if (thread->wdog_next != NULL) {
  565. thread->wdog_next->wdog_tick += thread->wdog_tick;
  566. }
  567. if (prev != NULL) {
  568. prev->wdog_next = thread->wdog_next;
  569. } else {
  570. osRtxInfo.thread.wdog_list = thread->wdog_next;
  571. }
  572. }
  573. /// Process Watchdog Tick (executed each System Tick).
  574. void osRtxThreadWatchdogTick (void) {
  575. os_thread_t *thread_running;
  576. os_thread_t *thread;
  577. #ifdef RTX_SAFETY_CLASS
  578. os_thread_t *next;
  579. #endif
  580. uint32_t ticks;
  581. thread = osRtxInfo.thread.wdog_list;
  582. if (thread == NULL) {
  583. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  584. return;
  585. }
  586. thread->wdog_tick--;
  587. if (thread->wdog_tick == 0U) {
  588. // Call watchdog handler for all expired threads
  589. thread_running = osRtxThreadGetRunning();
  590. do {
  591. osRtxThreadSetRunning(osRtxInfo.thread.run.next);
  592. #ifdef RTX_SAFETY_CLASS
  593. // First the highest safety thread (sorted by Safety Class)
  594. next = thread->wdog_next;
  595. while ((next != NULL) && (next->wdog_tick == 0U)) {
  596. if ((next->attr & osRtxAttrClass_Msk) > (thread->attr & osRtxAttrClass_Msk)) {
  597. thread = next;
  598. }
  599. next = next->wdog_next;
  600. }
  601. #endif
  602. osRtxThreadWatchdogRemove(thread);
  603. EvrRtxThreadWatchdogExpired(thread);
  604. #ifdef RTX_EXECUTION_ZONE
  605. WatchdogAlarmFlag = 1U;
  606. #endif
  607. ticks = osWatchdogAlarm_Handler(thread);
  608. #ifdef RTX_EXECUTION_ZONE
  609. WatchdogAlarmFlag = 0U;
  610. #endif
  611. osRtxThreadWatchdogInsert(thread, ticks);
  612. thread = osRtxInfo.thread.wdog_list;
  613. } while ((thread != NULL) && (thread->wdog_tick == 0U));
  614. osRtxThreadSetRunning(thread_running);
  615. }
  616. }
  617. #endif
  618. static __NO_RETURN void osThreadEntry (void *argument, osThreadFunc_t func) {
  619. func(argument);
  620. osThreadExit();
  621. }
  622. // ==== Post ISR processing ====
  623. /// Thread post ISR processing.
  624. /// \param[in] thread thread object.
  625. static void osRtxThreadPostProcess (os_thread_t *thread) {
  626. uint32_t thread_flags;
  627. // Check if Thread is waiting for Thread Flags
  628. if (thread->state == osRtxThreadWaitingThreadFlags) {
  629. thread_flags = ThreadFlagsCheck(thread, thread->wait_flags, thread->flags_options);
  630. if (thread_flags != 0U) {
  631. osRtxThreadWaitExit(thread, thread_flags, FALSE);
  632. EvrRtxThreadFlagsWaitCompleted(thread->wait_flags, thread->flags_options, thread_flags, thread);
  633. }
  634. }
  635. }
  636. // ==== Service Calls ====
  637. /// Create a thread and add it to Active Threads.
  638. /// \note API identical to osThreadNew
  639. static osThreadId_t svcRtxThreadNew (osThreadFunc_t func, void *argument, const osThreadAttr_t *attr) {
  640. os_thread_t *thread;
  641. #if defined(RTX_SAFETY_CLASS) || defined(RTX_EXECUTION_ZONE)
  642. const os_thread_t *thread_running = osRtxThreadGetRunning();
  643. #endif
  644. uint32_t attr_bits;
  645. void *stack_mem;
  646. uint32_t stack_size;
  647. osPriority_t priority;
  648. uint8_t flags;
  649. const char *name;
  650. uint32_t *ptr;
  651. uint32_t n;
  652. #ifdef RTX_TZ_CONTEXT
  653. TZ_ModuleId_t tz_module;
  654. TZ_MemoryId_t tz_memory;
  655. #endif
  656. // Check parameters
  657. if (func == NULL) {
  658. EvrRtxThreadError(NULL, (int32_t)osErrorParameter);
  659. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  660. return NULL;
  661. }
  662. // Process attributes
  663. if (attr != NULL) {
  664. name = attr->name;
  665. attr_bits = attr->attr_bits;
  666. //lint -e{9079} "conversion from pointer to void to pointer to other type" [MISRA Note 6]
  667. thread = attr->cb_mem;
  668. //lint -e{9079} "conversion from pointer to void to pointer to other type" [MISRA Note 6]
  669. stack_mem = attr->stack_mem;
  670. stack_size = attr->stack_size;
  671. priority = attr->priority;
  672. #ifdef RTX_TZ_CONTEXT
  673. tz_module = attr->tz_module;
  674. #endif
  675. if (((attr_bits & osThreadPrivileged) != 0U) && ((attr_bits & osThreadUnprivileged) != 0U)) {
  676. EvrRtxThreadError(NULL, (int32_t)osErrorParameter);
  677. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  678. return NULL;
  679. }
  680. #ifdef RTX_SAFETY_CLASS
  681. if ((attr_bits & osSafetyClass_Valid) != 0U) {
  682. if ((thread_running != NULL) &&
  683. ((thread_running->attr >> osRtxAttrClass_Pos) <
  684. (uint8_t)((attr_bits & osSafetyClass_Msk) >> osSafetyClass_Pos))) {
  685. EvrRtxThreadError(NULL, (int32_t)osErrorSafetyClass);
  686. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  687. return NULL;
  688. }
  689. }
  690. #endif
  691. if (thread != NULL) {
  692. if (!IsThreadPtrValid(thread) || (attr->cb_size != sizeof(os_thread_t))) {
  693. EvrRtxThreadError(NULL, osRtxErrorInvalidControlBlock);
  694. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  695. return NULL;
  696. }
  697. } else {
  698. if (attr->cb_size != 0U) {
  699. EvrRtxThreadError(NULL, osRtxErrorInvalidControlBlock);
  700. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  701. return NULL;
  702. }
  703. }
  704. if (stack_mem != NULL) {
  705. //lint -e{923} "cast from pointer to unsigned int" [MISRA Note 7]
  706. if ((((uint32_t)stack_mem & 7U) != 0U) || (stack_size == 0U)) {
  707. EvrRtxThreadError(NULL, osRtxErrorInvalidThreadStack);
  708. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  709. return NULL;
  710. }
  711. }
  712. if (priority == osPriorityNone) {
  713. priority = osPriorityNormal;
  714. } else {
  715. if ((priority < osPriorityIdle) || (priority > osPriorityISR)) {
  716. EvrRtxThreadError(NULL, osRtxErrorInvalidPriority);
  717. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  718. return NULL;
  719. }
  720. }
  721. } else {
  722. name = NULL;
  723. attr_bits = 0U;
  724. thread = NULL;
  725. stack_mem = NULL;
  726. stack_size = 0U;
  727. priority = osPriorityNormal;
  728. #ifdef RTX_TZ_CONTEXT
  729. tz_module = 0U;
  730. #endif
  731. }
  732. // Set default privilege if not specified
  733. if ((attr_bits & (osThreadPrivileged | osThreadUnprivileged)) == 0U) {
  734. if ((osRtxConfig.flags & osRtxConfigPrivilegedMode) != 0U) {
  735. attr_bits |= osThreadPrivileged;
  736. } else {
  737. attr_bits |= osThreadUnprivileged;
  738. }
  739. }
  740. #ifdef RTX_SAFETY_FEATURES
  741. // Check privilege protection
  742. if ((attr_bits & osThreadPrivileged) != 0U) {
  743. if ((osRtxInfo.kernel.protect & osRtxKernelProtectPrivileged) != 0U) {
  744. EvrRtxThreadError(NULL, osRtxErrorInvalidPrivilegedMode);
  745. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  746. return NULL;
  747. }
  748. }
  749. #endif
  750. #if defined(RTX_EXECUTION_ZONE) && defined(RTX_SAFETY_CLASS)
  751. // Check class to zone mapping
  752. if (!IsClassMappingValid(attr_bits, thread_running)) {
  753. EvrRtxThreadError(NULL, (int32_t)osErrorSafetyClass);
  754. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  755. return NULL;
  756. }
  757. #endif
  758. // Check stack size
  759. if (stack_size != 0U) {
  760. if (((stack_size & 7U) != 0U) || (stack_size < (64U + 8U)) || (stack_size > 0x7FFFFFFFU)) {
  761. EvrRtxThreadError(NULL, osRtxErrorInvalidThreadStack);
  762. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  763. return NULL;
  764. }
  765. }
  766. // Allocate object memory if not provided
  767. if (thread == NULL) {
  768. if (osRtxInfo.mpi.thread != NULL) {
  769. //lint -e{9079} "conversion from pointer to void to pointer to other type" [MISRA Note 5]
  770. thread = osRtxMemoryPoolAlloc(osRtxInfo.mpi.thread);
  771. #ifndef RTX_OBJ_PTR_CHECK
  772. } else {
  773. //lint -e{9079} "conversion from pointer to void to pointer to other type" [MISRA Note 5]
  774. thread = osRtxMemoryAlloc(osRtxInfo.mem.common, sizeof(os_thread_t), 1U);
  775. #endif
  776. }
  777. #ifdef RTX_OBJ_MEM_USAGE
  778. if (thread != NULL) {
  779. uint32_t used;
  780. osRtxThreadMemUsage.cnt_alloc++;
  781. used = osRtxThreadMemUsage.cnt_alloc - osRtxThreadMemUsage.cnt_free;
  782. if (osRtxThreadMemUsage.max_used < used) {
  783. osRtxThreadMemUsage.max_used = used;
  784. }
  785. }
  786. #endif
  787. flags = osRtxFlagSystemObject;
  788. } else {
  789. flags = 0U;
  790. }
  791. // Allocate stack memory if not provided
  792. if ((thread != NULL) && (stack_mem == NULL)) {
  793. if (stack_size == 0U) {
  794. stack_size = osRtxConfig.thread_stack_size;
  795. if (osRtxInfo.mpi.stack != NULL) {
  796. //lint -e{9079} "conversion from pointer to void to pointer to other type" [MISRA Note 5]
  797. stack_mem = osRtxMemoryPoolAlloc(osRtxInfo.mpi.stack);
  798. if (stack_mem != NULL) {
  799. flags |= osRtxThreadFlagDefStack;
  800. }
  801. } else {
  802. //lint -e{9079} "conversion from pointer to void to pointer to other type" [MISRA Note 5]
  803. stack_mem = osRtxMemoryAlloc(osRtxInfo.mem.stack, stack_size, 0U);
  804. }
  805. } else {
  806. //lint -e{9079} "conversion from pointer to void to pointer to other type" [MISRA Note 5]
  807. stack_mem = osRtxMemoryAlloc(osRtxInfo.mem.stack, stack_size, 0U);
  808. }
  809. if (stack_mem == NULL) {
  810. if ((flags & osRtxFlagSystemObject) != 0U) {
  811. #ifdef RTX_OBJ_PTR_CHECK
  812. (void)osRtxMemoryPoolFree(osRtxInfo.mpi.thread, thread);
  813. #else
  814. if (osRtxInfo.mpi.thread != NULL) {
  815. (void)osRtxMemoryPoolFree(osRtxInfo.mpi.thread, thread);
  816. } else {
  817. (void)osRtxMemoryFree(osRtxInfo.mem.common, thread);
  818. }
  819. #endif
  820. #ifdef RTX_OBJ_MEM_USAGE
  821. osRtxThreadMemUsage.cnt_free++;
  822. #endif
  823. }
  824. thread = NULL;
  825. }
  826. flags |= osRtxFlagSystemMemory;
  827. }
  828. #ifdef RTX_TZ_CONTEXT
  829. // Allocate secure process stack
  830. if ((thread != NULL) && (tz_module != 0U)) {
  831. tz_memory = TZ_AllocModuleContext_S(tz_module);
  832. if (tz_memory == 0U) {
  833. EvrRtxThreadError(NULL, osRtxErrorTZ_AllocContext_S);
  834. if ((flags & osRtxFlagSystemMemory) != 0U) {
  835. if ((flags & osRtxThreadFlagDefStack) != 0U) {
  836. (void)osRtxMemoryPoolFree(osRtxInfo.mpi.stack, thread->stack_mem);
  837. } else {
  838. (void)osRtxMemoryFree(osRtxInfo.mem.stack, thread->stack_mem);
  839. }
  840. }
  841. if ((flags & osRtxFlagSystemObject) != 0U) {
  842. #ifdef RTX_OBJ_PTR_CHECK
  843. (void)osRtxMemoryPoolFree(osRtxInfo.mpi.thread, thread);
  844. #else
  845. if (osRtxInfo.mpi.thread != NULL) {
  846. (void)osRtxMemoryPoolFree(osRtxInfo.mpi.thread, thread);
  847. } else {
  848. (void)osRtxMemoryFree(osRtxInfo.mem.common, thread);
  849. }
  850. #endif
  851. #ifdef RTX_OBJ_MEM_USAGE
  852. osRtxThreadMemUsage.cnt_free++;
  853. #endif
  854. }
  855. thread = NULL;
  856. }
  857. } else {
  858. tz_memory = 0U;
  859. }
  860. #endif
  861. if (thread != NULL) {
  862. // Initialize control block
  863. //lint --e{923} --e{9078} "cast between pointers and unsigned int"
  864. //lint --e{9079} --e{9087} "cast between pointers to different object types"
  865. //lint --e{9074} "conversion between a pointer to function and another type"
  866. thread->id = osRtxIdThread;
  867. thread->state = osRtxThreadReady;
  868. thread->flags = flags;
  869. thread->attr = (uint8_t)(attr_bits & ~osRtxAttrClass_Msk);
  870. thread->name = name;
  871. thread->thread_next = NULL;
  872. thread->thread_prev = NULL;
  873. thread->delay_next = NULL;
  874. thread->delay_prev = NULL;
  875. thread->thread_join = NULL;
  876. thread->delay = 0U;
  877. thread->priority = (int8_t)priority;
  878. thread->priority_base = (int8_t)priority;
  879. thread->stack_frame = STACK_FRAME_INIT_VAL;
  880. thread->flags_options = 0U;
  881. thread->wait_flags = 0U;
  882. thread->thread_flags = 0U;
  883. thread->mutex_list = NULL;
  884. thread->stack_mem = stack_mem;
  885. thread->stack_size = stack_size;
  886. thread->sp = (uint32_t)stack_mem + stack_size - 64U;
  887. thread->thread_addr = (uint32_t)func;
  888. #ifdef RTX_TZ_CONTEXT
  889. thread->tz_memory = tz_memory;
  890. #endif
  891. #ifdef RTX_SAFETY_CLASS
  892. if ((attr_bits & osSafetyClass_Valid) != 0U) {
  893. thread->attr |= (uint8_t)((attr_bits & osSafetyClass_Msk) >>
  894. (osSafetyClass_Pos - osRtxAttrClass_Pos));
  895. } else {
  896. // Inherit safety class from the running thread
  897. if (thread_running != NULL) {
  898. thread->attr |= (uint8_t)(thread_running->attr & osRtxAttrClass_Msk);
  899. }
  900. }
  901. #endif
  902. #ifdef RTX_EXECUTION_ZONE
  903. if ((attr_bits & osThreadZone_Valid) != 0U) {
  904. thread->zone = (uint8_t)((attr_bits & osThreadZone_Msk) >> osThreadZone_Pos);
  905. } else {
  906. // Inherit zone from the running thread
  907. if (thread_running != NULL) {
  908. thread->zone = thread_running->zone;
  909. } else {
  910. thread->zone = 0U;
  911. }
  912. }
  913. #endif
  914. #if defined(RTX_EXECUTION_ZONE) && defined(RTX_SAFETY_CLASS)
  915. // Update class to zone assignment table
  916. if (ThreadClassTable[thread->zone] == 0U) {
  917. ThreadClassTable[thread->zone] = (uint8_t)(0x80U | (thread->attr >> osRtxAttrClass_Pos));
  918. }
  919. #endif
  920. #ifdef RTX_THREAD_WATCHDOG
  921. thread->wdog_next = NULL;
  922. thread->wdog_tick = 0U;
  923. #endif
  924. // Initialize stack
  925. //lint --e{613} false detection: "Possible use of null pointer"
  926. ptr = (uint32_t *)stack_mem;
  927. ptr[0] = osRtxStackMagicWord;
  928. if ((osRtxConfig.flags & osRtxConfigStackWatermark) != 0U) {
  929. for (n = (stack_size/4U) - (16U + 1U); n != 0U; n--) {
  930. ptr++;
  931. *ptr = osRtxStackFillPattern;
  932. }
  933. }
  934. ptr = (uint32_t *)thread->sp;
  935. for (n = 0U; n != 14U; n++) {
  936. ptr[n] = 0U; // R4..R11, R0..R3, R12, LR
  937. }
  938. ptr[14] = (uint32_t)osThreadEntry; // PC
  939. ptr[15] = xPSR_InitVal(
  940. (bool_t)((attr_bits & osThreadPrivileged) != 0U),
  941. (bool_t)(((uint32_t)func & 1U) != 0U)
  942. ); // xPSR
  943. ptr[8] = (uint32_t)argument; // R0
  944. ptr[9] = (uint32_t)func; // R1
  945. // Register post ISR processing function
  946. osRtxInfo.post_process.thread = osRtxThreadPostProcess;
  947. EvrRtxThreadCreated(thread, thread->thread_addr, thread->name);
  948. } else {
  949. EvrRtxThreadError(NULL, (int32_t)osErrorNoMemory);
  950. }
  951. if (thread != NULL) {
  952. osRtxThreadDispatch(thread);
  953. }
  954. return thread;
  955. }
  956. /// Get name of a thread.
  957. /// \note API identical to osThreadGetName
  958. static const char *svcRtxThreadGetName (osThreadId_t thread_id) {
  959. os_thread_t *thread = osRtxThreadId(thread_id);
  960. // Check parameters
  961. if (!IsThreadPtrValid(thread) || (thread->id != osRtxIdThread)) {
  962. EvrRtxThreadGetName(thread, NULL);
  963. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  964. return NULL;
  965. }
  966. EvrRtxThreadGetName(thread, thread->name);
  967. return thread->name;
  968. }
  969. #ifdef RTX_SAFETY_CLASS
  970. /// Get safety class of a thread.
  971. /// \note API identical to osThreadGetClass
  972. static uint32_t svcRtxThreadGetClass (osThreadId_t thread_id) {
  973. os_thread_t *thread = osRtxThreadId(thread_id);
  974. // Check parameters
  975. if (!IsThreadPtrValid(thread) || (thread->id != osRtxIdThread)) {
  976. EvrRtxThreadGetClass(thread, osErrorId);
  977. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  978. return osErrorId;
  979. }
  980. EvrRtxThreadGetClass(thread, (uint32_t)thread->attr >> osRtxAttrClass_Pos);
  981. return ((uint32_t)thread->attr >> osRtxAttrClass_Pos);
  982. }
  983. #endif
  984. #ifdef RTX_EXECUTION_ZONE
  985. /// Get zone of a thread.
  986. /// \note API identical to osThreadGetZone
  987. static uint32_t svcRtxThreadGetZone (osThreadId_t thread_id) {
  988. os_thread_t *thread = osRtxThreadId(thread_id);
  989. // Check parameters
  990. if (!IsThreadPtrValid(thread) || (thread->id != osRtxIdThread)) {
  991. EvrRtxThreadGetZone(thread, osErrorId);
  992. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  993. return osErrorId;
  994. }
  995. EvrRtxThreadGetZone(thread, thread->zone);
  996. return thread->zone;
  997. }
  998. #endif
  999. /// Return the thread ID of the current running thread.
  1000. /// \note API identical to osThreadGetId
  1001. static osThreadId_t svcRtxThreadGetId (void) {
  1002. os_thread_t *thread;
  1003. thread = osRtxThreadGetRunning();
  1004. EvrRtxThreadGetId(thread);
  1005. return thread;
  1006. }
  1007. /// Get current thread state of a thread.
  1008. /// \note API identical to osThreadGetState
  1009. static osThreadState_t svcRtxThreadGetState (osThreadId_t thread_id) {
  1010. os_thread_t *thread = osRtxThreadId(thread_id);
  1011. osThreadState_t state;
  1012. // Check parameters
  1013. if (!IsThreadPtrValid(thread) || (thread->id != osRtxIdThread)) {
  1014. EvrRtxThreadGetState(thread, osThreadError);
  1015. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1016. return osThreadError;
  1017. }
  1018. state = osRtxThreadState(thread);
  1019. EvrRtxThreadGetState(thread, state);
  1020. return state;
  1021. }
  1022. /// Get stack size of a thread.
  1023. /// \note API identical to osThreadGetStackSize
  1024. static uint32_t svcRtxThreadGetStackSize (osThreadId_t thread_id) {
  1025. os_thread_t *thread = osRtxThreadId(thread_id);
  1026. // Check parameters
  1027. if (!IsThreadPtrValid(thread) || (thread->id != osRtxIdThread)) {
  1028. EvrRtxThreadGetStackSize(thread, 0U);
  1029. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1030. return 0U;
  1031. }
  1032. EvrRtxThreadGetStackSize(thread, thread->stack_size);
  1033. return thread->stack_size;
  1034. }
  1035. /// Get available stack space of a thread based on stack watermark recording during execution.
  1036. /// \note API identical to osThreadGetStackSpace
  1037. static uint32_t svcRtxThreadGetStackSpace (osThreadId_t thread_id) {
  1038. os_thread_t *thread = osRtxThreadId(thread_id);
  1039. const uint32_t *stack;
  1040. uint32_t space;
  1041. // Check parameters
  1042. if (!IsThreadPtrValid(thread) || (thread->id != osRtxIdThread)) {
  1043. EvrRtxThreadGetStackSpace(thread, 0U);
  1044. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1045. return 0U;
  1046. }
  1047. // Check if stack watermark is not enabled
  1048. if ((osRtxConfig.flags & osRtxConfigStackWatermark) == 0U) {
  1049. EvrRtxThreadGetStackSpace(thread, 0U);
  1050. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1051. return 0U;
  1052. }
  1053. //lint -e{9079} "conversion from pointer to void to pointer to other type"
  1054. stack = thread->stack_mem;
  1055. if (*stack++ == osRtxStackMagicWord) {
  1056. for (space = 4U; space < thread->stack_size; space += 4U) {
  1057. if (*stack++ != osRtxStackFillPattern) {
  1058. break;
  1059. }
  1060. }
  1061. } else {
  1062. space = 0U;
  1063. }
  1064. EvrRtxThreadGetStackSpace(thread, space);
  1065. return space;
  1066. }
  1067. /// Change priority of a thread.
  1068. /// \note API identical to osThreadSetPriority
  1069. static osStatus_t svcRtxThreadSetPriority (osThreadId_t thread_id, osPriority_t priority) {
  1070. os_thread_t *thread = osRtxThreadId(thread_id);
  1071. #ifdef RTX_SAFETY_CLASS
  1072. const os_thread_t *thread_running;
  1073. #endif
  1074. // Check parameters
  1075. if (!IsThreadPtrValid(thread) || (thread->id != osRtxIdThread) ||
  1076. (priority < osPriorityIdle) || (priority > osPriorityISR)) {
  1077. EvrRtxThreadError(thread, (int32_t)osErrorParameter);
  1078. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1079. return osErrorParameter;
  1080. }
  1081. #ifdef RTX_SAFETY_CLASS
  1082. // Check running thread safety class
  1083. thread_running = osRtxThreadGetRunning();
  1084. if ((thread_running != NULL) &&
  1085. ((thread_running->attr >> osRtxAttrClass_Pos) < (thread->attr >> osRtxAttrClass_Pos))) {
  1086. EvrRtxThreadError(thread, (int32_t)osErrorSafetyClass);
  1087. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1088. return osErrorSafetyClass;
  1089. }
  1090. #endif
  1091. // Check object state
  1092. if (thread->state == osRtxThreadTerminated) {
  1093. EvrRtxThreadError(thread, (int32_t)osErrorResource);
  1094. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1095. return osErrorResource;
  1096. }
  1097. if (thread->priority != (int8_t)priority) {
  1098. thread->priority = (int8_t)priority;
  1099. thread->priority_base = (int8_t)priority;
  1100. EvrRtxThreadPriorityUpdated(thread, priority);
  1101. osRtxThreadListSort(thread);
  1102. osRtxThreadDispatch(NULL);
  1103. }
  1104. return osOK;
  1105. }
  1106. /// Get current priority of a thread.
  1107. /// \note API identical to osThreadGetPriority
  1108. static osPriority_t svcRtxThreadGetPriority (osThreadId_t thread_id) {
  1109. os_thread_t *thread = osRtxThreadId(thread_id);
  1110. osPriority_t priority;
  1111. // Check parameters
  1112. if (!IsThreadPtrValid(thread) || (thread->id != osRtxIdThread)) {
  1113. EvrRtxThreadGetPriority(thread, osPriorityError);
  1114. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1115. return osPriorityError;
  1116. }
  1117. // Check object state
  1118. if (thread->state == osRtxThreadTerminated) {
  1119. EvrRtxThreadGetPriority(thread, osPriorityError);
  1120. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1121. return osPriorityError;
  1122. }
  1123. priority = osRtxThreadPriority(thread);
  1124. EvrRtxThreadGetPriority(thread, priority);
  1125. return priority;
  1126. }
  1127. /// Pass control to next thread that is in state READY.
  1128. /// \note API identical to osThreadYield
  1129. static osStatus_t svcRtxThreadYield (void) {
  1130. os_thread_t *thread_running;
  1131. os_thread_t *thread_ready;
  1132. if (osRtxKernelGetState() == osRtxKernelRunning) {
  1133. thread_running = osRtxThreadGetRunning();
  1134. thread_ready = osRtxInfo.thread.ready.thread_list;
  1135. if ((thread_ready != NULL) &&
  1136. (thread_ready->priority == thread_running->priority)) {
  1137. osRtxThreadListRemove(thread_ready);
  1138. osRtxThreadReadyPut(thread_running);
  1139. EvrRtxThreadPreempted(thread_running);
  1140. osRtxThreadSwitch(thread_ready);
  1141. }
  1142. }
  1143. return osOK;
  1144. }
  1145. /// Suspend execution of a thread.
  1146. /// \note API identical to osThreadSuspend
  1147. static osStatus_t svcRtxThreadSuspend (osThreadId_t thread_id) {
  1148. os_thread_t *thread = osRtxThreadId(thread_id);
  1149. #ifdef RTX_SAFETY_CLASS
  1150. const os_thread_t *thread_running;
  1151. #endif
  1152. osStatus_t status;
  1153. // Check parameters
  1154. if (!IsThreadPtrValid(thread) || (thread->id != osRtxIdThread)) {
  1155. EvrRtxThreadError(thread, (int32_t)osErrorParameter);
  1156. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1157. return osErrorParameter;
  1158. }
  1159. #ifdef RTX_SAFETY_CLASS
  1160. // Check running thread safety class
  1161. thread_running = osRtxThreadGetRunning();
  1162. if ((thread_running != NULL) &&
  1163. ((thread_running->attr >> osRtxAttrClass_Pos) < (thread->attr >> osRtxAttrClass_Pos))) {
  1164. EvrRtxThreadError(thread, (int32_t)osErrorSafetyClass);
  1165. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1166. return osErrorSafetyClass;
  1167. }
  1168. #endif
  1169. // Check object state
  1170. switch (thread->state & osRtxThreadStateMask) {
  1171. case osRtxThreadRunning:
  1172. if ((osRtxKernelGetState() != osRtxKernelRunning) ||
  1173. (osRtxInfo.thread.ready.thread_list == NULL)) {
  1174. EvrRtxThreadError(thread, (int32_t)osErrorResource);
  1175. status = osErrorResource;
  1176. } else {
  1177. status = osOK;
  1178. }
  1179. break;
  1180. case osRtxThreadReady:
  1181. osRtxThreadListRemove(thread);
  1182. status = osOK;
  1183. break;
  1184. case osRtxThreadBlocked:
  1185. osRtxThreadListRemove(thread);
  1186. osRtxThreadDelayRemove(thread);
  1187. status = osOK;
  1188. break;
  1189. case osRtxThreadInactive:
  1190. case osRtxThreadTerminated:
  1191. default:
  1192. EvrRtxThreadError(thread, (int32_t)osErrorResource);
  1193. status = osErrorResource;
  1194. break;
  1195. }
  1196. if (status == osOK) {
  1197. EvrRtxThreadSuspended(thread);
  1198. if (thread->state == osRtxThreadRunning) {
  1199. osRtxThreadSwitch(osRtxThreadListGet(&osRtxInfo.thread.ready));
  1200. }
  1201. // Update Thread State and put it into Delay list
  1202. thread->state = osRtxThreadBlocked;
  1203. osRtxThreadDelayInsert(thread, osWaitForever);
  1204. }
  1205. return status;
  1206. }
  1207. /// Resume execution of a thread.
  1208. /// \note API identical to osThreadResume
  1209. static osStatus_t svcRtxThreadResume (osThreadId_t thread_id) {
  1210. os_thread_t *thread = osRtxThreadId(thread_id);
  1211. #ifdef RTX_SAFETY_CLASS
  1212. const os_thread_t *thread_running;
  1213. #endif
  1214. // Check parameters
  1215. if (!IsThreadPtrValid(thread) || (thread->id != osRtxIdThread)) {
  1216. EvrRtxThreadError(thread, (int32_t)osErrorParameter);
  1217. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1218. return osErrorParameter;
  1219. }
  1220. #ifdef RTX_SAFETY_CLASS
  1221. // Check running thread safety class
  1222. thread_running = osRtxThreadGetRunning();
  1223. if ((thread_running != NULL) &&
  1224. ((thread_running->attr >> osRtxAttrClass_Pos) < (thread->attr >> osRtxAttrClass_Pos))) {
  1225. EvrRtxThreadError(thread, (int32_t)osErrorSafetyClass);
  1226. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1227. return osErrorSafetyClass;
  1228. }
  1229. #endif
  1230. // Check object state
  1231. if ((thread->state & osRtxThreadStateMask) != osRtxThreadBlocked) {
  1232. EvrRtxThreadError(thread, (int32_t)osErrorResource);
  1233. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1234. return osErrorResource;
  1235. }
  1236. EvrRtxThreadResumed(thread);
  1237. // Wakeup Thread
  1238. osRtxThreadListRemove(thread);
  1239. osRtxThreadDelayRemove(thread);
  1240. osRtxThreadDispatch(thread);
  1241. return osOK;
  1242. }
  1243. /// Wakeup a thread waiting to join.
  1244. /// \param[in] thread thread object.
  1245. void osRtxThreadJoinWakeup (const os_thread_t *thread) {
  1246. if (thread->thread_join != NULL) {
  1247. osRtxThreadWaitExit(thread->thread_join, (uint32_t)osOK, FALSE);
  1248. EvrRtxThreadJoined(thread->thread_join);
  1249. }
  1250. if (thread->state == osRtxThreadWaitingJoin) {
  1251. thread->thread_next->thread_join = NULL;
  1252. }
  1253. }
  1254. /// Free Thread resources.
  1255. /// \param[in] thread thread object.
  1256. static void osRtxThreadFree (os_thread_t *thread) {
  1257. osRtxThreadBeforeFree(thread);
  1258. // Mark object as inactive and invalid
  1259. thread->state = osRtxThreadInactive;
  1260. thread->id = osRtxIdInvalid;
  1261. #ifdef RTX_TZ_CONTEXT
  1262. // Free secure process stack
  1263. if (thread->tz_memory != 0U) {
  1264. (void)TZ_FreeModuleContext_S(thread->tz_memory);
  1265. }
  1266. #endif
  1267. // Free stack memory
  1268. if ((thread->flags & osRtxFlagSystemMemory) != 0U) {
  1269. if ((thread->flags & osRtxThreadFlagDefStack) != 0U) {
  1270. (void)osRtxMemoryPoolFree(osRtxInfo.mpi.stack, thread->stack_mem);
  1271. } else {
  1272. (void)osRtxMemoryFree(osRtxInfo.mem.stack, thread->stack_mem);
  1273. }
  1274. }
  1275. // Free object memory
  1276. if ((thread->flags & osRtxFlagSystemObject) != 0U) {
  1277. #ifdef RTX_OBJ_PTR_CHECK
  1278. (void)osRtxMemoryPoolFree(osRtxInfo.mpi.thread, thread);
  1279. #else
  1280. if (osRtxInfo.mpi.thread != NULL) {
  1281. (void)osRtxMemoryPoolFree(osRtxInfo.mpi.thread, thread);
  1282. } else {
  1283. (void)osRtxMemoryFree(osRtxInfo.mem.common, thread);
  1284. }
  1285. #endif
  1286. #ifdef RTX_OBJ_MEM_USAGE
  1287. osRtxThreadMemUsage.cnt_free++;
  1288. #endif
  1289. }
  1290. }
  1291. /// Destroy a Thread.
  1292. /// \param[in] thread thread object.
  1293. void osRtxThreadDestroy (os_thread_t *thread) {
  1294. if ((thread->attr & osThreadJoinable) == 0U) {
  1295. osRtxThreadFree(thread);
  1296. } else {
  1297. // Update Thread State and put it into Terminate Thread list
  1298. thread->state = osRtxThreadTerminated;
  1299. thread->thread_prev = NULL;
  1300. thread->thread_next = osRtxInfo.thread.terminate_list;
  1301. if (osRtxInfo.thread.terminate_list != NULL) {
  1302. osRtxInfo.thread.terminate_list->thread_prev = thread;
  1303. }
  1304. osRtxInfo.thread.terminate_list = thread;
  1305. }
  1306. EvrRtxThreadDestroyed(thread);
  1307. }
  1308. /// Detach a thread (thread storage can be reclaimed when thread terminates).
  1309. /// \note API identical to osThreadDetach
  1310. static osStatus_t svcRtxThreadDetach (osThreadId_t thread_id) {
  1311. os_thread_t *thread = osRtxThreadId(thread_id);
  1312. #ifdef RTX_SAFETY_CLASS
  1313. const os_thread_t *thread_running;
  1314. #endif
  1315. // Check parameters
  1316. if (!IsThreadPtrValid(thread) || (thread->id != osRtxIdThread)) {
  1317. EvrRtxThreadError(thread, (int32_t)osErrorParameter);
  1318. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1319. return osErrorParameter;
  1320. }
  1321. #ifdef RTX_SAFETY_CLASS
  1322. // Check running thread safety class
  1323. thread_running = osRtxThreadGetRunning();
  1324. if ((thread_running != NULL) &&
  1325. ((thread_running->attr >> osRtxAttrClass_Pos) < (thread->attr >> osRtxAttrClass_Pos))) {
  1326. EvrRtxThreadError(thread, (int32_t)osErrorSafetyClass);
  1327. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1328. return osErrorSafetyClass;
  1329. }
  1330. #endif
  1331. // Check object attributes
  1332. if ((thread->attr & osThreadJoinable) == 0U) {
  1333. EvrRtxThreadError(thread, osRtxErrorThreadNotJoinable);
  1334. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1335. return osErrorResource;
  1336. }
  1337. if (thread->state == osRtxThreadTerminated) {
  1338. osRtxThreadListUnlink(&osRtxInfo.thread.terminate_list, thread);
  1339. osRtxThreadFree(thread);
  1340. } else {
  1341. thread->attr &= ~osThreadJoinable;
  1342. }
  1343. EvrRtxThreadDetached(thread);
  1344. return osOK;
  1345. }
  1346. /// Wait for specified thread to terminate.
  1347. /// \note API identical to osThreadJoin
  1348. static osStatus_t svcRtxThreadJoin (osThreadId_t thread_id) {
  1349. os_thread_t *thread = osRtxThreadId(thread_id);
  1350. os_thread_t *thread_running;
  1351. osStatus_t status;
  1352. // Check parameters
  1353. if (!IsThreadPtrValid(thread) || (thread->id != osRtxIdThread)) {
  1354. EvrRtxThreadError(thread, (int32_t)osErrorParameter);
  1355. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1356. return osErrorParameter;
  1357. }
  1358. #ifdef RTX_SAFETY_CLASS
  1359. // Check running thread safety class
  1360. thread_running = osRtxThreadGetRunning();
  1361. if ((thread_running != NULL) &&
  1362. ((thread_running->attr >> osRtxAttrClass_Pos) < (thread->attr >> osRtxAttrClass_Pos))) {
  1363. EvrRtxThreadError(thread, (int32_t)osErrorSafetyClass);
  1364. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1365. return osErrorSafetyClass;
  1366. }
  1367. #endif
  1368. // Check object attributes
  1369. if ((thread->attr & osThreadJoinable) == 0U) {
  1370. EvrRtxThreadError(thread, osRtxErrorThreadNotJoinable);
  1371. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1372. return osErrorResource;
  1373. }
  1374. // Check object state
  1375. if (thread->state == osRtxThreadRunning) {
  1376. EvrRtxThreadError(thread, (int32_t)osErrorResource);
  1377. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1378. return osErrorResource;
  1379. }
  1380. if (thread->state == osRtxThreadTerminated) {
  1381. osRtxThreadListUnlink(&osRtxInfo.thread.terminate_list, thread);
  1382. osRtxThreadFree(thread);
  1383. EvrRtxThreadJoined(thread);
  1384. status = osOK;
  1385. } else {
  1386. // Suspend current Thread
  1387. if (osRtxThreadWaitEnter(osRtxThreadWaitingJoin, osWaitForever)) {
  1388. thread_running = osRtxThreadGetRunning();
  1389. thread_running->thread_next = thread;
  1390. thread->thread_join = thread_running;
  1391. thread->attr &= ~osThreadJoinable;
  1392. EvrRtxThreadJoinPending(thread);
  1393. } else {
  1394. EvrRtxThreadError(thread, (int32_t)osErrorResource);
  1395. }
  1396. status = osErrorResource;
  1397. }
  1398. return status;
  1399. }
  1400. /// Terminate execution of current running thread.
  1401. /// \note API identical to osThreadExit
  1402. static void svcRtxThreadExit (void) {
  1403. os_thread_t *thread;
  1404. // Check if switch to next Ready Thread is possible
  1405. if ((osRtxKernelGetState() != osRtxKernelRunning) ||
  1406. (osRtxInfo.thread.ready.thread_list == NULL)) {
  1407. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1408. return;
  1409. }
  1410. // Get running thread
  1411. thread = osRtxThreadGetRunning();
  1412. #ifdef RTX_THREAD_WATCHDOG
  1413. // Remove Thread from the Watchdog list
  1414. osRtxThreadWatchdogRemove(thread);
  1415. #endif
  1416. // Release owned Mutexes
  1417. osRtxMutexOwnerRelease(thread->mutex_list);
  1418. // Wakeup Thread waiting to Join
  1419. osRtxThreadJoinWakeup(thread);
  1420. // Switch to next Ready Thread
  1421. osRtxThreadSwitch(osRtxThreadListGet(&osRtxInfo.thread.ready));
  1422. // Update Stack Pointer
  1423. thread->sp = __get_PSP();
  1424. #ifdef RTX_STACK_CHECK
  1425. // Check Stack usage
  1426. if (!osRtxThreadStackCheck(thread)) {
  1427. osRtxThreadSetRunning(osRtxInfo.thread.run.next);
  1428. (void)osRtxKernelErrorNotify(osRtxErrorStackOverflow, thread);
  1429. }
  1430. #endif
  1431. // Mark running thread as deleted
  1432. osRtxThreadSetRunning(NULL);
  1433. // Destroy Thread
  1434. osRtxThreadDestroy(thread);
  1435. }
  1436. /// Terminate execution of a thread.
  1437. /// \note API identical to osThreadTerminate
  1438. static osStatus_t svcRtxThreadTerminate (osThreadId_t thread_id) {
  1439. os_thread_t *thread = osRtxThreadId(thread_id);
  1440. #ifdef RTX_SAFETY_CLASS
  1441. const os_thread_t *thread_running;
  1442. #endif
  1443. osStatus_t status;
  1444. // Check parameters
  1445. if (!IsThreadPtrValid(thread) || (thread->id != osRtxIdThread)) {
  1446. EvrRtxThreadError(thread, (int32_t)osErrorParameter);
  1447. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1448. return osErrorParameter;
  1449. }
  1450. #ifdef RTX_SAFETY_CLASS
  1451. // Check running thread safety class
  1452. thread_running = osRtxThreadGetRunning();
  1453. if ((thread_running != NULL) &&
  1454. ((thread_running->attr >> osRtxAttrClass_Pos) < (thread->attr >> osRtxAttrClass_Pos))) {
  1455. EvrRtxThreadError(thread, (int32_t)osErrorSafetyClass);
  1456. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1457. return osErrorSafetyClass;
  1458. }
  1459. #endif
  1460. // Check object state
  1461. switch (thread->state & osRtxThreadStateMask) {
  1462. case osRtxThreadRunning:
  1463. if ((osRtxKernelGetState() != osRtxKernelRunning) ||
  1464. (osRtxInfo.thread.ready.thread_list == NULL)) {
  1465. EvrRtxThreadError(thread, (int32_t)osErrorResource);
  1466. status = osErrorResource;
  1467. } else {
  1468. status = osOK;
  1469. }
  1470. break;
  1471. case osRtxThreadReady:
  1472. osRtxThreadListRemove(thread);
  1473. status = osOK;
  1474. break;
  1475. case osRtxThreadBlocked:
  1476. osRtxThreadListRemove(thread);
  1477. osRtxThreadDelayRemove(thread);
  1478. status = osOK;
  1479. break;
  1480. case osRtxThreadInactive:
  1481. case osRtxThreadTerminated:
  1482. default:
  1483. EvrRtxThreadError(thread, (int32_t)osErrorResource);
  1484. status = osErrorResource;
  1485. break;
  1486. }
  1487. if (status == osOK) {
  1488. #ifdef RTX_THREAD_WATCHDOG
  1489. // Remove Thread from the Watchdog list
  1490. osRtxThreadWatchdogRemove(thread);
  1491. #endif
  1492. // Release owned Mutexes
  1493. osRtxMutexOwnerRelease(thread->mutex_list);
  1494. // Wakeup Thread waiting to Join
  1495. osRtxThreadJoinWakeup(thread);
  1496. // Switch to next Ready Thread when terminating running Thread
  1497. if (thread->state == osRtxThreadRunning) {
  1498. osRtxThreadSwitch(osRtxThreadListGet(&osRtxInfo.thread.ready));
  1499. // Update Stack Pointer
  1500. thread->sp = __get_PSP();
  1501. #ifdef RTX_STACK_CHECK
  1502. // Check Stack usage
  1503. if (!osRtxThreadStackCheck(thread)) {
  1504. osRtxThreadSetRunning(osRtxInfo.thread.run.next);
  1505. (void)osRtxKernelErrorNotify(osRtxErrorStackOverflow, thread);
  1506. }
  1507. #endif
  1508. // Mark running thread as deleted
  1509. osRtxThreadSetRunning(NULL);
  1510. } else {
  1511. osRtxThreadDispatch(NULL);
  1512. }
  1513. // Destroy Thread
  1514. osRtxThreadDestroy(thread);
  1515. }
  1516. return status;
  1517. }
  1518. #ifdef RTX_THREAD_WATCHDOG
  1519. /// Feed watchdog of the current running thread.
  1520. /// \note API identical to osThreadFeedWatchdog
  1521. static osStatus_t svcRtxThreadFeedWatchdog (uint32_t ticks) {
  1522. os_thread_t *thread;
  1523. // Check running thread
  1524. thread = osRtxThreadGetRunning();
  1525. if (thread == NULL) {
  1526. EvrRtxThreadError(NULL, osRtxErrorKernelNotRunning);
  1527. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1528. return osError;
  1529. }
  1530. osRtxThreadWatchdogRemove(thread);
  1531. osRtxThreadWatchdogInsert(thread, ticks);
  1532. EvrRtxThreadFeedWatchdogDone();
  1533. return osOK;
  1534. }
  1535. #endif
  1536. #ifdef RTX_SAFETY_FEATURES
  1537. /// Protect the creation of privileged threads.
  1538. /// \note API identical to osThreadProtectPrivileged
  1539. static osStatus_t svcRtxThreadProtectPrivileged (void) {
  1540. // Check that Kernel is initialized
  1541. if (osRtxKernelGetState() == osRtxKernelInactive) {
  1542. EvrRtxThreadError(NULL, osRtxErrorKernelNotReady);
  1543. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1544. return osError;
  1545. }
  1546. osRtxInfo.kernel.protect |= osRtxKernelProtectPrivileged;
  1547. EvrRtxThreadPrivilegedProtected();
  1548. return osOK;
  1549. }
  1550. #endif
  1551. #ifdef RTX_SAFETY_CLASS
  1552. /// Suspend execution of threads for specified safety classes.
  1553. /// \note API identical to osThreadSuspendClass
  1554. static osStatus_t svcRtxThreadSuspendClass (uint32_t safety_class, uint32_t mode) {
  1555. os_thread_t *thread;
  1556. os_thread_t *thread_next;
  1557. // Check parameters
  1558. if (safety_class > 0x0FU) {
  1559. EvrRtxThreadError(NULL, (int32_t)osErrorParameter);
  1560. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1561. return osErrorParameter;
  1562. }
  1563. // Check running thread safety class (when called from thread)
  1564. thread = osRtxThreadGetRunning();
  1565. if ((thread != NULL) && IsSVCallIrq()) {
  1566. if ((((mode & osSafetyWithSameClass) != 0U) &&
  1567. ((thread->attr >> osRtxAttrClass_Pos) < (uint8_t)safety_class)) ||
  1568. (((mode & osSafetyWithLowerClass) != 0U) &&
  1569. (((thread->attr >> osRtxAttrClass_Pos) + 1U) < (uint8_t)safety_class))) {
  1570. EvrRtxThreadError(NULL, (int32_t)osErrorSafetyClass);
  1571. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1572. return osErrorSafetyClass;
  1573. }
  1574. }
  1575. // Threads in Wait List
  1576. thread = osRtxInfo.thread.wait_list;
  1577. while (thread != NULL) {
  1578. thread_next = thread->delay_next;
  1579. if ((((mode & osSafetyWithSameClass) != 0U) &&
  1580. ((thread->attr >> osRtxAttrClass_Pos) == (uint8_t)safety_class)) ||
  1581. (((mode & osSafetyWithLowerClass) != 0U) &&
  1582. ((thread->attr >> osRtxAttrClass_Pos) < (uint8_t)safety_class))) {
  1583. osRtxThreadListRemove(thread);
  1584. thread->state = osRtxThreadBlocked;
  1585. EvrRtxThreadSuspended(thread);
  1586. }
  1587. thread = thread_next;
  1588. }
  1589. // Threads in Delay List
  1590. thread = osRtxInfo.thread.delay_list;
  1591. while (thread != NULL) {
  1592. thread_next = thread->delay_next;
  1593. if ((((mode & osSafetyWithSameClass) != 0U) &&
  1594. ((thread->attr >> osRtxAttrClass_Pos) == (uint8_t)safety_class)) ||
  1595. (((mode & osSafetyWithLowerClass) != 0U) &&
  1596. ((thread->attr >> osRtxAttrClass_Pos) < (uint8_t)safety_class))) {
  1597. osRtxThreadListRemove(thread);
  1598. osRtxThreadDelayRemove(thread);
  1599. thread->state = osRtxThreadBlocked;
  1600. osRtxThreadDelayInsert(thread, osWaitForever);
  1601. EvrRtxThreadSuspended(thread);
  1602. }
  1603. thread = thread_next;
  1604. }
  1605. // Threads in Ready List
  1606. thread = osRtxInfo.thread.ready.thread_list;
  1607. while (thread != NULL) {
  1608. thread_next = thread->thread_next;
  1609. if ((((mode & osSafetyWithSameClass) != 0U) &&
  1610. ((thread->attr >> osRtxAttrClass_Pos) == (uint8_t)safety_class)) ||
  1611. (((mode & osSafetyWithLowerClass) != 0U) &&
  1612. ((thread->attr >> osRtxAttrClass_Pos) < (uint8_t)safety_class))) {
  1613. osRtxThreadListRemove(thread);
  1614. thread->state = osRtxThreadBlocked;
  1615. osRtxThreadDelayInsert(thread, osWaitForever);
  1616. EvrRtxThreadSuspended(thread);
  1617. }
  1618. thread = thread_next;
  1619. }
  1620. // Running Thread
  1621. thread = osRtxThreadGetRunning();
  1622. if ((thread != NULL) &&
  1623. ((((mode & osSafetyWithSameClass) != 0U) &&
  1624. ((thread->attr >> osRtxAttrClass_Pos) == (uint8_t)safety_class)) ||
  1625. (((mode & osSafetyWithLowerClass) != 0U) &&
  1626. ((thread->attr >> osRtxAttrClass_Pos) < (uint8_t)safety_class)))) {
  1627. if ((osRtxKernelGetState() == osRtxKernelRunning) &&
  1628. (osRtxInfo.thread.ready.thread_list != NULL)) {
  1629. thread->state = osRtxThreadBlocked;
  1630. osRtxThreadDelayInsert(thread, osWaitForever);
  1631. EvrRtxThreadSuspended(thread);
  1632. osRtxThreadSwitch(osRtxThreadListGet(&osRtxInfo.thread.ready));
  1633. } else {
  1634. EvrRtxThreadError(thread, (int32_t)osErrorResource);
  1635. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1636. return osErrorResource;
  1637. }
  1638. }
  1639. return osOK;
  1640. }
  1641. /// Resume execution of threads for specified safety classes.
  1642. /// \note API identical to osThreadResumeClass
  1643. static osStatus_t svcRtxThreadResumeClass (uint32_t safety_class, uint32_t mode) {
  1644. os_thread_t *thread;
  1645. os_thread_t *thread_next;
  1646. // Check parameters
  1647. if (safety_class > 0x0FU) {
  1648. EvrRtxThreadError(NULL, (int32_t)osErrorParameter);
  1649. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1650. return osErrorParameter;
  1651. }
  1652. // Check running thread safety class (when called from thread)
  1653. thread = osRtxThreadGetRunning();
  1654. if ((thread != NULL) && IsSVCallIrq()) {
  1655. if ((((mode & osSafetyWithSameClass) != 0U) &&
  1656. ((thread->attr >> osRtxAttrClass_Pos) < (uint8_t)safety_class)) ||
  1657. (((mode & osSafetyWithLowerClass) != 0U) &&
  1658. (((thread->attr >> osRtxAttrClass_Pos) + 1U) < (uint8_t)safety_class))) {
  1659. EvrRtxThreadError(NULL, (int32_t)osErrorSafetyClass);
  1660. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1661. return osErrorSafetyClass;
  1662. }
  1663. }
  1664. // Threads in Wait List
  1665. thread = osRtxInfo.thread.wait_list;
  1666. while (thread != NULL) {
  1667. thread_next = thread->delay_next;
  1668. if ((((mode & osSafetyWithSameClass) != 0U) &&
  1669. ((thread->attr >> osRtxAttrClass_Pos) == (uint8_t)safety_class)) ||
  1670. (((mode & osSafetyWithLowerClass) != 0U) &&
  1671. ((thread->attr >> osRtxAttrClass_Pos) < (uint8_t)safety_class))) {
  1672. // Wakeup Thread
  1673. osRtxThreadListRemove(thread);
  1674. osRtxThreadDelayRemove(thread);
  1675. osRtxThreadReadyPut(thread);
  1676. EvrRtxThreadResumed(thread);
  1677. }
  1678. thread = thread_next;
  1679. }
  1680. // Threads in Delay List
  1681. thread = osRtxInfo.thread.delay_list;
  1682. while (thread != NULL) {
  1683. thread_next = thread->delay_next;
  1684. if ((((mode & osSafetyWithSameClass) != 0U) &&
  1685. ((thread->attr >> osRtxAttrClass_Pos) == (uint8_t)safety_class)) ||
  1686. (((mode & osSafetyWithLowerClass) != 0U) &&
  1687. ((thread->attr >> osRtxAttrClass_Pos) < (uint8_t)safety_class))) {
  1688. // Wakeup Thread
  1689. osRtxThreadListRemove(thread);
  1690. osRtxThreadDelayRemove(thread);
  1691. osRtxThreadReadyPut(thread);
  1692. EvrRtxThreadResumed(thread);
  1693. }
  1694. thread = thread_next;
  1695. }
  1696. osRtxThreadDispatch(NULL);
  1697. return osOK;
  1698. }
  1699. #endif
  1700. #ifdef RTX_EXECUTION_ZONE
  1701. /// Terminate execution of threads assigned to a specified MPU protected zone.
  1702. /// \note API identical to osThreadTerminateZone
  1703. static osStatus_t svcRtxThreadTerminateZone (uint32_t zone) {
  1704. os_thread_t *thread;
  1705. os_thread_t *thread_next;
  1706. #ifdef RTX_THREAD_WATCHDOG
  1707. // Check Watchdog Alarm Flag
  1708. if (WatchdogAlarmFlag != 0U) {
  1709. EvrRtxThreadError(NULL, (int32_t)osErrorISR);
  1710. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1711. return osErrorISR;
  1712. }
  1713. #endif
  1714. // Check parameters
  1715. if (zone > 0x3FU) {
  1716. EvrRtxThreadError(NULL, (int32_t)osErrorParameter);
  1717. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1718. return osErrorParameter;
  1719. }
  1720. // Threads in Wait List
  1721. thread = osRtxInfo.thread.wait_list;
  1722. while (thread != NULL) {
  1723. thread_next = thread->delay_next;
  1724. if (thread->zone == zone) {
  1725. osRtxThreadListRemove(thread);
  1726. osRtxThreadDelayRemove(thread);
  1727. #ifdef RTX_THREAD_WATCHDOG
  1728. osRtxThreadWatchdogRemove(thread);
  1729. #endif
  1730. osRtxMutexOwnerRelease(thread->mutex_list);
  1731. osRtxThreadJoinWakeup(thread);
  1732. osRtxThreadDestroy(thread);
  1733. }
  1734. thread = thread_next;
  1735. }
  1736. // Threads in Delay List
  1737. thread = osRtxInfo.thread.delay_list;
  1738. while (thread != NULL) {
  1739. thread_next = thread->delay_next;
  1740. if (thread->zone == zone) {
  1741. osRtxThreadListRemove(thread);
  1742. osRtxThreadDelayRemove(thread);
  1743. #ifdef RTX_THREAD_WATCHDOG
  1744. osRtxThreadWatchdogRemove(thread);
  1745. #endif
  1746. osRtxMutexOwnerRelease(thread->mutex_list);
  1747. osRtxThreadJoinWakeup(thread);
  1748. osRtxThreadDestroy(thread);
  1749. }
  1750. thread = thread_next;
  1751. }
  1752. // Threads in Ready List
  1753. thread = osRtxInfo.thread.ready.thread_list;
  1754. while (thread != NULL) {
  1755. thread_next = thread->thread_next;
  1756. if (thread->zone == zone) {
  1757. osRtxThreadListRemove(thread);
  1758. #ifdef RTX_THREAD_WATCHDOG
  1759. osRtxThreadWatchdogRemove(thread);
  1760. #endif
  1761. osRtxMutexOwnerRelease(thread->mutex_list);
  1762. osRtxThreadJoinWakeup(thread);
  1763. osRtxThreadDestroy(thread);
  1764. }
  1765. thread = thread_next;
  1766. }
  1767. // Running Thread
  1768. thread = osRtxThreadGetRunning();
  1769. if ((thread != NULL) && (thread->zone == zone)) {
  1770. if ((osRtxKernelGetState() != osRtxKernelRunning) ||
  1771. (osRtxInfo.thread.ready.thread_list == NULL)) {
  1772. osRtxThreadDispatch(NULL);
  1773. EvrRtxThreadError(thread, (int32_t)osErrorResource);
  1774. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1775. return osErrorResource;
  1776. }
  1777. #ifdef RTX_THREAD_WATCHDOG
  1778. osRtxThreadWatchdogRemove(thread);
  1779. #endif
  1780. osRtxMutexOwnerRelease(thread->mutex_list);
  1781. osRtxThreadJoinWakeup(thread);
  1782. // Switch to next Ready Thread
  1783. osRtxThreadSwitch(osRtxThreadListGet(&osRtxInfo.thread.ready));
  1784. // Update Stack Pointer
  1785. thread->sp = __get_PSP();
  1786. #ifdef RTX_STACK_CHECK
  1787. // Check Stack usage
  1788. if (!osRtxThreadStackCheck(thread)) {
  1789. osRtxThreadSetRunning(osRtxInfo.thread.run.next);
  1790. (void)osRtxKernelErrorNotify(osRtxErrorStackOverflow, thread);
  1791. }
  1792. #endif
  1793. // Mark running thread as deleted
  1794. osRtxThreadSetRunning(NULL);
  1795. // Destroy Thread
  1796. osRtxThreadDestroy(thread);
  1797. } else {
  1798. osRtxThreadDispatch(NULL);
  1799. }
  1800. return osOK;
  1801. }
  1802. #endif
  1803. /// Get number of active threads.
  1804. /// \note API identical to osThreadGetCount
  1805. static uint32_t svcRtxThreadGetCount (void) {
  1806. const os_thread_t *thread;
  1807. uint32_t count;
  1808. // Running Thread
  1809. count = 1U;
  1810. // Ready List
  1811. for (thread = osRtxInfo.thread.ready.thread_list;
  1812. thread != NULL; thread = thread->thread_next) {
  1813. count++;
  1814. }
  1815. // Delay List
  1816. for (thread = osRtxInfo.thread.delay_list;
  1817. thread != NULL; thread = thread->delay_next) {
  1818. count++;
  1819. }
  1820. // Wait List
  1821. for (thread = osRtxInfo.thread.wait_list;
  1822. thread != NULL; thread = thread->delay_next) {
  1823. count++;
  1824. }
  1825. EvrRtxThreadGetCount(count);
  1826. return count;
  1827. }
  1828. /// Enumerate active threads.
  1829. /// \note API identical to osThreadEnumerate
  1830. static uint32_t svcRtxThreadEnumerate (osThreadId_t *thread_array, uint32_t array_items) {
  1831. os_thread_t *thread;
  1832. uint32_t count;
  1833. // Check parameters
  1834. if ((thread_array == NULL) || (array_items == 0U)) {
  1835. EvrRtxThreadEnumerate(thread_array, array_items, 0U);
  1836. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1837. return 0U;
  1838. }
  1839. // Running Thread
  1840. *thread_array = osRtxThreadGetRunning();
  1841. thread_array++;
  1842. count = 1U;
  1843. // Ready List
  1844. for (thread = osRtxInfo.thread.ready.thread_list;
  1845. (thread != NULL) && (count < array_items); thread = thread->thread_next) {
  1846. *thread_array = thread;
  1847. thread_array++;
  1848. count++;
  1849. }
  1850. // Delay List
  1851. for (thread = osRtxInfo.thread.delay_list;
  1852. (thread != NULL) && (count < array_items); thread = thread->delay_next) {
  1853. *thread_array = thread;
  1854. thread_array++;
  1855. count++;
  1856. }
  1857. // Wait List
  1858. for (thread = osRtxInfo.thread.wait_list;
  1859. (thread != NULL) && (count < array_items); thread = thread->delay_next) {
  1860. *thread_array = thread;
  1861. thread_array++;
  1862. count++;
  1863. }
  1864. EvrRtxThreadEnumerate(thread_array - count, array_items, count);
  1865. return count;
  1866. }
  1867. /// Set the specified Thread Flags of a thread.
  1868. /// \note API identical to osThreadFlagsSet
  1869. static uint32_t svcRtxThreadFlagsSet (osThreadId_t thread_id, uint32_t flags) {
  1870. os_thread_t *thread = osRtxThreadId(thread_id);
  1871. #ifdef RTX_SAFETY_CLASS
  1872. const os_thread_t *thread_running;
  1873. #endif
  1874. uint32_t thread_flags;
  1875. uint32_t thread_flags0;
  1876. // Check parameters
  1877. if (!IsThreadPtrValid(thread) || (thread->id != osRtxIdThread) ||
  1878. ((flags & ~(((uint32_t)1U << osRtxThreadFlagsLimit) - 1U)) != 0U)) {
  1879. EvrRtxThreadFlagsError(thread, (int32_t)osErrorParameter);
  1880. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1881. return ((uint32_t)osErrorParameter);
  1882. }
  1883. // Check object state
  1884. if (thread->state == osRtxThreadTerminated) {
  1885. EvrRtxThreadFlagsError(thread, (int32_t)osErrorResource);
  1886. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1887. return ((uint32_t)osErrorResource);
  1888. }
  1889. #ifdef RTX_SAFETY_CLASS
  1890. // Check running thread safety class
  1891. thread_running = osRtxThreadGetRunning();
  1892. if ((thread_running != NULL) &&
  1893. ((thread_running->attr >> osRtxAttrClass_Pos) < (thread->attr >> osRtxAttrClass_Pos))) {
  1894. EvrRtxThreadFlagsError(thread, (int32_t)osErrorSafetyClass);
  1895. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1896. return ((uint32_t)osErrorSafetyClass);
  1897. }
  1898. #endif
  1899. // Set Thread Flags
  1900. thread_flags = ThreadFlagsSet(thread, flags);
  1901. // Check if Thread is waiting for Thread Flags
  1902. if (thread->state == osRtxThreadWaitingThreadFlags) {
  1903. thread_flags0 = ThreadFlagsCheck(thread, thread->wait_flags, thread->flags_options);
  1904. if (thread_flags0 != 0U) {
  1905. if ((thread->flags_options & osFlagsNoClear) == 0U) {
  1906. thread_flags = thread_flags0 & ~thread->wait_flags;
  1907. } else {
  1908. thread_flags = thread_flags0;
  1909. }
  1910. osRtxThreadWaitExit(thread, thread_flags0, TRUE);
  1911. EvrRtxThreadFlagsWaitCompleted(thread->wait_flags, thread->flags_options, thread_flags0, thread);
  1912. }
  1913. }
  1914. EvrRtxThreadFlagsSetDone(thread, thread_flags);
  1915. return thread_flags;
  1916. }
  1917. /// Clear the specified Thread Flags of current running thread.
  1918. /// \note API identical to osThreadFlagsClear
  1919. static uint32_t svcRtxThreadFlagsClear (uint32_t flags) {
  1920. os_thread_t *thread;
  1921. uint32_t thread_flags;
  1922. // Check running thread
  1923. thread = osRtxThreadGetRunning();
  1924. if (thread == NULL) {
  1925. EvrRtxThreadFlagsError(NULL, osRtxErrorKernelNotRunning);
  1926. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1927. return ((uint32_t)osError);
  1928. }
  1929. // Check parameters
  1930. if ((flags & ~(((uint32_t)1U << osRtxThreadFlagsLimit) - 1U)) != 0U) {
  1931. EvrRtxThreadFlagsError(thread, (int32_t)osErrorParameter);
  1932. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1933. return ((uint32_t)osErrorParameter);
  1934. }
  1935. // Clear Thread Flags
  1936. thread_flags = ThreadFlagsClear(thread, flags);
  1937. EvrRtxThreadFlagsClearDone(thread_flags);
  1938. return thread_flags;
  1939. }
  1940. /// Get the current Thread Flags of current running thread.
  1941. /// \note API identical to osThreadFlagsGet
  1942. static uint32_t svcRtxThreadFlagsGet (void) {
  1943. const os_thread_t *thread;
  1944. // Check running thread
  1945. thread = osRtxThreadGetRunning();
  1946. if (thread == NULL) {
  1947. EvrRtxThreadFlagsGet(0U);
  1948. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1949. return 0U;
  1950. }
  1951. EvrRtxThreadFlagsGet(thread->thread_flags);
  1952. return thread->thread_flags;
  1953. }
  1954. /// Wait for one or more Thread Flags of the current running thread to become signaled.
  1955. /// \note API identical to osThreadFlagsWait
  1956. static uint32_t svcRtxThreadFlagsWait (uint32_t flags, uint32_t options, uint32_t timeout) {
  1957. os_thread_t *thread;
  1958. uint32_t thread_flags;
  1959. // Check running thread
  1960. thread = osRtxThreadGetRunning();
  1961. if (thread == NULL) {
  1962. EvrRtxThreadFlagsError(NULL, osRtxErrorKernelNotRunning);
  1963. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1964. return ((uint32_t)osError);
  1965. }
  1966. // Check parameters
  1967. if ((flags & ~(((uint32_t)1U << osRtxThreadFlagsLimit) - 1U)) != 0U) {
  1968. EvrRtxThreadFlagsError(thread, (int32_t)osErrorParameter);
  1969. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  1970. return ((uint32_t)osErrorParameter);
  1971. }
  1972. // Check Thread Flags
  1973. thread_flags = ThreadFlagsCheck(thread, flags, options);
  1974. if (thread_flags != 0U) {
  1975. EvrRtxThreadFlagsWaitCompleted(flags, options, thread_flags, thread);
  1976. } else {
  1977. // Check if timeout is specified
  1978. if (timeout != 0U) {
  1979. // Store waiting flags and options
  1980. EvrRtxThreadFlagsWaitPending(flags, options, timeout);
  1981. thread->wait_flags = flags;
  1982. thread->flags_options = (uint8_t)options;
  1983. // Suspend current Thread
  1984. if (!osRtxThreadWaitEnter(osRtxThreadWaitingThreadFlags, timeout)) {
  1985. EvrRtxThreadFlagsWaitTimeout(thread);
  1986. }
  1987. thread_flags = (uint32_t)osErrorTimeout;
  1988. } else {
  1989. EvrRtxThreadFlagsWaitNotCompleted(flags, options);
  1990. thread_flags = (uint32_t)osErrorResource;
  1991. }
  1992. }
  1993. return thread_flags;
  1994. }
  1995. // Service Calls definitions
  1996. //lint ++flb "Library Begin" [MISRA Note 11]
  1997. SVC0_3 (ThreadNew, osThreadId_t, osThreadFunc_t, void *, const osThreadAttr_t *)
  1998. SVC0_1 (ThreadGetName, const char *, osThreadId_t)
  1999. #ifdef RTX_SAFETY_CLASS
  2000. SVC0_1 (ThreadGetClass, uint32_t, osThreadId_t)
  2001. #endif
  2002. #ifdef RTX_EXECUTION_ZONE
  2003. SVC0_1 (ThreadGetZone, uint32_t, osThreadId_t)
  2004. #endif
  2005. SVC0_0 (ThreadGetId, osThreadId_t)
  2006. SVC0_1 (ThreadGetState, osThreadState_t, osThreadId_t)
  2007. SVC0_1 (ThreadGetStackSize, uint32_t, osThreadId_t)
  2008. SVC0_1 (ThreadGetStackSpace, uint32_t, osThreadId_t)
  2009. SVC0_2 (ThreadSetPriority, osStatus_t, osThreadId_t, osPriority_t)
  2010. SVC0_1 (ThreadGetPriority, osPriority_t, osThreadId_t)
  2011. SVC0_0 (ThreadYield, osStatus_t)
  2012. SVC0_1 (ThreadSuspend, osStatus_t, osThreadId_t)
  2013. SVC0_1 (ThreadResume, osStatus_t, osThreadId_t)
  2014. SVC0_1 (ThreadDetach, osStatus_t, osThreadId_t)
  2015. SVC0_1 (ThreadJoin, osStatus_t, osThreadId_t)
  2016. SVC0_0N(ThreadExit, void)
  2017. SVC0_1 (ThreadTerminate, osStatus_t, osThreadId_t)
  2018. #ifdef RTX_THREAD_WATCHDOG
  2019. SVC0_1 (ThreadFeedWatchdog, osStatus_t, uint32_t)
  2020. #endif
  2021. #ifdef RTX_SAFETY_FEATURES
  2022. SVC0_0 (ThreadProtectPrivileged, osStatus_t)
  2023. #endif
  2024. #ifdef RTX_SAFETY_CLASS
  2025. SVC0_2 (ThreadSuspendClass, osStatus_t, uint32_t, uint32_t)
  2026. SVC0_2 (ThreadResumeClass, osStatus_t, uint32_t, uint32_t)
  2027. #endif
  2028. SVC0_0 (ThreadGetCount, uint32_t)
  2029. SVC0_2 (ThreadEnumerate, uint32_t, osThreadId_t *, uint32_t)
  2030. SVC0_2 (ThreadFlagsSet, uint32_t, osThreadId_t, uint32_t)
  2031. SVC0_1 (ThreadFlagsClear, uint32_t, uint32_t)
  2032. SVC0_0 (ThreadFlagsGet, uint32_t)
  2033. SVC0_3 (ThreadFlagsWait, uint32_t, uint32_t, uint32_t, uint32_t)
  2034. //lint --flb "Library End"
  2035. // ==== ISR Calls ====
  2036. /// Set the specified Thread Flags of a thread.
  2037. /// \note API identical to osThreadFlagsSet
  2038. __STATIC_INLINE
  2039. uint32_t isrRtxThreadFlagsSet (osThreadId_t thread_id, uint32_t flags) {
  2040. os_thread_t *thread = osRtxThreadId(thread_id);
  2041. uint32_t thread_flags;
  2042. // Check parameters
  2043. if (!IsThreadPtrValid(thread) || (thread->id != osRtxIdThread) ||
  2044. ((flags & ~(((uint32_t)1U << osRtxThreadFlagsLimit) - 1U)) != 0U)) {
  2045. EvrRtxThreadFlagsError(thread, (int32_t)osErrorParameter);
  2046. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  2047. return ((uint32_t)osErrorParameter);
  2048. }
  2049. // Check object state
  2050. if (thread->state == osRtxThreadTerminated) {
  2051. EvrRtxThreadFlagsError(thread, (int32_t)osErrorResource);
  2052. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  2053. return ((uint32_t)osErrorResource);
  2054. }
  2055. // Set Thread Flags
  2056. thread_flags = ThreadFlagsSet(thread, flags);
  2057. // Register post ISR processing
  2058. osRtxPostProcess(osRtxObject(thread));
  2059. EvrRtxThreadFlagsSetDone(thread, thread_flags);
  2060. return thread_flags;
  2061. }
  2062. // ==== Library functions ====
  2063. /// RTOS Thread Before Free Hook.
  2064. //lint -esym(759,osRtxThreadBeforeFree) "Prototype in header"
  2065. //lint -esym(765,osRtxThreadBeforeFree) "Global scope (can be overridden)"
  2066. __WEAK void osRtxThreadBeforeFree (os_thread_t *thread) {
  2067. (void)thread;
  2068. }
  2069. /// Thread startup (Idle and Timer Thread).
  2070. /// \return true - success, false - failure.
  2071. bool_t osRtxThreadStartup (void) {
  2072. bool_t ret = FALSE;
  2073. // Create Idle Thread
  2074. osRtxInfo.thread.idle = osRtxThreadId(
  2075. svcRtxThreadNew(osRtxIdleThread, NULL, osRtxConfig.idle_thread_attr)
  2076. );
  2077. // Create Timer Thread
  2078. if (osRtxConfig.timer_setup != NULL) {
  2079. if (osRtxConfig.timer_setup() == 0) {
  2080. osRtxInfo.timer.thread = osRtxThreadId(
  2081. svcRtxThreadNew(osRtxConfig.timer_thread, osRtxInfo.timer.mq, osRtxConfig.timer_thread_attr)
  2082. );
  2083. if (osRtxInfo.timer.thread != NULL) {
  2084. ret = TRUE;
  2085. }
  2086. }
  2087. } else {
  2088. ret = TRUE;
  2089. }
  2090. return ret;
  2091. }
  2092. // ==== Public API ====
  2093. /// Create a thread and add it to Active Threads.
  2094. osThreadId_t osThreadNew (osThreadFunc_t func, void *argument, const osThreadAttr_t *attr) {
  2095. osThreadId_t thread_id;
  2096. EvrRtxThreadNew(func, argument, attr);
  2097. if (IsException() || IsIrqMasked()) {
  2098. EvrRtxThreadError(NULL, (int32_t)osErrorISR);
  2099. thread_id = NULL;
  2100. } else {
  2101. thread_id = __svcThreadNew(func, argument, attr);
  2102. }
  2103. return thread_id;
  2104. }
  2105. /// Get name of a thread.
  2106. const char *osThreadGetName (osThreadId_t thread_id) {
  2107. const char *name;
  2108. if (IsException() || IsIrqMasked()) {
  2109. name = svcRtxThreadGetName(thread_id);
  2110. } else {
  2111. name = __svcThreadGetName(thread_id);
  2112. }
  2113. return name;
  2114. }
  2115. #ifdef RTX_SAFETY_CLASS
  2116. /// Get safety class of a thread.
  2117. uint32_t osThreadGetClass (osThreadId_t thread_id) {
  2118. uint32_t safety_class;
  2119. if (IsException() || IsIrqMasked()) {
  2120. safety_class = svcRtxThreadGetClass(thread_id);
  2121. } else {
  2122. safety_class = __svcThreadGetClass(thread_id);
  2123. }
  2124. return safety_class;
  2125. }
  2126. #endif
  2127. #ifdef RTX_EXECUTION_ZONE
  2128. /// Get zone of a thread.
  2129. uint32_t osThreadGetZone (osThreadId_t thread_id) {
  2130. uint32_t zone;
  2131. if (IsException() || IsIrqMasked()) {
  2132. zone = svcRtxThreadGetZone(thread_id);
  2133. } else {
  2134. zone = __svcThreadGetZone(thread_id);
  2135. }
  2136. return zone;
  2137. }
  2138. #endif
  2139. /// Return the thread ID of the current running thread.
  2140. osThreadId_t osThreadGetId (void) {
  2141. osThreadId_t thread_id;
  2142. if (IsException() || IsIrqMasked()) {
  2143. thread_id = svcRtxThreadGetId();
  2144. } else {
  2145. thread_id = __svcThreadGetId();
  2146. }
  2147. return thread_id;
  2148. }
  2149. /// Get current thread state of a thread.
  2150. osThreadState_t osThreadGetState (osThreadId_t thread_id) {
  2151. osThreadState_t state;
  2152. if (IsException() || IsIrqMasked()) {
  2153. EvrRtxThreadGetState(thread_id, osThreadError);
  2154. state = osThreadError;
  2155. } else {
  2156. state = __svcThreadGetState(thread_id);
  2157. }
  2158. return state;
  2159. }
  2160. /// Get stack size of a thread.
  2161. uint32_t osThreadGetStackSize (osThreadId_t thread_id) {
  2162. uint32_t stack_size;
  2163. if (IsException() || IsIrqMasked()) {
  2164. EvrRtxThreadGetStackSize(thread_id, 0U);
  2165. stack_size = 0U;
  2166. } else {
  2167. stack_size = __svcThreadGetStackSize(thread_id);
  2168. }
  2169. return stack_size;
  2170. }
  2171. /// Get available stack space of a thread based on stack watermark recording during execution.
  2172. uint32_t osThreadGetStackSpace (osThreadId_t thread_id) {
  2173. uint32_t stack_space;
  2174. if (IsException() || IsIrqMasked()) {
  2175. EvrRtxThreadGetStackSpace(thread_id, 0U);
  2176. stack_space = 0U;
  2177. } else {
  2178. stack_space = __svcThreadGetStackSpace(thread_id);
  2179. }
  2180. return stack_space;
  2181. }
  2182. /// Change priority of a thread.
  2183. osStatus_t osThreadSetPriority (osThreadId_t thread_id, osPriority_t priority) {
  2184. osStatus_t status;
  2185. EvrRtxThreadSetPriority(thread_id, priority);
  2186. if (IsException() || IsIrqMasked()) {
  2187. EvrRtxThreadError(thread_id, (int32_t)osErrorISR);
  2188. status = osErrorISR;
  2189. } else {
  2190. status = __svcThreadSetPriority(thread_id, priority);
  2191. }
  2192. return status;
  2193. }
  2194. /// Get current priority of a thread.
  2195. osPriority_t osThreadGetPriority (osThreadId_t thread_id) {
  2196. osPriority_t priority;
  2197. if (IsException() || IsIrqMasked()) {
  2198. EvrRtxThreadGetPriority(thread_id, osPriorityError);
  2199. priority = osPriorityError;
  2200. } else {
  2201. priority = __svcThreadGetPriority(thread_id);
  2202. }
  2203. return priority;
  2204. }
  2205. /// Pass control to next thread that is in state READY.
  2206. osStatus_t osThreadYield (void) {
  2207. osStatus_t status;
  2208. EvrRtxThreadYield();
  2209. if (IsException() || IsIrqMasked()) {
  2210. EvrRtxThreadError(NULL, (int32_t)osErrorISR);
  2211. status = osErrorISR;
  2212. } else {
  2213. status = __svcThreadYield();
  2214. }
  2215. return status;
  2216. }
  2217. /// Suspend execution of a thread.
  2218. osStatus_t osThreadSuspend (osThreadId_t thread_id) {
  2219. osStatus_t status;
  2220. EvrRtxThreadSuspend(thread_id);
  2221. if (IsException() || IsIrqMasked()) {
  2222. EvrRtxThreadError(thread_id, (int32_t)osErrorISR);
  2223. status = osErrorISR;
  2224. } else {
  2225. status = __svcThreadSuspend(thread_id);
  2226. }
  2227. return status;
  2228. }
  2229. /// Resume execution of a thread.
  2230. osStatus_t osThreadResume (osThreadId_t thread_id) {
  2231. osStatus_t status;
  2232. EvrRtxThreadResume(thread_id);
  2233. if (IsException() || IsIrqMasked()) {
  2234. EvrRtxThreadError(thread_id, (int32_t)osErrorISR);
  2235. status = osErrorISR;
  2236. } else {
  2237. status = __svcThreadResume(thread_id);
  2238. }
  2239. return status;
  2240. }
  2241. /// Detach a thread (thread storage can be reclaimed when thread terminates).
  2242. osStatus_t osThreadDetach (osThreadId_t thread_id) {
  2243. osStatus_t status;
  2244. EvrRtxThreadDetach(thread_id);
  2245. if (IsException() || IsIrqMasked()) {
  2246. EvrRtxThreadError(thread_id, (int32_t)osErrorISR);
  2247. status = osErrorISR;
  2248. } else {
  2249. status = __svcThreadDetach(thread_id);
  2250. }
  2251. return status;
  2252. }
  2253. /// Wait for specified thread to terminate.
  2254. osStatus_t osThreadJoin (osThreadId_t thread_id) {
  2255. osStatus_t status;
  2256. EvrRtxThreadJoin(thread_id);
  2257. if (IsException() || IsIrqMasked()) {
  2258. EvrRtxThreadError(thread_id, (int32_t)osErrorISR);
  2259. status = osErrorISR;
  2260. } else {
  2261. status = __svcThreadJoin(thread_id);
  2262. }
  2263. return status;
  2264. }
  2265. /// Terminate execution of current running thread.
  2266. __NO_RETURN void osThreadExit (void) {
  2267. EvrRtxThreadExit();
  2268. __svcThreadExit();
  2269. EvrRtxThreadError(NULL, (int32_t)osError);
  2270. for (;;) {}
  2271. }
  2272. /// Terminate execution of a thread.
  2273. osStatus_t osThreadTerminate (osThreadId_t thread_id) {
  2274. osStatus_t status;
  2275. EvrRtxThreadTerminate(thread_id);
  2276. if (IsException() || IsIrqMasked()) {
  2277. EvrRtxThreadError(thread_id, (int32_t)osErrorISR);
  2278. status = osErrorISR;
  2279. } else {
  2280. status = __svcThreadTerminate(thread_id);
  2281. }
  2282. return status;
  2283. }
  2284. #ifdef RTX_THREAD_WATCHDOG
  2285. /// Feed watchdog of the current running thread.
  2286. osStatus_t osThreadFeedWatchdog (uint32_t ticks) {
  2287. osStatus_t status;
  2288. EvrRtxThreadFeedWatchdog(ticks);
  2289. if (IsException() || IsIrqMasked()) {
  2290. EvrRtxThreadError(NULL, (int32_t)osErrorISR);
  2291. status = osErrorISR;
  2292. } else {
  2293. status = __svcThreadFeedWatchdog(ticks);
  2294. }
  2295. return status;
  2296. }
  2297. #endif
  2298. #ifdef RTX_SAFETY_FEATURES
  2299. /// Protect the creation of privileged threads.
  2300. osStatus_t osThreadProtectPrivileged (void) {
  2301. osStatus_t status;
  2302. EvrRtxThreadProtectPrivileged();
  2303. if (IsException() || IsIrqMasked()) {
  2304. EvrRtxThreadError(NULL, (int32_t)osErrorISR);
  2305. status = osErrorISR;
  2306. } else {
  2307. status = __svcThreadProtectPrivileged();
  2308. }
  2309. return status;
  2310. }
  2311. #endif
  2312. #ifdef RTX_SAFETY_CLASS
  2313. /// Suspend execution of threads for specified safety classes.
  2314. osStatus_t osThreadSuspendClass (uint32_t safety_class, uint32_t mode) {
  2315. osStatus_t status;
  2316. EvrRtxThreadSuspendClass(safety_class, mode);
  2317. if (IsException() || IsIrqMasked()) {
  2318. if (IsTickIrq(osRtxInfo.tick_irqn)) {
  2319. status = svcRtxThreadSuspendClass(safety_class, mode);
  2320. } else {
  2321. EvrRtxThreadError(NULL, (int32_t)osErrorISR);
  2322. status = osErrorISR;
  2323. }
  2324. } else {
  2325. status = __svcThreadSuspendClass(safety_class, mode);
  2326. }
  2327. return status;
  2328. }
  2329. /// Resume execution of threads for specified safety classes.
  2330. osStatus_t osThreadResumeClass (uint32_t safety_class, uint32_t mode) {
  2331. osStatus_t status;
  2332. EvrRtxThreadResumeClass(safety_class, mode);
  2333. if (IsException() || IsIrqMasked()) {
  2334. if (IsTickIrq(osRtxInfo.tick_irqn)) {
  2335. status = svcRtxThreadResumeClass(safety_class, mode);
  2336. } else {
  2337. EvrRtxThreadError(NULL, (int32_t)osErrorISR);
  2338. status = osErrorISR;
  2339. }
  2340. } else {
  2341. status = __svcThreadResumeClass(safety_class, mode);
  2342. }
  2343. return status;
  2344. }
  2345. #endif
  2346. #ifdef RTX_EXECUTION_ZONE
  2347. /// Terminate execution of threads assigned to a specified MPU protected zone.
  2348. osStatus_t osThreadTerminateZone (uint32_t zone) {
  2349. osStatus_t status;
  2350. EvrRtxThreadTerminateZone(zone);
  2351. if (IsException() || IsIrqMasked()) {
  2352. if (IsFault() || IsSVCallIrq() || IsPendSvIrq() || IsTickIrq(osRtxInfo.tick_irqn)) {
  2353. status = svcRtxThreadTerminateZone(zone);
  2354. } else {
  2355. EvrRtxThreadError(NULL, (int32_t)osErrorISR);
  2356. status = osErrorISR;
  2357. }
  2358. } else {
  2359. EvrRtxThreadError(osRtxThreadGetRunning(), (int32_t)osError);
  2360. status = osError;
  2361. }
  2362. return status;
  2363. }
  2364. #endif
  2365. /// Get number of active threads.
  2366. uint32_t osThreadGetCount (void) {
  2367. uint32_t count;
  2368. if (IsException() || IsIrqMasked()) {
  2369. EvrRtxThreadGetCount(0U);
  2370. count = 0U;
  2371. } else {
  2372. count = __svcThreadGetCount();
  2373. }
  2374. return count;
  2375. }
  2376. /// Enumerate active threads.
  2377. uint32_t osThreadEnumerate (osThreadId_t *thread_array, uint32_t array_items) {
  2378. uint32_t count;
  2379. if (IsException() || IsIrqMasked()) {
  2380. EvrRtxThreadEnumerate(thread_array, array_items, 0U);
  2381. count = 0U;
  2382. } else {
  2383. count = __svcThreadEnumerate(thread_array, array_items);
  2384. }
  2385. return count;
  2386. }
  2387. /// Set the specified Thread Flags of a thread.
  2388. uint32_t osThreadFlagsSet (osThreadId_t thread_id, uint32_t flags) {
  2389. uint32_t thread_flags;
  2390. EvrRtxThreadFlagsSet(thread_id, flags);
  2391. if (IsException() || IsIrqMasked()) {
  2392. thread_flags = isrRtxThreadFlagsSet(thread_id, flags);
  2393. } else {
  2394. thread_flags = __svcThreadFlagsSet(thread_id, flags);
  2395. }
  2396. return thread_flags;
  2397. }
  2398. /// Clear the specified Thread Flags of current running thread.
  2399. uint32_t osThreadFlagsClear (uint32_t flags) {
  2400. uint32_t thread_flags;
  2401. EvrRtxThreadFlagsClear(flags);
  2402. if (IsException() || IsIrqMasked()) {
  2403. EvrRtxThreadFlagsError(NULL, (int32_t)osErrorISR);
  2404. thread_flags = (uint32_t)osErrorISR;
  2405. } else {
  2406. thread_flags = __svcThreadFlagsClear(flags);
  2407. }
  2408. return thread_flags;
  2409. }
  2410. /// Get the current Thread Flags of current running thread.
  2411. uint32_t osThreadFlagsGet (void) {
  2412. uint32_t thread_flags;
  2413. if (IsException() || IsIrqMasked()) {
  2414. EvrRtxThreadFlagsGet(0U);
  2415. thread_flags = 0U;
  2416. } else {
  2417. thread_flags = __svcThreadFlagsGet();
  2418. }
  2419. return thread_flags;
  2420. }
  2421. /// Wait for one or more Thread Flags of the current running thread to become signaled.
  2422. uint32_t osThreadFlagsWait (uint32_t flags, uint32_t options, uint32_t timeout) {
  2423. uint32_t thread_flags;
  2424. EvrRtxThreadFlagsWait(flags, options, timeout);
  2425. if (IsException() || IsIrqMasked()) {
  2426. EvrRtxThreadFlagsError(NULL, (int32_t)osErrorISR);
  2427. thread_flags = (uint32_t)osErrorISR;
  2428. } else {
  2429. thread_flags = __svcThreadFlagsWait(flags, options, timeout);
  2430. }
  2431. return thread_flags;
  2432. }