ipc.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161
  1. /*
  2. * File : ipc.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2006-03-14 Bernard the first version
  13. * 2006-04-25 Bernard implement semaphore
  14. * 2006-05-03 Bernard add RT_IPC_DEBUG
  15. * modify the type of IPC waiting time to rt_int32_t
  16. * 2006-05-10 Bernard fix the semaphore take bug and add IPC object
  17. * 2006-05-12 Bernard implement mailbox and message queue
  18. * 2006-05-20 Bernard implement mutex
  19. * 2006-05-23 Bernard implement fast event
  20. * 2006-05-24 Bernard implement event
  21. * 2006-06-03 Bernard fix the thread timer init bug
  22. * 2006-06-05 Bernard fix the mutex release bug
  23. * 2006-06-07 Bernard fix the message queue send bug
  24. * 2006-08-04 Bernard add hook support
  25. * 2009-05-21 Yi.qiu fix the sem release bug
  26. * 2009-07-18 Bernard fix the event clear bug
  27. * 2009-09-09 Bernard remove fast event and fix ipc release bug
  28. * 2009-10-10 Bernard change semaphore and mutex value to unsigned value
  29. * 2009-10-25 Bernard change the mb/mq receive timeout to 0 if the
  30. * re-calculated delta tick is a negative number.
  31. * 2009-12-16 Bernard fix the rt_ipc_object_suspend issue when IPC flag
  32. * is RT_IPC_FLAG_PRIO
  33. * 2010-01-20 mbbill remove rt_ipc_object_decrease function.
  34. * 2010-04-20 Bernard move memcpy outside interrupt disable in mq
  35. * 2010-10-26 yi.qiu add module support in rt_mp_delete and rt_mq_delete
  36. * 2010-11-10 Bernard add IPC reset command implementation.
  37. * 2011-12-18 Bernard add more parameter checking in message queue
  38. */
  39. #include <rtthread.h>
  40. #include <rthw.h>
  41. #include "kservice.h"
  42. #ifdef RT_USING_HOOK
  43. extern void (*rt_object_trytake_hook)(struct rt_object *object);
  44. extern void (*rt_object_take_hook)(struct rt_object *object);
  45. extern void (*rt_object_put_hook)(struct rt_object *object);
  46. #endif
  47. /**
  48. * @addtogroup IPC
  49. */
  50. /*@{*/
  51. /**
  52. * This function will initialize an IPC object
  53. *
  54. * @param ipc the IPC object
  55. *
  56. * @return the operation status, RT_EOK on successful
  57. */
  58. rt_inline rt_err_t rt_ipc_object_init(struct rt_ipc_object *ipc)
  59. {
  60. /* init ipc object */
  61. rt_list_init(&(ipc->suspend_thread));
  62. return RT_EOK;
  63. }
  64. /**
  65. * This function will suspend a thread to a specified list. IPC object or some double-queue
  66. * object (mailbox etc.) contains this kind of list.
  67. *
  68. * @param list the IPC suspended thread list
  69. * @param thread the thread object to be suspended
  70. * @param flag the IPC object flag, which shall be RT_IPC_FLAG_FIFO/RT_IPC_FLAG_PRIO.
  71. *
  72. * @return the operation status, RT_EOK on successful
  73. */
  74. rt_inline rt_err_t rt_ipc_list_suspend(rt_list_t *list, struct rt_thread *thread, rt_uint8_t flag)
  75. {
  76. /* suspend thread */
  77. rt_thread_suspend(thread);
  78. switch (flag)
  79. {
  80. case RT_IPC_FLAG_FIFO:
  81. rt_list_insert_before(list, &(thread->tlist));
  82. break;
  83. case RT_IPC_FLAG_PRIO:
  84. {
  85. struct rt_list_node *n;
  86. struct rt_thread *sthread;
  87. /* find a suitable position */
  88. for (n = list->next; n != list; n = n->next)
  89. {
  90. sthread = rt_list_entry(n, struct rt_thread, tlist);
  91. /* find out */
  92. if (thread->current_priority < sthread->current_priority)
  93. {
  94. /* insert this thread before the sthread */
  95. rt_list_insert_before(&(sthread->tlist), &(thread->tlist));
  96. break;
  97. }
  98. }
  99. /* not found a suitable position, append to the end of suspend_thread list */
  100. if (n == list)
  101. rt_list_insert_before(list, &(thread->tlist));
  102. }
  103. break;
  104. }
  105. return RT_EOK;
  106. }
  107. /**
  108. * This function will resume the first thread in the list of a IPC object:
  109. * - remove the thread from suspend queue of IPC object
  110. * - put the thread into system ready queue
  111. *
  112. * @param list the thread list
  113. *
  114. * @return the operation status, RT_EOK on successful
  115. */
  116. rt_inline rt_err_t rt_ipc_list_resume(rt_list_t *list)
  117. {
  118. struct rt_thread *thread;
  119. /* get thread entry */
  120. thread = rt_list_entry(list->next, struct rt_thread, tlist);
  121. RT_DEBUG_LOG(RT_DEBUG_IPC, ("resume thread:%s\n", thread->name));
  122. /* resume it */
  123. rt_thread_resume(thread);
  124. return RT_EOK;
  125. }
  126. /**
  127. * This function will resume all suspended threads in a list, including
  128. * suspend list of IPC object and private list of mailbox etc.
  129. *
  130. * @param list of the threads to resume
  131. *
  132. * @return the operation status, RT_EOK on successful
  133. */
  134. rt_inline rt_err_t rt_ipc_list_resume_all(rt_list_t *list)
  135. {
  136. struct rt_thread *thread;
  137. register rt_ubase_t temp;
  138. /* wakeup all suspend threads */
  139. while (!rt_list_isempty(list))
  140. {
  141. /* disable interrupt */
  142. temp = rt_hw_interrupt_disable();
  143. /* get next suspend thread */
  144. thread = rt_list_entry(list->next, struct rt_thread, tlist);
  145. /* set error code to RT_ERROR */
  146. thread->error = -RT_ERROR;
  147. /*
  148. * resume thread
  149. * In rt_thread_resume function, it will remove current thread from
  150. * suspend list
  151. */
  152. rt_thread_resume(thread);
  153. /* enable interrupt */
  154. rt_hw_interrupt_enable(temp);
  155. }
  156. return RT_EOK;
  157. }
  158. #ifdef RT_USING_SEMAPHORE
  159. /**
  160. * This function will initialize a semaphore and put it under control of resource
  161. * management.
  162. *
  163. * @param sem the semaphore object
  164. * @param name the name of semaphore
  165. * @param value the init value of semaphore
  166. * @param flag the flag of semaphore
  167. *
  168. * @return the operation status, RT_EOK on successful
  169. */
  170. rt_err_t rt_sem_init(rt_sem_t sem, const char *name, rt_uint32_t value, rt_uint8_t flag)
  171. {
  172. RT_ASSERT(sem != RT_NULL);
  173. /* init object */
  174. rt_object_init(&(sem->parent.parent), RT_Object_Class_Semaphore, name);
  175. /* init ipc object */
  176. rt_ipc_object_init(&(sem->parent));
  177. /* set init value */
  178. sem->value = value;
  179. /* set parent */
  180. sem->parent.parent.flag = flag;
  181. return RT_EOK;
  182. }
  183. /**
  184. * This function will detach a semaphore from resource management
  185. *
  186. * @param sem the semaphore object
  187. *
  188. * @return the operation status, RT_EOK on successful
  189. *
  190. * @see rt_sem_delete
  191. */
  192. rt_err_t rt_sem_detach(rt_sem_t sem)
  193. {
  194. RT_ASSERT(sem != RT_NULL);
  195. /* wakeup all suspend threads */
  196. rt_ipc_list_resume_all(&(sem->parent.suspend_thread));
  197. /* detach semaphore object */
  198. rt_object_detach(&(sem->parent.parent));
  199. return RT_EOK;
  200. }
  201. #ifdef RT_USING_HEAP
  202. /**
  203. * This function will create a semaphore from system resource
  204. *
  205. * @param name the name of semaphore
  206. * @param value the init value of semaphore
  207. * @param flag the flag of semaphore
  208. *
  209. * @return the created semaphore, RT_NULL on error happen
  210. *
  211. * @see rt_sem_init
  212. */
  213. rt_sem_t rt_sem_create(const char *name, rt_uint32_t value, rt_uint8_t flag)
  214. {
  215. rt_sem_t sem;
  216. RT_DEBUG_NOT_IN_INTERRUPT;
  217. /* allocate object */
  218. sem = (rt_sem_t)rt_object_allocate(RT_Object_Class_Semaphore, name);
  219. if (sem == RT_NULL)
  220. return sem;
  221. /* init ipc object */
  222. rt_ipc_object_init(&(sem->parent));
  223. /* set init value */
  224. sem->value = value;
  225. /* set parent */
  226. sem->parent.parent.flag = flag;
  227. return sem;
  228. }
  229. /**
  230. * This function will delete a semaphore object and release the memory
  231. *
  232. * @param sem the semaphore object
  233. *
  234. * @return the error code
  235. *
  236. * @see rt_sem_detach
  237. */
  238. rt_err_t rt_sem_delete(rt_sem_t sem)
  239. {
  240. RT_DEBUG_NOT_IN_INTERRUPT;
  241. RT_ASSERT(sem != RT_NULL);
  242. /* wakeup all suspend threads */
  243. rt_ipc_list_resume_all(&(sem->parent.suspend_thread));
  244. /* delete semaphore object */
  245. rt_object_delete(&(sem->parent.parent));
  246. return RT_EOK;
  247. }
  248. #endif
  249. /**
  250. * This function will take a semaphore, if the semaphore is unavailable, the
  251. * thread shall wait for a specified time.
  252. *
  253. * @param sem the semaphore object
  254. * @param time the waiting time
  255. *
  256. * @return the error code
  257. */
  258. rt_err_t rt_sem_take(rt_sem_t sem, rt_int32_t time)
  259. {
  260. register rt_base_t temp;
  261. struct rt_thread *thread;
  262. RT_ASSERT(sem != RT_NULL);
  263. RT_OBJECT_HOOK_CALL(rt_object_trytake_hook, (&(sem->parent.parent)));
  264. /* disable interrupt */
  265. temp = rt_hw_interrupt_disable();
  266. RT_DEBUG_LOG(RT_DEBUG_IPC,
  267. ("thread %s take sem:%s, which value is: %d\n", rt_thread_self()->name,
  268. ((struct rt_object *)sem)->name, sem->value));
  269. if (sem->value > 0)
  270. {
  271. /* semaphore is available */
  272. sem->value --;
  273. /* enable interrupt */
  274. rt_hw_interrupt_enable(temp);
  275. }
  276. else
  277. {
  278. /* no waiting, return with timeout */
  279. if (time == 0 )
  280. {
  281. rt_hw_interrupt_enable(temp);
  282. return -RT_ETIMEOUT;
  283. }
  284. else
  285. {
  286. /* current context checking */
  287. RT_DEBUG_NOT_IN_INTERRUPT;
  288. /* semaphore is unavailable, push to suspend list */
  289. /* get current thread */
  290. thread = rt_thread_self();
  291. /* reset thread error number */
  292. thread->error = RT_EOK;
  293. RT_DEBUG_LOG(RT_DEBUG_IPC, ("sem take: suspend thread - %s\n", thread->name));
  294. /* suspend thread */
  295. rt_ipc_list_suspend(&(sem->parent.suspend_thread),
  296. thread, sem->parent.parent.flag);
  297. /* has waiting time, start thread timer */
  298. if (time > 0)
  299. {
  300. RT_DEBUG_LOG(RT_DEBUG_IPC, ("set thread:%s to timer list\n", thread->name));
  301. /* reset the timeout of thread timer and start it */
  302. rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &time);
  303. rt_timer_start(&(thread->thread_timer));
  304. }
  305. /* enable interrupt */
  306. rt_hw_interrupt_enable(temp);
  307. /* do schedule */
  308. rt_schedule();
  309. if (thread->error != RT_EOK)
  310. {
  311. return thread->error;
  312. }
  313. }
  314. }
  315. RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(sem->parent.parent)));
  316. return RT_EOK;
  317. }
  318. /**
  319. * This function will try to take a semaphore and immediately return
  320. *
  321. * @param sem the semaphore object
  322. *
  323. * @return the error code
  324. */
  325. rt_err_t rt_sem_trytake(rt_sem_t sem)
  326. {
  327. return rt_sem_take(sem, 0);
  328. }
  329. /**
  330. * This function will release a semaphore, if there are threads suspended on
  331. * semaphore, it will be waked up.
  332. *
  333. * @param sem the semaphore object
  334. *
  335. * @return the error code
  336. */
  337. rt_err_t rt_sem_release(rt_sem_t sem)
  338. {
  339. register rt_base_t temp;
  340. register rt_bool_t need_schedule;
  341. RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(sem->parent.parent)));
  342. need_schedule = RT_FALSE;
  343. /* disable interrupt */
  344. temp = rt_hw_interrupt_disable();
  345. RT_DEBUG_LOG(RT_DEBUG_IPC,
  346. ("thread %s releases sem:%s, which value is: %d\n", rt_thread_self()->name,
  347. ((struct rt_object *)sem)->name, sem->value));
  348. if (!rt_list_isempty(&sem->parent.suspend_thread))
  349. {
  350. /* resume the suspended thread */
  351. rt_ipc_list_resume(&(sem->parent.suspend_thread));
  352. need_schedule = RT_TRUE;
  353. }
  354. else
  355. sem->value ++; /* increase value */
  356. /* enable interrupt */
  357. rt_hw_interrupt_enable(temp);
  358. /* resume a thread, re-schedule */
  359. if (need_schedule == RT_TRUE)
  360. rt_schedule();
  361. return RT_EOK;
  362. }
  363. /**
  364. * This function can get or set some extra attributions of a semaphore object.
  365. *
  366. * @param sem the semaphore object
  367. * @param cmd the execution command
  368. * @param arg the execution argument
  369. *
  370. * @return the error code
  371. */
  372. rt_err_t rt_sem_control(rt_sem_t sem, rt_uint8_t cmd, void *arg)
  373. {
  374. rt_ubase_t level;
  375. RT_ASSERT(sem != RT_NULL);
  376. if (cmd == RT_IPC_CMD_RESET)
  377. {
  378. rt_uint32_t value;
  379. /* get value */
  380. value = (rt_uint32_t)arg;
  381. /* disable interrupt */
  382. level = rt_hw_interrupt_disable();
  383. /* resume all waiting thread */
  384. rt_ipc_list_resume_all(&sem->parent.suspend_thread);
  385. /* set new value */
  386. sem->value = (rt_uint16_t)value;
  387. /* enable interrupt */
  388. rt_hw_interrupt_enable(level);
  389. rt_schedule();
  390. return RT_EOK;
  391. }
  392. return -RT_ERROR;
  393. }
  394. #endif /* end of RT_USING_SEMAPHORE */
  395. #ifdef RT_USING_MUTEX
  396. /**
  397. * This function will initialize a mutex and put it under control of resource
  398. * management.
  399. *
  400. * @param mutex the mutex object
  401. * @param name the name of mutex
  402. * @param flag the flag of mutex
  403. *
  404. * @return the operation status, RT_EOK on successful
  405. */
  406. rt_err_t rt_mutex_init(rt_mutex_t mutex, const char *name, rt_uint8_t flag)
  407. {
  408. RT_ASSERT(mutex != RT_NULL);
  409. /* init object */
  410. rt_object_init(&(mutex->parent.parent), RT_Object_Class_Mutex, name);
  411. /* init ipc object */
  412. rt_ipc_object_init(&(mutex->parent));
  413. mutex->value = 1;
  414. mutex->owner = RT_NULL;
  415. mutex->original_priority = 0xFF;
  416. mutex->hold = 0;
  417. /* set flag */
  418. mutex->parent.parent.flag = flag;
  419. return RT_EOK;
  420. }
  421. /**
  422. * This function will detach a mutex from resource management
  423. *
  424. * @param mutex the mutex object
  425. *
  426. * @return the operation status, RT_EOK on successful
  427. *
  428. * @see rt_mutex_delete
  429. */
  430. rt_err_t rt_mutex_detach(rt_mutex_t mutex)
  431. {
  432. RT_ASSERT(mutex != RT_NULL);
  433. /* wakeup all suspend threads */
  434. rt_ipc_list_resume_all(&(mutex->parent.suspend_thread));
  435. /* detach semaphore object */
  436. rt_object_detach(&(mutex->parent.parent));
  437. return RT_EOK;
  438. }
  439. #ifdef RT_USING_HEAP
  440. /**
  441. * This function will create a mutex from system resource
  442. *
  443. * @param name the name of mutex
  444. * @param flag the flag of mutex
  445. *
  446. * @return the created mutex, RT_NULL on error happen
  447. *
  448. * @see rt_mutex_init
  449. */
  450. rt_mutex_t rt_mutex_create(const char *name, rt_uint8_t flag)
  451. {
  452. struct rt_mutex *mutex;
  453. RT_DEBUG_NOT_IN_INTERRUPT;
  454. /* allocate object */
  455. mutex = (rt_mutex_t)rt_object_allocate(RT_Object_Class_Mutex, name);
  456. if (mutex == RT_NULL)
  457. return mutex;
  458. /* init ipc object */
  459. rt_ipc_object_init(&(mutex->parent));
  460. mutex->value = 1;
  461. mutex->owner = RT_NULL;
  462. mutex->original_priority = 0xFF;
  463. mutex->hold = 0;
  464. /* set flag */
  465. mutex->parent.parent.flag = flag;
  466. return mutex;
  467. }
  468. /**
  469. * This function will delete a mutex object and release the memory
  470. *
  471. * @param mutex the mutex object
  472. *
  473. * @return the error code
  474. *
  475. * @see rt_mutex_detach
  476. */
  477. rt_err_t rt_mutex_delete(rt_mutex_t mutex)
  478. {
  479. RT_DEBUG_NOT_IN_INTERRUPT;
  480. RT_ASSERT(mutex != RT_NULL);
  481. /* wakeup all suspend threads */
  482. rt_ipc_list_resume_all(&(mutex->parent.suspend_thread));
  483. /* delete semaphore object */
  484. rt_object_delete(&(mutex->parent.parent));
  485. return RT_EOK;
  486. }
  487. #endif
  488. /**
  489. * This function will take a mutex, if the mutex is unavailable, the
  490. * thread shall wait for a specified time.
  491. *
  492. * @param mutex the mutex object
  493. * @param time the waiting time
  494. *
  495. * @return the error code
  496. */
  497. rt_err_t rt_mutex_take(rt_mutex_t mutex, rt_int32_t time)
  498. {
  499. register rt_base_t temp;
  500. struct rt_thread *thread;
  501. /* this function must not be used in interrupt even if time = 0 */
  502. RT_DEBUG_NOT_IN_INTERRUPT;
  503. RT_ASSERT(mutex != RT_NULL);
  504. /* disable interrupt */
  505. temp = rt_hw_interrupt_disable();
  506. /* get current thread */
  507. thread = rt_thread_self();
  508. RT_OBJECT_HOOK_CALL(rt_object_trytake_hook, (&(mutex->parent.parent)));
  509. RT_DEBUG_LOG(RT_DEBUG_IPC,
  510. ("mutex_take: current thread %s, mutex value: %d, hold: %d\n",
  511. thread->name, mutex->value, mutex->hold));
  512. /* reset thread error */
  513. thread->error = RT_EOK;
  514. if (mutex->owner == thread)
  515. {
  516. /* it's the same thread */
  517. mutex->hold ++;
  518. }
  519. else
  520. {
  521. /* The value of mutex is 1 in initial status. Therefore, if the
  522. * value is great than 0, it indicates the mutex is avaible.
  523. */
  524. if (mutex->value > 0)
  525. {
  526. /* mutex is available */
  527. mutex->value --;
  528. /* set mutex owner and original priority */
  529. mutex->owner = thread;
  530. mutex->original_priority = thread->current_priority;
  531. mutex->hold ++;
  532. }
  533. else
  534. {
  535. /* no waiting, return with timeout */
  536. if (time == 0)
  537. {
  538. /* set error as timeout */
  539. thread->error = -RT_ETIMEOUT;
  540. /* enable interrupt */
  541. rt_hw_interrupt_enable(temp);
  542. return -RT_ETIMEOUT;
  543. }
  544. else
  545. {
  546. /* mutex is unavailable, push to suspend list */
  547. RT_DEBUG_LOG(RT_DEBUG_IPC,
  548. ("mutex_take: suspend thread: %s\n", thread->name));
  549. /* change the owner thread priority of mutex */
  550. if (thread->current_priority < mutex->owner->current_priority)
  551. {
  552. /* change the owner thread priority */
  553. rt_thread_control(mutex->owner, RT_THREAD_CTRL_CHANGE_PRIORITY,
  554. &thread->current_priority);
  555. }
  556. /* suspend current thread */
  557. rt_ipc_list_suspend(&(mutex->parent.suspend_thread),
  558. thread, mutex->parent.parent.flag);
  559. /* has waiting time, start thread timer */
  560. if (time > 0)
  561. {
  562. RT_DEBUG_LOG(RT_DEBUG_IPC,
  563. ("mutex_take: start the timer of thread:%s\n", thread->name));
  564. /* reset the timeout of thread timer and start it */
  565. rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &time);
  566. rt_timer_start(&(thread->thread_timer));
  567. }
  568. /* enable interrupt */
  569. rt_hw_interrupt_enable(temp);
  570. /* do schedule */
  571. rt_schedule();
  572. if (thread->error != RT_EOK)
  573. {
  574. /* return error */
  575. return thread->error;
  576. }
  577. else
  578. {
  579. /* the mutex is taken successfully. */
  580. /* disable interrupt */
  581. temp = rt_hw_interrupt_disable();
  582. }
  583. }
  584. }
  585. }
  586. /* enable interrupt */
  587. rt_hw_interrupt_enable(temp);
  588. RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(mutex->parent.parent)));
  589. return RT_EOK;
  590. }
  591. /**
  592. * This function will release a mutex, if there are threads suspended on mutex,
  593. * it will be waked up.
  594. *
  595. * @param mutex the mutex object
  596. *
  597. * @return the error code
  598. */
  599. rt_err_t rt_mutex_release(rt_mutex_t mutex)
  600. {
  601. register rt_base_t temp;
  602. struct rt_thread *thread;
  603. rt_bool_t need_schedule;
  604. need_schedule = RT_FALSE;
  605. /* get current thread */
  606. thread = rt_thread_self();
  607. /* disable interrupt */
  608. temp = rt_hw_interrupt_disable();
  609. RT_DEBUG_LOG(RT_DEBUG_IPC,
  610. ("mutex_release:current thread %s, mutex value: %d, hold: %d\n",
  611. thread->name, mutex->value, mutex->hold));
  612. RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(mutex->parent.parent)));
  613. /* mutex only can be released by owner */
  614. if (thread != mutex->owner)
  615. {
  616. thread->error = -RT_ERROR;
  617. /* enable interrupt */
  618. rt_hw_interrupt_enable(temp);
  619. return -RT_ERROR;
  620. }
  621. /* decrease hold */
  622. mutex->hold --;
  623. /* if no hold */
  624. if (mutex->hold == 0)
  625. {
  626. /* change the owner thread to original priority */
  627. if (mutex->original_priority != mutex->owner->current_priority)
  628. {
  629. rt_thread_control(mutex->owner, RT_THREAD_CTRL_CHANGE_PRIORITY,
  630. &(mutex->original_priority));
  631. }
  632. /* wakeup suspended thread */
  633. if (!rt_list_isempty(&mutex->parent.suspend_thread))
  634. {
  635. /* get suspended thread */
  636. thread = rt_list_entry(mutex->parent.suspend_thread.next, struct rt_thread, tlist);
  637. RT_DEBUG_LOG(RT_DEBUG_IPC, ("mutex_release: resume thread: %s\n", thread->name));
  638. /* set new owner and priority */
  639. mutex->owner = thread;
  640. mutex->original_priority = thread->current_priority;
  641. mutex->hold ++;
  642. /* resume thread */
  643. rt_ipc_list_resume(&(mutex->parent.suspend_thread));
  644. need_schedule = RT_TRUE;
  645. }
  646. else
  647. {
  648. /* increase value */
  649. mutex->value ++;
  650. /* clear owner */
  651. mutex->owner = RT_NULL;
  652. mutex->original_priority = 0xff;
  653. }
  654. }
  655. /* enable interrupt */
  656. rt_hw_interrupt_enable(temp);
  657. /* perform a schedule */
  658. if (need_schedule == RT_TRUE)
  659. rt_schedule();
  660. return RT_EOK;
  661. }
  662. /**
  663. * This function can get or set some extra attributions of a mutex object.
  664. *
  665. * @param mutex the mutex object
  666. * @param cmd the execution command
  667. * @param arg the execution argument
  668. *
  669. * @return the error code
  670. */
  671. rt_err_t rt_mutex_control(rt_mutex_t mutex, rt_uint8_t cmd, void *arg)
  672. {
  673. return -RT_ERROR;
  674. }
  675. #endif /* end of RT_USING_MUTEX */
  676. #ifdef RT_USING_EVENT
  677. /**
  678. * This function will initialize an event and put it under control of resource
  679. * management.
  680. *
  681. * @param event the event object
  682. * @param name the name of event
  683. * @param flag the flag of event
  684. *
  685. * @return the operation status, RT_EOK on successful
  686. */
  687. rt_err_t rt_event_init(rt_event_t event, const char *name, rt_uint8_t flag)
  688. {
  689. RT_ASSERT(event != RT_NULL);
  690. /* init object */
  691. rt_object_init(&(event->parent.parent), RT_Object_Class_Event, name);
  692. /* set parent flag */
  693. event->parent.parent.flag = flag;
  694. /* init ipc object */
  695. rt_ipc_object_init(&(event->parent));
  696. /* init event */
  697. event->set = 0;
  698. return RT_EOK;
  699. }
  700. /**
  701. * This function will detach an event object from resource management
  702. *
  703. * @param event the event object
  704. *
  705. * @return the operation status, RT_EOK on successful
  706. */
  707. rt_err_t rt_event_detach(rt_event_t event)
  708. {
  709. /* parameter check */
  710. RT_ASSERT(event != RT_NULL);
  711. /* resume all suspended thread */
  712. rt_ipc_list_resume_all(&(event->parent.suspend_thread));
  713. /* detach event object */
  714. rt_object_detach(&(event->parent.parent));
  715. return RT_EOK;
  716. }
  717. #ifdef RT_USING_HEAP
  718. /**
  719. * This function will create an event object from system resource
  720. *
  721. * @param name the name of event
  722. * @param flag the flag of event
  723. *
  724. * @return the created event, RT_NULL on error happen
  725. */
  726. rt_event_t rt_event_create(const char *name, rt_uint8_t flag)
  727. {
  728. rt_event_t event;
  729. RT_DEBUG_NOT_IN_INTERRUPT;
  730. /* allocate object */
  731. event = (rt_event_t)rt_object_allocate(RT_Object_Class_Event, name);
  732. if (event == RT_NULL)
  733. return event;
  734. /* set parent */
  735. event->parent.parent.flag = flag;
  736. /* init ipc object */
  737. rt_ipc_object_init(&(event->parent));
  738. /* init event */
  739. event->set = 0;
  740. return event;
  741. }
  742. /**
  743. * This function will delete an event object and release the memory
  744. *
  745. * @param event the event object
  746. *
  747. * @return the error code
  748. */
  749. rt_err_t rt_event_delete(rt_event_t event)
  750. {
  751. /* parameter check */
  752. RT_ASSERT(event != RT_NULL);
  753. RT_DEBUG_NOT_IN_INTERRUPT;
  754. /* resume all suspended thread */
  755. rt_ipc_list_resume_all(&(event->parent.suspend_thread));
  756. /* delete event object */
  757. rt_object_delete(&(event->parent.parent));
  758. return RT_EOK;
  759. }
  760. #endif
  761. /**
  762. * This function will send an event to the event object, if there are threads
  763. * suspended on event object, it will be waked up.
  764. *
  765. * @param event the event object
  766. * @param set the event set
  767. *
  768. * @return the error code
  769. */
  770. rt_err_t rt_event_send(rt_event_t event, rt_uint32_t set)
  771. {
  772. struct rt_list_node *n;
  773. struct rt_thread *thread;
  774. register rt_ubase_t level;
  775. register rt_base_t status;
  776. rt_bool_t need_schedule;
  777. /* parameter check */
  778. RT_ASSERT(event != RT_NULL);
  779. if (set == 0)
  780. return -RT_ERROR;
  781. need_schedule = RT_FALSE;
  782. RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(event->parent.parent)));
  783. /* disable interrupt */
  784. level = rt_hw_interrupt_disable();
  785. /* set event */
  786. event->set |= set;
  787. if (!rt_list_isempty(&event->parent.suspend_thread))
  788. {
  789. /* search thread list to resume thread */
  790. n = event->parent.suspend_thread.next;
  791. while (n != &(event->parent.suspend_thread))
  792. {
  793. /* get thread */
  794. thread = rt_list_entry(n, struct rt_thread, tlist);
  795. status = -RT_ERROR;
  796. if (thread->event_info & RT_EVENT_FLAG_AND)
  797. {
  798. if ((thread->event_set & event->set) == thread->event_set)
  799. {
  800. /* received an AND event */
  801. status = RT_EOK;
  802. }
  803. }
  804. else if (thread->event_info & RT_EVENT_FLAG_OR)
  805. {
  806. if (thread->event_set & event->set)
  807. {
  808. /* save recieved event set */
  809. thread->event_set = thread->event_set & event->set;
  810. /* received an OR event */
  811. status = RT_EOK;
  812. }
  813. }
  814. /* move node to the next */
  815. n = n->next;
  816. /* condition is satisfied, resume thread */
  817. if (status == RT_EOK)
  818. {
  819. /* clear event */
  820. if (thread->event_info & RT_EVENT_FLAG_CLEAR)
  821. event->set &= ~thread->event_set;
  822. /* resume thread, and thread list breaks out */
  823. rt_thread_resume(thread);
  824. /* need do a scheduling */
  825. need_schedule = RT_TRUE;
  826. }
  827. }
  828. }
  829. /* enable interrupt */
  830. rt_hw_interrupt_enable(level);
  831. /* do a schedule */
  832. if (need_schedule == RT_TRUE)
  833. rt_schedule();
  834. return RT_EOK;
  835. }
  836. /**
  837. * This function will receive an event from event object, if the event is unavailable,
  838. * the thread shall wait for a specified time.
  839. *
  840. * @param event the fast event object
  841. * @param set the interested event set
  842. * @param option the receive option
  843. * @param timeout the waiting time
  844. * @param recved the received event
  845. *
  846. * @return the error code
  847. */
  848. rt_err_t rt_event_recv(rt_event_t event, rt_uint32_t set, rt_uint8_t option, rt_int32_t timeout, rt_uint32_t *recved)
  849. {
  850. struct rt_thread *thread;
  851. register rt_ubase_t level;
  852. register rt_base_t status;
  853. RT_DEBUG_NOT_IN_INTERRUPT;
  854. /* parameter check */
  855. RT_ASSERT(event != RT_NULL);
  856. if (set == 0)
  857. return -RT_ERROR;
  858. /* init status */
  859. status = -RT_ERROR;
  860. /* get current thread */
  861. thread = rt_thread_self();
  862. /* reset thread error */
  863. thread->error = RT_EOK;
  864. RT_OBJECT_HOOK_CALL(rt_object_trytake_hook, (&(event->parent.parent)));
  865. /* disable interrupt */
  866. level = rt_hw_interrupt_disable();
  867. /* check event set */
  868. if (option & RT_EVENT_FLAG_AND)
  869. {
  870. if ((event->set & set) == set)
  871. status = RT_EOK;
  872. }
  873. else if (option & RT_EVENT_FLAG_OR)
  874. {
  875. if (event->set & set)
  876. status = RT_EOK;
  877. }
  878. if (status == RT_EOK)
  879. {
  880. /* set received event */
  881. *recved = (event->set & set);
  882. /* received event */
  883. if (option & RT_EVENT_FLAG_CLEAR)
  884. event->set &= ~set;
  885. }
  886. else if (timeout == 0)
  887. {
  888. /* no waiting */
  889. thread->error = -RT_ETIMEOUT;
  890. }
  891. else
  892. {
  893. /* fill thread event info */
  894. thread->event_set = set;
  895. thread->event_info = option;
  896. /* put thread to suspended thread list */
  897. rt_ipc_list_suspend(&(event->parent.suspend_thread), thread, event->parent.parent.flag);
  898. /* if there is a waiting timeout, active thread timer */
  899. if (timeout > 0)
  900. {
  901. /* reset the timeout of thread timer and start it */
  902. rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &timeout);
  903. rt_timer_start(&(thread->thread_timer));
  904. }
  905. /* enable interrupt */
  906. rt_hw_interrupt_enable(level);
  907. /* do a schedule */
  908. rt_schedule();
  909. if (thread->error != RT_EOK)
  910. {
  911. /* return error */
  912. return thread->error;
  913. }
  914. /* received an event, disable interrupt to protect */
  915. level = rt_hw_interrupt_disable();
  916. /* set received event */
  917. *recved = thread->event_set;
  918. }
  919. /* enable interrupt */
  920. rt_hw_interrupt_enable(level);
  921. RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(event->parent.parent)));
  922. return thread->error;
  923. }
  924. /**
  925. * This function can get or set some extra attributions of an event object.
  926. *
  927. * @param event the event object
  928. * @param cmd the execution command
  929. * @param arg the execution argument
  930. *
  931. * @return the error code
  932. */
  933. rt_err_t rt_event_control(rt_event_t event, rt_uint8_t cmd, void *arg)
  934. {
  935. rt_ubase_t level;
  936. RT_ASSERT(event != RT_NULL);
  937. if (cmd == RT_IPC_CMD_RESET)
  938. {
  939. /* disable interrupt */
  940. level = rt_hw_interrupt_disable();
  941. /* resume all waiting thread */
  942. rt_ipc_list_resume_all(&event->parent.suspend_thread);
  943. /* init event set */
  944. event->set = 0;
  945. /* enable interrupt */
  946. rt_hw_interrupt_enable(level);
  947. rt_schedule();
  948. return RT_EOK;
  949. }
  950. return -RT_ERROR;
  951. }
  952. #endif /* end of RT_USING_EVENT */
  953. #ifdef RT_USING_MAILBOX
  954. /**
  955. * This function will initialize a mailbox and put it under control of resource
  956. * management.
  957. *
  958. * @param mb the mailbox object
  959. * @param name the name of mailbox
  960. * @param msgpool the begin address of buffer to save received mail
  961. * @param size the size of mailbox
  962. * @param flag the flag of mailbox
  963. *
  964. * @return the operation status, RT_EOK on successful
  965. */
  966. rt_err_t rt_mb_init(rt_mailbox_t mb, const char *name, void *msgpool, rt_size_t size, rt_uint8_t flag)
  967. {
  968. RT_ASSERT(mb != RT_NULL);
  969. /* init object */
  970. rt_object_init(&(mb->parent.parent), RT_Object_Class_MailBox, name);
  971. /* set parent flag */
  972. mb->parent.parent.flag = flag;
  973. /* init ipc object */
  974. rt_ipc_object_init(&(mb->parent));
  975. /* init mailbox */
  976. mb->msg_pool = msgpool;
  977. mb->size = size;
  978. mb->entry = 0;
  979. mb->in_offset = 0;
  980. mb->out_offset = 0;
  981. /* init an additional list of sender suspend thread */
  982. rt_list_init(&(mb->suspend_sender_thread));
  983. return RT_EOK;
  984. }
  985. /**
  986. * This function will detach a mailbox from resource management
  987. *
  988. * @param mb the mailbox object
  989. *
  990. * @return the operation status, RT_EOK on successful
  991. */
  992. rt_err_t rt_mb_detach(rt_mailbox_t mb)
  993. {
  994. /* parameter check */
  995. RT_ASSERT(mb != RT_NULL);
  996. /* resume all suspended thread */
  997. rt_ipc_list_resume_all(&(mb->parent.suspend_thread));
  998. /* also resume all mailbox private suspended thread */
  999. rt_ipc_list_resume_all(&(mb->suspend_sender_thread));
  1000. /* detach mailbox object */
  1001. rt_object_detach(&(mb->parent.parent));
  1002. return RT_EOK;
  1003. }
  1004. #ifdef RT_USING_HEAP
  1005. /**
  1006. * This function will create a mailbox object from system resource
  1007. *
  1008. * @param name the name of mailbox
  1009. * @param size the size of mailbox
  1010. * @param flag the flag of mailbox
  1011. *
  1012. * @return the created mailbox, RT_NULL on error happen
  1013. */
  1014. rt_mailbox_t rt_mb_create(const char *name, rt_size_t size, rt_uint8_t flag)
  1015. {
  1016. rt_mailbox_t mb;
  1017. RT_DEBUG_NOT_IN_INTERRUPT;
  1018. /* allocate object */
  1019. mb = (rt_mailbox_t)rt_object_allocate(RT_Object_Class_MailBox, name);
  1020. if (mb == RT_NULL)
  1021. return mb;
  1022. /* set parent */
  1023. mb->parent.parent.flag = flag;
  1024. /* init ipc object */
  1025. rt_ipc_object_init(&(mb->parent));
  1026. /* init mailbox */
  1027. mb->size = size;
  1028. mb->msg_pool = rt_malloc(mb->size * sizeof(rt_uint32_t));
  1029. if (mb->msg_pool == RT_NULL)
  1030. {
  1031. /* delete mailbox object */
  1032. rt_object_delete(&(mb->parent.parent));
  1033. return RT_NULL;
  1034. }
  1035. mb->entry = 0;
  1036. mb->in_offset = 0;
  1037. mb->out_offset = 0;
  1038. /* init an additional list of sender suspend thread */
  1039. rt_list_init(&(mb->suspend_sender_thread));
  1040. return mb;
  1041. }
  1042. /**
  1043. * This function will delete a mailbox object and release the memory
  1044. *
  1045. * @param mb the mailbox object
  1046. *
  1047. * @return the error code
  1048. */
  1049. rt_err_t rt_mb_delete(rt_mailbox_t mb)
  1050. {
  1051. RT_DEBUG_NOT_IN_INTERRUPT;
  1052. /* parameter check */
  1053. RT_ASSERT(mb != RT_NULL);
  1054. /* resume all suspended thread */
  1055. rt_ipc_list_resume_all(&(mb->parent.suspend_thread));
  1056. /* also resume all mailbox private suspended thread */
  1057. rt_ipc_list_resume_all(&(mb->suspend_sender_thread));
  1058. #if defined(RT_USING_MODULE) && defined(RT_USING_SLAB)
  1059. /* the mb object belongs to an application module */
  1060. if (mb->parent.parent.flag & RT_OBJECT_FLAG_MODULE)
  1061. rt_module_free(mb->parent.parent.module_id, mb->msg_pool);
  1062. else
  1063. #endif
  1064. /* free mailbox pool */
  1065. rt_free(mb->msg_pool);
  1066. /* delete mailbox object */
  1067. rt_object_delete(&(mb->parent.parent));
  1068. return RT_EOK;
  1069. }
  1070. #endif
  1071. /**
  1072. * This function will send a mail to mailbox object. If the mailbox is full,
  1073. * current thread will be suspended until timeout.
  1074. *
  1075. * @param mb the mailbox object
  1076. * @param value the mail
  1077. * @param timeout the waiting time
  1078. *
  1079. * @return the error code
  1080. */
  1081. rt_err_t rt_mb_send_wait(rt_mailbox_t mb, rt_uint32_t value, rt_int32_t timeout)
  1082. {
  1083. struct rt_thread *thread;
  1084. register rt_ubase_t temp;
  1085. rt_uint32_t tick_delta;
  1086. /* parameter check */
  1087. RT_ASSERT(mb != RT_NULL);
  1088. /* initialize delta tick */
  1089. tick_delta = 0;
  1090. /* get current thread */
  1091. thread = rt_thread_self();
  1092. RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(mb->parent.parent)));
  1093. /* disable interrupt */
  1094. temp = rt_hw_interrupt_disable();
  1095. /* for non-blocking call */
  1096. if (mb->entry == mb->size && timeout == 0)
  1097. {
  1098. rt_hw_interrupt_enable(temp);
  1099. return -RT_EFULL;
  1100. }
  1101. /* mailbox is full */
  1102. while (mb->entry == mb->size)
  1103. {
  1104. /* reset error number in thread */
  1105. thread->error = RT_EOK;
  1106. /* no waiting, return timeout */
  1107. if (timeout == 0)
  1108. {
  1109. /* enable interrupt */
  1110. rt_hw_interrupt_enable(temp);
  1111. return -RT_EFULL;
  1112. }
  1113. RT_DEBUG_NOT_IN_INTERRUPT;
  1114. /* suspend current thread */
  1115. rt_ipc_list_suspend(&(mb->suspend_sender_thread), thread, mb->parent.parent.flag);
  1116. /* has waiting time, start thread timer */
  1117. if (timeout > 0)
  1118. {
  1119. /* get the start tick of timer */
  1120. tick_delta = rt_tick_get();
  1121. RT_DEBUG_LOG(RT_DEBUG_IPC,
  1122. ("mb_send_wait: start timer of thread:%s\n", thread->name));
  1123. /* reset the timeout of thread timer and start it */
  1124. rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &timeout);
  1125. rt_timer_start(&(thread->thread_timer));
  1126. }
  1127. /* enable interrupt */
  1128. rt_hw_interrupt_enable(temp);
  1129. /* re-schedule */
  1130. rt_schedule();
  1131. /* resume from suspend state */
  1132. if (thread->error != RT_EOK)
  1133. {
  1134. /* return error */
  1135. return thread->error;
  1136. }
  1137. /* disable interrupt */
  1138. temp = rt_hw_interrupt_disable();
  1139. /* if it's not waiting forever and then re-calculate timeout tick */
  1140. if (timeout > 0)
  1141. {
  1142. tick_delta = rt_tick_get() - tick_delta;
  1143. timeout -= tick_delta;
  1144. if (timeout < 0)
  1145. timeout = 0;
  1146. }
  1147. }
  1148. /* set ptr */
  1149. mb->msg_pool[mb->in_offset] = value;
  1150. /* increase input offset */
  1151. ++ mb->in_offset;
  1152. if (mb->in_offset >= mb->size)
  1153. mb->in_offset = 0;
  1154. /* increase message entry */
  1155. mb->entry ++;
  1156. /* resume suspended thread */
  1157. if (!rt_list_isempty(&mb->parent.suspend_thread))
  1158. {
  1159. rt_ipc_list_resume(&(mb->parent.suspend_thread));
  1160. /* enable interrupt */
  1161. rt_hw_interrupt_enable(temp);
  1162. rt_schedule();
  1163. return RT_EOK;
  1164. }
  1165. /* enable interrupt */
  1166. rt_hw_interrupt_enable(temp);
  1167. return RT_EOK;
  1168. }
  1169. /**
  1170. * This function will send a mail to mailbox object, if there are threads suspended
  1171. * on mailbox object, it will be waked up. This function will return immediately, if
  1172. * you want blocking send, use rt_mb_send_wait instead.
  1173. *
  1174. * @param mb the mailbox object
  1175. * @param value the mail
  1176. *
  1177. * @return the error code
  1178. */
  1179. rt_err_t rt_mb_send(rt_mailbox_t mb, rt_uint32_t value)
  1180. {
  1181. return rt_mb_send_wait(mb, value, 0);
  1182. }
  1183. /**
  1184. * This function will receive a mail from mailbox object, if there is no mail in
  1185. * mailbox object, the thread shall wait for a specified time.
  1186. *
  1187. * @param mb the mailbox object
  1188. * @param value the received mail will be saved in
  1189. * @param timeout the waiting time
  1190. *
  1191. * @return the error code
  1192. */
  1193. rt_err_t rt_mb_recv(rt_mailbox_t mb, rt_uint32_t *value, rt_int32_t timeout)
  1194. {
  1195. struct rt_thread *thread;
  1196. register rt_ubase_t temp;
  1197. rt_uint32_t tick_delta;
  1198. /* parameter check */
  1199. RT_ASSERT(mb != RT_NULL);
  1200. /* initialize delta tick */
  1201. tick_delta = 0;
  1202. /* get current thread */
  1203. thread = rt_thread_self();
  1204. RT_OBJECT_HOOK_CALL(rt_object_trytake_hook, (&(mb->parent.parent)));
  1205. /* disable interrupt */
  1206. temp = rt_hw_interrupt_disable();
  1207. /* for non-blocking call */
  1208. if (mb->entry == 0 && timeout == 0)
  1209. {
  1210. rt_hw_interrupt_enable(temp);
  1211. return -RT_ETIMEOUT;
  1212. }
  1213. /* mailbox is empty */
  1214. while (mb->entry == 0)
  1215. {
  1216. /* reset error number in thread */
  1217. thread->error = RT_EOK;
  1218. /* no waiting, return timeout */
  1219. if (timeout == 0)
  1220. {
  1221. /* enable interrupt */
  1222. rt_hw_interrupt_enable(temp);
  1223. thread->error = -RT_ETIMEOUT;
  1224. return -RT_ETIMEOUT;
  1225. }
  1226. RT_DEBUG_NOT_IN_INTERRUPT;
  1227. /* suspend current thread */
  1228. rt_ipc_list_suspend(&(mb->parent.suspend_thread), thread, mb->parent.parent.flag);
  1229. /* has waiting time, start thread timer */
  1230. if (timeout > 0)
  1231. {
  1232. /* get the start tick of timer */
  1233. tick_delta = rt_tick_get();
  1234. RT_DEBUG_LOG(RT_DEBUG_IPC,
  1235. ("mb_recv: start timer of thread:%s\n", thread->name));
  1236. /* reset the timeout of thread timer and start it */
  1237. rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &timeout);
  1238. rt_timer_start(&(thread->thread_timer));
  1239. }
  1240. /* enable interrupt */
  1241. rt_hw_interrupt_enable(temp);
  1242. /* re-schedule */
  1243. rt_schedule();
  1244. /* resume from suspend state */
  1245. if (thread->error != RT_EOK)
  1246. {
  1247. /* return error */
  1248. return thread->error;
  1249. }
  1250. /* disable interrupt */
  1251. temp = rt_hw_interrupt_disable();
  1252. /* if it's not waiting forever and then re-calculate timeout tick */
  1253. if (timeout > 0)
  1254. {
  1255. tick_delta = rt_tick_get() - tick_delta;
  1256. timeout -= tick_delta;
  1257. if (timeout < 0)
  1258. timeout = 0;
  1259. }
  1260. }
  1261. /* fill ptr */
  1262. *value = mb->msg_pool[mb->out_offset];
  1263. /* increase output offset */
  1264. ++ mb->out_offset;
  1265. if (mb->out_offset >= mb->size)
  1266. mb->out_offset = 0;
  1267. /* decrease message entry */
  1268. mb->entry --;
  1269. /* resume suspended thread */
  1270. if (!rt_list_isempty(&(mb->suspend_sender_thread)))
  1271. {
  1272. rt_ipc_list_resume(&(mb->suspend_sender_thread));
  1273. /* enable interrupt */
  1274. rt_hw_interrupt_enable(temp);
  1275. RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(mb->parent.parent)));
  1276. rt_schedule();
  1277. return RT_EOK;
  1278. }
  1279. /* enable interrupt */
  1280. rt_hw_interrupt_enable(temp);
  1281. RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(mb->parent.parent)));
  1282. return RT_EOK;
  1283. }
  1284. /**
  1285. * This function can get or set some extra attributions of a mailbox object.
  1286. *
  1287. * @param mb the mailbox object
  1288. * @param cmd the execution command
  1289. * @param arg the execution argument
  1290. *
  1291. * @return the error code
  1292. */
  1293. rt_err_t rt_mb_control(rt_mailbox_t mb, rt_uint8_t cmd, void *arg)
  1294. {
  1295. rt_ubase_t level;
  1296. RT_ASSERT(mb != RT_NULL);
  1297. if (cmd == RT_IPC_CMD_RESET)
  1298. {
  1299. /* disable interrupt */
  1300. level = rt_hw_interrupt_disable();
  1301. /* resume all waiting thread */
  1302. rt_ipc_list_resume_all(&(mb->parent.suspend_thread));
  1303. /* also resume all mailbox private suspended thread */
  1304. rt_ipc_list_resume_all(&(mb->suspend_sender_thread));
  1305. /* re-init mailbox */
  1306. mb->entry = 0;
  1307. mb->in_offset = 0;
  1308. mb->out_offset = 0;
  1309. /* enable interrupt */
  1310. rt_hw_interrupt_enable(level);
  1311. rt_schedule();
  1312. return RT_EOK;
  1313. }
  1314. return -RT_ERROR;
  1315. }
  1316. #endif /* end of RT_USING_MAILBOX */
  1317. #ifdef RT_USING_MESSAGEQUEUE
  1318. struct rt_mq_message
  1319. {
  1320. struct rt_mq_message *next;
  1321. };
  1322. /**
  1323. * This function will initialize a message queue and put it under control of resource
  1324. * management.
  1325. *
  1326. * @param mq the message object
  1327. * @param name the name of message queue
  1328. * @param msgpool the beginning address of buffer to save messages
  1329. * @param msg_size the maximum size of message
  1330. * @param pool_size the size of buffer to save messages
  1331. * @param flag the flag of message queue
  1332. *
  1333. * @return the operation status, RT_EOK on successful
  1334. */
  1335. rt_err_t rt_mq_init(rt_mq_t mq, const char *name, void *msgpool, rt_size_t msg_size, rt_size_t pool_size, rt_uint8_t flag)
  1336. {
  1337. struct rt_mq_message *head;
  1338. register rt_base_t temp;
  1339. /* parameter check */
  1340. RT_ASSERT(mq != RT_NULL);
  1341. /* init object */
  1342. rt_object_init(&(mq->parent.parent), RT_Object_Class_MessageQueue, name);
  1343. /* set parent flag */
  1344. mq->parent.parent.flag = flag;
  1345. /* init ipc object */
  1346. rt_ipc_object_init(&(mq->parent));
  1347. /* set messasge pool */
  1348. mq->msg_pool = msgpool;
  1349. /* get correct message size */
  1350. mq->msg_size = RT_ALIGN(msg_size, RT_ALIGN_SIZE);
  1351. mq->max_msgs = pool_size / (mq->msg_size + sizeof(struct rt_mq_message));
  1352. /* init message list */
  1353. mq->msg_queue_head = RT_NULL;
  1354. mq->msg_queue_tail = RT_NULL;
  1355. /* init message empty list */
  1356. mq->msg_queue_free = RT_NULL;
  1357. for (temp = 0; temp < mq->max_msgs; temp ++)
  1358. {
  1359. head = (struct rt_mq_message *)((rt_uint8_t *)mq->msg_pool +
  1360. temp * (mq->msg_size + sizeof(struct rt_mq_message)));
  1361. head->next = mq->msg_queue_free;
  1362. mq->msg_queue_free = head;
  1363. }
  1364. /* the initial entry is zero */
  1365. mq->entry = 0;
  1366. return RT_EOK;
  1367. }
  1368. /**
  1369. * This function will detach a message queue object from resource management
  1370. *
  1371. * @param mq the message queue object
  1372. *
  1373. * @return the operation status, RT_EOK on successful
  1374. */
  1375. rt_err_t rt_mq_detach(rt_mq_t mq)
  1376. {
  1377. /* parameter check */
  1378. RT_ASSERT(mq != RT_NULL);
  1379. /* resume all suspended thread */
  1380. rt_ipc_list_resume_all(&mq->parent.suspend_thread);
  1381. /* detach message queue object */
  1382. rt_object_detach(&(mq->parent.parent));
  1383. return RT_EOK;
  1384. }
  1385. #ifdef RT_USING_HEAP
  1386. /**
  1387. * This function will create a message queue object from system resource
  1388. *
  1389. * @param name the name of message queue
  1390. * @param msg_size the size of message
  1391. * @param max_msgs the maximum number of message in queue
  1392. * @param flag the flag of message queue
  1393. *
  1394. * @return the created message queue, RT_NULL on error happen
  1395. */
  1396. rt_mq_t rt_mq_create(const char *name, rt_size_t msg_size, rt_size_t max_msgs, rt_uint8_t flag)
  1397. {
  1398. struct rt_messagequeue *mq;
  1399. struct rt_mq_message *head;
  1400. register rt_base_t temp;
  1401. RT_DEBUG_NOT_IN_INTERRUPT;
  1402. /* allocate object */
  1403. mq = (rt_mq_t)rt_object_allocate(RT_Object_Class_MessageQueue, name);
  1404. if (mq == RT_NULL)
  1405. return mq;
  1406. /* set parent */
  1407. mq->parent.parent.flag = flag;
  1408. /* init ipc object */
  1409. rt_ipc_object_init(&(mq->parent));
  1410. /* init message queue */
  1411. /* get correct message size */
  1412. mq->msg_size = RT_ALIGN(msg_size, RT_ALIGN_SIZE);
  1413. mq->max_msgs = max_msgs;
  1414. /* allocate message pool */
  1415. mq->msg_pool = rt_malloc((mq->msg_size + sizeof(struct rt_mq_message))* mq->max_msgs);
  1416. if (mq->msg_pool == RT_NULL)
  1417. {
  1418. rt_mq_delete(mq);
  1419. return RT_NULL;
  1420. }
  1421. /* init message list */
  1422. mq->msg_queue_head = RT_NULL;
  1423. mq->msg_queue_tail = RT_NULL;
  1424. /* init message empty list */
  1425. mq->msg_queue_free = RT_NULL;
  1426. for (temp = 0; temp < mq->max_msgs; temp ++)
  1427. {
  1428. head = (struct rt_mq_message*)((rt_uint8_t*)mq->msg_pool +
  1429. temp * (mq->msg_size + sizeof(struct rt_mq_message)));
  1430. head->next = mq->msg_queue_free;
  1431. mq->msg_queue_free = head;
  1432. }
  1433. /* the initial entry is zero */
  1434. mq->entry = 0;
  1435. return mq;
  1436. }
  1437. /**
  1438. * This function will delete a message queue object and release the memory
  1439. *
  1440. * @param mq the message queue object
  1441. *
  1442. * @return the error code
  1443. */
  1444. rt_err_t rt_mq_delete(rt_mq_t mq)
  1445. {
  1446. RT_DEBUG_NOT_IN_INTERRUPT;
  1447. /* parameter check */
  1448. RT_ASSERT(mq != RT_NULL);
  1449. /* resume all suspended thread */
  1450. rt_ipc_list_resume_all(&(mq->parent.suspend_thread));
  1451. #if defined(RT_USING_MODULE) && defined(RT_USING_SLAB)
  1452. /* the mq object belongs to an application module */
  1453. if (mq->parent.parent.flag & RT_OBJECT_FLAG_MODULE)
  1454. rt_module_free(mq->parent.parent.module_id, mq->msg_pool);
  1455. else
  1456. #endif
  1457. /* free message queue pool */
  1458. rt_free(mq->msg_pool);
  1459. /* delete message queue object */
  1460. rt_object_delete(&(mq->parent.parent));
  1461. return RT_EOK;
  1462. }
  1463. #endif
  1464. /**
  1465. * This function will send a message to message queue object, if there are threads
  1466. * suspended on message queue object, it will be waked up.
  1467. *
  1468. * @param mq the message queue object
  1469. * @param buffer the message
  1470. * @param size the size of buffer
  1471. *
  1472. * @return the error code
  1473. */
  1474. rt_err_t rt_mq_send(rt_mq_t mq, void *buffer, rt_size_t size)
  1475. {
  1476. register rt_ubase_t temp;
  1477. struct rt_mq_message *msg;
  1478. RT_ASSERT(mq != RT_NULL);
  1479. RT_ASSERT(buffer != RT_NULL);
  1480. RT_ASSERT(size != 0);
  1481. /* greater than one message size */
  1482. if (size > mq->msg_size)
  1483. return -RT_ERROR;
  1484. RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(mq->parent.parent)));
  1485. /* disable interrupt */
  1486. temp = rt_hw_interrupt_disable();
  1487. /* get a free list, there must be an empty item */
  1488. msg = (struct rt_mq_message*)mq->msg_queue_free;
  1489. /* message queue is full */
  1490. if (msg == RT_NULL)
  1491. {
  1492. /* enable interrupt */
  1493. rt_hw_interrupt_enable(temp);
  1494. return -RT_EFULL;
  1495. }
  1496. /* move free list pointer */
  1497. mq->msg_queue_free = msg->next;
  1498. /* enable interrupt */
  1499. rt_hw_interrupt_enable(temp);
  1500. /* the msg is the new tailer of list, the next shall be NULL */
  1501. msg->next = RT_NULL;
  1502. /* copy buffer */
  1503. rt_memcpy(msg + 1, buffer, size);
  1504. /* disable interrupt */
  1505. temp = rt_hw_interrupt_disable();
  1506. /* link msg to message queue */
  1507. if (mq->msg_queue_tail != RT_NULL)
  1508. {
  1509. /* if the tail exists, */
  1510. ((struct rt_mq_message *)mq->msg_queue_tail)->next = msg;
  1511. }
  1512. /* set new tail */
  1513. mq->msg_queue_tail = msg;
  1514. /* if the head is empty, set head */
  1515. if (mq->msg_queue_head == RT_NULL)
  1516. mq->msg_queue_head = msg;
  1517. /* increase message entry */
  1518. mq->entry ++;
  1519. /* resume suspended thread */
  1520. if (!rt_list_isempty(&mq->parent.suspend_thread))
  1521. {
  1522. rt_ipc_list_resume(&(mq->parent.suspend_thread));
  1523. /* enable interrupt */
  1524. rt_hw_interrupt_enable(temp);
  1525. rt_schedule();
  1526. return RT_EOK;
  1527. }
  1528. /* enable interrupt */
  1529. rt_hw_interrupt_enable(temp);
  1530. return RT_EOK;
  1531. }
  1532. /**
  1533. * This function will send urgently a message to message queue object, which means
  1534. * the message will be inserted to the head of message queue. If there are threads
  1535. * suspended on message queue object, it will be waked up.
  1536. *
  1537. * @param mq the message queue object
  1538. * @param buffer the message
  1539. * @param size the size of buffer
  1540. *
  1541. * @return the error code
  1542. */
  1543. rt_err_t rt_mq_urgent(rt_mq_t mq, void *buffer, rt_size_t size)
  1544. {
  1545. register rt_ubase_t temp;
  1546. struct rt_mq_message *msg;
  1547. RT_ASSERT(mq != RT_NULL);
  1548. RT_ASSERT(buffer != RT_NULL);
  1549. RT_ASSERT(size != 0);
  1550. /* greater than one message size */
  1551. if (size > mq->msg_size)
  1552. return -RT_ERROR;
  1553. RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(mq->parent.parent)));
  1554. /* disable interrupt */
  1555. temp = rt_hw_interrupt_disable();
  1556. /* get a free list, there must be an empty item */
  1557. msg = (struct rt_mq_message *)mq->msg_queue_free;
  1558. /* message queue is full */
  1559. if (msg == RT_NULL)
  1560. {
  1561. /* enable interrupt */
  1562. rt_hw_interrupt_enable(temp);
  1563. return -RT_EFULL;
  1564. }
  1565. /* move free list pointer */
  1566. mq->msg_queue_free = msg->next;
  1567. /* enable interrupt */
  1568. rt_hw_interrupt_enable(temp);
  1569. /* copy buffer */
  1570. rt_memcpy(msg + 1, buffer, size);
  1571. /* disable interrupt */
  1572. temp = rt_hw_interrupt_disable();
  1573. /* link msg to the beginning of message queue */
  1574. msg->next = mq->msg_queue_head;
  1575. mq->msg_queue_head = msg;
  1576. /* if there is no tail */
  1577. if (mq->msg_queue_tail == RT_NULL)
  1578. mq->msg_queue_tail = msg;
  1579. /* increase message entry */
  1580. mq->entry ++;
  1581. /* resume suspended thread */
  1582. if (!rt_list_isempty(&mq->parent.suspend_thread))
  1583. {
  1584. rt_ipc_list_resume(&(mq->parent.suspend_thread));
  1585. /* enable interrupt */
  1586. rt_hw_interrupt_enable(temp);
  1587. rt_schedule();
  1588. return RT_EOK;
  1589. }
  1590. /* enable interrupt */
  1591. rt_hw_interrupt_enable(temp);
  1592. return RT_EOK;
  1593. }
  1594. /**
  1595. * This function will receive a message from message queue object, if there is no
  1596. * message in message queue object, the thread shall wait for a specified time.
  1597. *
  1598. * @param mq the message queue object
  1599. * @param buffer the received message will be saved in
  1600. * @param size the size of buffer
  1601. * @param timeout the waiting time
  1602. *
  1603. * @return the error code
  1604. */
  1605. rt_err_t rt_mq_recv(rt_mq_t mq, void *buffer, rt_size_t size, rt_int32_t timeout)
  1606. {
  1607. struct rt_thread *thread;
  1608. register rt_ubase_t temp;
  1609. struct rt_mq_message *msg;
  1610. rt_uint32_t tick_delta;
  1611. RT_ASSERT(mq != RT_NULL);
  1612. RT_ASSERT(buffer != RT_NULL);
  1613. RT_ASSERT(size != 0);
  1614. /* initialize delta tick */
  1615. tick_delta = 0;
  1616. /* get current thread */
  1617. thread = rt_thread_self();
  1618. RT_OBJECT_HOOK_CALL(rt_object_trytake_hook, (&(mq->parent.parent)));
  1619. /* disable interrupt */
  1620. temp = rt_hw_interrupt_disable();
  1621. /* for non-blocking call */
  1622. if (mq->entry == 0 && timeout == 0)
  1623. {
  1624. rt_hw_interrupt_enable(temp);
  1625. return -RT_ETIMEOUT;
  1626. }
  1627. /* message queue is empty */
  1628. while (mq->entry == 0)
  1629. {
  1630. RT_DEBUG_NOT_IN_INTERRUPT;
  1631. /* reset error number in thread */
  1632. thread->error = RT_EOK;
  1633. /* no waiting, return timeout */
  1634. if (timeout == 0)
  1635. {
  1636. /* enable interrupt */
  1637. rt_hw_interrupt_enable(temp);
  1638. thread->error = -RT_ETIMEOUT;
  1639. return -RT_ETIMEOUT;
  1640. }
  1641. /* suspend current thread */
  1642. rt_ipc_list_suspend(&(mq->parent.suspend_thread), thread, mq->parent.parent.flag);
  1643. /* has waiting time, start thread timer */
  1644. if (timeout > 0)
  1645. {
  1646. /* get the start tick of timer */
  1647. tick_delta = rt_tick_get();
  1648. RT_DEBUG_LOG(RT_DEBUG_IPC, ("set thread:%s to timer list\n", thread->name));
  1649. /* reset the timeout of thread timer and start it */
  1650. rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &timeout);
  1651. rt_timer_start(&(thread->thread_timer));
  1652. }
  1653. /* enable interrupt */
  1654. rt_hw_interrupt_enable(temp);
  1655. /* re-schedule */
  1656. rt_schedule();
  1657. /* recv message */
  1658. if (thread->error != RT_EOK)
  1659. {
  1660. /* return error */
  1661. return thread->error;
  1662. }
  1663. /* disable interrupt */
  1664. temp = rt_hw_interrupt_disable();
  1665. /* if it's not waiting forever and then re-calculate timeout tick */
  1666. if (timeout > 0)
  1667. {
  1668. tick_delta = rt_tick_get() - tick_delta;
  1669. timeout -= tick_delta;
  1670. if (timeout < 0)
  1671. timeout = 0;
  1672. }
  1673. }
  1674. /* get message from queue */
  1675. msg = (struct rt_mq_message *)mq->msg_queue_head;
  1676. /* move message queue head */
  1677. mq->msg_queue_head = msg->next;
  1678. /* reach queue tail, set to NULL */
  1679. if (mq->msg_queue_tail == msg)
  1680. mq->msg_queue_tail = RT_NULL;
  1681. /* decrease message entry */
  1682. mq->entry --;
  1683. /* enable interrupt */
  1684. rt_hw_interrupt_enable(temp);
  1685. /* copy message */
  1686. rt_memcpy(buffer, msg + 1, size > mq->msg_size ? mq->msg_size : size);
  1687. /* disable interrupt */
  1688. temp = rt_hw_interrupt_disable();
  1689. /* put message to free list */
  1690. msg->next = (struct rt_mq_message *)mq->msg_queue_free;
  1691. mq->msg_queue_free = msg;
  1692. /* enable interrupt */
  1693. rt_hw_interrupt_enable(temp);
  1694. RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(mq->parent.parent)));
  1695. return RT_EOK;
  1696. }
  1697. /**
  1698. * This function can get or set some extra attributions of a message queue object.
  1699. *
  1700. * @param mq the message queue object
  1701. * @param cmd the execution command
  1702. * @param arg the execution argument
  1703. *
  1704. * @return the error code
  1705. */
  1706. rt_err_t rt_mq_control(rt_mq_t mq, rt_uint8_t cmd, void *arg)
  1707. {
  1708. rt_ubase_t level;
  1709. struct rt_mq_message *msg;
  1710. RT_ASSERT(mq != RT_NULL);
  1711. if (cmd == RT_IPC_CMD_RESET)
  1712. {
  1713. /* disable interrupt */
  1714. level = rt_hw_interrupt_disable();
  1715. /* resume all waiting thread */
  1716. rt_ipc_list_resume_all(&mq->parent.suspend_thread);
  1717. /* release all message in the queue */
  1718. while (mq->msg_queue_head != RT_NULL)
  1719. {
  1720. /* get message from queue */
  1721. msg = (struct rt_mq_message *)mq->msg_queue_head;
  1722. /* move message queue head */
  1723. mq->msg_queue_head = msg->next;
  1724. /* reach queue tail, set to NULL */
  1725. if (mq->msg_queue_tail == msg)
  1726. mq->msg_queue_tail = RT_NULL;
  1727. /* put message to free list */
  1728. msg->next = (struct rt_mq_message *)mq->msg_queue_free;
  1729. mq->msg_queue_free = msg;
  1730. }
  1731. /* clean entry */
  1732. mq->entry = 0;
  1733. /* enable interrupt */
  1734. rt_hw_interrupt_enable(level);
  1735. rt_schedule();
  1736. return RT_EOK;
  1737. }
  1738. return -RT_ERROR;
  1739. }
  1740. #endif /* end of RT_USING_MESSAGEQUEUE */
  1741. /*@}*/