ipc.c 61 KB

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