ipc.c 61 KB

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