ipc.c 49 KB

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