ipc.c 47 KB

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