ipc.c 108 KB

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