ipc.c 128 KB

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