ipc.c 127 KB

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