ipc.c 135 KB

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