ipc.c 69 KB

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