ipc.c 127 KB

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