ipc.c 118 KB

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