ipc.c 134 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2006-03-14 Bernard the first version
  9. * 2006-04-25 Bernard implement semaphore
  10. * 2006-05-03 Bernard add RT_IPC_DEBUG
  11. * modify the type of IPC waiting time to rt_int32_t
  12. * 2006-05-10 Bernard fix the semaphore take bug and add IPC object
  13. * 2006-05-12 Bernard implement mailbox and message queue
  14. * 2006-05-20 Bernard implement mutex
  15. * 2006-05-23 Bernard implement fast event
  16. * 2006-05-24 Bernard implement event
  17. * 2006-06-03 Bernard fix the thread timer init bug
  18. * 2006-06-05 Bernard fix the mutex release bug
  19. * 2006-06-07 Bernard fix the message queue send bug
  20. * 2006-08-04 Bernard add hook support
  21. * 2009-05-21 Yi.qiu fix the sem release bug
  22. * 2009-07-18 Bernard fix the event clear bug
  23. * 2009-09-09 Bernard remove fast event and fix ipc release bug
  24. * 2009-10-10 Bernard change semaphore and mutex value to unsigned value
  25. * 2009-10-25 Bernard change the mb/mq receive timeout to 0 if the
  26. * re-calculated delta tick is a negative number.
  27. * 2009-12-16 Bernard fix the rt_ipc_object_suspend issue when IPC flag
  28. * is RT_IPC_FLAG_PRIO
  29. * 2010-01-20 mbbill remove rt_ipc_object_decrease function.
  30. * 2010-04-20 Bernard move memcpy outside interrupt disable in mq
  31. * 2010-10-26 yi.qiu add module support in rt_mp_delete and rt_mq_delete
  32. * 2010-11-10 Bernard add IPC reset command implementation.
  33. * 2011-12-18 Bernard add more parameter checking in message queue
  34. * 2013-09-14 Grissiom add an option check in rt_event_recv
  35. * 2018-10-02 Bernard add 64bit support for mailbox
  36. * 2019-09-16 tyx add send wait support for message queue
  37. * 2020-07-29 Meco Man fix thread->event_set/event_info when received an
  38. * event without pending
  39. * 2020-10-11 Meco Man add value overflow-check code
  40. * 2021-01-03 Meco Man implement rt_mb_urgent()
  41. * 2021-05-30 Meco Man implement rt_mutex_trytake()
  42. * 2022-01-07 Gabriel Moving __on_rt_xxxxx_hook to ipc.c
  43. * 2022-01-24 THEWON let rt_mutex_take return thread->error when using signal
  44. * 2022-04-08 Stanley Correct descriptions
  45. * 2022-10-15 Bernard add nested mutex feature
  46. * 2022-10-16 Bernard add prioceiling feature in mutex
  47. * 2023-04-16 Xin-zheqi redesigen queue recv and send function return real message size
  48. * 2023-09-15 xqyjlj perf rt_hw_interrupt_disable/enable
  49. */
  50. #include <rtthread.h>
  51. #include <rthw.h>
  52. #define DBG_TAG "kernel.ipc"
  53. #define DBG_LVL DBG_INFO
  54. #include <rtdbg.h>
  55. #define GET_MESSAGEBYTE_ADDR(msg) ((struct rt_mq_message *) msg + 1)
  56. #if defined(RT_USING_HOOK) && defined(RT_HOOK_USING_FUNC_PTR)
  57. extern void (*rt_object_trytake_hook)(struct rt_object *object);
  58. extern void (*rt_object_take_hook)(struct rt_object *object);
  59. extern void (*rt_object_put_hook)(struct rt_object *object);
  60. #endif /* RT_USING_HOOK */
  61. /**
  62. * @addtogroup IPC
  63. * @{
  64. */
  65. /**
  66. * @brief This function will initialize an IPC object, such as semaphore, mutex, messagequeue and mailbox.
  67. *
  68. * @note Executing this function will complete an initialization of the suspend thread list of the ipc object.
  69. *
  70. * @param ipc is a pointer to the IPC object.
  71. *
  72. * @return Return the operation status. When the return value is RT_EOK, the initialization is successful.
  73. * When the return value is any other values, it means the initialization failed.
  74. *
  75. * @warning This function can be called from all IPC initialization and creation.
  76. */
  77. rt_inline rt_err_t _ipc_object_init(struct rt_ipc_object *ipc)
  78. {
  79. /* initialize ipc object */
  80. rt_list_init(&(ipc->suspend_thread));
  81. return RT_EOK;
  82. }
  83. /**
  84. * @brief Dequeue a thread from suspended list and set it to ready. The 2 are
  85. * taken as an atomic operation, so if a thread is returned, it's
  86. * resumed by us, not any other threads or async events. This is useful
  87. * if a consumer may be resumed by timeout, signals... besides its
  88. * producer.
  89. *
  90. * @param susp_list the list thread dequeued from. RT_NULL if no list.
  91. * @param thread_error thread error number of the resuming thread.
  92. * A negative value in this set will be discarded, and thread error
  93. * will not be changed.
  94. *
  95. * @return struct rt_thread * RT_NULL if failed, otherwise the thread resumed
  96. */
  97. struct rt_thread *rt_susp_list_dequeue(rt_list_t *susp_list, rt_err_t thread_error)
  98. {
  99. rt_sched_lock_level_t slvl;
  100. rt_thread_t thread;
  101. rt_err_t error;
  102. RT_SCHED_DEBUG_IS_UNLOCKED;
  103. RT_ASSERT(susp_list != RT_NULL);
  104. rt_sched_lock(&slvl);
  105. if (!rt_list_isempty(susp_list))
  106. {
  107. thread = RT_THREAD_LIST_NODE_ENTRY(susp_list->next);
  108. error = rt_sched_thread_ready(thread);
  109. if (error)
  110. {
  111. LOG_D("%s [error:%d] failed to resume thread:%p from suspended list",
  112. __func__, error, thread);
  113. thread = RT_NULL;
  114. }
  115. else
  116. {
  117. /* thread error should not be a negative value */
  118. if (thread_error >= 0)
  119. {
  120. /* set thread error code to notified resuming thread */
  121. thread->error = thread_error;
  122. }
  123. }
  124. }
  125. else
  126. {
  127. thread = RT_NULL;
  128. }
  129. rt_sched_unlock(slvl);
  130. LOG_D("resume thread:%s\n", thread->parent.name);
  131. return thread;
  132. }
  133. /**
  134. * @brief This function will resume all suspended threads in the IPC object list,
  135. * including the suspended list of IPC object, and private list of mailbox etc.
  136. *
  137. * @note This function will resume all threads in the IPC object list.
  138. * By contrast, the rt_ipc_list_resume() function will resume a suspended thread in the list of a IPC object.
  139. *
  140. * @param susp_list is a pointer to a suspended thread list of the IPC object.
  141. * @param thread_error thread error number of the resuming thread.
  142. * A negative value in this set will be discarded, and thread error
  143. * will not be changed.
  144. *
  145. * @return Return the operation status. When the return value is RT_EOK, the function is successfully executed.
  146. * When the return value is any other values, it means this operation failed.
  147. *
  148. */
  149. rt_err_t rt_susp_list_resume_all(rt_list_t *susp_list, rt_err_t thread_error)
  150. {
  151. struct rt_thread *thread;
  152. RT_SCHED_DEBUG_IS_UNLOCKED;
  153. /* wakeup all suspended threads */
  154. thread = rt_susp_list_dequeue(susp_list, thread_error);
  155. while (thread)
  156. {
  157. /*
  158. * resume NEXT thread
  159. * In rt_thread_resume function, it will remove current thread from
  160. * suspended list
  161. */
  162. thread = rt_susp_list_dequeue(susp_list, thread_error);
  163. }
  164. return RT_EOK;
  165. }
  166. /**
  167. * @brief This function will resume all suspended threads in the IPC object list,
  168. * including the suspended list of IPC object, and private list of mailbox etc.
  169. * A lock is passing and hold while operating.
  170. *
  171. * @note This function will resume all threads in the IPC object list.
  172. * By contrast, the rt_ipc_list_resume() function will resume a suspended thread in the list of a IPC object.
  173. *
  174. * @param susp_list is a pointer to a suspended thread list of the IPC object.
  175. * @param thread_error thread error number of the resuming thread.
  176. * A negative value in this set will be discarded, and thread error
  177. * will not be changed.
  178. * @param lock the lock to be held while operating susp_list
  179. *
  180. * @return Return the operation status. When the return value is RT_EOK, the function is successfully executed.
  181. * When the return value is any other values, it means this operation failed.
  182. *
  183. */
  184. rt_err_t rt_susp_list_resume_all_irq(rt_list_t *susp_list,
  185. rt_err_t thread_error,
  186. struct rt_spinlock *lock)
  187. {
  188. struct rt_thread *thread;
  189. rt_base_t level;
  190. RT_SCHED_DEBUG_IS_UNLOCKED;
  191. do
  192. {
  193. level = rt_spin_lock_irqsave(lock);
  194. /*
  195. * resume NEXT thread
  196. * In rt_thread_resume function, it will remove current thread from
  197. * suspended list
  198. */
  199. thread = rt_susp_list_dequeue(susp_list, thread_error);
  200. rt_spin_unlock_irqrestore(lock, level);
  201. }
  202. while (thread);
  203. return RT_EOK;
  204. }
  205. /**
  206. * @brief Add a thread to the suspend list
  207. *
  208. * @note Caller must hold the scheduler lock
  209. *
  210. * @param susp_list the list thread enqueued to
  211. * @param thread the suspended thread
  212. * @param ipc_flags the pattern of suspend list
  213. * @return RT_EOK on succeed, otherwise a failure
  214. */
  215. rt_err_t rt_susp_list_enqueue(rt_list_t *susp_list, rt_thread_t thread, int ipc_flags)
  216. {
  217. RT_SCHED_DEBUG_IS_LOCKED;
  218. switch (ipc_flags)
  219. {
  220. case RT_IPC_FLAG_FIFO:
  221. rt_list_insert_before(susp_list, &RT_THREAD_LIST_NODE(thread));
  222. break; /* RT_IPC_FLAG_FIFO */
  223. case RT_IPC_FLAG_PRIO:
  224. {
  225. struct rt_list_node *n;
  226. struct rt_thread *sthread;
  227. /* find a suitable position */
  228. for (n = susp_list->next; n != susp_list; n = n->next)
  229. {
  230. sthread = RT_THREAD_LIST_NODE_ENTRY(n);
  231. /* find out */
  232. if (rt_sched_thread_get_curr_prio(thread) < rt_sched_thread_get_curr_prio(sthread))
  233. {
  234. /* insert this thread before the sthread */
  235. rt_list_insert_before(&RT_THREAD_LIST_NODE(sthread), &RT_THREAD_LIST_NODE(thread));
  236. break;
  237. }
  238. }
  239. /*
  240. * not found a suitable position,
  241. * append to the end of suspend_thread list
  242. */
  243. if (n == susp_list)
  244. rt_list_insert_before(susp_list, &RT_THREAD_LIST_NODE(thread));
  245. }
  246. break;/* RT_IPC_FLAG_PRIO */
  247. default:
  248. RT_ASSERT(0);
  249. break;
  250. }
  251. return RT_EOK;
  252. }
  253. /**
  254. * @brief Print thread on suspend list to system console
  255. */
  256. void rt_susp_list_print(rt_list_t *list)
  257. {
  258. #ifdef RT_USING_CONSOLE
  259. rt_sched_lock_level_t slvl;
  260. struct rt_thread *thread;
  261. struct rt_list_node *node;
  262. rt_sched_lock(&slvl);
  263. for (node = list->next; node != list; node = node->next)
  264. {
  265. thread = RT_THREAD_LIST_NODE_ENTRY(node);
  266. rt_kprintf("%.*s", RT_NAME_MAX, thread->parent.name);
  267. if (node->next != list)
  268. rt_kprintf("/");
  269. }
  270. rt_sched_unlock(slvl);
  271. #else
  272. (void)list;
  273. #endif
  274. }
  275. #ifdef RT_USING_SEMAPHORE
  276. /**
  277. * @addtogroup semaphore
  278. * @{
  279. */
  280. static void _sem_object_init(rt_sem_t sem,
  281. rt_uint16_t value,
  282. rt_uint8_t flag,
  283. rt_uint16_t max_value)
  284. {
  285. /* initialize ipc object */
  286. _ipc_object_init(&(sem->parent));
  287. sem->max_value = max_value;
  288. /* set initial value */
  289. sem->value = value;
  290. /* set parent */
  291. sem->parent.parent.flag = flag;
  292. rt_spin_lock_init(&(sem->spinlock));
  293. }
  294. /**
  295. * @brief This function will initialize a static semaphore object.
  296. *
  297. * @note For the static semaphore object, its memory space is allocated by the compiler during compiling,
  298. * and shall placed on the read-write data segment or on the uninitialized data segment.
  299. * By contrast, the rt_sem_create() function will allocate memory space automatically and initialize
  300. * the semaphore.
  301. *
  302. * @see rt_sem_create()
  303. *
  304. * @param sem is a pointer to the semaphore to initialize. It is assumed that storage for the semaphore will be
  305. * allocated in your application.
  306. *
  307. * @param name is a pointer to the name you would like to give the semaphore.
  308. *
  309. * @param value is the initial value for the semaphore.
  310. * If used to share resources, you should initialize the value as the number of available resources.
  311. * If used to signal the occurrence of an event, you should initialize the value as 0.
  312. *
  313. * @param flag is the semaphore flag, which determines the queuing way of how multiple threads wait
  314. * when the semaphore is not available.
  315. * The semaphore flag can be ONE of the following values:
  316. *
  317. * RT_IPC_FLAG_PRIO The pending threads will queue in order of priority.
  318. *
  319. * RT_IPC_FLAG_FIFO The pending threads will queue in the first-in-first-out method
  320. * (also known as first-come-first-served (FCFS) scheduling strategy).
  321. *
  322. * NOTE: RT_IPC_FLAG_FIFO is a non-real-time scheduling mode. It is strongly recommended to
  323. * use RT_IPC_FLAG_PRIO to ensure the thread is real-time UNLESS your applications concern about
  324. * the first-in-first-out principle, and you clearly understand that all threads involved in
  325. * this semaphore will become non-real-time threads.
  326. *
  327. * @return Return the operation status. When the return value is RT_EOK, the initialization is successful.
  328. * If the return value is any other values, it represents the initialization failed.
  329. *
  330. * @warning This function can ONLY be called from threads.
  331. */
  332. rt_err_t rt_sem_init(rt_sem_t sem,
  333. const char *name,
  334. rt_uint32_t value,
  335. rt_uint8_t flag)
  336. {
  337. RT_ASSERT(sem != RT_NULL);
  338. RT_ASSERT(value < 0x10000U);
  339. RT_ASSERT((flag == RT_IPC_FLAG_FIFO) || (flag == RT_IPC_FLAG_PRIO));
  340. /* initialize object */
  341. rt_object_init(&(sem->parent.parent), RT_Object_Class_Semaphore, name);
  342. _sem_object_init(sem, value, flag, RT_SEM_VALUE_MAX);
  343. return RT_EOK;
  344. }
  345. RTM_EXPORT(rt_sem_init);
  346. /**
  347. * @brief This function will detach a static semaphore object.
  348. *
  349. * @note This function is used to detach a static semaphore object which is initialized by rt_sem_init() function.
  350. * By contrast, the rt_sem_delete() function will delete a semaphore object.
  351. * When the semaphore is successfully detached, it will resume all suspended threads in the semaphore list.
  352. *
  353. * @see rt_sem_delete()
  354. *
  355. * @param sem is a pointer to a semaphore object to be detached.
  356. *
  357. * @return Return the operation status. When the return value is RT_EOK, the initialization is successful.
  358. * If the return value is any other values, it means that the semaphore detach failed.
  359. *
  360. * @warning This function can ONLY detach a static semaphore initialized by the rt_sem_init() function.
  361. * If the semaphore is created by the rt_sem_create() function, you MUST NOT USE this function to detach it,
  362. * ONLY USE the rt_sem_delete() function to complete the deletion.
  363. */
  364. rt_err_t rt_sem_detach(rt_sem_t sem)
  365. {
  366. rt_base_t level;
  367. /* parameter check */
  368. RT_ASSERT(sem != RT_NULL);
  369. RT_ASSERT(rt_object_get_type(&sem->parent.parent) == RT_Object_Class_Semaphore);
  370. RT_ASSERT(rt_object_is_systemobject(&sem->parent.parent));
  371. level = rt_spin_lock_irqsave(&(sem->spinlock));
  372. /* wakeup all suspended threads */
  373. rt_susp_list_resume_all(&(sem->parent.suspend_thread), RT_ERROR);
  374. rt_spin_unlock_irqrestore(&(sem->spinlock), level);
  375. /* detach semaphore object */
  376. rt_object_detach(&(sem->parent.parent));
  377. return RT_EOK;
  378. }
  379. RTM_EXPORT(rt_sem_detach);
  380. #ifdef RT_USING_HEAP
  381. /**
  382. * @brief Creating a semaphore object.
  383. *
  384. * @note For the semaphore object, its memory space is allocated automatically.
  385. * By contrast, the rt_sem_init() function will initialize a static semaphore object.
  386. *
  387. * @see rt_sem_init()
  388. *
  389. * @param name is a pointer to the name you would like to give the semaphore.
  390. *
  391. * @param value is the initial value for the semaphore.
  392. * If used to share resources, you should initialize the value as the number of available resources.
  393. * If used to signal the occurrence of an event, you should initialize the value as 0.
  394. *
  395. * @param flag is the semaphore flag, which determines the queuing way of how multiple threads wait
  396. * when the semaphore is not available.
  397. * The semaphore flag can be ONE of the following values:
  398. *
  399. * RT_IPC_FLAG_PRIO The pending threads will queue in order of priority.
  400. *
  401. * RT_IPC_FLAG_FIFO The pending threads will queue in the first-in-first-out method
  402. * (also known as first-come-first-served (FCFS) scheduling strategy).
  403. *
  404. * NOTE: RT_IPC_FLAG_FIFO is a non-real-time scheduling mode. It is strongly recommended to
  405. * use RT_IPC_FLAG_PRIO to ensure the thread is real-time UNLESS your applications concern about
  406. * the first-in-first-out principle, and you clearly understand that all threads involved in
  407. * this semaphore will become non-real-time threads.
  408. *
  409. * @return Return a pointer to the semaphore object. When the return value is RT_NULL, it means the creation failed.
  410. *
  411. * @warning This function can NOT be called in interrupt context. You can use macor RT_DEBUG_NOT_IN_INTERRUPT to check it.
  412. */
  413. rt_sem_t rt_sem_create(const char *name, rt_uint32_t value, rt_uint8_t flag)
  414. {
  415. rt_sem_t sem;
  416. RT_ASSERT(value < 0x10000U);
  417. RT_ASSERT((flag == RT_IPC_FLAG_FIFO) || (flag == RT_IPC_FLAG_PRIO));
  418. RT_DEBUG_NOT_IN_INTERRUPT;
  419. /* allocate object */
  420. sem = (rt_sem_t)rt_object_allocate(RT_Object_Class_Semaphore, name);
  421. if (sem == RT_NULL)
  422. return sem;
  423. _sem_object_init(sem, value, flag, RT_SEM_VALUE_MAX);
  424. return sem;
  425. }
  426. RTM_EXPORT(rt_sem_create);
  427. /**
  428. * @brief This function will delete a semaphore object and release the memory space.
  429. *
  430. * @note This function is used to delete a semaphore object which is created by the rt_sem_create() function.
  431. * By contrast, the rt_sem_detach() function will detach a static semaphore object.
  432. * When the semaphore is successfully deleted, it will resume all suspended threads in the semaphore list.
  433. *
  434. * @see rt_sem_detach()
  435. *
  436. * @param sem is a pointer to a semaphore object to be deleted.
  437. *
  438. * @return Return the operation status. When the return value is RT_EOK, the operation is successful.
  439. * If the return value is any other values, it means that the semaphore detach failed.
  440. *
  441. * @warning This function can ONLY delete a semaphore initialized by the rt_sem_create() function.
  442. * If the semaphore is initialized by the rt_sem_init() function, you MUST NOT USE this function to delete it,
  443. * ONLY USE the rt_sem_detach() function to complete the detachment.
  444. */
  445. rt_err_t rt_sem_delete(rt_sem_t sem)
  446. {
  447. rt_ubase_t level;
  448. /* parameter check */
  449. RT_ASSERT(sem != RT_NULL);
  450. RT_ASSERT(rt_object_get_type(&sem->parent.parent) == RT_Object_Class_Semaphore);
  451. RT_ASSERT(rt_object_is_systemobject(&sem->parent.parent) == RT_FALSE);
  452. RT_DEBUG_NOT_IN_INTERRUPT;
  453. level = rt_spin_lock_irqsave(&(sem->spinlock));
  454. /* wakeup all suspended threads */
  455. rt_susp_list_resume_all(&(sem->parent.suspend_thread), RT_ERROR);
  456. rt_spin_unlock_irqrestore(&(sem->spinlock), level);
  457. /* delete semaphore object */
  458. rt_object_delete(&(sem->parent.parent));
  459. return RT_EOK;
  460. }
  461. RTM_EXPORT(rt_sem_delete);
  462. #endif /* RT_USING_HEAP */
  463. /**
  464. * @brief This function will take a semaphore, if the semaphore is unavailable, the thread shall wait for
  465. * the semaphore up to a specified time.
  466. *
  467. * @note When this function is called, the count value of the sem->value will decrease 1 until it is equal to 0.
  468. * When the sem->value is 0, it means that the semaphore is unavailable. At this time, it will suspend the
  469. * thread preparing to take the semaphore.
  470. * On the contrary, the rt_sem_release() function will increase the count value of sem->value by 1 each time.
  471. *
  472. * @see rt_sem_trytake()
  473. *
  474. * @param sem is a pointer to a semaphore object.
  475. *
  476. * @param timeout is a timeout period (unit: an OS tick). If the semaphore is unavailable, the thread will wait for
  477. * the semaphore up to the amount of time specified by this parameter.
  478. *
  479. * NOTE:
  480. * If use Macro RT_WAITING_FOREVER to set this parameter, which means that when the
  481. * message is unavailable in the queue, the thread will be waiting forever.
  482. * If use macro RT_WAITING_NO to set this parameter, which means that this
  483. * function is non-blocking and will return immediately.
  484. *
  485. * @return Return the operation status. ONLY When the return value is RT_EOK, the operation is successful.
  486. * If the return value is any other values, it means that the semaphore take failed.
  487. *
  488. * @warning This function can ONLY be called in the thread context. It MUST NOT BE called in interrupt context.
  489. */
  490. static rt_err_t _rt_sem_take(rt_sem_t sem, rt_int32_t timeout, int suspend_flag)
  491. {
  492. rt_base_t level;
  493. struct rt_thread *thread;
  494. rt_err_t ret;
  495. /* parameter check */
  496. RT_ASSERT(sem != RT_NULL);
  497. RT_ASSERT(rt_object_get_type(&sem->parent.parent) == RT_Object_Class_Semaphore);
  498. RT_OBJECT_HOOK_CALL(rt_object_trytake_hook, (&(sem->parent.parent)));
  499. /* current context checking */
  500. RT_DEBUG_SCHEDULER_AVAILABLE(1);
  501. level = rt_spin_lock_irqsave(&(sem->spinlock));
  502. LOG_D("thread %s take sem:%s, which value is: %d",
  503. rt_thread_self()->parent.name,
  504. sem->parent.parent.name,
  505. sem->value);
  506. if (sem->value > 0)
  507. {
  508. /* semaphore is available */
  509. sem->value --;
  510. rt_spin_unlock_irqrestore(&(sem->spinlock), level);
  511. }
  512. else
  513. {
  514. /* no waiting, return with timeout */
  515. if (timeout == 0)
  516. {
  517. rt_spin_unlock_irqrestore(&(sem->spinlock), level);
  518. return -RT_ETIMEOUT;
  519. }
  520. else
  521. {
  522. /* semaphore is unavailable, push to suspend list */
  523. /* get current thread */
  524. thread = rt_thread_self();
  525. /* reset thread error number */
  526. thread->error = RT_EINTR;
  527. LOG_D("sem take: suspend thread - %s", thread->parent.name);
  528. /* suspend thread */
  529. ret = rt_thread_suspend_to_list(thread, &(sem->parent.suspend_thread),
  530. sem->parent.parent.flag, suspend_flag);
  531. if (ret != RT_EOK)
  532. {
  533. rt_spin_unlock_irqrestore(&(sem->spinlock), level);
  534. return ret;
  535. }
  536. /* has waiting time, start thread timer */
  537. if (timeout > 0)
  538. {
  539. LOG_D("set thread:%s to timer list", thread->parent.name);
  540. /* reset the timeout of thread timer and start it */
  541. rt_timer_control(&(thread->thread_timer),
  542. RT_TIMER_CTRL_SET_TIME,
  543. &timeout);
  544. rt_timer_start(&(thread->thread_timer));
  545. }
  546. /* enable interrupt */
  547. rt_spin_unlock_irqrestore(&(sem->spinlock), level);
  548. /* do schedule */
  549. rt_schedule();
  550. if (thread->error != RT_EOK)
  551. {
  552. return thread->error > 0 ? -thread->error : thread->error;
  553. }
  554. }
  555. }
  556. RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(sem->parent.parent)));
  557. return RT_EOK;
  558. }
  559. rt_err_t rt_sem_take(rt_sem_t sem, rt_int32_t time)
  560. {
  561. return _rt_sem_take(sem, time, RT_UNINTERRUPTIBLE);
  562. }
  563. RTM_EXPORT(rt_sem_take);
  564. rt_err_t rt_sem_take_interruptible(rt_sem_t sem, rt_int32_t time)
  565. {
  566. return _rt_sem_take(sem, time, RT_INTERRUPTIBLE);
  567. }
  568. RTM_EXPORT(rt_sem_take_interruptible);
  569. rt_err_t rt_sem_take_killable(rt_sem_t sem, rt_int32_t time)
  570. {
  571. return _rt_sem_take(sem, time, RT_KILLABLE);
  572. }
  573. RTM_EXPORT(rt_sem_take_killable);
  574. /**
  575. * @brief This function will try to take a semaphore, if the semaphore is unavailable, the thread returns immediately.
  576. *
  577. * @note This function is very similar to the rt_sem_take() function, when the semaphore is not available,
  578. * the rt_sem_trytake() function will return immediately without waiting for a timeout.
  579. * In other words, rt_sem_trytake(sem) has the same effect as rt_sem_take(sem, 0).
  580. *
  581. * @see rt_sem_take()
  582. *
  583. * @param sem is a pointer to a semaphore object.
  584. *
  585. * @return Return the operation status. ONLY When the return value is RT_EOK, the operation is successful.
  586. * If the return value is any other values, it means that the semaphore take failed.
  587. */
  588. rt_err_t rt_sem_trytake(rt_sem_t sem)
  589. {
  590. return rt_sem_take(sem, RT_WAITING_NO);
  591. }
  592. RTM_EXPORT(rt_sem_trytake);
  593. /**
  594. * @brief This function will release a semaphore. If there is thread suspended on the semaphore, it will get resumed.
  595. *
  596. * @note If there are threads suspended on this semaphore, the first thread in the list of this semaphore object
  597. * will be resumed, and a thread scheduling (rt_schedule) will be executed.
  598. * If no threads are suspended on this semaphore, the count value sem->value of this semaphore will increase by 1.
  599. *
  600. * @param sem is a pointer to a semaphore object.
  601. *
  602. * @return Return the operation status. When the return value is RT_EOK, the operation is successful.
  603. * If the return value is any other values, it means that the semaphore release failed.
  604. */
  605. rt_err_t rt_sem_release(rt_sem_t sem)
  606. {
  607. rt_base_t level;
  608. rt_bool_t need_schedule;
  609. /* parameter check */
  610. RT_ASSERT(sem != RT_NULL);
  611. RT_ASSERT(rt_object_get_type(&sem->parent.parent) == RT_Object_Class_Semaphore);
  612. RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(sem->parent.parent)));
  613. need_schedule = RT_FALSE;
  614. level = rt_spin_lock_irqsave(&(sem->spinlock));
  615. LOG_D("thread %s releases sem:%s, which value is: %d",
  616. rt_thread_self()->parent.name,
  617. sem->parent.parent.name,
  618. sem->value);
  619. if (!rt_list_isempty(&sem->parent.suspend_thread))
  620. {
  621. /* resume the suspended thread */
  622. rt_susp_list_dequeue(&(sem->parent.suspend_thread), RT_EOK);
  623. need_schedule = RT_TRUE;
  624. }
  625. else
  626. {
  627. if(sem->value < sem->max_value)
  628. {
  629. sem->value ++; /* increase value */
  630. }
  631. else
  632. {
  633. rt_spin_unlock_irqrestore(&(sem->spinlock), level);
  634. return -RT_EFULL; /* value overflowed */
  635. }
  636. }
  637. rt_spin_unlock_irqrestore(&(sem->spinlock), level);
  638. /* resume a thread, re-schedule */
  639. if (need_schedule == RT_TRUE)
  640. rt_schedule();
  641. return RT_EOK;
  642. }
  643. RTM_EXPORT(rt_sem_release);
  644. /**
  645. * @brief This function will set some extra attributions of a semaphore object.
  646. *
  647. * @note Currently this function only supports the RT_IPC_CMD_RESET command to reset the semaphore.
  648. *
  649. * @param sem is a pointer to a semaphore object.
  650. *
  651. * @param cmd is a command word used to configure some attributions of the semaphore.
  652. *
  653. * @param arg is the argument of the function to execute the command.
  654. *
  655. * @return Return the operation status. When the return value is RT_EOK, the operation is successful.
  656. * If the return value is any other values, it means that this function failed to execute.
  657. */
  658. rt_err_t rt_sem_control(rt_sem_t sem, int cmd, void *arg)
  659. {
  660. rt_base_t level;
  661. /* parameter check */
  662. RT_ASSERT(sem != RT_NULL);
  663. RT_ASSERT(rt_object_get_type(&sem->parent.parent) == RT_Object_Class_Semaphore);
  664. if (cmd == RT_IPC_CMD_RESET)
  665. {
  666. rt_ubase_t value;
  667. /* get value */
  668. value = (rt_ubase_t)arg;
  669. level = rt_spin_lock_irqsave(&(sem->spinlock));
  670. /* resume all waiting thread */
  671. rt_susp_list_resume_all(&sem->parent.suspend_thread, RT_ERROR);
  672. /* set new value */
  673. sem->value = (rt_uint16_t)value;
  674. rt_spin_unlock_irqrestore(&(sem->spinlock), level);
  675. rt_schedule();
  676. return RT_EOK;
  677. }
  678. else if (cmd == RT_IPC_CMD_SET_VLIMIT)
  679. {
  680. rt_ubase_t max_value;
  681. rt_bool_t need_schedule = RT_FALSE;
  682. max_value = (rt_uint16_t)((rt_ubase_t)arg);
  683. if (max_value > RT_SEM_VALUE_MAX || max_value < 1)
  684. {
  685. return -RT_EINVAL;
  686. }
  687. level = rt_spin_lock_irqsave(&(sem->spinlock));
  688. if (max_value < sem->value)
  689. {
  690. if (!rt_list_isempty(&sem->parent.suspend_thread))
  691. {
  692. /* resume all waiting thread */
  693. rt_susp_list_resume_all(&sem->parent.suspend_thread, RT_ERROR);
  694. need_schedule = RT_TRUE;
  695. }
  696. }
  697. /* set new value */
  698. sem->max_value = max_value;
  699. rt_spin_unlock_irqrestore(&(sem->spinlock), level);
  700. if (need_schedule)
  701. {
  702. rt_schedule();
  703. }
  704. return RT_EOK;
  705. }
  706. return -RT_ERROR;
  707. }
  708. RTM_EXPORT(rt_sem_control);
  709. /**@}*/
  710. #endif /* RT_USING_SEMAPHORE */
  711. #ifdef RT_USING_MUTEX
  712. /* iterate over each suspended thread to update highest priority in pending threads */
  713. rt_inline rt_uint8_t _mutex_update_priority(struct rt_mutex *mutex)
  714. {
  715. struct rt_thread *thread;
  716. if (!rt_list_isempty(&mutex->parent.suspend_thread))
  717. {
  718. thread = RT_THREAD_LIST_NODE_ENTRY(mutex->parent.suspend_thread.next);
  719. mutex->priority = rt_sched_thread_get_curr_prio(thread);
  720. }
  721. else
  722. {
  723. mutex->priority = 0xff;
  724. }
  725. return mutex->priority;
  726. }
  727. /* get highest priority inside its taken object and its init priority */
  728. rt_inline rt_uint8_t _thread_get_mutex_priority(struct rt_thread* thread)
  729. {
  730. rt_list_t *node = RT_NULL;
  731. struct rt_mutex *mutex = RT_NULL;
  732. rt_uint8_t priority = rt_sched_thread_get_init_prio(thread);
  733. rt_list_for_each(node, &(thread->taken_object_list))
  734. {
  735. mutex = rt_list_entry(node, struct rt_mutex, taken_list);
  736. rt_uint8_t mutex_prio = mutex->priority;
  737. /* prio at least be priority ceiling */
  738. mutex_prio = mutex_prio < mutex->ceiling_priority ? mutex_prio : mutex->ceiling_priority;
  739. if (priority > mutex_prio)
  740. {
  741. priority = mutex_prio;
  742. }
  743. }
  744. return priority;
  745. }
  746. /* update priority of target thread and the thread suspended it if any */
  747. rt_inline void _thread_update_priority(struct rt_thread *thread, rt_uint8_t priority, int suspend_flag)
  748. {
  749. rt_err_t ret = -RT_ERROR;
  750. struct rt_object* pending_obj = RT_NULL;
  751. LOG_D("thread:%s priority -> %d", thread->parent.name, priority);
  752. /* change priority of the thread */
  753. ret = rt_sched_thread_change_priority(thread, priority);
  754. while ((ret == RT_EOK) && rt_sched_thread_is_suspended(thread))
  755. {
  756. /* whether change the priority of taken mutex */
  757. pending_obj = thread->pending_object;
  758. if (pending_obj && rt_object_get_type(pending_obj) == RT_Object_Class_Mutex)
  759. {
  760. rt_uint8_t mutex_priority = 0xff;
  761. struct rt_mutex* pending_mutex = (struct rt_mutex *)pending_obj;
  762. /* re-insert thread to suspended thread list to resort priority list */
  763. rt_list_remove(&RT_THREAD_LIST_NODE(thread));
  764. ret = rt_susp_list_enqueue(
  765. &(pending_mutex->parent.suspend_thread), thread,
  766. pending_mutex->parent.parent.flag);
  767. if (ret == RT_EOK)
  768. {
  769. /* update priority */
  770. _mutex_update_priority(pending_mutex);
  771. /* change the priority of mutex owner thread */
  772. LOG_D("mutex: %s priority -> %d", pending_mutex->parent.parent.name,
  773. pending_mutex->priority);
  774. mutex_priority = _thread_get_mutex_priority(pending_mutex->owner);
  775. if (mutex_priority != rt_sched_thread_get_curr_prio(pending_mutex->owner))
  776. {
  777. thread = pending_mutex->owner;
  778. ret = rt_sched_thread_change_priority(thread, mutex_priority);
  779. }
  780. else
  781. {
  782. ret = -RT_ERROR;
  783. }
  784. }
  785. }
  786. else
  787. {
  788. ret = -RT_ERROR;
  789. }
  790. }
  791. }
  792. static rt_bool_t _check_and_update_prio(rt_thread_t thread, rt_mutex_t mutex)
  793. {
  794. RT_SCHED_DEBUG_IS_LOCKED;
  795. rt_bool_t do_sched = RT_FALSE;
  796. if ((mutex->ceiling_priority != 0xFF) || (rt_sched_thread_get_curr_prio(thread) == mutex->priority))
  797. {
  798. rt_uint8_t priority = 0xff;
  799. /* get the highest priority in the taken list of thread */
  800. priority = _thread_get_mutex_priority(thread);
  801. rt_sched_thread_change_priority(thread, priority);
  802. /**
  803. * notify a pending reschedule. Since scheduler is locked, we will not
  804. * really do a re-schedule at this point
  805. */
  806. do_sched = RT_TRUE;
  807. }
  808. return do_sched;
  809. }
  810. static void _mutex_before_delete_detach(rt_mutex_t mutex)
  811. {
  812. rt_sched_lock_level_t slvl;
  813. rt_bool_t need_schedule;
  814. rt_spin_lock(&(mutex->spinlock));
  815. /* wakeup all suspended threads */
  816. rt_susp_list_resume_all(&(mutex->parent.suspend_thread), RT_ERROR);
  817. /* remove mutex from thread's taken list */
  818. rt_list_remove(&mutex->taken_list);
  819. /* whether change the thread priority */
  820. if (mutex->owner)
  821. {
  822. rt_sched_lock(&slvl);
  823. need_schedule = _check_and_update_prio(mutex->owner, mutex);
  824. if (need_schedule)
  825. {
  826. rt_sched_unlock_n_resched(slvl);
  827. }
  828. else
  829. {
  830. rt_sched_unlock(slvl);
  831. }
  832. }
  833. /* unlock and do necessary reschedule if required */
  834. rt_spin_unlock(&(mutex->spinlock));
  835. }
  836. /**
  837. * @addtogroup mutex
  838. * @{
  839. */
  840. /**
  841. * @brief Initialize a static mutex object.
  842. *
  843. * @note For the static mutex object, its memory space is allocated by the compiler during compiling,
  844. * and shall placed on the read-write data segment or on the uninitialized data segment.
  845. * By contrast, the rt_mutex_create() function will automatically allocate memory space
  846. * and initialize the mutex.
  847. *
  848. * @see rt_mutex_create()
  849. *
  850. * @param mutex is a pointer to the mutex to initialize. It is assumed that storage for the mutex will be
  851. * allocated in your application.
  852. *
  853. * @param name is a pointer to the name that given to the mutex.
  854. *
  855. * @param flag is the mutex flag, which determines the queuing way of how multiple threads wait
  856. * when the mutex is not available.
  857. * NOTE: This parameter has been obsoleted. It can be RT_IPC_FLAG_PRIO, RT_IPC_FLAG_FIFO or RT_NULL.
  858. *
  859. * @return Return the operation status. When the return value is RT_EOK, the initialization is successful.
  860. * If the return value is any other values, it represents the initialization failed.
  861. *
  862. * @warning This function can ONLY be called from threads.
  863. */
  864. rt_err_t rt_mutex_init(rt_mutex_t mutex, const char *name, rt_uint8_t flag)
  865. {
  866. /* flag parameter has been obsoleted */
  867. RT_UNUSED(flag);
  868. /* parameter check */
  869. RT_ASSERT(mutex != RT_NULL);
  870. /* initialize object */
  871. rt_object_init(&(mutex->parent.parent), RT_Object_Class_Mutex, name);
  872. /* initialize ipc object */
  873. _ipc_object_init(&(mutex->parent));
  874. mutex->owner = RT_NULL;
  875. mutex->priority = 0xFF;
  876. mutex->hold = 0;
  877. mutex->ceiling_priority = 0xFF;
  878. rt_list_init(&(mutex->taken_list));
  879. /* flag can only be RT_IPC_FLAG_PRIO. RT_IPC_FLAG_FIFO cannot solve the unbounded priority inversion problem */
  880. mutex->parent.parent.flag = RT_IPC_FLAG_PRIO;
  881. rt_spin_lock_init(&(mutex->spinlock));
  882. return RT_EOK;
  883. }
  884. RTM_EXPORT(rt_mutex_init);
  885. /**
  886. * @brief This function will detach a static mutex object.
  887. *
  888. * @note This function is used to detach a static mutex object which is initialized by rt_mutex_init() function.
  889. * By contrast, the rt_mutex_delete() function will delete a mutex object.
  890. * When the mutex is successfully detached, it will resume all suspended threads in the mutex list.
  891. *
  892. * @see rt_mutex_delete()
  893. *
  894. * @param mutex is a pointer to a mutex object to be detached.
  895. *
  896. * @return Return the operation status. When the return value is RT_EOK, the initialization is successful.
  897. * If the return value is any other values, it means that the mutex detach failed.
  898. *
  899. * @warning This function can ONLY detach a static mutex initialized by the rt_mutex_init() function.
  900. * If the mutex is created by the rt_mutex_create() function, you MUST NOT USE this function to detach it,
  901. * ONLY USE the rt_mutex_delete() function to complete the deletion.
  902. */
  903. rt_err_t rt_mutex_detach(rt_mutex_t mutex)
  904. {
  905. /* parameter check */
  906. RT_ASSERT(mutex != RT_NULL);
  907. RT_ASSERT(rt_object_get_type(&mutex->parent.parent) == RT_Object_Class_Mutex);
  908. RT_ASSERT(rt_object_is_systemobject(&mutex->parent.parent));
  909. _mutex_before_delete_detach(mutex);
  910. /* detach mutex object */
  911. rt_object_detach(&(mutex->parent.parent));
  912. return RT_EOK;
  913. }
  914. RTM_EXPORT(rt_mutex_detach);
  915. /* drop a thread from the suspend list of mutex */
  916. /**
  917. * @brief drop a thread from the suspend list of mutex
  918. *
  919. * @param mutex is a pointer to a mutex object.
  920. * @param thread is the thread should be dropped from mutex.
  921. */
  922. void rt_mutex_drop_thread(rt_mutex_t mutex, rt_thread_t thread)
  923. {
  924. rt_uint8_t priority;
  925. rt_bool_t need_update = RT_FALSE;
  926. rt_sched_lock_level_t slvl;
  927. /* parameter check */
  928. RT_DEBUG_IN_THREAD_CONTEXT;
  929. RT_ASSERT(mutex != RT_NULL);
  930. RT_ASSERT(thread != RT_NULL);
  931. rt_spin_lock(&(mutex->spinlock));
  932. RT_ASSERT(thread->pending_object == &mutex->parent.parent);
  933. rt_sched_lock(&slvl);
  934. /* detach from suspended list */
  935. rt_list_remove(&RT_THREAD_LIST_NODE(thread));
  936. /**
  937. * Should change the priority of mutex owner thread
  938. * Note: After current thread is detached from mutex pending list, there is
  939. * a chance that the mutex owner has been released the mutex. Which
  940. * means mutex->owner can be NULL at this point. If that happened,
  941. * it had already reset its priority. So it's okay to skip
  942. */
  943. if (mutex->owner && rt_sched_thread_get_curr_prio(mutex->owner) ==
  944. rt_sched_thread_get_curr_prio(thread))
  945. {
  946. need_update = RT_TRUE;
  947. }
  948. /* update the priority of mutex */
  949. if (!rt_list_isempty(&mutex->parent.suspend_thread))
  950. {
  951. /* more thread suspended in the list */
  952. struct rt_thread *th;
  953. th = RT_THREAD_LIST_NODE_ENTRY(mutex->parent.suspend_thread.next);
  954. /* update the priority of mutex */
  955. mutex->priority = rt_sched_thread_get_curr_prio(th);
  956. }
  957. else
  958. {
  959. /* set mutex priority to maximal priority */
  960. mutex->priority = 0xff;
  961. }
  962. /* try to change the priority of mutex owner thread */
  963. if (need_update)
  964. {
  965. /* get the maximal priority of mutex in thread */
  966. priority = _thread_get_mutex_priority(mutex->owner);
  967. if (priority != rt_sched_thread_get_curr_prio(mutex->owner))
  968. {
  969. _thread_update_priority(mutex->owner, priority, RT_UNINTERRUPTIBLE);
  970. }
  971. }
  972. rt_sched_unlock(slvl);
  973. rt_spin_unlock(&(mutex->spinlock));
  974. }
  975. /**
  976. * @brief set the prioceiling attribute of the mutex.
  977. *
  978. * @param mutex is a pointer to a mutex object.
  979. * @param priority is the priority should be set to mutex.
  980. *
  981. * @return return the old priority ceiling
  982. */
  983. rt_uint8_t rt_mutex_setprioceiling(rt_mutex_t mutex, rt_uint8_t priority)
  984. {
  985. rt_uint8_t ret_priority = 0xFF;
  986. rt_uint8_t highest_prio;
  987. rt_sched_lock_level_t slvl;
  988. RT_DEBUG_IN_THREAD_CONTEXT;
  989. if ((mutex) && (priority < RT_THREAD_PRIORITY_MAX))
  990. {
  991. /* critical section here if multiple updates to one mutex happen */
  992. rt_spin_lock(&(mutex->spinlock));
  993. ret_priority = mutex->ceiling_priority;
  994. mutex->ceiling_priority = priority;
  995. if (mutex->owner)
  996. {
  997. rt_sched_lock(&slvl);
  998. highest_prio = _thread_get_mutex_priority(mutex->owner);
  999. if (highest_prio != rt_sched_thread_get_curr_prio(mutex->owner))
  1000. {
  1001. _thread_update_priority(mutex->owner, highest_prio, RT_UNINTERRUPTIBLE);
  1002. }
  1003. rt_sched_unlock(slvl);
  1004. }
  1005. rt_spin_unlock(&(mutex->spinlock));
  1006. }
  1007. else
  1008. {
  1009. rt_set_errno(-RT_EINVAL);
  1010. }
  1011. return ret_priority;
  1012. }
  1013. RTM_EXPORT(rt_mutex_setprioceiling);
  1014. /**
  1015. * @brief set the prioceiling attribute of the mutex.
  1016. *
  1017. * @param mutex is a pointer to a mutex object.
  1018. *
  1019. * @return return the current priority ceiling of the mutex.
  1020. */
  1021. rt_uint8_t rt_mutex_getprioceiling(rt_mutex_t mutex)
  1022. {
  1023. rt_uint8_t prio = 0xFF;
  1024. /* parameter check */
  1025. RT_DEBUG_IN_THREAD_CONTEXT;
  1026. RT_ASSERT(mutex != RT_NULL);
  1027. if (mutex)
  1028. {
  1029. rt_spin_lock(&(mutex->spinlock));
  1030. prio = mutex->ceiling_priority;
  1031. rt_spin_unlock(&(mutex->spinlock));
  1032. }
  1033. return prio;
  1034. }
  1035. RTM_EXPORT(rt_mutex_getprioceiling);
  1036. #ifdef RT_USING_HEAP
  1037. /**
  1038. * @brief This function will create a mutex object.
  1039. *
  1040. * @note For the mutex object, its memory space is automatically allocated.
  1041. * By contrast, the rt_mutex_init() function will initialize a static mutex object.
  1042. *
  1043. * @see rt_mutex_init()
  1044. *
  1045. * @param name is a pointer to the name that given to the mutex.
  1046. *
  1047. * @param flag is the mutex flag, which determines the queuing way of how multiple threads wait
  1048. * when the mutex is not available.
  1049. * NOTE: This parameter has been obsoleted. It can be RT_IPC_FLAG_PRIO, RT_IPC_FLAG_FIFO or RT_NULL.
  1050. *
  1051. * @return Return a pointer to the mutex object. When the return value is RT_NULL, it means the creation failed.
  1052. *
  1053. * @warning This function can ONLY be called from threads.
  1054. */
  1055. rt_mutex_t rt_mutex_create(const char *name, rt_uint8_t flag)
  1056. {
  1057. struct rt_mutex *mutex;
  1058. /* flag parameter has been obsoleted */
  1059. RT_UNUSED(flag);
  1060. RT_DEBUG_NOT_IN_INTERRUPT;
  1061. /* allocate object */
  1062. mutex = (rt_mutex_t)rt_object_allocate(RT_Object_Class_Mutex, name);
  1063. if (mutex == RT_NULL)
  1064. return mutex;
  1065. /* initialize ipc object */
  1066. _ipc_object_init(&(mutex->parent));
  1067. mutex->owner = RT_NULL;
  1068. mutex->priority = 0xFF;
  1069. mutex->hold = 0;
  1070. mutex->ceiling_priority = 0xFF;
  1071. rt_list_init(&(mutex->taken_list));
  1072. /* flag can only be RT_IPC_FLAG_PRIO. RT_IPC_FLAG_FIFO cannot solve the unbounded priority inversion problem */
  1073. mutex->parent.parent.flag = RT_IPC_FLAG_PRIO;
  1074. rt_spin_lock_init(&(mutex->spinlock));
  1075. return mutex;
  1076. }
  1077. RTM_EXPORT(rt_mutex_create);
  1078. /**
  1079. * @brief This function will delete a mutex object and release this memory space.
  1080. *
  1081. * @note This function is used to delete a mutex object which is created by the rt_mutex_create() function.
  1082. * By contrast, the rt_mutex_detach() function will detach a static mutex object.
  1083. * When the mutex is successfully deleted, it will resume all suspended threads in the mutex list.
  1084. *
  1085. * @see rt_mutex_detach()
  1086. *
  1087. * @param mutex is a pointer to a mutex object to be deleted.
  1088. *
  1089. * @return Return the operation status. When the return value is RT_EOK, the operation is successful.
  1090. * If the return value is any other values, it means that the mutex detach failed.
  1091. *
  1092. * @warning This function can ONLY delete a mutex initialized by the rt_mutex_create() function.
  1093. * If the mutex is initialized by the rt_mutex_init() function, you MUST NOT USE this function to delete it,
  1094. * ONLY USE the rt_mutex_detach() function to complete the detachment.
  1095. */
  1096. rt_err_t rt_mutex_delete(rt_mutex_t mutex)
  1097. {
  1098. /* parameter check */
  1099. RT_ASSERT(mutex != RT_NULL);
  1100. RT_ASSERT(rt_object_get_type(&mutex->parent.parent) == RT_Object_Class_Mutex);
  1101. RT_ASSERT(rt_object_is_systemobject(&mutex->parent.parent) == RT_FALSE);
  1102. RT_DEBUG_NOT_IN_INTERRUPT;
  1103. _mutex_before_delete_detach(mutex);
  1104. /* delete mutex object */
  1105. rt_object_delete(&(mutex->parent.parent));
  1106. return RT_EOK;
  1107. }
  1108. RTM_EXPORT(rt_mutex_delete);
  1109. #endif /* RT_USING_HEAP */
  1110. /**
  1111. * @brief This function will take a mutex, if the mutex is unavailable, the thread shall wait for
  1112. * the mutex up to a specified time.
  1113. *
  1114. * @note When this function is called, the count value of the mutex->value will decrease 1 until it is equal to 0.
  1115. * When the mutex->value is 0, it means that the mutex is unavailable. At this time, it will suspend the
  1116. * thread preparing to take the mutex.
  1117. * On the contrary, the rt_mutex_release() function will increase the count value of mutex->value by 1 each time.
  1118. *
  1119. * @see rt_mutex_trytake()
  1120. *
  1121. * @param mutex is a pointer to a mutex object.
  1122. *
  1123. * @param timeout is a timeout period (unit: an OS tick). If the mutex is unavailable, the thread will wait for
  1124. * the mutex up to the amount of time specified by the argument.
  1125. * NOTE: Generally, we set this parameter to RT_WAITING_FOREVER, which means that when the mutex is unavailable,
  1126. * the thread will be waitting forever.
  1127. *
  1128. * @return Return the operation status. ONLY When the return value is RT_EOK, the operation is successful.
  1129. * If the return value is any other values, it means that the mutex take failed.
  1130. *
  1131. * @warning This function can ONLY be called in the thread context. It MUST NOT BE called in interrupt context.
  1132. */
  1133. static rt_err_t _rt_mutex_take(rt_mutex_t mutex, rt_int32_t timeout, int suspend_flag)
  1134. {
  1135. struct rt_thread *thread;
  1136. rt_err_t ret;
  1137. /* this function must not be used in interrupt even if time = 0 */
  1138. /* current context checking */
  1139. //RT_DEBUG_SCHEDULER_AVAILABLE(RT_TRUE);
  1140. /* parameter check */
  1141. RT_ASSERT(mutex != RT_NULL);
  1142. RT_ASSERT(rt_object_get_type(&mutex->parent.parent) == RT_Object_Class_Mutex);
  1143. /* get current thread */
  1144. thread = rt_thread_self();
  1145. rt_spin_lock(&(mutex->spinlock));
  1146. RT_OBJECT_HOOK_CALL(rt_object_trytake_hook, (&(mutex->parent.parent)));
  1147. LOG_D("mutex_take: current thread %s, hold: %d",
  1148. thread->parent.name, mutex->hold);
  1149. /* reset thread error */
  1150. thread->error = RT_EOK;
  1151. if (mutex->owner == thread)
  1152. {
  1153. if(mutex->hold < RT_MUTEX_HOLD_MAX)
  1154. {
  1155. /* it's the same thread */
  1156. mutex->hold ++;
  1157. }
  1158. else
  1159. {
  1160. rt_spin_unlock(&(mutex->spinlock));
  1161. return -RT_EFULL; /* value overflowed */
  1162. }
  1163. }
  1164. else
  1165. {
  1166. /* whether the mutex has owner thread. */
  1167. if (mutex->owner == RT_NULL)
  1168. {
  1169. /* set mutex owner and original priority */
  1170. mutex->owner = thread;
  1171. mutex->priority = 0xff;
  1172. mutex->hold = 1;
  1173. if (mutex->ceiling_priority != 0xFF)
  1174. {
  1175. /* set the priority of thread to the ceiling priority */
  1176. if (mutex->ceiling_priority < rt_sched_thread_get_curr_prio(mutex->owner))
  1177. _thread_update_priority(mutex->owner, mutex->ceiling_priority, suspend_flag);
  1178. }
  1179. /* insert mutex to thread's taken object list */
  1180. rt_list_insert_after(&thread->taken_object_list, &mutex->taken_list);
  1181. }
  1182. else
  1183. {
  1184. /* no waiting, return with timeout */
  1185. if (timeout == 0)
  1186. {
  1187. /* set error as timeout */
  1188. thread->error = RT_ETIMEOUT;
  1189. rt_spin_unlock(&(mutex->spinlock));
  1190. return -RT_ETIMEOUT;
  1191. }
  1192. else
  1193. {
  1194. rt_sched_lock_level_t slvl;
  1195. rt_uint8_t priority;
  1196. /* mutex is unavailable, push to suspend list */
  1197. LOG_D("mutex_take: suspend thread: %s",
  1198. thread->parent.name);
  1199. /* suspend current thread */
  1200. ret = rt_thread_suspend_to_list(thread, &(mutex->parent.suspend_thread),
  1201. mutex->parent.parent.flag, suspend_flag);
  1202. if (ret != RT_EOK)
  1203. {
  1204. rt_spin_unlock(&(mutex->spinlock));
  1205. return ret;
  1206. }
  1207. /* set pending object in thread to this mutex */
  1208. thread->pending_object = &(mutex->parent.parent);
  1209. rt_sched_lock(&slvl);
  1210. priority = rt_sched_thread_get_curr_prio(thread);
  1211. /* update the priority level of mutex */
  1212. if (priority < mutex->priority)
  1213. {
  1214. mutex->priority = priority;
  1215. if (mutex->priority < rt_sched_thread_get_curr_prio(mutex->owner))
  1216. {
  1217. _thread_update_priority(mutex->owner, priority, RT_UNINTERRUPTIBLE); /* TODO */
  1218. }
  1219. }
  1220. rt_sched_unlock(slvl);
  1221. /* has waiting time, start thread timer */
  1222. if (timeout > 0)
  1223. {
  1224. LOG_D("mutex_take: start the timer of thread:%s",
  1225. thread->parent.name);
  1226. /* reset the timeout of thread timer and start it */
  1227. rt_timer_control(&(thread->thread_timer),
  1228. RT_TIMER_CTRL_SET_TIME,
  1229. &timeout);
  1230. rt_timer_start(&(thread->thread_timer));
  1231. }
  1232. rt_spin_unlock(&(mutex->spinlock));
  1233. /* do schedule */
  1234. rt_schedule();
  1235. rt_spin_lock(&(mutex->spinlock));
  1236. if (thread->error == RT_EOK)
  1237. {
  1238. /**
  1239. * get mutex successfully
  1240. * Note: assert to avoid an unexpected resume
  1241. */
  1242. RT_ASSERT(mutex->owner == thread);
  1243. }
  1244. else
  1245. {
  1246. /* the mutex has not been taken and thread has detach from the pending list. */
  1247. rt_bool_t need_update = RT_FALSE;
  1248. RT_ASSERT(mutex->owner != thread);
  1249. /* get value first before calling to other APIs */
  1250. ret = thread->error;
  1251. rt_sched_lock(&slvl);
  1252. /**
  1253. * Should change the priority of mutex owner thread
  1254. * Note: After current thread is detached from mutex pending list, there is
  1255. * a chance that the mutex owner has been released the mutex. Which
  1256. * means mutex->owner can be NULL at this point. If that happened,
  1257. * it had already reset its priority. So it's okay to skip
  1258. */
  1259. if (mutex->owner && rt_sched_thread_get_curr_prio(mutex->owner) == rt_sched_thread_get_curr_prio(thread))
  1260. need_update = RT_TRUE;
  1261. /* update the priority of mutex */
  1262. if (!rt_list_isempty(&mutex->parent.suspend_thread))
  1263. {
  1264. /* more thread suspended in the list */
  1265. struct rt_thread *th;
  1266. th = RT_THREAD_LIST_NODE_ENTRY(mutex->parent.suspend_thread.next);
  1267. /* update the priority of mutex */
  1268. mutex->priority = rt_sched_thread_get_curr_prio(th);
  1269. }
  1270. else
  1271. {
  1272. /* set mutex priority to maximal priority */
  1273. mutex->priority = 0xff;
  1274. }
  1275. /* try to change the priority of mutex owner thread */
  1276. if (need_update)
  1277. {
  1278. /* get the maximal priority of mutex in thread */
  1279. priority = _thread_get_mutex_priority(mutex->owner);
  1280. if (priority != rt_sched_thread_get_curr_prio(mutex->owner))
  1281. {
  1282. _thread_update_priority(mutex->owner, priority, RT_UNINTERRUPTIBLE);
  1283. }
  1284. }
  1285. rt_sched_unlock(slvl);
  1286. rt_spin_unlock(&(mutex->spinlock));
  1287. /* clear pending object before exit */
  1288. thread->pending_object = RT_NULL;
  1289. /* fix thread error number to negative value and return */
  1290. return ret > 0 ? -ret : ret;
  1291. }
  1292. }
  1293. }
  1294. }
  1295. rt_spin_unlock(&(mutex->spinlock));
  1296. RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(mutex->parent.parent)));
  1297. return RT_EOK;
  1298. }
  1299. rt_err_t rt_mutex_take(rt_mutex_t mutex, rt_int32_t time)
  1300. {
  1301. return _rt_mutex_take(mutex, time, RT_UNINTERRUPTIBLE);
  1302. }
  1303. RTM_EXPORT(rt_mutex_take);
  1304. rt_err_t rt_mutex_take_interruptible(rt_mutex_t mutex, rt_int32_t time)
  1305. {
  1306. return _rt_mutex_take(mutex, time, RT_INTERRUPTIBLE);
  1307. }
  1308. RTM_EXPORT(rt_mutex_take_interruptible);
  1309. rt_err_t rt_mutex_take_killable(rt_mutex_t mutex, rt_int32_t time)
  1310. {
  1311. return _rt_mutex_take(mutex, time, RT_KILLABLE);
  1312. }
  1313. RTM_EXPORT(rt_mutex_take_killable);
  1314. /**
  1315. * @brief This function will try to take a mutex, if the mutex is unavailable, the thread returns immediately.
  1316. *
  1317. * @note This function is very similar to the rt_mutex_take() function, when the mutex is not available,
  1318. * except that rt_mutex_trytake() will return immediately without waiting for a timeout
  1319. * when the mutex is not available.
  1320. * In other words, rt_mutex_trytake(mutex) has the same effect as rt_mutex_take(mutex, 0).
  1321. *
  1322. * @see rt_mutex_take()
  1323. *
  1324. * @param mutex is a pointer to a mutex object.
  1325. *
  1326. * @return Return the operation status. ONLY When the return value is RT_EOK, the operation is successful.
  1327. * If the return value is any other values, it means that the mutex take failed.
  1328. */
  1329. rt_err_t rt_mutex_trytake(rt_mutex_t mutex)
  1330. {
  1331. return rt_mutex_take(mutex, RT_WAITING_NO);
  1332. }
  1333. RTM_EXPORT(rt_mutex_trytake);
  1334. /**
  1335. * @brief This function will release a mutex. If there is thread suspended on the mutex, the thread will be resumed.
  1336. *
  1337. * @note If there are threads suspended on this mutex, the first thread in the list of this mutex object
  1338. * will be resumed, and a thread scheduling (rt_schedule) will be executed.
  1339. * If no threads are suspended on this mutex, the count value mutex->value of this mutex will increase by 1.
  1340. *
  1341. * @param mutex is a pointer to a mutex object.
  1342. *
  1343. * @return Return the operation status. When the return value is RT_EOK, the operation is successful.
  1344. * If the return value is any other values, it means that the mutex release failed.
  1345. */
  1346. rt_err_t rt_mutex_release(rt_mutex_t mutex)
  1347. {
  1348. rt_sched_lock_level_t slvl;
  1349. struct rt_thread *thread;
  1350. rt_bool_t need_schedule;
  1351. /* parameter check */
  1352. RT_ASSERT(mutex != RT_NULL);
  1353. RT_ASSERT(rt_object_get_type(&mutex->parent.parent) == RT_Object_Class_Mutex);
  1354. need_schedule = RT_FALSE;
  1355. /* only thread could release mutex because we need test the ownership */
  1356. RT_DEBUG_IN_THREAD_CONTEXT;
  1357. /* get current thread */
  1358. thread = rt_thread_self();
  1359. rt_spin_lock(&(mutex->spinlock));
  1360. LOG_D("mutex_release:current thread %s, hold: %d",
  1361. thread->parent.name, mutex->hold);
  1362. RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(mutex->parent.parent)));
  1363. /* mutex only can be released by owner */
  1364. if (thread != mutex->owner)
  1365. {
  1366. thread->error = -RT_ERROR;
  1367. rt_spin_unlock(&(mutex->spinlock));
  1368. return -RT_ERROR;
  1369. }
  1370. /* decrease hold */
  1371. mutex->hold --;
  1372. /* if no hold */
  1373. if (mutex->hold == 0)
  1374. {
  1375. /* remove mutex from thread's taken list */
  1376. rt_list_remove(&mutex->taken_list);
  1377. rt_sched_lock(&slvl);
  1378. /* whether change the thread priority */
  1379. need_schedule = _check_and_update_prio(thread, mutex);
  1380. /* wakeup suspended thread */
  1381. if (!rt_list_isempty(&mutex->parent.suspend_thread))
  1382. {
  1383. struct rt_thread *next_thread;
  1384. do
  1385. {
  1386. /* get the first suspended thread */
  1387. next_thread = RT_THREAD_LIST_NODE_ENTRY(mutex->parent.suspend_thread.next);
  1388. RT_ASSERT(rt_sched_thread_is_suspended(next_thread));
  1389. /* remove the thread from the suspended list of mutex */
  1390. rt_list_remove(&RT_THREAD_LIST_NODE(next_thread));
  1391. /* resume thread to ready queue */
  1392. if (rt_sched_thread_ready(next_thread) != RT_EOK)
  1393. {
  1394. /**
  1395. * a timeout timer had triggered while we try. So we skip
  1396. * this thread and try again.
  1397. */
  1398. next_thread = RT_NULL;
  1399. }
  1400. } while (!next_thread && !rt_list_isempty(&mutex->parent.suspend_thread));
  1401. if (next_thread)
  1402. {
  1403. LOG_D("mutex_release: resume thread: %s",
  1404. next_thread->parent.name);
  1405. /* set new owner and put mutex into taken list of thread */
  1406. mutex->owner = next_thread;
  1407. mutex->hold = 1;
  1408. rt_list_insert_after(&next_thread->taken_object_list, &mutex->taken_list);
  1409. /* cleanup pending object */
  1410. next_thread->pending_object = RT_NULL;
  1411. /* update mutex priority */
  1412. if (!rt_list_isempty(&(mutex->parent.suspend_thread)))
  1413. {
  1414. struct rt_thread *th;
  1415. th = RT_THREAD_LIST_NODE_ENTRY(mutex->parent.suspend_thread.next);
  1416. mutex->priority = rt_sched_thread_get_curr_prio(th);
  1417. }
  1418. else
  1419. {
  1420. mutex->priority = 0xff;
  1421. }
  1422. need_schedule = RT_TRUE;
  1423. }
  1424. else
  1425. {
  1426. /* no waiting thread is woke up, clear owner */
  1427. mutex->owner = RT_NULL;
  1428. mutex->priority = 0xff;
  1429. }
  1430. rt_sched_unlock(slvl);
  1431. }
  1432. else
  1433. {
  1434. rt_sched_unlock(slvl);
  1435. /* clear owner */
  1436. mutex->owner = RT_NULL;
  1437. mutex->priority = 0xff;
  1438. }
  1439. }
  1440. rt_spin_unlock(&(mutex->spinlock));
  1441. /* perform a schedule */
  1442. if (need_schedule == RT_TRUE)
  1443. rt_schedule();
  1444. return RT_EOK;
  1445. }
  1446. RTM_EXPORT(rt_mutex_release);
  1447. /**
  1448. * @brief This function will set some extra attributions of a mutex object.
  1449. *
  1450. * @note Currently this function does not implement the control function.
  1451. *
  1452. * @param mutex is a pointer to a mutex object.
  1453. *
  1454. * @param cmd is a command word used to configure some attributions of the mutex.
  1455. *
  1456. * @param arg is the argument of the function to execute the command.
  1457. *
  1458. * @return Return the operation status. When the return value is RT_EOK, the operation is successful.
  1459. * If the return value is any other values, it means that this function failed to execute.
  1460. */
  1461. rt_err_t rt_mutex_control(rt_mutex_t mutex, int cmd, void *arg)
  1462. {
  1463. RT_UNUSED(mutex);
  1464. RT_UNUSED(cmd);
  1465. RT_UNUSED(arg);
  1466. return -RT_EINVAL;
  1467. }
  1468. RTM_EXPORT(rt_mutex_control);
  1469. /**@}*/
  1470. #endif /* RT_USING_MUTEX */
  1471. #ifdef RT_USING_EVENT
  1472. /**
  1473. * @addtogroup event
  1474. * @{
  1475. */
  1476. /**
  1477. * @brief The function will initialize a static event object.
  1478. *
  1479. * @note For the static event object, its memory space is allocated by the compiler during compiling,
  1480. * and shall placed on the read-write data segment or on the uninitialized data segment.
  1481. * By contrast, the rt_event_create() function will allocate memory space automatically
  1482. * and initialize the event.
  1483. *
  1484. * @see rt_event_create()
  1485. *
  1486. * @param event is a pointer to the event to initialize. It is assumed that storage for the event
  1487. * will be allocated in your application.
  1488. *
  1489. * @param name is a pointer to the name that given to the event.
  1490. *
  1491. * @param flag is the event flag, which determines the queuing way of how multiple threads wait
  1492. * when the event is not available.
  1493. * The event flag can be ONE of the following values:
  1494. *
  1495. * RT_IPC_FLAG_PRIO The pending threads will queue in order of priority.
  1496. *
  1497. * RT_IPC_FLAG_FIFO The pending threads will queue in the first-in-first-out method
  1498. * (also known as first-come-first-served (FCFS) scheduling strategy).
  1499. *
  1500. * NOTE: RT_IPC_FLAG_FIFO is a non-real-time scheduling mode. It is strongly recommended to
  1501. * use RT_IPC_FLAG_PRIO to ensure the thread is real-time UNLESS your applications concern about
  1502. * the first-in-first-out principle, and you clearly understand that all threads involved in
  1503. * this event will become non-real-time threads.
  1504. *
  1505. * @return Return the operation status. When the return value is RT_EOK, the initialization is successful.
  1506. * If the return value is any other values, it represents the initialization failed.
  1507. *
  1508. * @warning This function can ONLY be called from threads.
  1509. */
  1510. rt_err_t rt_event_init(rt_event_t event, const char *name, rt_uint8_t flag)
  1511. {
  1512. /* parameter check */
  1513. RT_ASSERT(event != RT_NULL);
  1514. RT_ASSERT((flag == RT_IPC_FLAG_FIFO) || (flag == RT_IPC_FLAG_PRIO));
  1515. /* initialize object */
  1516. rt_object_init(&(event->parent.parent), RT_Object_Class_Event, name);
  1517. /* set parent flag */
  1518. event->parent.parent.flag = flag;
  1519. /* initialize ipc object */
  1520. _ipc_object_init(&(event->parent));
  1521. /* initialize event */
  1522. event->set = 0;
  1523. rt_spin_lock_init(&(event->spinlock));
  1524. return RT_EOK;
  1525. }
  1526. RTM_EXPORT(rt_event_init);
  1527. /**
  1528. * @brief This function will detach a static event object.
  1529. *
  1530. * @note This function is used to detach a static event object which is initialized by rt_event_init() function.
  1531. * By contrast, the rt_event_delete() function will delete an event object.
  1532. * When the event is successfully detached, it will resume all suspended threads in the event list.
  1533. *
  1534. * @see rt_event_delete()
  1535. *
  1536. * @param event is a pointer to an event object to be detached.
  1537. *
  1538. * @return Return the operation status. When the return value is RT_EOK, the initialization is successful.
  1539. * If the return value is any other values, it means that the event detach failed.
  1540. *
  1541. * @warning This function can ONLY detach a static event initialized by the rt_event_init() function.
  1542. * If the event is created by the rt_event_create() function, you MUST NOT USE this function to detach it,
  1543. * ONLY USE the rt_event_delete() function to complete the deletion.
  1544. */
  1545. rt_err_t rt_event_detach(rt_event_t event)
  1546. {
  1547. rt_base_t level;
  1548. /* parameter check */
  1549. RT_ASSERT(event != RT_NULL);
  1550. RT_ASSERT(rt_object_get_type(&event->parent.parent) == RT_Object_Class_Event);
  1551. RT_ASSERT(rt_object_is_systemobject(&event->parent.parent));
  1552. level = rt_spin_lock_irqsave(&(event->spinlock));
  1553. /* resume all suspended thread */
  1554. rt_susp_list_resume_all(&(event->parent.suspend_thread), RT_ERROR);
  1555. rt_spin_unlock_irqrestore(&(event->spinlock), level);
  1556. /* detach event object */
  1557. rt_object_detach(&(event->parent.parent));
  1558. return RT_EOK;
  1559. }
  1560. RTM_EXPORT(rt_event_detach);
  1561. #ifdef RT_USING_HEAP
  1562. /**
  1563. * @brief Creating an event object.
  1564. *
  1565. * @note For the event object, its memory space is allocated automatically.
  1566. * By contrast, the rt_event_init() function will initialize a static event object.
  1567. *
  1568. * @see rt_event_init()
  1569. *
  1570. * @param name is a pointer to the name that given to the event.
  1571. *
  1572. * @param flag is the event flag, which determines the queuing way of how multiple threads wait when the event
  1573. * is not available.
  1574. * The event flag can be ONE of the following values:
  1575. *
  1576. * RT_IPC_FLAG_PRIO The pending threads will queue in order of priority.
  1577. *
  1578. * RT_IPC_FLAG_FIFO The pending threads will queue in the first-in-first-out method
  1579. * (also known as first-come-first-served (FCFS) scheduling strategy).
  1580. *
  1581. * NOTE: RT_IPC_FLAG_FIFO is a non-real-time scheduling mode. It is strongly recommended to
  1582. * use RT_IPC_FLAG_PRIO to ensure the thread is real-time UNLESS your applications concern about
  1583. * the first-in-first-out principle, and you clearly understand that all threads involved in
  1584. * this event will become non-real-time threads.
  1585. *
  1586. * @return Return a pointer to the event object. When the return value is RT_NULL, it means the creation failed.
  1587. *
  1588. * @warning This function can ONLY be called from threads.
  1589. */
  1590. rt_event_t rt_event_create(const char *name, rt_uint8_t flag)
  1591. {
  1592. rt_event_t event;
  1593. RT_ASSERT((flag == RT_IPC_FLAG_FIFO) || (flag == RT_IPC_FLAG_PRIO));
  1594. RT_DEBUG_NOT_IN_INTERRUPT;
  1595. /* allocate object */
  1596. event = (rt_event_t)rt_object_allocate(RT_Object_Class_Event, name);
  1597. if (event == RT_NULL)
  1598. return event;
  1599. /* set parent */
  1600. event->parent.parent.flag = flag;
  1601. /* initialize ipc object */
  1602. _ipc_object_init(&(event->parent));
  1603. /* initialize event */
  1604. event->set = 0;
  1605. rt_spin_lock_init(&(event->spinlock));
  1606. return event;
  1607. }
  1608. RTM_EXPORT(rt_event_create);
  1609. /**
  1610. * @brief This function will delete an event object and release the memory space.
  1611. *
  1612. * @note This function is used to delete an event object which is created by the rt_event_create() function.
  1613. * By contrast, the rt_event_detach() function will detach a static event object.
  1614. * When the event is successfully deleted, it will resume all suspended threads in the event list.
  1615. *
  1616. * @see rt_event_detach()
  1617. *
  1618. * @param event is a pointer to an event object to be deleted.
  1619. *
  1620. * @return Return the operation status. When the return value is RT_EOK, the operation is successful.
  1621. * If the return value is any other values, it means that the event detach failed.
  1622. *
  1623. * @warning This function can ONLY delete an event initialized by the rt_event_create() function.
  1624. * If the event is initialized by the rt_event_init() function, you MUST NOT USE this function to delete it,
  1625. * ONLY USE the rt_event_detach() function to complete the detachment.
  1626. */
  1627. rt_err_t rt_event_delete(rt_event_t event)
  1628. {
  1629. /* parameter check */
  1630. RT_ASSERT(event != RT_NULL);
  1631. RT_ASSERT(rt_object_get_type(&event->parent.parent) == RT_Object_Class_Event);
  1632. RT_ASSERT(rt_object_is_systemobject(&event->parent.parent) == RT_FALSE);
  1633. RT_DEBUG_NOT_IN_INTERRUPT;
  1634. rt_spin_lock(&(event->spinlock));
  1635. /* resume all suspended thread */
  1636. rt_susp_list_resume_all(&(event->parent.suspend_thread), RT_ERROR);
  1637. rt_spin_unlock(&(event->spinlock));
  1638. /* delete event object */
  1639. rt_object_delete(&(event->parent.parent));
  1640. return RT_EOK;
  1641. }
  1642. RTM_EXPORT(rt_event_delete);
  1643. #endif /* RT_USING_HEAP */
  1644. /**
  1645. * @brief This function will send an event to the event object.
  1646. * If there is a thread suspended on the event, the thread will be resumed.
  1647. *
  1648. * @note When using this function, you need to use the parameter (set) to specify the event flag of the event object,
  1649. * then the function will traverse the list of suspended threads waiting on the event object.
  1650. * If there is a thread suspended on the event, and the thread's event_info and the event flag of
  1651. * the current event object matches, the thread will be resumed.
  1652. *
  1653. * @param event is a pointer to the event object to be sent.
  1654. *
  1655. * @param set is a flag that you will set for this event's flag.
  1656. * You can set an event flag, or you can set multiple flags through OR logic operation.
  1657. *
  1658. * @return Return the operation status. When the return value is RT_EOK, the operation is successful.
  1659. * If the return value is any other values, it means that the event detach failed.
  1660. */
  1661. rt_err_t rt_event_send(rt_event_t event, rt_uint32_t set)
  1662. {
  1663. struct rt_list_node *n;
  1664. struct rt_thread *thread;
  1665. rt_sched_lock_level_t slvl;
  1666. rt_base_t level;
  1667. rt_base_t status;
  1668. rt_bool_t need_schedule;
  1669. rt_uint32_t need_clear_set = 0;
  1670. /* parameter check */
  1671. RT_ASSERT(event != RT_NULL);
  1672. RT_ASSERT(rt_object_get_type(&event->parent.parent) == RT_Object_Class_Event);
  1673. if (set == 0)
  1674. return -RT_ERROR;
  1675. need_schedule = RT_FALSE;
  1676. level = rt_spin_lock_irqsave(&(event->spinlock));
  1677. /* set event */
  1678. event->set |= set;
  1679. RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(event->parent.parent)));
  1680. rt_sched_lock(&slvl);
  1681. if (!rt_list_isempty(&event->parent.suspend_thread))
  1682. {
  1683. /* search thread list to resume thread */
  1684. n = event->parent.suspend_thread.next;
  1685. while (n != &(event->parent.suspend_thread))
  1686. {
  1687. /* get thread */
  1688. thread = RT_THREAD_LIST_NODE_ENTRY(n);
  1689. status = -RT_ERROR;
  1690. if (thread->event_info & RT_EVENT_FLAG_AND)
  1691. {
  1692. if ((thread->event_set & event->set) == thread->event_set)
  1693. {
  1694. /* received an AND event */
  1695. status = RT_EOK;
  1696. }
  1697. }
  1698. else if (thread->event_info & RT_EVENT_FLAG_OR)
  1699. {
  1700. if (thread->event_set & event->set)
  1701. {
  1702. /* save the received event set */
  1703. thread->event_set = thread->event_set & event->set;
  1704. /* received an OR event */
  1705. status = RT_EOK;
  1706. }
  1707. }
  1708. else
  1709. {
  1710. rt_sched_unlock(slvl);
  1711. rt_spin_unlock_irqrestore(&(event->spinlock), level);
  1712. return -RT_EINVAL;
  1713. }
  1714. /* move node to the next */
  1715. n = n->next;
  1716. /* condition is satisfied, resume thread */
  1717. if (status == RT_EOK)
  1718. {
  1719. /* clear event */
  1720. if (thread->event_info & RT_EVENT_FLAG_CLEAR)
  1721. need_clear_set |= thread->event_set;
  1722. /* resume thread, and thread list breaks out */
  1723. rt_sched_thread_ready(thread);
  1724. thread->error = RT_EOK;
  1725. /* need do a scheduling */
  1726. need_schedule = RT_TRUE;
  1727. }
  1728. }
  1729. if (need_clear_set)
  1730. {
  1731. event->set &= ~need_clear_set;
  1732. }
  1733. }
  1734. rt_sched_unlock(slvl);
  1735. rt_spin_unlock_irqrestore(&(event->spinlock), level);
  1736. /* do a schedule */
  1737. if (need_schedule == RT_TRUE)
  1738. rt_schedule();
  1739. return RT_EOK;
  1740. }
  1741. RTM_EXPORT(rt_event_send);
  1742. /**
  1743. * @brief This function will receive an event from event object. if the event is unavailable, the thread shall wait for
  1744. * the event up to a specified time.
  1745. *
  1746. * @note If there are threads suspended on this semaphore, the first thread in the list of this semaphore object
  1747. * will be resumed, and a thread scheduling (rt_schedule) will be executed.
  1748. * If no threads are suspended on this semaphore, the count value sem->value of this semaphore will increase by 1.
  1749. *
  1750. * @param event is a pointer to the event object to be received.
  1751. *
  1752. * @param set is a flag that you will set for this event's flag.
  1753. * You can set an event flag, or you can set multiple flags through OR logic operation.
  1754. *
  1755. * @param option is the option of this receiving event, it indicates how the receiving event is operated.
  1756. * The option can be one or more of the following values, When selecting multiple values,use logical OR to operate.
  1757. * (NOTE: RT_EVENT_FLAG_OR and RT_EVENT_FLAG_AND can only select one):
  1758. *
  1759. *
  1760. * RT_EVENT_FLAG_OR The thread select to use logical OR to receive the event.
  1761. *
  1762. * RT_EVENT_FLAG_AND The thread select to use logical OR to receive the event.
  1763. *
  1764. * RT_EVENT_FLAG_CLEAR When the thread receives the corresponding event, the function
  1765. * determines whether to clear the event flag.
  1766. *
  1767. * @param timeout is a timeout period (unit: an OS tick).
  1768. *
  1769. * @param recved is a pointer to the received event. If you don't care about this value, you can use RT_NULL to set.
  1770. *
  1771. * @return Return the operation status. When the return value is RT_EOK, the operation is successful.
  1772. * If the return value is any other values, it means that the semaphore release failed.
  1773. */
  1774. static rt_err_t _rt_event_recv(rt_event_t event,
  1775. rt_uint32_t set,
  1776. rt_uint8_t option,
  1777. rt_int32_t timeout,
  1778. rt_uint32_t *recved,
  1779. int suspend_flag)
  1780. {
  1781. struct rt_thread *thread;
  1782. rt_base_t level;
  1783. rt_base_t status;
  1784. rt_err_t ret;
  1785. /* parameter check */
  1786. RT_ASSERT(event != RT_NULL);
  1787. RT_ASSERT(rt_object_get_type(&event->parent.parent) == RT_Object_Class_Event);
  1788. /* current context checking */
  1789. RT_DEBUG_SCHEDULER_AVAILABLE(RT_TRUE);
  1790. if (set == 0)
  1791. return -RT_ERROR;
  1792. /* initialize status */
  1793. status = -RT_ERROR;
  1794. /* get current thread */
  1795. thread = rt_thread_self();
  1796. /* reset thread error */
  1797. thread->error = -RT_EINTR;
  1798. RT_OBJECT_HOOK_CALL(rt_object_trytake_hook, (&(event->parent.parent)));
  1799. level = rt_spin_lock_irqsave(&(event->spinlock));
  1800. /* check event set */
  1801. if (option & RT_EVENT_FLAG_AND)
  1802. {
  1803. if ((event->set & set) == set)
  1804. status = RT_EOK;
  1805. }
  1806. else if (option & RT_EVENT_FLAG_OR)
  1807. {
  1808. if (event->set & set)
  1809. status = RT_EOK;
  1810. }
  1811. else
  1812. {
  1813. /* either RT_EVENT_FLAG_AND or RT_EVENT_FLAG_OR should be set */
  1814. RT_ASSERT(0);
  1815. }
  1816. if (status == RT_EOK)
  1817. {
  1818. thread->error = RT_EOK;
  1819. /* set received event */
  1820. if (recved)
  1821. *recved = (event->set & set);
  1822. /* fill thread event info */
  1823. thread->event_set = (event->set & set);
  1824. thread->event_info = option;
  1825. /* received event */
  1826. if (option & RT_EVENT_FLAG_CLEAR)
  1827. event->set &= ~set;
  1828. }
  1829. else if (timeout == 0)
  1830. {
  1831. /* no waiting */
  1832. thread->error = -RT_ETIMEOUT;
  1833. rt_spin_unlock_irqrestore(&(event->spinlock), level);
  1834. return -RT_ETIMEOUT;
  1835. }
  1836. else
  1837. {
  1838. /* fill thread event info */
  1839. thread->event_set = set;
  1840. thread->event_info = option;
  1841. /* put thread to suspended thread list */
  1842. ret = rt_thread_suspend_to_list(thread, &(event->parent.suspend_thread),
  1843. event->parent.parent.flag, suspend_flag);
  1844. if (ret != RT_EOK)
  1845. {
  1846. rt_spin_unlock_irqrestore(&(event->spinlock), level);
  1847. return ret;
  1848. }
  1849. /* if there is a waiting timeout, active thread timer */
  1850. if (timeout > 0)
  1851. {
  1852. /* reset the timeout of thread timer and start it */
  1853. rt_timer_control(&(thread->thread_timer),
  1854. RT_TIMER_CTRL_SET_TIME,
  1855. &timeout);
  1856. rt_timer_start(&(thread->thread_timer));
  1857. }
  1858. rt_spin_unlock_irqrestore(&(event->spinlock), level);
  1859. /* do a schedule */
  1860. rt_schedule();
  1861. if (thread->error != RT_EOK)
  1862. {
  1863. /* return error */
  1864. return thread->error;
  1865. }
  1866. /* received an event, disable interrupt to protect */
  1867. level = rt_spin_lock_irqsave(&(event->spinlock));
  1868. /* set received event */
  1869. if (recved)
  1870. *recved = thread->event_set;
  1871. }
  1872. rt_spin_unlock_irqrestore(&(event->spinlock), level);
  1873. RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(event->parent.parent)));
  1874. return thread->error;
  1875. }
  1876. rt_err_t rt_event_recv(rt_event_t event,
  1877. rt_uint32_t set,
  1878. rt_uint8_t option,
  1879. rt_int32_t timeout,
  1880. rt_uint32_t *recved)
  1881. {
  1882. return _rt_event_recv(event, set, option, timeout, recved, RT_UNINTERRUPTIBLE);
  1883. }
  1884. RTM_EXPORT(rt_event_recv);
  1885. rt_err_t rt_event_recv_interruptible(rt_event_t event,
  1886. rt_uint32_t set,
  1887. rt_uint8_t option,
  1888. rt_int32_t timeout,
  1889. rt_uint32_t *recved)
  1890. {
  1891. return _rt_event_recv(event, set, option, timeout, recved, RT_INTERRUPTIBLE);
  1892. }
  1893. RTM_EXPORT(rt_event_recv_interruptible);
  1894. rt_err_t rt_event_recv_killable(rt_event_t event,
  1895. rt_uint32_t set,
  1896. rt_uint8_t option,
  1897. rt_int32_t timeout,
  1898. rt_uint32_t *recved)
  1899. {
  1900. return _rt_event_recv(event, set, option, timeout, recved, RT_KILLABLE);
  1901. }
  1902. RTM_EXPORT(rt_event_recv_killable);
  1903. /**
  1904. * @brief This function will set some extra attributions of an event object.
  1905. *
  1906. * @note Currently this function only supports the RT_IPC_CMD_RESET command to reset the event.
  1907. *
  1908. * @param event is a pointer to an event object.
  1909. *
  1910. * @param cmd is a command word used to configure some attributions of the event.
  1911. *
  1912. * @param arg is the argument of the function to execute the command.
  1913. *
  1914. * @return Return the operation status. When the return value is RT_EOK, the operation is successful.
  1915. * If the return value is any other values, it means that this function failed to execute.
  1916. */
  1917. rt_err_t rt_event_control(rt_event_t event, int cmd, void *arg)
  1918. {
  1919. rt_base_t level;
  1920. RT_UNUSED(arg);
  1921. /* parameter check */
  1922. RT_ASSERT(event != RT_NULL);
  1923. RT_ASSERT(rt_object_get_type(&event->parent.parent) == RT_Object_Class_Event);
  1924. if (cmd == RT_IPC_CMD_RESET)
  1925. {
  1926. level = rt_spin_lock_irqsave(&(event->spinlock));
  1927. /* resume all waiting thread */
  1928. rt_susp_list_resume_all(&event->parent.suspend_thread, RT_ERROR);
  1929. /* initialize event set */
  1930. event->set = 0;
  1931. rt_spin_unlock_irqrestore(&(event->spinlock), level);
  1932. rt_schedule();
  1933. return RT_EOK;
  1934. }
  1935. return -RT_ERROR;
  1936. }
  1937. RTM_EXPORT(rt_event_control);
  1938. /**@}*/
  1939. #endif /* RT_USING_EVENT */
  1940. #ifdef RT_USING_MAILBOX
  1941. /**
  1942. * @addtogroup mailbox
  1943. * @{
  1944. */
  1945. /**
  1946. * @brief Initialize a static mailbox object.
  1947. *
  1948. * @note For the static mailbox object, its memory space is allocated by the compiler during compiling,
  1949. * and shall placed on the read-write data segment or on the uninitialized data segment.
  1950. * By contrast, the rt_mb_create() function will allocate memory space automatically and initialize the mailbox.
  1951. *
  1952. * @see rt_mb_create()
  1953. *
  1954. * @param mb is a pointer to the mailbox to initialize.
  1955. * It is assumed that storage for the mailbox will be allocated in your application.
  1956. *
  1957. * @param name is a pointer to the name that given to the mailbox.
  1958. *
  1959. * @param msgpool the begin address of buffer to save received mail.
  1960. *
  1961. * @param size is the maximum number of mails in the mailbox.
  1962. * For example, when the mailbox buffer capacity is N, size is N/4.
  1963. *
  1964. * @param flag is the mailbox flag, which determines the queuing way of how multiple threads wait
  1965. * when the mailbox is not available.
  1966. * The mailbox flag can be ONE of the following values:
  1967. *
  1968. * RT_IPC_FLAG_PRIO The pending threads will queue in order of priority.
  1969. *
  1970. * RT_IPC_FLAG_FIFO The pending threads will queue in the first-in-first-out method
  1971. * (also known as first-come-first-served (FCFS) scheduling strategy).
  1972. *
  1973. * NOTE: RT_IPC_FLAG_FIFO is a non-real-time scheduling mode. It is strongly recommended to
  1974. * use RT_IPC_FLAG_PRIO to ensure the thread is real-time UNLESS your applications concern about
  1975. * the first-in-first-out principle, and you clearly understand that all threads involved in
  1976. * this mailbox will become non-real-time threads.
  1977. *
  1978. * @return Return the operation status. When the return value is RT_EOK, the initialization is successful.
  1979. * If the return value is any other values, it represents the initialization failed.
  1980. *
  1981. * @warning This function can ONLY be called from threads.
  1982. */
  1983. rt_err_t rt_mb_init(rt_mailbox_t mb,
  1984. const char *name,
  1985. void *msgpool,
  1986. rt_size_t size,
  1987. rt_uint8_t flag)
  1988. {
  1989. RT_ASSERT(mb != RT_NULL);
  1990. RT_ASSERT((flag == RT_IPC_FLAG_FIFO) || (flag == RT_IPC_FLAG_PRIO));
  1991. /* initialize object */
  1992. rt_object_init(&(mb->parent.parent), RT_Object_Class_MailBox, name);
  1993. /* set parent flag */
  1994. mb->parent.parent.flag = flag;
  1995. /* initialize ipc object */
  1996. _ipc_object_init(&(mb->parent));
  1997. /* initialize mailbox */
  1998. mb->msg_pool = (rt_ubase_t *)msgpool;
  1999. mb->size = (rt_uint16_t)size;
  2000. mb->entry = 0;
  2001. mb->in_offset = 0;
  2002. mb->out_offset = 0;
  2003. /* initialize an additional list of sender suspend thread */
  2004. rt_list_init(&(mb->suspend_sender_thread));
  2005. rt_spin_lock_init(&(mb->spinlock));
  2006. return RT_EOK;
  2007. }
  2008. RTM_EXPORT(rt_mb_init);
  2009. /**
  2010. * @brief This function will detach a static mailbox object.
  2011. *
  2012. * @note This function is used to detach a static mailbox object which is initialized by rt_mb_init() function.
  2013. * By contrast, the rt_mb_delete() function will delete a mailbox object.
  2014. * When the mailbox is successfully detached, it will resume all suspended threads in the mailbox list.
  2015. *
  2016. * @see rt_mb_delete()
  2017. *
  2018. * @param mb is a pointer to a mailbox object to be detached.
  2019. *
  2020. * @return Return the operation status. When the return value is RT_EOK, the initialization is successful.
  2021. * If the return value is any other values, it means that the mailbox detach failed.
  2022. *
  2023. * @warning This function can ONLY detach a static mailbox initialized by the rt_mb_init() function.
  2024. * If the mailbox is created by the rt_mb_create() function, you MUST NOT USE this function to detach it,
  2025. * ONLY USE the rt_mb_delete() function to complete the deletion.
  2026. */
  2027. rt_err_t rt_mb_detach(rt_mailbox_t mb)
  2028. {
  2029. rt_base_t level;
  2030. /* parameter check */
  2031. RT_ASSERT(mb != RT_NULL);
  2032. RT_ASSERT(rt_object_get_type(&mb->parent.parent) == RT_Object_Class_MailBox);
  2033. RT_ASSERT(rt_object_is_systemobject(&mb->parent.parent));
  2034. level = rt_spin_lock_irqsave(&(mb->spinlock));
  2035. /* resume all suspended thread */
  2036. rt_susp_list_resume_all(&(mb->parent.suspend_thread), RT_ERROR);
  2037. /* also resume all mailbox private suspended thread */
  2038. rt_susp_list_resume_all(&(mb->suspend_sender_thread), RT_ERROR);
  2039. rt_spin_unlock_irqrestore(&(mb->spinlock), level);
  2040. /* detach mailbox object */
  2041. rt_object_detach(&(mb->parent.parent));
  2042. return RT_EOK;
  2043. }
  2044. RTM_EXPORT(rt_mb_detach);
  2045. #ifdef RT_USING_HEAP
  2046. /**
  2047. * @brief Creating a mailbox object.
  2048. *
  2049. * @note For the mailbox object, its memory space is allocated automatically.
  2050. * By contrast, the rt_mb_init() function will initialize a static mailbox object.
  2051. *
  2052. * @see rt_mb_init()
  2053. *
  2054. * @param name is a pointer that given to the mailbox.
  2055. *
  2056. * @param size is the maximum number of mails in the mailbox.
  2057. * For example, when mailbox buffer capacity is N, size is N/4.
  2058. *
  2059. * @param flag is the mailbox flag, which determines the queuing way of how multiple threads wait
  2060. * when the mailbox is not available.
  2061. * The mailbox flag can be ONE of the following values:
  2062. *
  2063. * RT_IPC_FLAG_PRIO The pending threads will queue in order of priority.
  2064. *
  2065. * RT_IPC_FLAG_FIFO The pending threads will queue in the first-in-first-out method
  2066. * (also known as first-come-first-served (FCFS) scheduling strategy).
  2067. *
  2068. * NOTE: RT_IPC_FLAG_FIFO is a non-real-time scheduling mode. It is strongly recommended to
  2069. * use RT_IPC_FLAG_PRIO to ensure the thread is real-time UNLESS your applications concern about
  2070. * the first-in-first-out principle, and you clearly understand that all threads involved in
  2071. * this mailbox will become non-real-time threads.
  2072. *
  2073. * @return Return a pointer to the mailbox object. When the return value is RT_NULL, it means the creation failed.
  2074. *
  2075. * @warning This function can ONLY be called from threads.
  2076. */
  2077. rt_mailbox_t rt_mb_create(const char *name, rt_size_t size, rt_uint8_t flag)
  2078. {
  2079. rt_mailbox_t mb;
  2080. RT_ASSERT((flag == RT_IPC_FLAG_FIFO) || (flag == RT_IPC_FLAG_PRIO));
  2081. RT_DEBUG_NOT_IN_INTERRUPT;
  2082. /* allocate object */
  2083. mb = (rt_mailbox_t)rt_object_allocate(RT_Object_Class_MailBox, name);
  2084. if (mb == RT_NULL)
  2085. return mb;
  2086. /* set parent */
  2087. mb->parent.parent.flag = flag;
  2088. /* initialize ipc object */
  2089. _ipc_object_init(&(mb->parent));
  2090. /* initialize mailbox */
  2091. mb->size = (rt_uint16_t)size;
  2092. mb->msg_pool = (rt_ubase_t *)RT_KERNEL_MALLOC(mb->size * sizeof(rt_ubase_t));
  2093. if (mb->msg_pool == RT_NULL)
  2094. {
  2095. /* delete mailbox object */
  2096. rt_object_delete(&(mb->parent.parent));
  2097. return RT_NULL;
  2098. }
  2099. mb->entry = 0;
  2100. mb->in_offset = 0;
  2101. mb->out_offset = 0;
  2102. /* initialize an additional list of sender suspend thread */
  2103. rt_list_init(&(mb->suspend_sender_thread));
  2104. rt_spin_lock_init(&(mb->spinlock));
  2105. return mb;
  2106. }
  2107. RTM_EXPORT(rt_mb_create);
  2108. /**
  2109. * @brief This function will delete a mailbox object and release the memory space.
  2110. *
  2111. * @note This function is used to delete a mailbox object which is created by the rt_mb_create() function.
  2112. * By contrast, the rt_mb_detach() function will detach a static mailbox object.
  2113. * When the mailbox is successfully deleted, it will resume all suspended threads in the mailbox list.
  2114. *
  2115. * @see rt_mb_detach()
  2116. *
  2117. * @param mb is a pointer to a mailbox object to be deleted.
  2118. *
  2119. * @return Return the operation status. When the return value is RT_EOK, the operation is successful.
  2120. * If the return value is any other values, it means that the mailbox detach failed.
  2121. *
  2122. * @warning This function can only delete mailbox created by the rt_mb_create() function.
  2123. * If the mailbox is initialized by the rt_mb_init() function, you MUST NOT USE this function to delete it,
  2124. * ONLY USE the rt_mb_detach() function to complete the detachment.
  2125. */
  2126. rt_err_t rt_mb_delete(rt_mailbox_t mb)
  2127. {
  2128. /* parameter check */
  2129. RT_ASSERT(mb != RT_NULL);
  2130. RT_ASSERT(rt_object_get_type(&mb->parent.parent) == RT_Object_Class_MailBox);
  2131. RT_ASSERT(rt_object_is_systemobject(&mb->parent.parent) == RT_FALSE);
  2132. RT_DEBUG_NOT_IN_INTERRUPT;
  2133. rt_spin_lock(&(mb->spinlock));
  2134. /* resume all suspended thread */
  2135. rt_susp_list_resume_all(&(mb->parent.suspend_thread), RT_ERROR);
  2136. /* also resume all mailbox private suspended thread */
  2137. rt_susp_list_resume_all(&(mb->suspend_sender_thread), RT_ERROR);
  2138. rt_spin_unlock(&(mb->spinlock));
  2139. /* free mailbox pool */
  2140. RT_KERNEL_FREE(mb->msg_pool);
  2141. /* delete mailbox object */
  2142. rt_object_delete(&(mb->parent.parent));
  2143. return RT_EOK;
  2144. }
  2145. RTM_EXPORT(rt_mb_delete);
  2146. #endif /* RT_USING_HEAP */
  2147. /**
  2148. * @brief This function will send an mail to the mailbox object. If there is a thread suspended on the mailbox,
  2149. * the thread will be resumed.
  2150. *
  2151. * @note When using this function to send a mail, if the mailbox if fully used, the current thread will
  2152. * wait for a timeout. If the set timeout time is reached and there is still no space available,
  2153. * the sending thread will be resumed and an error code will be returned.
  2154. * By contrast, the rt_mb_send() function will return an error code immediately without waiting time
  2155. * when the mailbox if fully used.
  2156. *
  2157. * @see rt_mb_send()
  2158. *
  2159. * @param mb is a pointer to the mailbox object to be sent.
  2160. *
  2161. * @param value is a value to the content of the mail you want to send.
  2162. *
  2163. * @param timeout is a timeout period (unit: an OS tick).
  2164. *
  2165. * @return Return the operation status. When the return value is RT_EOK, the operation is successful.
  2166. * If the return value is any other values, it means that the mailbox detach failed.
  2167. *
  2168. * @warning This function can be called in interrupt context and thread context.
  2169. */
  2170. static rt_err_t _rt_mb_send_wait(rt_mailbox_t mb,
  2171. rt_ubase_t value,
  2172. rt_int32_t timeout,
  2173. int suspend_flag)
  2174. {
  2175. struct rt_thread *thread;
  2176. rt_base_t level;
  2177. rt_uint32_t tick_delta;
  2178. rt_err_t ret;
  2179. /* parameter check */
  2180. RT_ASSERT(mb != RT_NULL);
  2181. RT_ASSERT(rt_object_get_type(&mb->parent.parent) == RT_Object_Class_MailBox);
  2182. /* current context checking */
  2183. RT_DEBUG_SCHEDULER_AVAILABLE(timeout != 0);
  2184. /* initialize delta tick */
  2185. tick_delta = 0;
  2186. /* get current thread */
  2187. thread = rt_thread_self();
  2188. RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(mb->parent.parent)));
  2189. /* disable interrupt */
  2190. level = rt_spin_lock_irqsave(&(mb->spinlock));
  2191. /* for non-blocking call */
  2192. if (mb->entry == mb->size && timeout == 0)
  2193. {
  2194. rt_spin_unlock_irqrestore(&(mb->spinlock), level);
  2195. return -RT_EFULL;
  2196. }
  2197. /* mailbox is full */
  2198. while (mb->entry == mb->size)
  2199. {
  2200. /* reset error number in thread */
  2201. thread->error = -RT_EINTR;
  2202. /* no waiting, return timeout */
  2203. if (timeout == 0)
  2204. {
  2205. rt_spin_unlock_irqrestore(&(mb->spinlock), level);
  2206. return -RT_EFULL;
  2207. }
  2208. /* suspend current thread */
  2209. ret = rt_thread_suspend_to_list(thread, &(mb->suspend_sender_thread),
  2210. mb->parent.parent.flag, suspend_flag);
  2211. if (ret != RT_EOK)
  2212. {
  2213. rt_spin_unlock_irqrestore(&(mb->spinlock), level);
  2214. return ret;
  2215. }
  2216. /* has waiting time, start thread timer */
  2217. if (timeout > 0)
  2218. {
  2219. /* get the start tick of timer */
  2220. tick_delta = rt_tick_get();
  2221. LOG_D("mb_send_wait: start timer of thread:%s",
  2222. thread->parent.name);
  2223. /* reset the timeout of thread timer and start it */
  2224. rt_timer_control(&(thread->thread_timer),
  2225. RT_TIMER_CTRL_SET_TIME,
  2226. &timeout);
  2227. rt_timer_start(&(thread->thread_timer));
  2228. }
  2229. rt_spin_unlock_irqrestore(&(mb->spinlock), level);
  2230. /* re-schedule */
  2231. rt_schedule();
  2232. /* resume from suspend state */
  2233. if (thread->error != RT_EOK)
  2234. {
  2235. /* return error */
  2236. return thread->error;
  2237. }
  2238. level = rt_spin_lock_irqsave(&(mb->spinlock));
  2239. /* if it's not waiting forever and then re-calculate timeout tick */
  2240. if (timeout > 0)
  2241. {
  2242. tick_delta = rt_tick_get() - tick_delta;
  2243. timeout -= tick_delta;
  2244. if (timeout < 0)
  2245. timeout = 0;
  2246. }
  2247. }
  2248. /* set ptr */
  2249. mb->msg_pool[mb->in_offset] = value;
  2250. /* increase input offset */
  2251. ++ mb->in_offset;
  2252. if (mb->in_offset >= mb->size)
  2253. mb->in_offset = 0;
  2254. if(mb->entry < RT_MB_ENTRY_MAX)
  2255. {
  2256. /* increase message entry */
  2257. mb->entry ++;
  2258. }
  2259. else
  2260. {
  2261. rt_spin_unlock_irqrestore(&(mb->spinlock), level);
  2262. return -RT_EFULL; /* value overflowed */
  2263. }
  2264. /* resume suspended thread */
  2265. if (!rt_list_isempty(&mb->parent.suspend_thread))
  2266. {
  2267. rt_susp_list_dequeue(&(mb->parent.suspend_thread), RT_EOK);
  2268. rt_spin_unlock_irqrestore(&(mb->spinlock), level);
  2269. rt_schedule();
  2270. return RT_EOK;
  2271. }
  2272. rt_spin_unlock_irqrestore(&(mb->spinlock), level);
  2273. return RT_EOK;
  2274. }
  2275. rt_err_t rt_mb_send_wait(rt_mailbox_t mb,
  2276. rt_ubase_t value,
  2277. rt_int32_t timeout)
  2278. {
  2279. return _rt_mb_send_wait(mb, value, timeout, RT_UNINTERRUPTIBLE);
  2280. }
  2281. RTM_EXPORT(rt_mb_send_wait);
  2282. rt_err_t rt_mb_send_wait_interruptible(rt_mailbox_t mb,
  2283. rt_ubase_t value,
  2284. rt_int32_t timeout)
  2285. {
  2286. return _rt_mb_send_wait(mb, value, timeout, RT_INTERRUPTIBLE);
  2287. }
  2288. RTM_EXPORT(rt_mb_send_wait_interruptible);
  2289. rt_err_t rt_mb_send_wait_killable(rt_mailbox_t mb,
  2290. rt_ubase_t value,
  2291. rt_int32_t timeout)
  2292. {
  2293. return _rt_mb_send_wait(mb, value, timeout, RT_KILLABLE);
  2294. }
  2295. RTM_EXPORT(rt_mb_send_wait_killable);
  2296. /**
  2297. * @brief This function will send an mail to the mailbox object. If there is a thread suspended on the mailbox,
  2298. * the thread will be resumed.
  2299. *
  2300. * @note When using this function to send a mail, if the mailbox is fully used, this function will return an error
  2301. * code immediately without waiting time.
  2302. * By contrast, the rt_mb_send_wait() function is set a timeout to wait for the mail to be sent.
  2303. *
  2304. * @see rt_mb_send_wait()
  2305. *
  2306. * @param mb is a pointer to the mailbox object to be sent.
  2307. *
  2308. * @param value is a value to the content of the mail you want to send.
  2309. *
  2310. * @return Return the operation status. When the return value is RT_EOK, the operation is successful.
  2311. * If the return value is any other values, it means that the mailbox detach failed.
  2312. */
  2313. rt_err_t rt_mb_send(rt_mailbox_t mb, rt_ubase_t value)
  2314. {
  2315. return rt_mb_send_wait(mb, value, 0);
  2316. }
  2317. RTM_EXPORT(rt_mb_send);
  2318. rt_err_t rt_mb_send_interruptible(rt_mailbox_t mb, rt_ubase_t value)
  2319. {
  2320. return rt_mb_send_wait_interruptible(mb, value, 0);
  2321. }
  2322. RTM_EXPORT(rt_mb_send_interruptible);
  2323. rt_err_t rt_mb_send_killable(rt_mailbox_t mb, rt_ubase_t value)
  2324. {
  2325. return rt_mb_send_wait_killable(mb, value, 0);
  2326. }
  2327. RTM_EXPORT(rt_mb_send_killable);
  2328. /**
  2329. * @brief This function will send an urgent mail to the mailbox object.
  2330. *
  2331. * @note This function is almost the same as the rt_mb_send() function. The only difference is that
  2332. * when sending an urgent mail, the mail will be placed at the head of the mail queue so that
  2333. * the recipient can receive the urgent mail first.
  2334. *
  2335. * @see rt_mb_send()
  2336. *
  2337. * @param mb is a pointer to the mailbox object to be sent.
  2338. *
  2339. * @param value is the content of the mail you want to send.
  2340. *
  2341. * @return Return the operation status. When the return value is RT_EOK, the operation is successful.
  2342. * If the return value is any other values, it means that the mailbox detach failed.
  2343. */
  2344. rt_err_t rt_mb_urgent(rt_mailbox_t mb, rt_ubase_t value)
  2345. {
  2346. rt_base_t level;
  2347. /* parameter check */
  2348. RT_ASSERT(mb != RT_NULL);
  2349. RT_ASSERT(rt_object_get_type(&mb->parent.parent) == RT_Object_Class_MailBox);
  2350. RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(mb->parent.parent)));
  2351. level = rt_spin_lock_irqsave(&(mb->spinlock));
  2352. if (mb->entry == mb->size)
  2353. {
  2354. rt_spin_unlock_irqrestore(&(mb->spinlock), level);
  2355. return -RT_EFULL;
  2356. }
  2357. /* rewind to the previous position */
  2358. if (mb->out_offset > 0)
  2359. {
  2360. mb->out_offset --;
  2361. }
  2362. else
  2363. {
  2364. mb->out_offset = mb->size - 1;
  2365. }
  2366. /* set ptr */
  2367. mb->msg_pool[mb->out_offset] = value;
  2368. /* increase message entry */
  2369. mb->entry ++;
  2370. /* resume suspended thread */
  2371. if (!rt_list_isempty(&mb->parent.suspend_thread))
  2372. {
  2373. rt_susp_list_dequeue(&(mb->parent.suspend_thread), RT_EOK);
  2374. rt_spin_unlock_irqrestore(&(mb->spinlock), level);
  2375. rt_schedule();
  2376. return RT_EOK;
  2377. }
  2378. rt_spin_unlock_irqrestore(&(mb->spinlock), level);
  2379. return RT_EOK;
  2380. }
  2381. RTM_EXPORT(rt_mb_urgent);
  2382. /**
  2383. * @brief This function will receive a mail from mailbox object, if there is no mail in mailbox object,
  2384. * the thread shall wait for a specified time.
  2385. *
  2386. * @note Only when there is mail in the mailbox, the receiving thread can get the mail immediately and
  2387. * return RT_EOK, otherwise the receiving thread will be suspended until the set timeout. If the mail
  2388. * is still not received within the specified time, it will return-RT_ETIMEOUT.
  2389. *
  2390. * @param mb is a pointer to the mailbox object to be received.
  2391. *
  2392. * @param value is a flag that you will set for this mailbox's flag.
  2393. * You can set an mailbox flag, or you can set multiple flags through OR logic operations.
  2394. *
  2395. * @param timeout is a timeout period (unit: an OS tick). If the mailbox object is not avaliable in the queue,
  2396. * the thread will wait for the object in the queue up to the amount of time specified by this parameter.
  2397. *
  2398. * NOTE:
  2399. * If use Macro RT_WAITING_FOREVER to set this parameter, which means that when the
  2400. * mailbox object is unavailable in the queue, the thread will be waiting forever.
  2401. * If use macro RT_WAITING_NO to set this parameter, which means that this
  2402. * function is non-blocking and will return immediately.
  2403. *
  2404. * @return Return the operation status. When the return value is RT_EOK, the operation is successful.
  2405. * If the return value is any other values, it means that the mailbox release failed.
  2406. */
  2407. static rt_err_t _rt_mb_recv(rt_mailbox_t mb, rt_ubase_t *value, rt_int32_t timeout, int suspend_flag)
  2408. {
  2409. struct rt_thread *thread;
  2410. rt_base_t level;
  2411. rt_uint32_t tick_delta;
  2412. rt_err_t ret;
  2413. /* parameter check */
  2414. RT_ASSERT(mb != RT_NULL);
  2415. RT_ASSERT(rt_object_get_type(&mb->parent.parent) == RT_Object_Class_MailBox);
  2416. /* current context checking */
  2417. RT_DEBUG_SCHEDULER_AVAILABLE(timeout != 0);
  2418. /* initialize delta tick */
  2419. tick_delta = 0;
  2420. /* get current thread */
  2421. thread = rt_thread_self();
  2422. RT_OBJECT_HOOK_CALL(rt_object_trytake_hook, (&(mb->parent.parent)));
  2423. level = rt_spin_lock_irqsave(&(mb->spinlock));
  2424. /* for non-blocking call */
  2425. if (mb->entry == 0 && timeout == 0)
  2426. {
  2427. rt_spin_unlock_irqrestore(&(mb->spinlock), level);
  2428. return -RT_ETIMEOUT;
  2429. }
  2430. /* mailbox is empty */
  2431. while (mb->entry == 0)
  2432. {
  2433. /* reset error number in thread */
  2434. thread->error = -RT_EINTR;
  2435. /* no waiting, return timeout */
  2436. if (timeout == 0)
  2437. {
  2438. rt_spin_unlock_irqrestore(&(mb->spinlock), level);
  2439. thread->error = -RT_ETIMEOUT;
  2440. return -RT_ETIMEOUT;
  2441. }
  2442. /* suspend current thread */
  2443. ret = rt_thread_suspend_to_list(thread, &(mb->parent.suspend_thread),
  2444. mb->parent.parent.flag, suspend_flag);
  2445. if (ret != RT_EOK)
  2446. {
  2447. rt_spin_unlock_irqrestore(&(mb->spinlock), level);
  2448. return ret;
  2449. }
  2450. /* has waiting time, start thread timer */
  2451. if (timeout > 0)
  2452. {
  2453. /* get the start tick of timer */
  2454. tick_delta = rt_tick_get();
  2455. LOG_D("mb_recv: start timer of thread:%s",
  2456. thread->parent.name);
  2457. /* reset the timeout of thread timer and start it */
  2458. rt_timer_control(&(thread->thread_timer),
  2459. RT_TIMER_CTRL_SET_TIME,
  2460. &timeout);
  2461. rt_timer_start(&(thread->thread_timer));
  2462. }
  2463. rt_spin_unlock_irqrestore(&(mb->spinlock), level);
  2464. /* re-schedule */
  2465. rt_schedule();
  2466. /* resume from suspend state */
  2467. if (thread->error != RT_EOK)
  2468. {
  2469. /* return error */
  2470. return thread->error;
  2471. }
  2472. level = rt_spin_lock_irqsave(&(mb->spinlock));
  2473. /* if it's not waiting forever and then re-calculate timeout tick */
  2474. if (timeout > 0)
  2475. {
  2476. tick_delta = rt_tick_get() - tick_delta;
  2477. timeout -= tick_delta;
  2478. if (timeout < 0)
  2479. timeout = 0;
  2480. }
  2481. }
  2482. /* fill ptr */
  2483. *value = mb->msg_pool[mb->out_offset];
  2484. /* increase output offset */
  2485. ++ mb->out_offset;
  2486. if (mb->out_offset >= mb->size)
  2487. mb->out_offset = 0;
  2488. /* decrease message entry */
  2489. if(mb->entry > 0)
  2490. {
  2491. mb->entry --;
  2492. }
  2493. /* resume suspended thread */
  2494. if (!rt_list_isempty(&(mb->suspend_sender_thread)))
  2495. {
  2496. rt_susp_list_dequeue(&(mb->suspend_sender_thread), RT_EOK);
  2497. rt_spin_unlock_irqrestore(&(mb->spinlock), level);
  2498. RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(mb->parent.parent)));
  2499. rt_schedule();
  2500. return RT_EOK;
  2501. }
  2502. rt_spin_unlock_irqrestore(&(mb->spinlock), level);
  2503. RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(mb->parent.parent)));
  2504. return RT_EOK;
  2505. }
  2506. rt_err_t rt_mb_recv(rt_mailbox_t mb, rt_ubase_t *value, rt_int32_t timeout)
  2507. {
  2508. return _rt_mb_recv(mb, value, timeout, RT_UNINTERRUPTIBLE);
  2509. }
  2510. RTM_EXPORT(rt_mb_recv);
  2511. rt_err_t rt_mb_recv_interruptible(rt_mailbox_t mb, rt_ubase_t *value, rt_int32_t timeout)
  2512. {
  2513. return _rt_mb_recv(mb, value, timeout, RT_INTERRUPTIBLE);
  2514. }
  2515. RTM_EXPORT(rt_mb_recv_interruptible);
  2516. rt_err_t rt_mb_recv_killable(rt_mailbox_t mb, rt_ubase_t *value, rt_int32_t timeout)
  2517. {
  2518. return _rt_mb_recv(mb, value, timeout, RT_KILLABLE);
  2519. }
  2520. RTM_EXPORT(rt_mb_recv_killable);
  2521. /**
  2522. * @brief This function will set some extra attributions of a mailbox object.
  2523. *
  2524. * @note Currently this function only supports the RT_IPC_CMD_RESET command to reset the mailbox.
  2525. *
  2526. * @param mb is a pointer to a mailbox object.
  2527. *
  2528. * @param cmd is a command used to configure some attributions of the mailbox.
  2529. *
  2530. * @param arg is the argument of the function to execute the command.
  2531. *
  2532. * @return Return the operation status. When the return value is RT_EOK, the operation is successful.
  2533. * If the return value is any other values, it means that this function failed to execute.
  2534. */
  2535. rt_err_t rt_mb_control(rt_mailbox_t mb, int cmd, void *arg)
  2536. {
  2537. rt_base_t level;
  2538. RT_UNUSED(arg);
  2539. /* parameter check */
  2540. RT_ASSERT(mb != RT_NULL);
  2541. RT_ASSERT(rt_object_get_type(&mb->parent.parent) == RT_Object_Class_MailBox);
  2542. if (cmd == RT_IPC_CMD_RESET)
  2543. {
  2544. level = rt_spin_lock_irqsave(&(mb->spinlock));
  2545. /* resume all waiting thread */
  2546. rt_susp_list_resume_all(&(mb->parent.suspend_thread), RT_ERROR);
  2547. /* also resume all mailbox private suspended thread */
  2548. rt_susp_list_resume_all(&(mb->suspend_sender_thread), RT_ERROR);
  2549. /* re-init mailbox */
  2550. mb->entry = 0;
  2551. mb->in_offset = 0;
  2552. mb->out_offset = 0;
  2553. rt_spin_unlock_irqrestore(&(mb->spinlock), level);
  2554. rt_schedule();
  2555. return RT_EOK;
  2556. }
  2557. return -RT_ERROR;
  2558. }
  2559. RTM_EXPORT(rt_mb_control);
  2560. /**@}*/
  2561. #endif /* RT_USING_MAILBOX */
  2562. #ifdef RT_USING_MESSAGEQUEUE
  2563. /**
  2564. * @addtogroup messagequeue
  2565. * @{
  2566. */
  2567. /**
  2568. * @brief Initialize a static messagequeue object.
  2569. *
  2570. * @note For the static messagequeue object, its memory space is allocated by the compiler during compiling,
  2571. * and shall placed on the read-write data segment or on the uninitialized data segment.
  2572. * By contrast, the rt_mq_create() function will allocate memory space automatically
  2573. * and initialize the messagequeue.
  2574. *
  2575. * @see rt_mq_create()
  2576. *
  2577. * @param mq is a pointer to the messagequeue to initialize. It is assumed that storage for
  2578. * the messagequeue will be allocated in your application.
  2579. *
  2580. * @param name is a pointer to the name that given to the messagequeue.
  2581. *
  2582. * @param msgpool is a pointer to the starting address of the memory space you allocated for
  2583. * the messagequeue in advance.
  2584. * In other words, msgpool is a pointer to the messagequeue buffer of the starting address.
  2585. *
  2586. * @param msg_size is the maximum length of a message in the messagequeue (Unit: Byte).
  2587. *
  2588. * @param pool_size is the size of the memory space allocated for the messagequeue in advance.
  2589. *
  2590. * @param flag is the messagequeue flag, which determines the queuing way of how multiple threads wait
  2591. * when the messagequeue is not available.
  2592. * The messagequeue flag can be ONE of the following values:
  2593. *
  2594. * RT_IPC_FLAG_PRIO The pending threads will queue in order of priority.
  2595. *
  2596. * RT_IPC_FLAG_FIFO The pending threads will queue in the first-in-first-out method
  2597. * (also known as first-come-first-served (FCFS) scheduling strategy).
  2598. *
  2599. * NOTE: RT_IPC_FLAG_FIFO is a non-real-time scheduling mode. It is strongly recommended to
  2600. * use RT_IPC_FLAG_PRIO to ensure the thread is real-time UNLESS your applications concern about
  2601. * the first-in-first-out principle, and you clearly understand that all threads involved in
  2602. * this messagequeue will become non-real-time threads.
  2603. *
  2604. * @return Return the operation status. When the return value is RT_EOK, the initialization is successful.
  2605. * If the return value is any other values, it represents the initialization failed.
  2606. *
  2607. * @warning This function can ONLY be called from threads.
  2608. */
  2609. rt_err_t rt_mq_init(rt_mq_t mq,
  2610. const char *name,
  2611. void *msgpool,
  2612. rt_size_t msg_size,
  2613. rt_size_t pool_size,
  2614. rt_uint8_t flag)
  2615. {
  2616. struct rt_mq_message *head;
  2617. rt_base_t temp;
  2618. register rt_size_t msg_align_size;
  2619. /* parameter check */
  2620. RT_ASSERT(mq != RT_NULL);
  2621. RT_ASSERT((flag == RT_IPC_FLAG_FIFO) || (flag == RT_IPC_FLAG_PRIO));
  2622. /* initialize object */
  2623. rt_object_init(&(mq->parent.parent), RT_Object_Class_MessageQueue, name);
  2624. /* set parent flag */
  2625. mq->parent.parent.flag = flag;
  2626. /* initialize ipc object */
  2627. _ipc_object_init(&(mq->parent));
  2628. /* set message pool */
  2629. mq->msg_pool = msgpool;
  2630. /* get correct message size */
  2631. msg_align_size = RT_ALIGN(msg_size, RT_ALIGN_SIZE);
  2632. mq->msg_size = msg_size;
  2633. mq->max_msgs = pool_size / (msg_align_size + sizeof(struct rt_mq_message));
  2634. if (0 == mq->max_msgs)
  2635. {
  2636. return -RT_EINVAL;
  2637. }
  2638. /* initialize message list */
  2639. mq->msg_queue_head = RT_NULL;
  2640. mq->msg_queue_tail = RT_NULL;
  2641. /* initialize message empty list */
  2642. mq->msg_queue_free = RT_NULL;
  2643. for (temp = 0; temp < mq->max_msgs; temp ++)
  2644. {
  2645. head = (struct rt_mq_message *)((rt_uint8_t *)mq->msg_pool +
  2646. temp * (msg_align_size + sizeof(struct rt_mq_message)));
  2647. head->next = (struct rt_mq_message *)mq->msg_queue_free;
  2648. mq->msg_queue_free = head;
  2649. }
  2650. /* the initial entry is zero */
  2651. mq->entry = 0;
  2652. /* initialize an additional list of sender suspend thread */
  2653. rt_list_init(&(mq->suspend_sender_thread));
  2654. rt_spin_lock_init(&(mq->spinlock));
  2655. return RT_EOK;
  2656. }
  2657. RTM_EXPORT(rt_mq_init);
  2658. /**
  2659. * @brief This function will detach a static messagequeue object.
  2660. *
  2661. * @note This function is used to detach a static messagequeue object which is initialized by rt_mq_init() function.
  2662. * By contrast, the rt_mq_delete() function will delete a messagequeue object.
  2663. * When the messagequeue is successfully detached, it will resume all suspended threads in the messagequeue list.
  2664. *
  2665. * @see rt_mq_delete()
  2666. *
  2667. * @param mq is a pointer to a messagequeue object to be detached.
  2668. *
  2669. * @return Return the operation status. When the return value is RT_EOK, the initialization is successful.
  2670. * If the return value is any other values, it means that the messagequeue detach failed.
  2671. *
  2672. * @warning This function can ONLY detach a static messagequeue initialized by the rt_mq_init() function.
  2673. * If the messagequeue is created by the rt_mq_create() function, you MUST NOT USE this function to detach it,
  2674. * and ONLY USE the rt_mq_delete() function to complete the deletion.
  2675. */
  2676. rt_err_t rt_mq_detach(rt_mq_t mq)
  2677. {
  2678. rt_base_t level;
  2679. /* parameter check */
  2680. RT_ASSERT(mq != RT_NULL);
  2681. RT_ASSERT(rt_object_get_type(&mq->parent.parent) == RT_Object_Class_MessageQueue);
  2682. RT_ASSERT(rt_object_is_systemobject(&mq->parent.parent));
  2683. level = rt_spin_lock_irqsave(&(mq->spinlock));
  2684. /* resume all suspended thread */
  2685. rt_susp_list_resume_all(&mq->parent.suspend_thread, RT_ERROR);
  2686. /* also resume all message queue private suspended thread */
  2687. rt_susp_list_resume_all(&(mq->suspend_sender_thread), RT_ERROR);
  2688. rt_spin_unlock_irqrestore(&(mq->spinlock), level);
  2689. /* detach message queue object */
  2690. rt_object_detach(&(mq->parent.parent));
  2691. return RT_EOK;
  2692. }
  2693. RTM_EXPORT(rt_mq_detach);
  2694. #ifdef RT_USING_HEAP
  2695. /**
  2696. * @brief Creating a messagequeue object.
  2697. *
  2698. * @note For the messagequeue object, its memory space is allocated automatically.
  2699. * By contrast, the rt_mq_init() function will initialize a static messagequeue object.
  2700. *
  2701. * @see rt_mq_init()
  2702. *
  2703. * @param name is a pointer that given to the messagequeue.
  2704. *
  2705. * @param msg_size is the maximum length of a message in the messagequeue (Unit: Byte).
  2706. *
  2707. * @param max_msgs is the maximum number of messages in the messagequeue.
  2708. *
  2709. * @param flag is the messagequeue flag, which determines the queuing way of how multiple threads wait
  2710. * when the messagequeue is not available.
  2711. * The messagequeue flag can be ONE of the following values:
  2712. *
  2713. * RT_IPC_FLAG_PRIO The pending threads will queue in order of priority.
  2714. *
  2715. * RT_IPC_FLAG_FIFO The pending threads will queue in the first-in-first-out method
  2716. * (also known as first-come-first-served (FCFS) scheduling strategy).
  2717. *
  2718. * NOTE: RT_IPC_FLAG_FIFO is a non-real-time scheduling mode. It is strongly recommended to
  2719. * use RT_IPC_FLAG_PRIO to ensure the thread is real-time UNLESS your applications concern about
  2720. * the first-in-first-out principle, and you clearly understand that all threads involved in
  2721. * this messagequeue will become non-real-time threads.
  2722. *
  2723. * @return Return a pointer to the messagequeue object. When the return value is RT_NULL, it means the creation failed.
  2724. *
  2725. * @warning This function can NOT be called in interrupt context. You can use macor RT_DEBUG_NOT_IN_INTERRUPT to check it.
  2726. */
  2727. rt_mq_t rt_mq_create(const char *name,
  2728. rt_size_t msg_size,
  2729. rt_size_t max_msgs,
  2730. rt_uint8_t flag)
  2731. {
  2732. struct rt_messagequeue *mq;
  2733. struct rt_mq_message *head;
  2734. rt_base_t temp;
  2735. register rt_size_t msg_align_size;
  2736. RT_ASSERT((flag == RT_IPC_FLAG_FIFO) || (flag == RT_IPC_FLAG_PRIO));
  2737. RT_DEBUG_NOT_IN_INTERRUPT;
  2738. /* allocate object */
  2739. mq = (rt_mq_t)rt_object_allocate(RT_Object_Class_MessageQueue, name);
  2740. if (mq == RT_NULL)
  2741. return mq;
  2742. /* set parent */
  2743. mq->parent.parent.flag = flag;
  2744. /* initialize ipc object */
  2745. _ipc_object_init(&(mq->parent));
  2746. /* initialize message queue */
  2747. /* get correct message size */
  2748. msg_align_size = RT_ALIGN(msg_size, RT_ALIGN_SIZE);
  2749. mq->msg_size = msg_size;
  2750. mq->max_msgs = max_msgs;
  2751. /* allocate message pool */
  2752. mq->msg_pool = RT_KERNEL_MALLOC((msg_align_size + sizeof(struct rt_mq_message)) * mq->max_msgs);
  2753. if (mq->msg_pool == RT_NULL)
  2754. {
  2755. rt_object_delete(&(mq->parent.parent));
  2756. return RT_NULL;
  2757. }
  2758. /* initialize message list */
  2759. mq->msg_queue_head = RT_NULL;
  2760. mq->msg_queue_tail = RT_NULL;
  2761. /* initialize message empty list */
  2762. mq->msg_queue_free = RT_NULL;
  2763. for (temp = 0; temp < mq->max_msgs; temp ++)
  2764. {
  2765. head = (struct rt_mq_message *)((rt_uint8_t *)mq->msg_pool +
  2766. temp * (msg_align_size + sizeof(struct rt_mq_message)));
  2767. head->next = (struct rt_mq_message *)mq->msg_queue_free;
  2768. mq->msg_queue_free = head;
  2769. }
  2770. /* the initial entry is zero */
  2771. mq->entry = 0;
  2772. /* initialize an additional list of sender suspend thread */
  2773. rt_list_init(&(mq->suspend_sender_thread));
  2774. rt_spin_lock_init(&(mq->spinlock));
  2775. return mq;
  2776. }
  2777. RTM_EXPORT(rt_mq_create);
  2778. /**
  2779. * @brief This function will delete a messagequeue object and release the memory.
  2780. *
  2781. * @note This function is used to delete a messagequeue object which is created by the rt_mq_create() function.
  2782. * By contrast, the rt_mq_detach() function will detach a static messagequeue object.
  2783. * When the messagequeue is successfully deleted, it will resume all suspended threads in the messagequeue list.
  2784. *
  2785. * @see rt_mq_detach()
  2786. *
  2787. * @param mq is a pointer to a messagequeue object to be deleted.
  2788. *
  2789. * @return Return the operation status. When the return value is RT_EOK, the operation is successful.
  2790. * If the return value is any other values, it means that the messagequeue detach failed.
  2791. *
  2792. * @warning This function can ONLY delete a messagequeue initialized by the rt_mq_create() function.
  2793. * If the messagequeue is initialized by the rt_mq_init() function, you MUST NOT USE this function to delete it,
  2794. * ONLY USE the rt_mq_detach() function to complete the detachment.
  2795. * for example,the rt_mq_create() function, it cannot be called in interrupt context.
  2796. */
  2797. rt_err_t rt_mq_delete(rt_mq_t mq)
  2798. {
  2799. /* parameter check */
  2800. RT_ASSERT(mq != RT_NULL);
  2801. RT_ASSERT(rt_object_get_type(&mq->parent.parent) == RT_Object_Class_MessageQueue);
  2802. RT_ASSERT(rt_object_is_systemobject(&mq->parent.parent) == RT_FALSE);
  2803. RT_DEBUG_NOT_IN_INTERRUPT;
  2804. rt_spin_lock(&(mq->spinlock));
  2805. /* resume all suspended thread */
  2806. rt_susp_list_resume_all(&(mq->parent.suspend_thread), RT_ERROR);
  2807. /* also resume all message queue private suspended thread */
  2808. rt_susp_list_resume_all(&(mq->suspend_sender_thread), RT_ERROR);
  2809. rt_spin_unlock(&(mq->spinlock));
  2810. /* free message queue pool */
  2811. RT_KERNEL_FREE(mq->msg_pool);
  2812. /* delete message queue object */
  2813. rt_object_delete(&(mq->parent.parent));
  2814. return RT_EOK;
  2815. }
  2816. RTM_EXPORT(rt_mq_delete);
  2817. #endif /* RT_USING_HEAP */
  2818. /**
  2819. * @brief This function will send a message to the messagequeue object. If
  2820. * there is a thread suspended on the messagequeue, the thread will be
  2821. * resumed.
  2822. *
  2823. * @note When using this function to send a message, if the messagequeue is
  2824. * fully used, the current thread will wait for a timeout. If reaching
  2825. * the timeout and there is still no space available, the sending
  2826. * thread will be resumed and an error code will be returned. By
  2827. * contrast, the _rt_mq_send_wait() function will return an error code
  2828. * immediately without waiting when the messagequeue if fully used.
  2829. *
  2830. * @see _rt_mq_send_wait()
  2831. *
  2832. * @param mq is a pointer to the messagequeue object to be sent.
  2833. *
  2834. * @param buffer is the content of the message.
  2835. *
  2836. * @param size is the length of the message(Unit: Byte).
  2837. *
  2838. * @param prio is message priority, A larger value indicates a higher priority
  2839. *
  2840. * @param timeout is a timeout period (unit: an OS tick).
  2841. *
  2842. * @param suspend_flag status flag of the thread to be suspended.
  2843. *
  2844. * @return Return the operation status. When the return value is RT_EOK, the
  2845. * operation is successful. If the return value is any other values,
  2846. * it means that the messagequeue detach failed.
  2847. *
  2848. * @warning This function can be called in interrupt context and thread
  2849. * context.
  2850. */
  2851. static rt_err_t _rt_mq_send_wait(rt_mq_t mq,
  2852. const void *buffer,
  2853. rt_size_t size,
  2854. rt_int32_t prio,
  2855. rt_int32_t timeout,
  2856. int suspend_flag)
  2857. {
  2858. rt_base_t level;
  2859. struct rt_mq_message *msg;
  2860. rt_uint32_t tick_delta;
  2861. struct rt_thread *thread;
  2862. rt_err_t ret;
  2863. RT_UNUSED(prio);
  2864. /* parameter check */
  2865. RT_ASSERT(mq != RT_NULL);
  2866. RT_ASSERT(rt_object_get_type(&mq->parent.parent) == RT_Object_Class_MessageQueue);
  2867. RT_ASSERT(buffer != RT_NULL);
  2868. RT_ASSERT(size != 0);
  2869. /* current context checking */
  2870. RT_DEBUG_SCHEDULER_AVAILABLE(timeout != 0);
  2871. /* greater than one message size */
  2872. if (size > mq->msg_size)
  2873. return -RT_ERROR;
  2874. /* initialize delta tick */
  2875. tick_delta = 0;
  2876. /* get current thread */
  2877. thread = rt_thread_self();
  2878. RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(mq->parent.parent)));
  2879. level = rt_spin_lock_irqsave(&(mq->spinlock));
  2880. /* get a free list, there must be an empty item */
  2881. msg = (struct rt_mq_message *)mq->msg_queue_free;
  2882. /* for non-blocking call */
  2883. if (msg == RT_NULL && timeout == 0)
  2884. {
  2885. rt_spin_unlock_irqrestore(&(mq->spinlock), level);
  2886. return -RT_EFULL;
  2887. }
  2888. /* message queue is full */
  2889. while ((msg = (struct rt_mq_message *)mq->msg_queue_free) == RT_NULL)
  2890. {
  2891. /* reset error number in thread */
  2892. thread->error = -RT_EINTR;
  2893. /* no waiting, return timeout */
  2894. if (timeout == 0)
  2895. {
  2896. rt_spin_unlock_irqrestore(&(mq->spinlock), level);
  2897. return -RT_EFULL;
  2898. }
  2899. /* suspend current thread */
  2900. ret = rt_thread_suspend_to_list(thread, &(mq->suspend_sender_thread),
  2901. mq->parent.parent.flag, suspend_flag);
  2902. if (ret != RT_EOK)
  2903. {
  2904. rt_spin_unlock_irqrestore(&(mq->spinlock), level);
  2905. return ret;
  2906. }
  2907. /* has waiting time, start thread timer */
  2908. if (timeout > 0)
  2909. {
  2910. /* get the start tick of timer */
  2911. tick_delta = rt_tick_get();
  2912. LOG_D("mq_send_wait: start timer of thread:%s",
  2913. thread->parent.name);
  2914. /* reset the timeout of thread timer and start it */
  2915. rt_timer_control(&(thread->thread_timer),
  2916. RT_TIMER_CTRL_SET_TIME,
  2917. &timeout);
  2918. rt_timer_start(&(thread->thread_timer));
  2919. }
  2920. rt_spin_unlock_irqrestore(&(mq->spinlock), level);
  2921. /* re-schedule */
  2922. rt_schedule();
  2923. /* resume from suspend state */
  2924. if (thread->error != RT_EOK)
  2925. {
  2926. /* return error */
  2927. return thread->error;
  2928. }
  2929. level = rt_spin_lock_irqsave(&(mq->spinlock));
  2930. /* if it's not waiting forever and then re-calculate timeout tick */
  2931. if (timeout > 0)
  2932. {
  2933. tick_delta = rt_tick_get() - tick_delta;
  2934. timeout -= tick_delta;
  2935. if (timeout < 0)
  2936. timeout = 0;
  2937. }
  2938. }
  2939. /* move free list pointer */
  2940. mq->msg_queue_free = msg->next;
  2941. rt_spin_unlock_irqrestore(&(mq->spinlock), level);
  2942. /* the msg is the new tailer of list, the next shall be NULL */
  2943. msg->next = RT_NULL;
  2944. /* add the length */
  2945. ((struct rt_mq_message *)msg)->length = size;
  2946. /* copy buffer */
  2947. rt_memcpy(GET_MESSAGEBYTE_ADDR(msg), buffer, size);
  2948. /* disable interrupt */
  2949. level = rt_spin_lock_irqsave(&(mq->spinlock));
  2950. #ifdef RT_USING_MESSAGEQUEUE_PRIORITY
  2951. msg->prio = prio;
  2952. if (mq->msg_queue_head == RT_NULL)
  2953. mq->msg_queue_head = msg;
  2954. struct rt_mq_message *node, *prev_node = RT_NULL;
  2955. for (node = mq->msg_queue_head; node != RT_NULL; node = node->next)
  2956. {
  2957. if (node->prio < msg->prio)
  2958. {
  2959. if (prev_node == RT_NULL)
  2960. mq->msg_queue_head = msg;
  2961. else
  2962. prev_node->next = msg;
  2963. msg->next = node;
  2964. break;
  2965. }
  2966. if (node->next == RT_NULL)
  2967. {
  2968. if (node != msg)
  2969. node->next = msg;
  2970. mq->msg_queue_tail = msg;
  2971. break;
  2972. }
  2973. prev_node = node;
  2974. }
  2975. #else
  2976. /* link msg to message queue */
  2977. if (mq->msg_queue_tail != RT_NULL)
  2978. {
  2979. /* if the tail exists, */
  2980. ((struct rt_mq_message *)mq->msg_queue_tail)->next = msg;
  2981. }
  2982. /* set new tail */
  2983. mq->msg_queue_tail = msg;
  2984. /* if the head is empty, set head */
  2985. if (mq->msg_queue_head == RT_NULL)
  2986. mq->msg_queue_head = msg;
  2987. #endif
  2988. if(mq->entry < RT_MQ_ENTRY_MAX)
  2989. {
  2990. /* increase message entry */
  2991. mq->entry ++;
  2992. }
  2993. else
  2994. {
  2995. rt_spin_unlock_irqrestore(&(mq->spinlock), level);
  2996. return -RT_EFULL; /* value overflowed */
  2997. }
  2998. /* resume suspended thread */
  2999. if (!rt_list_isempty(&mq->parent.suspend_thread))
  3000. {
  3001. rt_susp_list_dequeue(&(mq->parent.suspend_thread), RT_EOK);
  3002. rt_spin_unlock_irqrestore(&(mq->spinlock), level);
  3003. rt_schedule();
  3004. return RT_EOK;
  3005. }
  3006. rt_spin_unlock_irqrestore(&(mq->spinlock), level);
  3007. return RT_EOK;
  3008. }
  3009. rt_err_t rt_mq_send_wait(rt_mq_t mq,
  3010. const void *buffer,
  3011. rt_size_t size,
  3012. rt_int32_t timeout)
  3013. {
  3014. return _rt_mq_send_wait(mq, buffer, size, 0, timeout, RT_UNINTERRUPTIBLE);
  3015. }
  3016. RTM_EXPORT(rt_mq_send_wait);
  3017. rt_err_t rt_mq_send_wait_interruptible(rt_mq_t mq,
  3018. const void *buffer,
  3019. rt_size_t size,
  3020. rt_int32_t timeout)
  3021. {
  3022. return _rt_mq_send_wait(mq, buffer, size, 0, timeout, RT_INTERRUPTIBLE);
  3023. }
  3024. RTM_EXPORT(rt_mq_send_wait_interruptible);
  3025. rt_err_t rt_mq_send_wait_killable(rt_mq_t mq,
  3026. const void *buffer,
  3027. rt_size_t size,
  3028. rt_int32_t timeout)
  3029. {
  3030. return _rt_mq_send_wait(mq, buffer, size, 0, timeout, RT_KILLABLE);
  3031. }
  3032. RTM_EXPORT(rt_mq_send_wait_killable);
  3033. /**
  3034. * @brief This function will send a message to the messagequeue object.
  3035. * If there is a thread suspended on the messagequeue, the thread will be resumed.
  3036. *
  3037. * @note When using this function to send a message, if the messagequeue is fully used,
  3038. * the current thread will wait for a timeout.
  3039. * By contrast, when the messagequeue is fully used, the rt_mq_send_wait() function will
  3040. * return an error code immediately without waiting.
  3041. *
  3042. * @see rt_mq_send_wait()
  3043. *
  3044. * @param mq is a pointer to the messagequeue object to be sent.
  3045. *
  3046. * @param buffer is the content of the message.
  3047. *
  3048. * @param size is the length of the message(Unit: Byte).
  3049. *
  3050. * @return Return the operation status. When the return value is RT_EOK, the operation is successful.
  3051. * If the return value is any other values, it means that the messagequeue detach failed.
  3052. *
  3053. * @warning This function can be called in interrupt context and thread context.
  3054. */
  3055. rt_err_t rt_mq_send(rt_mq_t mq, const void *buffer, rt_size_t size)
  3056. {
  3057. return rt_mq_send_wait(mq, buffer, size, 0);
  3058. }
  3059. RTM_EXPORT(rt_mq_send);
  3060. rt_err_t rt_mq_send_interruptible(rt_mq_t mq, const void *buffer, rt_size_t size)
  3061. {
  3062. return rt_mq_send_wait_interruptible(mq, buffer, size, 0);
  3063. }
  3064. RTM_EXPORT(rt_mq_send_interruptible);
  3065. rt_err_t rt_mq_send_killable(rt_mq_t mq, const void *buffer, rt_size_t size)
  3066. {
  3067. return rt_mq_send_wait_killable(mq, buffer, size, 0);
  3068. }
  3069. RTM_EXPORT(rt_mq_send_killable);
  3070. /**
  3071. * @brief This function will send an urgent message to the messagequeue object.
  3072. *
  3073. * @note This function is almost the same as the rt_mq_send() function. The only difference is that
  3074. * when sending an urgent message, the message is placed at the head of the messagequeue so that
  3075. * the recipient can receive the urgent message first.
  3076. *
  3077. * @see rt_mq_send()
  3078. *
  3079. * @param mq is a pointer to the messagequeue object to be sent.
  3080. *
  3081. * @param buffer is the content of the message.
  3082. *
  3083. * @param size is the length of the message(Unit: Byte).
  3084. *
  3085. * @return Return the operation status. When the return value is RT_EOK, the operation is successful.
  3086. * If the return value is any other values, it means that the mailbox detach failed.
  3087. */
  3088. rt_err_t rt_mq_urgent(rt_mq_t mq, const void *buffer, rt_size_t size)
  3089. {
  3090. rt_base_t level;
  3091. struct rt_mq_message *msg;
  3092. /* parameter check */
  3093. RT_ASSERT(mq != RT_NULL);
  3094. RT_ASSERT(rt_object_get_type(&mq->parent.parent) == RT_Object_Class_MessageQueue);
  3095. RT_ASSERT(buffer != RT_NULL);
  3096. RT_ASSERT(size != 0);
  3097. /* greater than one message size */
  3098. if (size > mq->msg_size)
  3099. return -RT_ERROR;
  3100. RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(mq->parent.parent)));
  3101. level = rt_spin_lock_irqsave(&(mq->spinlock));
  3102. /* get a free list, there must be an empty item */
  3103. msg = (struct rt_mq_message *)mq->msg_queue_free;
  3104. /* message queue is full */
  3105. if (msg == RT_NULL)
  3106. {
  3107. rt_spin_unlock_irqrestore(&(mq->spinlock), level);
  3108. return -RT_EFULL;
  3109. }
  3110. /* move free list pointer */
  3111. mq->msg_queue_free = msg->next;
  3112. rt_spin_unlock_irqrestore(&(mq->spinlock), level);
  3113. /* add the length */
  3114. ((struct rt_mq_message *)msg)->length = size;
  3115. /* copy buffer */
  3116. rt_memcpy(GET_MESSAGEBYTE_ADDR(msg), buffer, size);
  3117. level = rt_spin_lock_irqsave(&(mq->spinlock));
  3118. /* link msg to the beginning of message queue */
  3119. msg->next = (struct rt_mq_message *)mq->msg_queue_head;
  3120. mq->msg_queue_head = msg;
  3121. /* if there is no tail */
  3122. if (mq->msg_queue_tail == RT_NULL)
  3123. mq->msg_queue_tail = msg;
  3124. if(mq->entry < RT_MQ_ENTRY_MAX)
  3125. {
  3126. /* increase message entry */
  3127. mq->entry ++;
  3128. }
  3129. else
  3130. {
  3131. rt_spin_unlock_irqrestore(&(mq->spinlock), level);
  3132. return -RT_EFULL; /* value overflowed */
  3133. }
  3134. /* resume suspended thread */
  3135. if (!rt_list_isempty(&mq->parent.suspend_thread))
  3136. {
  3137. rt_susp_list_dequeue(&(mq->parent.suspend_thread), RT_EOK);
  3138. rt_spin_unlock_irqrestore(&(mq->spinlock), level);
  3139. rt_schedule();
  3140. return RT_EOK;
  3141. }
  3142. rt_spin_unlock_irqrestore(&(mq->spinlock), level);
  3143. return RT_EOK;
  3144. }
  3145. RTM_EXPORT(rt_mq_urgent);
  3146. /**
  3147. * @brief This function will receive a message from message queue object,
  3148. * if there is no message in messagequeue object, the thread shall wait for a specified time.
  3149. *
  3150. * @note Only when there is mail in the mailbox, the receiving thread can get the mail immediately and return RT_EOK,
  3151. * otherwise the receiving thread will be suspended until timeout.
  3152. * If the mail is not received within the specified time, it will return -RT_ETIMEOUT.
  3153. *
  3154. * @param mq is a pointer to the messagequeue object to be received.
  3155. *
  3156. * @param buffer is the content of the message.
  3157. *
  3158. * @param prio is message priority, A larger value indicates a higher priority
  3159. *
  3160. * @param size is the length of the message(Unit: Byte).
  3161. *
  3162. * @param timeout is a timeout period (unit: an OS tick). If the message is unavailable, the thread will wait for
  3163. * the message in the queue up to the amount of time specified by this parameter.
  3164. *
  3165. * @param suspend_flag status flag of the thread to be suspended.
  3166. *
  3167. * NOTE:
  3168. * If use Macro RT_WAITING_FOREVER to set this parameter, which means that when the
  3169. * message is unavailable in the queue, the thread will be waiting forever.
  3170. * If use macro RT_WAITING_NO to set this parameter, which means that this
  3171. * function is non-blocking and will return immediately.
  3172. *
  3173. * @return Return the real length of the message. When the return value is larger than zero, the operation is successful.
  3174. * If the return value is any other values, it means that the mailbox release failed.
  3175. */
  3176. static rt_ssize_t _rt_mq_recv(rt_mq_t mq,
  3177. void *buffer,
  3178. rt_size_t size,
  3179. rt_int32_t *prio,
  3180. rt_int32_t timeout,
  3181. int suspend_flag)
  3182. {
  3183. struct rt_thread *thread;
  3184. rt_base_t level;
  3185. struct rt_mq_message *msg;
  3186. rt_uint32_t tick_delta;
  3187. rt_err_t ret;
  3188. rt_size_t len;
  3189. RT_UNUSED(prio);
  3190. /* parameter check */
  3191. RT_ASSERT(mq != RT_NULL);
  3192. RT_ASSERT(rt_object_get_type(&mq->parent.parent) == RT_Object_Class_MessageQueue);
  3193. RT_ASSERT(buffer != RT_NULL);
  3194. RT_ASSERT(size != 0);
  3195. /* current context checking */
  3196. RT_DEBUG_SCHEDULER_AVAILABLE(timeout != 0);
  3197. /* initialize delta tick */
  3198. tick_delta = 0;
  3199. /* get current thread */
  3200. thread = rt_thread_self();
  3201. RT_OBJECT_HOOK_CALL(rt_object_trytake_hook, (&(mq->parent.parent)));
  3202. level = rt_spin_lock_irqsave(&(mq->spinlock));
  3203. /* for non-blocking call */
  3204. if (mq->entry == 0 && timeout == 0)
  3205. {
  3206. rt_spin_unlock_irqrestore(&(mq->spinlock), level);
  3207. return -RT_ETIMEOUT;
  3208. }
  3209. /* message queue is empty */
  3210. while (mq->entry == 0)
  3211. {
  3212. /* reset error number in thread */
  3213. thread->error = -RT_EINTR;
  3214. /* no waiting, return timeout */
  3215. if (timeout == 0)
  3216. {
  3217. /* enable interrupt */
  3218. rt_spin_unlock_irqrestore(&(mq->spinlock), level);
  3219. thread->error = -RT_ETIMEOUT;
  3220. return -RT_ETIMEOUT;
  3221. }
  3222. /* suspend current thread */
  3223. ret = rt_thread_suspend_to_list(thread, &(mq->parent.suspend_thread),
  3224. mq->parent.parent.flag, suspend_flag);
  3225. if (ret != RT_EOK)
  3226. {
  3227. rt_spin_unlock_irqrestore(&(mq->spinlock), level);
  3228. return ret;
  3229. }
  3230. /* has waiting time, start thread timer */
  3231. if (timeout > 0)
  3232. {
  3233. /* get the start tick of timer */
  3234. tick_delta = rt_tick_get();
  3235. LOG_D("set thread:%s to timer list",
  3236. thread->parent.name);
  3237. /* reset the timeout of thread timer and start it */
  3238. rt_timer_control(&(thread->thread_timer),
  3239. RT_TIMER_CTRL_SET_TIME,
  3240. &timeout);
  3241. rt_timer_start(&(thread->thread_timer));
  3242. }
  3243. rt_spin_unlock_irqrestore(&(mq->spinlock), level);
  3244. /* re-schedule */
  3245. rt_schedule();
  3246. /* recv message */
  3247. if (thread->error != RT_EOK)
  3248. {
  3249. /* return error */
  3250. return thread->error;
  3251. }
  3252. level = rt_spin_lock_irqsave(&(mq->spinlock));
  3253. /* if it's not waiting forever and then re-calculate timeout tick */
  3254. if (timeout > 0)
  3255. {
  3256. tick_delta = rt_tick_get() - tick_delta;
  3257. timeout -= tick_delta;
  3258. if (timeout < 0)
  3259. timeout = 0;
  3260. }
  3261. }
  3262. /* get message from queue */
  3263. msg = (struct rt_mq_message *)mq->msg_queue_head;
  3264. /* move message queue head */
  3265. mq->msg_queue_head = msg->next;
  3266. /* reach queue tail, set to NULL */
  3267. if (mq->msg_queue_tail == msg)
  3268. mq->msg_queue_tail = RT_NULL;
  3269. /* decrease message entry */
  3270. if(mq->entry > 0)
  3271. {
  3272. mq->entry --;
  3273. }
  3274. rt_spin_unlock_irqrestore(&(mq->spinlock), level);
  3275. /* get real message length */
  3276. len = ((struct rt_mq_message *)msg)->length;
  3277. if (len > size)
  3278. len = size;
  3279. /* copy message */
  3280. rt_memcpy(buffer, GET_MESSAGEBYTE_ADDR(msg), len);
  3281. #ifdef RT_USING_MESSAGEQUEUE_PRIORITY
  3282. if (prio != RT_NULL)
  3283. *prio = msg->prio;
  3284. #endif
  3285. level = rt_spin_lock_irqsave(&(mq->spinlock));
  3286. /* put message to free list */
  3287. msg->next = (struct rt_mq_message *)mq->msg_queue_free;
  3288. mq->msg_queue_free = msg;
  3289. /* resume suspended thread */
  3290. if (!rt_list_isempty(&(mq->suspend_sender_thread)))
  3291. {
  3292. rt_susp_list_dequeue(&(mq->suspend_sender_thread), RT_EOK);
  3293. rt_spin_unlock_irqrestore(&(mq->spinlock), level);
  3294. RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(mq->parent.parent)));
  3295. rt_schedule();
  3296. return len;
  3297. }
  3298. rt_spin_unlock_irqrestore(&(mq->spinlock), level);
  3299. RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(mq->parent.parent)));
  3300. return len;
  3301. }
  3302. rt_ssize_t rt_mq_recv(rt_mq_t mq,
  3303. void *buffer,
  3304. rt_size_t size,
  3305. rt_int32_t timeout)
  3306. {
  3307. return _rt_mq_recv(mq, buffer, size, 0, timeout, RT_UNINTERRUPTIBLE);
  3308. }
  3309. RTM_EXPORT(rt_mq_recv);
  3310. rt_ssize_t rt_mq_recv_interruptible(rt_mq_t mq,
  3311. void *buffer,
  3312. rt_size_t size,
  3313. rt_int32_t timeout)
  3314. {
  3315. return _rt_mq_recv(mq, buffer, size, 0, timeout, RT_INTERRUPTIBLE);
  3316. }
  3317. RTM_EXPORT(rt_mq_recv_interruptible);
  3318. rt_ssize_t rt_mq_recv_killable(rt_mq_t mq,
  3319. void *buffer,
  3320. rt_size_t size,
  3321. rt_int32_t timeout)
  3322. {
  3323. return _rt_mq_recv(mq, buffer, size, 0, timeout, RT_KILLABLE);
  3324. }
  3325. #ifdef RT_USING_MESSAGEQUEUE_PRIORITY
  3326. rt_err_t rt_mq_send_wait_prio(rt_mq_t mq,
  3327. const void *buffer,
  3328. rt_size_t size,
  3329. rt_int32_t prio,
  3330. rt_int32_t timeout,
  3331. int suspend_flag)
  3332. {
  3333. return _rt_mq_send_wait(mq, buffer, size, prio, timeout, suspend_flag);
  3334. }
  3335. rt_ssize_t rt_mq_recv_prio(rt_mq_t mq,
  3336. void *buffer,
  3337. rt_size_t size,
  3338. rt_int32_t *prio,
  3339. rt_int32_t timeout,
  3340. int suspend_flag)
  3341. {
  3342. return _rt_mq_recv(mq, buffer, size, prio, timeout, suspend_flag);
  3343. }
  3344. #endif
  3345. RTM_EXPORT(rt_mq_recv_killable);
  3346. /**
  3347. * @brief This function will set some extra attributions of a messagequeue object.
  3348. *
  3349. * @note Currently this function only supports the RT_IPC_CMD_RESET command to reset the messagequeue.
  3350. *
  3351. * @param mq is a pointer to a messagequeue object.
  3352. *
  3353. * @param cmd is a command used to configure some attributions of the messagequeue.
  3354. *
  3355. * @param arg is the argument of the function to execute the command.
  3356. *
  3357. * @return Return the operation status. When the return value is RT_EOK, the operation is successful.
  3358. * If the return value is any other values, it means that this function failed to execute.
  3359. */
  3360. rt_err_t rt_mq_control(rt_mq_t mq, int cmd, void *arg)
  3361. {
  3362. rt_base_t level;
  3363. struct rt_mq_message *msg;
  3364. RT_UNUSED(arg);
  3365. /* parameter check */
  3366. RT_ASSERT(mq != RT_NULL);
  3367. RT_ASSERT(rt_object_get_type(&mq->parent.parent) == RT_Object_Class_MessageQueue);
  3368. if (cmd == RT_IPC_CMD_RESET)
  3369. {
  3370. level = rt_spin_lock_irqsave(&(mq->spinlock));
  3371. /* resume all waiting thread */
  3372. rt_susp_list_resume_all(&mq->parent.suspend_thread, RT_ERROR);
  3373. /* also resume all message queue private suspended thread */
  3374. rt_susp_list_resume_all(&(mq->suspend_sender_thread), RT_ERROR);
  3375. /* release all message in the queue */
  3376. while (mq->msg_queue_head != RT_NULL)
  3377. {
  3378. /* get message from queue */
  3379. msg = (struct rt_mq_message *)mq->msg_queue_head;
  3380. /* move message queue head */
  3381. mq->msg_queue_head = msg->next;
  3382. /* reach queue tail, set to NULL */
  3383. if (mq->msg_queue_tail == msg)
  3384. mq->msg_queue_tail = RT_NULL;
  3385. /* put message to free list */
  3386. msg->next = (struct rt_mq_message *)mq->msg_queue_free;
  3387. mq->msg_queue_free = msg;
  3388. }
  3389. /* clean entry */
  3390. mq->entry = 0;
  3391. rt_spin_unlock_irqrestore(&(mq->spinlock), level);
  3392. rt_schedule();
  3393. return RT_EOK;
  3394. }
  3395. return -RT_ERROR;
  3396. }
  3397. RTM_EXPORT(rt_mq_control);
  3398. /**@}*/
  3399. #endif /* RT_USING_MESSAGEQUEUE */
  3400. /**@}*/