ipc.c 58 KB

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