ipc.c 118 KB

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