ipc.c 134 KB

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