ipc.c 108 KB

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