ipc.c 47 KB

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