ipc.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284
  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, rt_uint8_t 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. /* disable interrupt */
  543. temp = rt_hw_interrupt_disable();
  544. /* get current thread */
  545. thread = rt_thread_self();
  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. /* The value of mutex is 1 in initial status. Therefore, if the
  560. * value is great than 0, it indicates the mutex is avaible.
  561. */
  562. if (mutex->value > 0)
  563. {
  564. /* mutex is available */
  565. mutex->value --;
  566. /* set mutex owner and original priority */
  567. mutex->owner = thread;
  568. mutex->original_priority = thread->current_priority;
  569. mutex->hold ++;
  570. }
  571. else
  572. {
  573. /* no waiting, return with timeout */
  574. if (time == 0)
  575. {
  576. /* set error as timeout */
  577. thread->error = -RT_ETIMEOUT;
  578. /* enable interrupt */
  579. rt_hw_interrupt_enable(temp);
  580. return -RT_ETIMEOUT;
  581. }
  582. else
  583. {
  584. /* mutex is unavailable, push to suspend list */
  585. RT_DEBUG_LOG(RT_DEBUG_IPC, ("mutex_take: suspend thread: %s\n",
  586. thread->name));
  587. /* change the owner thread priority of mutex */
  588. if (thread->current_priority < mutex->owner->current_priority)
  589. {
  590. /* change the owner thread priority */
  591. rt_thread_control(mutex->owner,
  592. RT_THREAD_CTRL_CHANGE_PRIORITY,
  593. &thread->current_priority);
  594. }
  595. /* suspend current thread */
  596. rt_ipc_list_suspend(&(mutex->parent.suspend_thread),
  597. thread,
  598. mutex->parent.parent.flag);
  599. /* has waiting time, start thread timer */
  600. if (time > 0)
  601. {
  602. RT_DEBUG_LOG(RT_DEBUG_IPC,
  603. ("mutex_take: start the timer of thread:%s\n",
  604. thread->name));
  605. /* reset the timeout of thread timer and start it */
  606. rt_timer_control(&(thread->thread_timer),
  607. RT_TIMER_CTRL_SET_TIME,
  608. &time);
  609. rt_timer_start(&(thread->thread_timer));
  610. }
  611. /* enable interrupt */
  612. rt_hw_interrupt_enable(temp);
  613. /* do schedule */
  614. rt_schedule();
  615. if (thread->error != RT_EOK)
  616. {
  617. /* return error */
  618. return thread->error;
  619. }
  620. else
  621. {
  622. /* the mutex is taken successfully. */
  623. /* disable interrupt */
  624. temp = rt_hw_interrupt_disable();
  625. }
  626. }
  627. }
  628. }
  629. /* enable interrupt */
  630. rt_hw_interrupt_enable(temp);
  631. RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(mutex->parent.parent)));
  632. return RT_EOK;
  633. }
  634. RTM_EXPORT(rt_mutex_take);
  635. /**
  636. * This function will release a mutex, if there are threads suspended on mutex,
  637. * it will be waked up.
  638. *
  639. * @param mutex the mutex object
  640. *
  641. * @return the error code
  642. */
  643. rt_err_t rt_mutex_release(rt_mutex_t mutex)
  644. {
  645. register rt_base_t temp;
  646. struct rt_thread *thread;
  647. rt_bool_t need_schedule;
  648. need_schedule = RT_FALSE;
  649. /* only thread could release mutex because we need test the ownership */
  650. RT_DEBUG_IN_THREAD_CONTEXT;
  651. /* get current thread */
  652. thread = rt_thread_self();
  653. /* disable interrupt */
  654. temp = rt_hw_interrupt_disable();
  655. RT_DEBUG_LOG(RT_DEBUG_IPC,
  656. ("mutex_release:current thread %s, mutex value: %d, hold: %d\n",
  657. thread->name, mutex->value, mutex->hold));
  658. RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(mutex->parent.parent)));
  659. /* mutex only can be released by owner */
  660. if (thread != mutex->owner)
  661. {
  662. thread->error = -RT_ERROR;
  663. /* enable interrupt */
  664. rt_hw_interrupt_enable(temp);
  665. return -RT_ERROR;
  666. }
  667. /* decrease hold */
  668. mutex->hold --;
  669. /* if no hold */
  670. if (mutex->hold == 0)
  671. {
  672. /* change the owner thread to original priority */
  673. if (mutex->original_priority != mutex->owner->current_priority)
  674. {
  675. rt_thread_control(mutex->owner,
  676. RT_THREAD_CTRL_CHANGE_PRIORITY,
  677. &(mutex->original_priority));
  678. }
  679. /* wakeup suspended thread */
  680. if (!rt_list_isempty(&mutex->parent.suspend_thread))
  681. {
  682. /* get suspended thread */
  683. thread = rt_list_entry(mutex->parent.suspend_thread.next,
  684. struct rt_thread,
  685. tlist);
  686. RT_DEBUG_LOG(RT_DEBUG_IPC, ("mutex_release: resume thread: %s\n",
  687. thread->name));
  688. /* set new owner and priority */
  689. mutex->owner = thread;
  690. mutex->original_priority = thread->current_priority;
  691. mutex->hold ++;
  692. /* resume thread */
  693. rt_ipc_list_resume(&(mutex->parent.suspend_thread));
  694. need_schedule = RT_TRUE;
  695. }
  696. else
  697. {
  698. /* increase value */
  699. mutex->value ++;
  700. /* clear owner */
  701. mutex->owner = RT_NULL;
  702. mutex->original_priority = 0xff;
  703. }
  704. }
  705. /* enable interrupt */
  706. rt_hw_interrupt_enable(temp);
  707. /* perform a schedule */
  708. if (need_schedule == RT_TRUE)
  709. rt_schedule();
  710. return RT_EOK;
  711. }
  712. RTM_EXPORT(rt_mutex_release);
  713. /**
  714. * This function can get or set some extra attributions of a mutex object.
  715. *
  716. * @param mutex the mutex object
  717. * @param cmd the execution command
  718. * @param arg the execution argument
  719. *
  720. * @return the error code
  721. */
  722. rt_err_t rt_mutex_control(rt_mutex_t mutex, rt_uint8_t cmd, void *arg)
  723. {
  724. return -RT_ERROR;
  725. }
  726. RTM_EXPORT(rt_mutex_control);
  727. #endif /* end of RT_USING_MUTEX */
  728. #ifdef RT_USING_EVENT
  729. /**
  730. * This function will initialize an event and put it under control of resource
  731. * management.
  732. *
  733. * @param event the event object
  734. * @param name the name of event
  735. * @param flag the flag of event
  736. *
  737. * @return the operation status, RT_EOK on successful
  738. */
  739. rt_err_t rt_event_init(rt_event_t event, const char *name, rt_uint8_t flag)
  740. {
  741. RT_ASSERT(event != RT_NULL);
  742. /* init object */
  743. rt_object_init(&(event->parent.parent), RT_Object_Class_Event, name);
  744. /* set parent flag */
  745. event->parent.parent.flag = flag;
  746. /* init ipc object */
  747. rt_ipc_object_init(&(event->parent));
  748. /* init event */
  749. event->set = 0;
  750. return RT_EOK;
  751. }
  752. RTM_EXPORT(rt_event_init);
  753. /**
  754. * This function will detach an event object from resource management
  755. *
  756. * @param event the event object
  757. *
  758. * @return the operation status, RT_EOK on successful
  759. */
  760. rt_err_t rt_event_detach(rt_event_t event)
  761. {
  762. /* parameter check */
  763. RT_ASSERT(event != RT_NULL);
  764. /* resume all suspended thread */
  765. rt_ipc_list_resume_all(&(event->parent.suspend_thread));
  766. /* detach event object */
  767. rt_object_detach(&(event->parent.parent));
  768. return RT_EOK;
  769. }
  770. RTM_EXPORT(rt_event_detach);
  771. #ifdef RT_USING_HEAP
  772. /**
  773. * This function will create an event object from system resource
  774. *
  775. * @param name the name of event
  776. * @param flag the flag of event
  777. *
  778. * @return the created event, RT_NULL on error happen
  779. */
  780. rt_event_t rt_event_create(const char *name, rt_uint8_t flag)
  781. {
  782. rt_event_t event;
  783. RT_DEBUG_NOT_IN_INTERRUPT;
  784. /* allocate object */
  785. event = (rt_event_t)rt_object_allocate(RT_Object_Class_Event, name);
  786. if (event == RT_NULL)
  787. return event;
  788. /* set parent */
  789. event->parent.parent.flag = flag;
  790. /* init ipc object */
  791. rt_ipc_object_init(&(event->parent));
  792. /* init event */
  793. event->set = 0;
  794. return event;
  795. }
  796. RTM_EXPORT(rt_event_create);
  797. /**
  798. * This function will delete an event object and release the memory
  799. *
  800. * @param event the event object
  801. *
  802. * @return the error code
  803. */
  804. rt_err_t rt_event_delete(rt_event_t event)
  805. {
  806. /* parameter check */
  807. RT_ASSERT(event != RT_NULL);
  808. RT_DEBUG_NOT_IN_INTERRUPT;
  809. /* resume all suspended thread */
  810. rt_ipc_list_resume_all(&(event->parent.suspend_thread));
  811. /* delete event object */
  812. rt_object_delete(&(event->parent.parent));
  813. return RT_EOK;
  814. }
  815. RTM_EXPORT(rt_event_delete);
  816. #endif
  817. /**
  818. * This function will send an event to the event object, if there are threads
  819. * suspended on event object, it will be waked up.
  820. *
  821. * @param event the event object
  822. * @param set the event set
  823. *
  824. * @return the error code
  825. */
  826. rt_err_t rt_event_send(rt_event_t event, rt_uint32_t set)
  827. {
  828. struct rt_list_node *n;
  829. struct rt_thread *thread;
  830. register rt_ubase_t level;
  831. register rt_base_t status;
  832. rt_bool_t need_schedule;
  833. /* parameter check */
  834. RT_ASSERT(event != RT_NULL);
  835. if (set == 0)
  836. return -RT_ERROR;
  837. need_schedule = RT_FALSE;
  838. RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(event->parent.parent)));
  839. /* disable interrupt */
  840. level = rt_hw_interrupt_disable();
  841. /* set event */
  842. event->set |= set;
  843. if (!rt_list_isempty(&event->parent.suspend_thread))
  844. {
  845. /* search thread list to resume thread */
  846. n = event->parent.suspend_thread.next;
  847. while (n != &(event->parent.suspend_thread))
  848. {
  849. /* get thread */
  850. thread = rt_list_entry(n, struct rt_thread, tlist);
  851. status = -RT_ERROR;
  852. if (thread->event_info & RT_EVENT_FLAG_AND)
  853. {
  854. if ((thread->event_set & event->set) == thread->event_set)
  855. {
  856. /* received an AND event */
  857. status = RT_EOK;
  858. }
  859. }
  860. else if (thread->event_info & RT_EVENT_FLAG_OR)
  861. {
  862. if (thread->event_set & event->set)
  863. {
  864. /* save recieved event set */
  865. thread->event_set = thread->event_set & event->set;
  866. /* received an OR event */
  867. status = RT_EOK;
  868. }
  869. }
  870. /* move node to the next */
  871. n = n->next;
  872. /* condition is satisfied, resume thread */
  873. if (status == RT_EOK)
  874. {
  875. /* clear event */
  876. if (thread->event_info & RT_EVENT_FLAG_CLEAR)
  877. event->set &= ~thread->event_set;
  878. /* resume thread, and thread list breaks out */
  879. rt_thread_resume(thread);
  880. /* need do a scheduling */
  881. need_schedule = RT_TRUE;
  882. }
  883. }
  884. }
  885. /* enable interrupt */
  886. rt_hw_interrupt_enable(level);
  887. /* do a schedule */
  888. if (need_schedule == RT_TRUE)
  889. rt_schedule();
  890. return RT_EOK;
  891. }
  892. RTM_EXPORT(rt_event_send);
  893. /**
  894. * This function will receive an event from event object, if the event is
  895. * unavailable, the thread shall wait for a specified time.
  896. *
  897. * @param event the fast event object
  898. * @param set the interested event set
  899. * @param option the receive option, either RT_EVENT_FLAG_AND or
  900. * RT_EVENT_FLAG_OR should be set.
  901. * @param timeout the waiting time
  902. * @param recved the received event, if you don't care, RT_NULL can be set.
  903. *
  904. * @return the error code
  905. */
  906. rt_err_t rt_event_recv(rt_event_t event,
  907. rt_uint32_t set,
  908. rt_uint8_t option,
  909. rt_int32_t timeout,
  910. rt_uint32_t *recved)
  911. {
  912. struct rt_thread *thread;
  913. register rt_ubase_t level;
  914. register rt_base_t status;
  915. RT_DEBUG_IN_THREAD_CONTEXT;
  916. /* parameter check */
  917. RT_ASSERT(event != RT_NULL);
  918. if (set == 0)
  919. return -RT_ERROR;
  920. /* init status */
  921. status = -RT_ERROR;
  922. /* get current thread */
  923. thread = rt_thread_self();
  924. /* reset thread error */
  925. thread->error = RT_EOK;
  926. RT_OBJECT_HOOK_CALL(rt_object_trytake_hook, (&(event->parent.parent)));
  927. /* disable interrupt */
  928. level = rt_hw_interrupt_disable();
  929. /* check event set */
  930. if (option & RT_EVENT_FLAG_AND)
  931. {
  932. if ((event->set & set) == set)
  933. status = RT_EOK;
  934. }
  935. else if (option & RT_EVENT_FLAG_OR)
  936. {
  937. if (event->set & set)
  938. status = RT_EOK;
  939. }
  940. else
  941. {
  942. /* either RT_EVENT_FLAG_AND or RT_EVENT_FLAG_OR should be set */
  943. RT_ASSERT(0);
  944. }
  945. if (status == RT_EOK)
  946. {
  947. /* set received event */
  948. if (recved)
  949. *recved = (event->set & set);
  950. /* received event */
  951. if (option & RT_EVENT_FLAG_CLEAR)
  952. event->set &= ~set;
  953. }
  954. else if (timeout == 0)
  955. {
  956. /* no waiting */
  957. thread->error = -RT_ETIMEOUT;
  958. }
  959. else
  960. {
  961. /* fill thread event info */
  962. thread->event_set = set;
  963. thread->event_info = option;
  964. /* put thread to suspended thread list */
  965. rt_ipc_list_suspend(&(event->parent.suspend_thread),
  966. thread,
  967. event->parent.parent.flag);
  968. /* if there is a waiting timeout, active thread timer */
  969. if (timeout > 0)
  970. {
  971. /* reset the timeout of thread timer and start it */
  972. rt_timer_control(&(thread->thread_timer),
  973. RT_TIMER_CTRL_SET_TIME,
  974. &timeout);
  975. rt_timer_start(&(thread->thread_timer));
  976. }
  977. /* enable interrupt */
  978. rt_hw_interrupt_enable(level);
  979. /* do a schedule */
  980. rt_schedule();
  981. if (thread->error != RT_EOK)
  982. {
  983. /* return error */
  984. return thread->error;
  985. }
  986. /* received an event, disable interrupt to protect */
  987. level = rt_hw_interrupt_disable();
  988. /* set received event */
  989. if (recved)
  990. *recved = thread->event_set;
  991. }
  992. /* enable interrupt */
  993. rt_hw_interrupt_enable(level);
  994. RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(event->parent.parent)));
  995. return thread->error;
  996. }
  997. RTM_EXPORT(rt_event_recv);
  998. /**
  999. * This function can get or set some extra attributions of an event object.
  1000. *
  1001. * @param event the event object
  1002. * @param cmd the execution command
  1003. * @param arg the execution argument
  1004. *
  1005. * @return the error code
  1006. */
  1007. rt_err_t rt_event_control(rt_event_t event, rt_uint8_t cmd, void *arg)
  1008. {
  1009. rt_ubase_t level;
  1010. RT_ASSERT(event != RT_NULL);
  1011. if (cmd == RT_IPC_CMD_RESET)
  1012. {
  1013. /* disable interrupt */
  1014. level = rt_hw_interrupt_disable();
  1015. /* resume all waiting thread */
  1016. rt_ipc_list_resume_all(&event->parent.suspend_thread);
  1017. /* init event set */
  1018. event->set = 0;
  1019. /* enable interrupt */
  1020. rt_hw_interrupt_enable(level);
  1021. rt_schedule();
  1022. return RT_EOK;
  1023. }
  1024. return -RT_ERROR;
  1025. }
  1026. RTM_EXPORT(rt_event_control);
  1027. #endif /* end of RT_USING_EVENT */
  1028. #ifdef RT_USING_MAILBOX
  1029. /**
  1030. * This function will initialize a mailbox and put it under control of resource
  1031. * management.
  1032. *
  1033. * @param mb the mailbox object
  1034. * @param name the name of mailbox
  1035. * @param msgpool the begin address of buffer to save received mail
  1036. * @param size the size of mailbox
  1037. * @param flag the flag of mailbox
  1038. *
  1039. * @return the operation status, RT_EOK on successful
  1040. */
  1041. rt_err_t rt_mb_init(rt_mailbox_t mb,
  1042. const char *name,
  1043. void *msgpool,
  1044. rt_size_t size,
  1045. rt_uint8_t flag)
  1046. {
  1047. RT_ASSERT(mb != RT_NULL);
  1048. /* init object */
  1049. rt_object_init(&(mb->parent.parent), RT_Object_Class_MailBox, name);
  1050. /* set parent flag */
  1051. mb->parent.parent.flag = flag;
  1052. /* init ipc object */
  1053. rt_ipc_object_init(&(mb->parent));
  1054. /* init mailbox */
  1055. mb->msg_pool = msgpool;
  1056. mb->size = size;
  1057. mb->entry = 0;
  1058. mb->in_offset = 0;
  1059. mb->out_offset = 0;
  1060. /* init an additional list of sender suspend thread */
  1061. rt_list_init(&(mb->suspend_sender_thread));
  1062. return RT_EOK;
  1063. }
  1064. RTM_EXPORT(rt_mb_init);
  1065. /**
  1066. * This function will detach a mailbox from resource management
  1067. *
  1068. * @param mb the mailbox object
  1069. *
  1070. * @return the operation status, RT_EOK on successful
  1071. */
  1072. rt_err_t rt_mb_detach(rt_mailbox_t mb)
  1073. {
  1074. /* parameter check */
  1075. RT_ASSERT(mb != RT_NULL);
  1076. /* resume all suspended thread */
  1077. rt_ipc_list_resume_all(&(mb->parent.suspend_thread));
  1078. /* also resume all mailbox private suspended thread */
  1079. rt_ipc_list_resume_all(&(mb->suspend_sender_thread));
  1080. /* detach mailbox object */
  1081. rt_object_detach(&(mb->parent.parent));
  1082. return RT_EOK;
  1083. }
  1084. RTM_EXPORT(rt_mb_detach);
  1085. #ifdef RT_USING_HEAP
  1086. /**
  1087. * This function will create a mailbox object from system resource
  1088. *
  1089. * @param name the name of mailbox
  1090. * @param size the size of mailbox
  1091. * @param flag the flag of mailbox
  1092. *
  1093. * @return the created mailbox, RT_NULL on error happen
  1094. */
  1095. rt_mailbox_t rt_mb_create(const char *name, rt_size_t size, rt_uint8_t flag)
  1096. {
  1097. rt_mailbox_t mb;
  1098. RT_DEBUG_NOT_IN_INTERRUPT;
  1099. /* allocate object */
  1100. mb = (rt_mailbox_t)rt_object_allocate(RT_Object_Class_MailBox, name);
  1101. if (mb == RT_NULL)
  1102. return mb;
  1103. /* set parent */
  1104. mb->parent.parent.flag = flag;
  1105. /* init ipc object */
  1106. rt_ipc_object_init(&(mb->parent));
  1107. /* init mailbox */
  1108. mb->size = size;
  1109. mb->msg_pool = RT_KERNEL_MALLOC(mb->size * sizeof(rt_uint32_t));
  1110. if (mb->msg_pool == RT_NULL)
  1111. {
  1112. /* delete mailbox object */
  1113. rt_object_delete(&(mb->parent.parent));
  1114. return RT_NULL;
  1115. }
  1116. mb->entry = 0;
  1117. mb->in_offset = 0;
  1118. mb->out_offset = 0;
  1119. /* init an additional list of sender suspend thread */
  1120. rt_list_init(&(mb->suspend_sender_thread));
  1121. return mb;
  1122. }
  1123. RTM_EXPORT(rt_mb_create);
  1124. /**
  1125. * This function will delete a mailbox object and release the memory
  1126. *
  1127. * @param mb the mailbox object
  1128. *
  1129. * @return the error code
  1130. */
  1131. rt_err_t rt_mb_delete(rt_mailbox_t mb)
  1132. {
  1133. RT_DEBUG_NOT_IN_INTERRUPT;
  1134. /* parameter check */
  1135. RT_ASSERT(mb != RT_NULL);
  1136. /* resume all suspended thread */
  1137. rt_ipc_list_resume_all(&(mb->parent.suspend_thread));
  1138. /* also resume all mailbox private suspended thread */
  1139. rt_ipc_list_resume_all(&(mb->suspend_sender_thread));
  1140. #if defined(RT_USING_MODULE) && defined(RT_USING_SLAB)
  1141. /* the mb object belongs to an application module */
  1142. if (mb->parent.parent.flag & RT_OBJECT_FLAG_MODULE)
  1143. rt_module_free(mb->parent.parent.module_id, mb->msg_pool);
  1144. else
  1145. #endif
  1146. /* free mailbox pool */
  1147. RT_KERNEL_FREE(mb->msg_pool);
  1148. /* delete mailbox object */
  1149. rt_object_delete(&(mb->parent.parent));
  1150. return RT_EOK;
  1151. }
  1152. RTM_EXPORT(rt_mb_delete);
  1153. #endif
  1154. /**
  1155. * This function will send a mail to mailbox object. If the mailbox is full,
  1156. * current thread will be suspended until timeout.
  1157. *
  1158. * @param mb the mailbox object
  1159. * @param value the mail
  1160. * @param timeout the waiting time
  1161. *
  1162. * @return the error code
  1163. */
  1164. rt_err_t rt_mb_send_wait(rt_mailbox_t mb,
  1165. rt_uint32_t value,
  1166. rt_int32_t timeout)
  1167. {
  1168. struct rt_thread *thread;
  1169. register rt_ubase_t temp;
  1170. rt_uint32_t tick_delta;
  1171. /* parameter check */
  1172. RT_ASSERT(mb != RT_NULL);
  1173. /* initialize delta tick */
  1174. tick_delta = 0;
  1175. /* get current thread */
  1176. thread = rt_thread_self();
  1177. RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(mb->parent.parent)));
  1178. /* disable interrupt */
  1179. temp = rt_hw_interrupt_disable();
  1180. /* for non-blocking call */
  1181. if (mb->entry == mb->size && timeout == 0)
  1182. {
  1183. rt_hw_interrupt_enable(temp);
  1184. return -RT_EFULL;
  1185. }
  1186. /* mailbox is full */
  1187. while (mb->entry == mb->size)
  1188. {
  1189. /* reset error number in thread */
  1190. thread->error = RT_EOK;
  1191. /* no waiting, return timeout */
  1192. if (timeout == 0)
  1193. {
  1194. /* enable interrupt */
  1195. rt_hw_interrupt_enable(temp);
  1196. return -RT_EFULL;
  1197. }
  1198. RT_DEBUG_IN_THREAD_CONTEXT;
  1199. /* suspend current thread */
  1200. rt_ipc_list_suspend(&(mb->suspend_sender_thread),
  1201. thread,
  1202. mb->parent.parent.flag);
  1203. /* has waiting time, start thread timer */
  1204. if (timeout > 0)
  1205. {
  1206. /* get the start tick of timer */
  1207. tick_delta = rt_tick_get();
  1208. RT_DEBUG_LOG(RT_DEBUG_IPC, ("mb_send_wait: start timer of thread:%s\n",
  1209. thread->name));
  1210. /* reset the timeout of thread timer and start it */
  1211. rt_timer_control(&(thread->thread_timer),
  1212. RT_TIMER_CTRL_SET_TIME,
  1213. &timeout);
  1214. rt_timer_start(&(thread->thread_timer));
  1215. }
  1216. /* enable interrupt */
  1217. rt_hw_interrupt_enable(temp);
  1218. /* re-schedule */
  1219. rt_schedule();
  1220. /* resume from suspend state */
  1221. if (thread->error != RT_EOK)
  1222. {
  1223. /* return error */
  1224. return thread->error;
  1225. }
  1226. /* disable interrupt */
  1227. temp = rt_hw_interrupt_disable();
  1228. /* if it's not waiting forever and then re-calculate timeout tick */
  1229. if (timeout > 0)
  1230. {
  1231. tick_delta = rt_tick_get() - tick_delta;
  1232. timeout -= tick_delta;
  1233. if (timeout < 0)
  1234. timeout = 0;
  1235. }
  1236. }
  1237. /* set ptr */
  1238. mb->msg_pool[mb->in_offset] = value;
  1239. /* increase input offset */
  1240. ++ mb->in_offset;
  1241. if (mb->in_offset >= mb->size)
  1242. mb->in_offset = 0;
  1243. /* increase message entry */
  1244. mb->entry ++;
  1245. /* resume suspended thread */
  1246. if (!rt_list_isempty(&mb->parent.suspend_thread))
  1247. {
  1248. rt_ipc_list_resume(&(mb->parent.suspend_thread));
  1249. /* enable interrupt */
  1250. rt_hw_interrupt_enable(temp);
  1251. rt_schedule();
  1252. return RT_EOK;
  1253. }
  1254. /* enable interrupt */
  1255. rt_hw_interrupt_enable(temp);
  1256. return RT_EOK;
  1257. }
  1258. RTM_EXPORT(rt_mb_send_wait);
  1259. /**
  1260. * This function will send a mail to mailbox object, if there are threads
  1261. * suspended on mailbox object, it will be waked up. This function will return
  1262. * immediately, if you want blocking send, use rt_mb_send_wait instead.
  1263. *
  1264. * @param mb the mailbox object
  1265. * @param value the mail
  1266. *
  1267. * @return the error code
  1268. */
  1269. rt_err_t rt_mb_send(rt_mailbox_t mb, rt_uint32_t value)
  1270. {
  1271. return rt_mb_send_wait(mb, value, 0);
  1272. }
  1273. RTM_EXPORT(rt_mb_send);
  1274. /**
  1275. * This function will receive a mail from mailbox object, if there is no mail
  1276. * in mailbox object, the thread shall wait for a specified time.
  1277. *
  1278. * @param mb the mailbox object
  1279. * @param value the received mail will be saved in
  1280. * @param timeout the waiting time
  1281. *
  1282. * @return the error code
  1283. */
  1284. rt_err_t rt_mb_recv(rt_mailbox_t mb, rt_uint32_t *value, rt_int32_t timeout)
  1285. {
  1286. struct rt_thread *thread;
  1287. register rt_ubase_t temp;
  1288. rt_uint32_t tick_delta;
  1289. /* parameter check */
  1290. RT_ASSERT(mb != RT_NULL);
  1291. /* initialize delta tick */
  1292. tick_delta = 0;
  1293. /* get current thread */
  1294. thread = rt_thread_self();
  1295. RT_OBJECT_HOOK_CALL(rt_object_trytake_hook, (&(mb->parent.parent)));
  1296. /* disable interrupt */
  1297. temp = rt_hw_interrupt_disable();
  1298. /* for non-blocking call */
  1299. if (mb->entry == 0 && timeout == 0)
  1300. {
  1301. rt_hw_interrupt_enable(temp);
  1302. return -RT_ETIMEOUT;
  1303. }
  1304. /* mailbox is empty */
  1305. while (mb->entry == 0)
  1306. {
  1307. /* reset error number in thread */
  1308. thread->error = RT_EOK;
  1309. /* no waiting, return timeout */
  1310. if (timeout == 0)
  1311. {
  1312. /* enable interrupt */
  1313. rt_hw_interrupt_enable(temp);
  1314. thread->error = -RT_ETIMEOUT;
  1315. return -RT_ETIMEOUT;
  1316. }
  1317. RT_DEBUG_IN_THREAD_CONTEXT;
  1318. /* suspend current thread */
  1319. rt_ipc_list_suspend(&(mb->parent.suspend_thread),
  1320. thread,
  1321. mb->parent.parent.flag);
  1322. /* has waiting time, start thread timer */
  1323. if (timeout > 0)
  1324. {
  1325. /* get the start tick of timer */
  1326. tick_delta = rt_tick_get();
  1327. RT_DEBUG_LOG(RT_DEBUG_IPC, ("mb_recv: start timer of thread:%s\n",
  1328. thread->name));
  1329. /* reset the timeout of thread timer and start it */
  1330. rt_timer_control(&(thread->thread_timer),
  1331. RT_TIMER_CTRL_SET_TIME,
  1332. &timeout);
  1333. rt_timer_start(&(thread->thread_timer));
  1334. }
  1335. /* enable interrupt */
  1336. rt_hw_interrupt_enable(temp);
  1337. /* re-schedule */
  1338. rt_schedule();
  1339. /* resume from suspend state */
  1340. if (thread->error != RT_EOK)
  1341. {
  1342. /* return error */
  1343. return thread->error;
  1344. }
  1345. /* disable interrupt */
  1346. temp = rt_hw_interrupt_disable();
  1347. /* if it's not waiting forever and then re-calculate timeout tick */
  1348. if (timeout > 0)
  1349. {
  1350. tick_delta = rt_tick_get() - tick_delta;
  1351. timeout -= tick_delta;
  1352. if (timeout < 0)
  1353. timeout = 0;
  1354. }
  1355. }
  1356. /* fill ptr */
  1357. *value = mb->msg_pool[mb->out_offset];
  1358. /* increase output offset */
  1359. ++ mb->out_offset;
  1360. if (mb->out_offset >= mb->size)
  1361. mb->out_offset = 0;
  1362. /* decrease message entry */
  1363. mb->entry --;
  1364. /* resume suspended thread */
  1365. if (!rt_list_isempty(&(mb->suspend_sender_thread)))
  1366. {
  1367. rt_ipc_list_resume(&(mb->suspend_sender_thread));
  1368. /* enable interrupt */
  1369. rt_hw_interrupt_enable(temp);
  1370. RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(mb->parent.parent)));
  1371. rt_schedule();
  1372. return RT_EOK;
  1373. }
  1374. /* enable interrupt */
  1375. rt_hw_interrupt_enable(temp);
  1376. RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(mb->parent.parent)));
  1377. return RT_EOK;
  1378. }
  1379. RTM_EXPORT(rt_mb_recv);
  1380. /**
  1381. * This function can get or set some extra attributions of a mailbox object.
  1382. *
  1383. * @param mb the mailbox object
  1384. * @param cmd the execution command
  1385. * @param arg the execution argument
  1386. *
  1387. * @return the error code
  1388. */
  1389. rt_err_t rt_mb_control(rt_mailbox_t mb, rt_uint8_t cmd, void *arg)
  1390. {
  1391. rt_ubase_t level;
  1392. RT_ASSERT(mb != RT_NULL);
  1393. if (cmd == RT_IPC_CMD_RESET)
  1394. {
  1395. /* disable interrupt */
  1396. level = rt_hw_interrupt_disable();
  1397. /* resume all waiting thread */
  1398. rt_ipc_list_resume_all(&(mb->parent.suspend_thread));
  1399. /* also resume all mailbox private suspended thread */
  1400. rt_ipc_list_resume_all(&(mb->suspend_sender_thread));
  1401. /* re-init mailbox */
  1402. mb->entry = 0;
  1403. mb->in_offset = 0;
  1404. mb->out_offset = 0;
  1405. /* enable interrupt */
  1406. rt_hw_interrupt_enable(level);
  1407. rt_schedule();
  1408. return RT_EOK;
  1409. }
  1410. return -RT_ERROR;
  1411. }
  1412. RTM_EXPORT(rt_mb_control);
  1413. #endif /* end of RT_USING_MAILBOX */
  1414. #ifdef RT_USING_MESSAGEQUEUE
  1415. struct rt_mq_message
  1416. {
  1417. struct rt_mq_message *next;
  1418. };
  1419. /**
  1420. * This function will initialize a message queue and put it under control of
  1421. * resource management.
  1422. *
  1423. * @param mq the message object
  1424. * @param name the name of message queue
  1425. * @param msgpool the beginning address of buffer to save messages
  1426. * @param msg_size the maximum size of message
  1427. * @param pool_size the size of buffer to save messages
  1428. * @param flag the flag of message queue
  1429. *
  1430. * @return the operation status, RT_EOK on successful
  1431. */
  1432. rt_err_t rt_mq_init(rt_mq_t mq,
  1433. const char *name,
  1434. void *msgpool,
  1435. rt_size_t msg_size,
  1436. rt_size_t pool_size,
  1437. rt_uint8_t flag)
  1438. {
  1439. struct rt_mq_message *head;
  1440. register rt_base_t temp;
  1441. /* parameter check */
  1442. RT_ASSERT(mq != RT_NULL);
  1443. /* init object */
  1444. rt_object_init(&(mq->parent.parent), RT_Object_Class_MessageQueue, name);
  1445. /* set parent flag */
  1446. mq->parent.parent.flag = flag;
  1447. /* init ipc object */
  1448. rt_ipc_object_init(&(mq->parent));
  1449. /* set messasge pool */
  1450. mq->msg_pool = msgpool;
  1451. /* get correct message size */
  1452. mq->msg_size = RT_ALIGN(msg_size, RT_ALIGN_SIZE);
  1453. mq->max_msgs = pool_size / (mq->msg_size + sizeof(struct rt_mq_message));
  1454. /* init message list */
  1455. mq->msg_queue_head = RT_NULL;
  1456. mq->msg_queue_tail = RT_NULL;
  1457. /* init message empty list */
  1458. mq->msg_queue_free = RT_NULL;
  1459. for (temp = 0; temp < mq->max_msgs; temp ++)
  1460. {
  1461. head = (struct rt_mq_message *)((rt_uint8_t *)mq->msg_pool +
  1462. temp * (mq->msg_size + sizeof(struct rt_mq_message)));
  1463. head->next = mq->msg_queue_free;
  1464. mq->msg_queue_free = head;
  1465. }
  1466. /* the initial entry is zero */
  1467. mq->entry = 0;
  1468. return RT_EOK;
  1469. }
  1470. RTM_EXPORT(rt_mq_init);
  1471. /**
  1472. * This function will detach a message queue object from resource management
  1473. *
  1474. * @param mq the message queue object
  1475. *
  1476. * @return the operation status, RT_EOK on successful
  1477. */
  1478. rt_err_t rt_mq_detach(rt_mq_t mq)
  1479. {
  1480. /* parameter check */
  1481. RT_ASSERT(mq != RT_NULL);
  1482. /* resume all suspended thread */
  1483. rt_ipc_list_resume_all(&mq->parent.suspend_thread);
  1484. /* detach message queue object */
  1485. rt_object_detach(&(mq->parent.parent));
  1486. return RT_EOK;
  1487. }
  1488. RTM_EXPORT(rt_mq_detach);
  1489. #ifdef RT_USING_HEAP
  1490. /**
  1491. * This function will create a message queue object from system resource
  1492. *
  1493. * @param name the name of message queue
  1494. * @param msg_size the size of message
  1495. * @param max_msgs the maximum number of message in queue
  1496. * @param flag the flag of message queue
  1497. *
  1498. * @return the created message queue, RT_NULL on error happen
  1499. */
  1500. rt_mq_t rt_mq_create(const char *name,
  1501. rt_size_t msg_size,
  1502. rt_size_t max_msgs,
  1503. rt_uint8_t flag)
  1504. {
  1505. struct rt_messagequeue *mq;
  1506. struct rt_mq_message *head;
  1507. register rt_base_t temp;
  1508. RT_DEBUG_NOT_IN_INTERRUPT;
  1509. /* allocate object */
  1510. mq = (rt_mq_t)rt_object_allocate(RT_Object_Class_MessageQueue, name);
  1511. if (mq == RT_NULL)
  1512. return mq;
  1513. /* set parent */
  1514. mq->parent.parent.flag = flag;
  1515. /* init ipc object */
  1516. rt_ipc_object_init(&(mq->parent));
  1517. /* init message queue */
  1518. /* get correct message size */
  1519. mq->msg_size = RT_ALIGN(msg_size, RT_ALIGN_SIZE);
  1520. mq->max_msgs = max_msgs;
  1521. /* allocate message pool */
  1522. mq->msg_pool = RT_KERNEL_MALLOC((mq->msg_size + sizeof(struct rt_mq_message))* mq->max_msgs);
  1523. if (mq->msg_pool == RT_NULL)
  1524. {
  1525. rt_mq_delete(mq);
  1526. return RT_NULL;
  1527. }
  1528. /* init message list */
  1529. mq->msg_queue_head = RT_NULL;
  1530. mq->msg_queue_tail = RT_NULL;
  1531. /* init message empty list */
  1532. mq->msg_queue_free = RT_NULL;
  1533. for (temp = 0; temp < mq->max_msgs; temp ++)
  1534. {
  1535. head = (struct rt_mq_message *)((rt_uint8_t *)mq->msg_pool +
  1536. temp * (mq->msg_size + sizeof(struct rt_mq_message)));
  1537. head->next = mq->msg_queue_free;
  1538. mq->msg_queue_free = head;
  1539. }
  1540. /* the initial entry is zero */
  1541. mq->entry = 0;
  1542. return mq;
  1543. }
  1544. RTM_EXPORT(rt_mq_create);
  1545. /**
  1546. * This function will delete a message queue object and release the memory
  1547. *
  1548. * @param mq the message queue object
  1549. *
  1550. * @return the error code
  1551. */
  1552. rt_err_t rt_mq_delete(rt_mq_t mq)
  1553. {
  1554. RT_DEBUG_NOT_IN_INTERRUPT;
  1555. /* parameter check */
  1556. RT_ASSERT(mq != RT_NULL);
  1557. /* resume all suspended thread */
  1558. rt_ipc_list_resume_all(&(mq->parent.suspend_thread));
  1559. #if defined(RT_USING_MODULE) && defined(RT_USING_SLAB)
  1560. /* the mq object belongs to an application module */
  1561. if (mq->parent.parent.flag & RT_OBJECT_FLAG_MODULE)
  1562. rt_module_free(mq->parent.parent.module_id, mq->msg_pool);
  1563. else
  1564. #endif
  1565. /* free message queue pool */
  1566. RT_KERNEL_FREE(mq->msg_pool);
  1567. /* delete message queue object */
  1568. rt_object_delete(&(mq->parent.parent));
  1569. return RT_EOK;
  1570. }
  1571. RTM_EXPORT(rt_mq_delete);
  1572. #endif
  1573. /**
  1574. * This function will send a message to message queue object, if there are
  1575. * threads suspended on message queue object, it will be waked up.
  1576. *
  1577. * @param mq the message queue object
  1578. * @param buffer the message
  1579. * @param size the size of buffer
  1580. *
  1581. * @return the error code
  1582. */
  1583. rt_err_t rt_mq_send(rt_mq_t mq, void *buffer, rt_size_t size)
  1584. {
  1585. register rt_ubase_t temp;
  1586. struct rt_mq_message *msg;
  1587. RT_ASSERT(mq != RT_NULL);
  1588. RT_ASSERT(buffer != RT_NULL);
  1589. RT_ASSERT(size != 0);
  1590. /* greater than one message size */
  1591. if (size > mq->msg_size)
  1592. return -RT_ERROR;
  1593. RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(mq->parent.parent)));
  1594. /* disable interrupt */
  1595. temp = rt_hw_interrupt_disable();
  1596. /* get a free list, there must be an empty item */
  1597. msg = (struct rt_mq_message*)mq->msg_queue_free;
  1598. /* message queue is full */
  1599. if (msg == RT_NULL)
  1600. {
  1601. /* enable interrupt */
  1602. rt_hw_interrupt_enable(temp);
  1603. return -RT_EFULL;
  1604. }
  1605. /* move free list pointer */
  1606. mq->msg_queue_free = msg->next;
  1607. /* enable interrupt */
  1608. rt_hw_interrupt_enable(temp);
  1609. /* the msg is the new tailer of list, the next shall be NULL */
  1610. msg->next = RT_NULL;
  1611. /* copy buffer */
  1612. rt_memcpy(msg + 1, buffer, size);
  1613. /* disable interrupt */
  1614. temp = rt_hw_interrupt_disable();
  1615. /* link msg to message queue */
  1616. if (mq->msg_queue_tail != RT_NULL)
  1617. {
  1618. /* if the tail exists, */
  1619. ((struct rt_mq_message *)mq->msg_queue_tail)->next = msg;
  1620. }
  1621. /* set new tail */
  1622. mq->msg_queue_tail = msg;
  1623. /* if the head is empty, set head */
  1624. if (mq->msg_queue_head == RT_NULL)
  1625. mq->msg_queue_head = msg;
  1626. /* increase message entry */
  1627. mq->entry ++;
  1628. /* resume suspended thread */
  1629. if (!rt_list_isempty(&mq->parent.suspend_thread))
  1630. {
  1631. rt_ipc_list_resume(&(mq->parent.suspend_thread));
  1632. /* enable interrupt */
  1633. rt_hw_interrupt_enable(temp);
  1634. rt_schedule();
  1635. return RT_EOK;
  1636. }
  1637. /* enable interrupt */
  1638. rt_hw_interrupt_enable(temp);
  1639. return RT_EOK;
  1640. }
  1641. RTM_EXPORT(rt_mq_send);
  1642. /**
  1643. * This function will send an urgent message to message queue object, which
  1644. * means the message will be inserted to the head of message queue. If there
  1645. * are threads suspended on message queue object, it will be waked up.
  1646. *
  1647. * @param mq the message queue object
  1648. * @param buffer the message
  1649. * @param size the size of buffer
  1650. *
  1651. * @return the error code
  1652. */
  1653. rt_err_t rt_mq_urgent(rt_mq_t mq, void *buffer, rt_size_t size)
  1654. {
  1655. register rt_ubase_t temp;
  1656. struct rt_mq_message *msg;
  1657. RT_ASSERT(mq != RT_NULL);
  1658. RT_ASSERT(buffer != RT_NULL);
  1659. RT_ASSERT(size != 0);
  1660. /* greater than one message size */
  1661. if (size > mq->msg_size)
  1662. return -RT_ERROR;
  1663. RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(mq->parent.parent)));
  1664. /* disable interrupt */
  1665. temp = rt_hw_interrupt_disable();
  1666. /* get a free list, there must be an empty item */
  1667. msg = (struct rt_mq_message *)mq->msg_queue_free;
  1668. /* message queue is full */
  1669. if (msg == RT_NULL)
  1670. {
  1671. /* enable interrupt */
  1672. rt_hw_interrupt_enable(temp);
  1673. return -RT_EFULL;
  1674. }
  1675. /* move free list pointer */
  1676. mq->msg_queue_free = msg->next;
  1677. /* enable interrupt */
  1678. rt_hw_interrupt_enable(temp);
  1679. /* copy buffer */
  1680. rt_memcpy(msg + 1, buffer, size);
  1681. /* disable interrupt */
  1682. temp = rt_hw_interrupt_disable();
  1683. /* link msg to the beginning of message queue */
  1684. msg->next = mq->msg_queue_head;
  1685. mq->msg_queue_head = msg;
  1686. /* if there is no tail */
  1687. if (mq->msg_queue_tail == RT_NULL)
  1688. mq->msg_queue_tail = msg;
  1689. /* increase message entry */
  1690. mq->entry ++;
  1691. /* resume suspended thread */
  1692. if (!rt_list_isempty(&mq->parent.suspend_thread))
  1693. {
  1694. rt_ipc_list_resume(&(mq->parent.suspend_thread));
  1695. /* enable interrupt */
  1696. rt_hw_interrupt_enable(temp);
  1697. rt_schedule();
  1698. return RT_EOK;
  1699. }
  1700. /* enable interrupt */
  1701. rt_hw_interrupt_enable(temp);
  1702. return RT_EOK;
  1703. }
  1704. RTM_EXPORT(rt_mq_urgent);
  1705. /**
  1706. * This function will receive a message from message queue object, if there is
  1707. * no message in message queue object, the thread shall wait for a specified
  1708. * time.
  1709. *
  1710. * @param mq the message queue object
  1711. * @param buffer the received message will be saved in
  1712. * @param size the size of buffer
  1713. * @param timeout the waiting time
  1714. *
  1715. * @return the error code
  1716. */
  1717. rt_err_t rt_mq_recv(rt_mq_t mq,
  1718. void *buffer,
  1719. rt_size_t size,
  1720. rt_int32_t timeout)
  1721. {
  1722. struct rt_thread *thread;
  1723. register rt_ubase_t temp;
  1724. struct rt_mq_message *msg;
  1725. rt_uint32_t tick_delta;
  1726. RT_ASSERT(mq != RT_NULL);
  1727. RT_ASSERT(buffer != RT_NULL);
  1728. RT_ASSERT(size != 0);
  1729. /* initialize delta tick */
  1730. tick_delta = 0;
  1731. /* get current thread */
  1732. thread = rt_thread_self();
  1733. RT_OBJECT_HOOK_CALL(rt_object_trytake_hook, (&(mq->parent.parent)));
  1734. /* disable interrupt */
  1735. temp = rt_hw_interrupt_disable();
  1736. /* for non-blocking call */
  1737. if (mq->entry == 0 && timeout == 0)
  1738. {
  1739. rt_hw_interrupt_enable(temp);
  1740. return -RT_ETIMEOUT;
  1741. }
  1742. /* message queue is empty */
  1743. while (mq->entry == 0)
  1744. {
  1745. RT_DEBUG_IN_THREAD_CONTEXT;
  1746. /* reset error number in thread */
  1747. thread->error = RT_EOK;
  1748. /* no waiting, return timeout */
  1749. if (timeout == 0)
  1750. {
  1751. /* enable interrupt */
  1752. rt_hw_interrupt_enable(temp);
  1753. thread->error = -RT_ETIMEOUT;
  1754. return -RT_ETIMEOUT;
  1755. }
  1756. /* suspend current thread */
  1757. rt_ipc_list_suspend(&(mq->parent.suspend_thread),
  1758. thread,
  1759. mq->parent.parent.flag);
  1760. /* has waiting time, start thread timer */
  1761. if (timeout > 0)
  1762. {
  1763. /* get the start tick of timer */
  1764. tick_delta = rt_tick_get();
  1765. RT_DEBUG_LOG(RT_DEBUG_IPC, ("set thread:%s to timer list\n",
  1766. thread->name));
  1767. /* reset the timeout of thread timer and start it */
  1768. rt_timer_control(&(thread->thread_timer),
  1769. RT_TIMER_CTRL_SET_TIME,
  1770. &timeout);
  1771. rt_timer_start(&(thread->thread_timer));
  1772. }
  1773. /* enable interrupt */
  1774. rt_hw_interrupt_enable(temp);
  1775. /* re-schedule */
  1776. rt_schedule();
  1777. /* recv message */
  1778. if (thread->error != RT_EOK)
  1779. {
  1780. /* return error */
  1781. return thread->error;
  1782. }
  1783. /* disable interrupt */
  1784. temp = rt_hw_interrupt_disable();
  1785. /* if it's not waiting forever and then re-calculate timeout tick */
  1786. if (timeout > 0)
  1787. {
  1788. tick_delta = rt_tick_get() - tick_delta;
  1789. timeout -= tick_delta;
  1790. if (timeout < 0)
  1791. timeout = 0;
  1792. }
  1793. }
  1794. /* get message from queue */
  1795. msg = (struct rt_mq_message *)mq->msg_queue_head;
  1796. /* move message queue head */
  1797. mq->msg_queue_head = msg->next;
  1798. /* reach queue tail, set to NULL */
  1799. if (mq->msg_queue_tail == msg)
  1800. mq->msg_queue_tail = RT_NULL;
  1801. /* decrease message entry */
  1802. mq->entry --;
  1803. /* enable interrupt */
  1804. rt_hw_interrupt_enable(temp);
  1805. /* copy message */
  1806. rt_memcpy(buffer, msg + 1, size > mq->msg_size ? mq->msg_size : size);
  1807. /* disable interrupt */
  1808. temp = rt_hw_interrupt_disable();
  1809. /* put message to free list */
  1810. msg->next = (struct rt_mq_message *)mq->msg_queue_free;
  1811. mq->msg_queue_free = msg;
  1812. /* enable interrupt */
  1813. rt_hw_interrupt_enable(temp);
  1814. RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(mq->parent.parent)));
  1815. return RT_EOK;
  1816. }
  1817. RTM_EXPORT(rt_mq_recv);
  1818. /**
  1819. * This function can get or set some extra attributions of a message queue
  1820. * object.
  1821. *
  1822. * @param mq the message queue object
  1823. * @param cmd the execution command
  1824. * @param arg the execution argument
  1825. *
  1826. * @return the error code
  1827. */
  1828. rt_err_t rt_mq_control(rt_mq_t mq, rt_uint8_t cmd, void *arg)
  1829. {
  1830. rt_ubase_t level;
  1831. struct rt_mq_message *msg;
  1832. RT_ASSERT(mq != RT_NULL);
  1833. if (cmd == RT_IPC_CMD_RESET)
  1834. {
  1835. /* disable interrupt */
  1836. level = rt_hw_interrupt_disable();
  1837. /* resume all waiting thread */
  1838. rt_ipc_list_resume_all(&mq->parent.suspend_thread);
  1839. /* release all message in the queue */
  1840. while (mq->msg_queue_head != RT_NULL)
  1841. {
  1842. /* get message from queue */
  1843. msg = (struct rt_mq_message *)mq->msg_queue_head;
  1844. /* move message queue head */
  1845. mq->msg_queue_head = msg->next;
  1846. /* reach queue tail, set to NULL */
  1847. if (mq->msg_queue_tail == msg)
  1848. mq->msg_queue_tail = RT_NULL;
  1849. /* put message to free list */
  1850. msg->next = (struct rt_mq_message *)mq->msg_queue_free;
  1851. mq->msg_queue_free = msg;
  1852. }
  1853. /* clean entry */
  1854. mq->entry = 0;
  1855. /* enable interrupt */
  1856. rt_hw_interrupt_enable(level);
  1857. rt_schedule();
  1858. return RT_EOK;
  1859. }
  1860. return -RT_ERROR;
  1861. }
  1862. RTM_EXPORT(rt_mq_control);
  1863. #endif /* end of RT_USING_MESSAGEQUEUE */
  1864. /**@}*/