ipc.c 42 KB

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