ipc.c 110 KB

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