ipc.c 109 KB

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