ipc.c 128 KB

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