ipc.c 67 KB

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