ipc.c 126 KB

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