ipc.c 58 KB

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