ipc.c 135 KB

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