ipc.c 65 KB

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