ipc.c 109 KB

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