ipc.c 110 KB

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