ipc.c 108 KB

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