ipc.c 127 KB

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