ipc.c 109 KB

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