ipc.c 129 KB

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