ipc.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899
  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. * 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. */
  32. #include <rtthread.h>
  33. #include <rthw.h>
  34. #include "kservice.h"
  35. /* #define RT_IPC_DEBUG */
  36. #ifdef RT_USING_HOOK
  37. extern void (*rt_object_trytake_hook)(struct rt_object* object);
  38. extern void (*rt_object_take_hook)(struct rt_object* object);
  39. extern void (*rt_object_put_hook)(struct rt_object* object);
  40. #endif
  41. /**
  42. * @addtogroup IPC
  43. */
  44. /*@{*/
  45. /**
  46. * This function will initialize an IPC object
  47. *
  48. * @param ipc the IPC object
  49. *
  50. * @return the operation status, RT_EOK on successful
  51. */
  52. rt_inline rt_err_t rt_ipc_object_init(struct rt_ipc_object *ipc)
  53. {
  54. /* init ipc object */
  55. rt_list_init(&(ipc->suspend_thread));
  56. ipc->suspend_thread_count = 0;
  57. return RT_EOK;
  58. }
  59. /**
  60. * This function will suspend a thread for a specified IPC object and put the
  61. * thread into suspend queue of IPC object
  62. *
  63. * @param ipc the IPC object
  64. * @param thread the thread object to be suspended
  65. *
  66. * @return the operation status, RT_EOK on successful
  67. */
  68. rt_inline rt_err_t rt_ipc_object_suspend(struct rt_ipc_object *ipc, struct rt_thread *thread)
  69. {
  70. /* suspend thread */
  71. rt_thread_suspend(thread);
  72. ipc->suspend_thread_count ++;
  73. switch (ipc->parent.flag)
  74. {
  75. case RT_IPC_FLAG_FIFO:
  76. rt_list_insert_before(&(ipc->suspend_thread), &(thread->tlist));
  77. break;
  78. case RT_IPC_FLAG_PRIO:
  79. {
  80. struct rt_list_node* n;
  81. struct rt_thread* sthread;
  82. /* find a suitable position */
  83. for (n = ipc->suspend_thread.next; n != &(ipc->suspend_thread);
  84. n = n->next)
  85. {
  86. sthread = rt_list_entry(n, struct rt_thread, tlist);
  87. /* find out */
  88. if (thread->current_priority < sthread->current_priority) break;
  89. }
  90. rt_list_insert_before(&(ipc->suspend_thread), &(thread->tlist));
  91. }
  92. break;
  93. }
  94. return RT_EOK;
  95. }
  96. /**
  97. * This function will resume a thread from an IPC object:
  98. * - remove the thread from suspend queue of IPC object
  99. * - put the thread into system ready queue
  100. *
  101. * @param ipc the IPC object
  102. *
  103. * @return the operation status, RT_EOK on successful
  104. */
  105. rt_inline rt_err_t rt_ipc_object_resume(struct rt_ipc_object* ipc)
  106. {
  107. struct rt_thread *thread;
  108. /* get thread entry */
  109. thread = rt_list_entry(ipc->suspend_thread.next, struct rt_thread, tlist);
  110. #ifdef RT_IPC_DEBUG
  111. rt_kprintf("resume thread:%s\n", thread->name);
  112. #endif
  113. /* resume it */
  114. rt_thread_resume(thread);
  115. /* decrease suspended thread count */
  116. ipc->suspend_thread_count --;
  117. return RT_EOK;
  118. }
  119. /**
  120. * This function will resume all suspended threads in an IPC object.
  121. *
  122. * @param ipc the IPC object
  123. *
  124. * @return the operation status, RT_EOK on successful
  125. */
  126. rt_inline rt_err_t rt_ipc_object_resume_all(struct rt_ipc_object* ipc)
  127. {
  128. struct rt_thread* thread;
  129. register rt_ubase_t temp;
  130. /* wakeup all suspend threads */
  131. while (!rt_list_isempty(&(ipc->suspend_thread)))
  132. {
  133. /* disable interrupt */
  134. temp = rt_hw_interrupt_disable();
  135. /* get next suspend thread */
  136. thread = rt_list_entry(ipc->suspend_thread.next, struct rt_thread, tlist);
  137. /* set error code to RT_ERROR */
  138. thread->error = -RT_ERROR;
  139. /*
  140. * resume thread
  141. * In rt_thread_resume function, it will remove current thread from
  142. * suspend list
  143. */
  144. rt_thread_resume(thread);
  145. /* decrease suspended thread count */
  146. ipc->suspend_thread_count --;
  147. /* enable interrupt */
  148. rt_hw_interrupt_enable(temp);
  149. }
  150. return RT_EOK;
  151. }
  152. /* decrease ipc suspended thread number when thread can not take resource successfully */
  153. rt_inline void rt_ipc_object_decrease(struct rt_ipc_object* ipc)
  154. {
  155. register rt_ubase_t level;
  156. /* disable interrupt */
  157. level = rt_hw_interrupt_disable();
  158. /* decrease suspended thread count */
  159. ipc->suspend_thread_count --;
  160. /* enable interrupt */
  161. rt_hw_interrupt_enable(level);
  162. }
  163. #ifdef RT_USING_SEMAPHORE
  164. /**
  165. * This function will initialize a semaphore and put it under control of resource
  166. * management.
  167. *
  168. * @param sem the semaphore object
  169. * @param name the name of semaphore
  170. * @param value the init value of semaphore
  171. * @param flag the flag of semaphore
  172. *
  173. * @return the operation status, RT_EOK on successful
  174. */
  175. rt_err_t rt_sem_init (rt_sem_t sem, const char* name, rt_uint32_t value, rt_uint8_t flag)
  176. {
  177. RT_ASSERT(sem != RT_NULL);
  178. /* init object */
  179. rt_object_init(&(sem->parent.parent), RT_Object_Class_Semaphore, name);
  180. /* init ipc object */
  181. rt_ipc_object_init(&(sem->parent));
  182. /* set init value */
  183. sem->value = value;
  184. /* set parent */
  185. sem->parent.parent.flag = flag;
  186. return RT_EOK;
  187. }
  188. /**
  189. * This function will detach a semaphore from resource management
  190. *
  191. * @param sem the semaphore object
  192. *
  193. * @return the operation status, RT_EOK on successful
  194. *
  195. * @see rt_sem_delete
  196. */
  197. rt_err_t rt_sem_detach (rt_sem_t sem)
  198. {
  199. RT_ASSERT(sem != RT_NULL);
  200. /* wakeup all suspend threads */
  201. rt_ipc_object_resume_all(&(sem->parent));
  202. /* detach semaphore object */
  203. rt_object_detach(&(sem->parent.parent));
  204. return RT_EOK;
  205. }
  206. #ifdef RT_USING_HEAP
  207. /**
  208. * This function will create a semaphore from system resource
  209. *
  210. * @param name the name of semaphore
  211. * @param value the init value of semaphore
  212. * @param flag the flag of semaphore
  213. *
  214. * @return the created semaphore, RT_NULL on error happen
  215. *
  216. * @see rt_sem_init
  217. */
  218. rt_sem_t rt_sem_create (const char* name, rt_uint32_t value, rt_uint8_t flag)
  219. {
  220. rt_sem_t sem;
  221. /* allocate object */
  222. sem = (rt_sem_t) rt_object_allocate(RT_Object_Class_Semaphore, name);
  223. if (sem == RT_NULL) return sem;
  224. /* init ipc object */
  225. rt_ipc_object_init(&(sem->parent));
  226. /* set init value */
  227. sem->value = value;
  228. /* set parent */
  229. sem->parent.parent.flag = flag;
  230. return sem;
  231. }
  232. /**
  233. * This function will delete a semaphore object and release the memory
  234. *
  235. * @param sem the semaphore object
  236. *
  237. * @return the error code
  238. *
  239. * @see rt_sem_detach
  240. */
  241. rt_err_t rt_sem_delete (rt_sem_t sem)
  242. {
  243. RT_ASSERT(sem != RT_NULL);
  244. /* wakeup all suspend threads */
  245. rt_ipc_object_resume_all(&(sem->parent));
  246. /* delete semaphore object */
  247. rt_object_delete(&(sem->parent.parent));
  248. return RT_EOK;
  249. }
  250. #endif
  251. /**
  252. * This function will take a semaphore, if the semaphore is unavailable, the
  253. * thread shall wait for a specified time.
  254. *
  255. * @param sem the semaphore object
  256. * @param time the waiting time
  257. *
  258. * @return the error code
  259. */
  260. rt_err_t rt_sem_take (rt_sem_t sem, rt_int32_t time)
  261. {
  262. register rt_base_t temp;
  263. struct rt_thread* thread;
  264. RT_ASSERT(sem != RT_NULL);
  265. #ifdef RT_USING_HOOK
  266. if (rt_object_trytake_hook != RT_NULL) rt_object_trytake_hook(&(sem->parent.parent));
  267. #endif
  268. /* disable interrupt */
  269. temp = rt_hw_interrupt_disable();
  270. #ifdef RT_IPC_DEBUG
  271. rt_kprintf("thread %s take sem:%s, which value is: %d\n", rt_thread_self()->name,
  272. ((struct rt_object*)sem)->name, sem->value);
  273. #endif
  274. if (sem->value > 0)
  275. {
  276. /* semaphore is available */
  277. sem->value --;
  278. /* enable interrupt */
  279. rt_hw_interrupt_enable(temp);
  280. }
  281. else
  282. {
  283. /* no waiting, return with timeout */
  284. if (time == 0 )
  285. {
  286. rt_hw_interrupt_enable(temp);
  287. return -RT_ETIMEOUT;
  288. }
  289. else
  290. {
  291. /* semaphore is unavailable, push to suspend list */
  292. /* get current thread */
  293. thread = rt_thread_self();
  294. /* reset thread error number */
  295. thread->error = RT_EOK;
  296. #ifdef RT_IPC_DEBUG
  297. rt_kprintf("sem take: suspend thread - %s\n", thread->name);
  298. #endif
  299. /* suspend thread */
  300. rt_ipc_object_suspend(&(sem->parent), thread);
  301. /* has waiting time, start thread timer */
  302. if (time > 0)
  303. {
  304. #ifdef RT_IPC_DEBUG
  305. rt_kprintf("set thread:%s to timer list\n", thread->name);
  306. #endif
  307. /* reset the timeout of thread timer and start it */
  308. rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &time);
  309. rt_timer_start(&(thread->thread_timer));
  310. }
  311. /* enable interrupt */
  312. rt_hw_interrupt_enable(temp);
  313. /* do schedule */
  314. rt_schedule();
  315. if (thread->error != RT_EOK)
  316. {
  317. /* decrease suspended thread count */
  318. rt_ipc_object_decrease(&(sem->parent));
  319. return thread->error;
  320. }
  321. }
  322. }
  323. #ifdef RT_USING_HOOK
  324. if (rt_object_take_hook != RT_NULL) rt_object_take_hook(&(sem->parent.parent));
  325. #endif
  326. return RT_EOK;
  327. }
  328. /**
  329. * This function will try to take a semaphore and immediately return
  330. *
  331. * @param sem the semaphore object
  332. *
  333. * @return the error code
  334. */
  335. rt_err_t rt_sem_trytake(rt_sem_t sem)
  336. {
  337. return rt_sem_take(sem, 0);
  338. }
  339. /**
  340. * This function will release a semaphore, if there are threads suspended on
  341. * semaphore, it will be waked up.
  342. *
  343. * @param sem the semaphore object
  344. *
  345. * @return the error code
  346. */
  347. rt_err_t rt_sem_release(rt_sem_t sem)
  348. {
  349. register rt_base_t temp;
  350. register rt_bool_t need_schedule;
  351. #ifdef RT_USING_HOOK
  352. if (rt_object_put_hook != RT_NULL) rt_object_put_hook(&(sem->parent.parent));
  353. #endif
  354. need_schedule = RT_FALSE;
  355. /* disable interrupt */
  356. temp = rt_hw_interrupt_disable();
  357. #ifdef RT_IPC_DEBUG
  358. rt_kprintf("thread %s releases sem:%s, which value is: %d\n", rt_thread_self()->name,
  359. ((struct rt_object*)sem)->name, sem->value);
  360. #endif
  361. if (sem->parent.suspend_thread_count > 0)
  362. {
  363. /* resume the suspended thread */
  364. rt_ipc_object_resume(&(sem->parent));
  365. need_schedule = RT_TRUE;
  366. }
  367. else sem->value ++; /* increase value */
  368. /* enable interrupt */
  369. rt_hw_interrupt_enable(temp);
  370. /* resume a thread, re-schedule */
  371. if (need_schedule == RT_TRUE) rt_schedule();
  372. return RT_EOK;
  373. }
  374. /**
  375. * This function can get or set some extra attributions of a semaphore object.
  376. *
  377. * @param sem the semaphore object
  378. * @param cmd the execution command
  379. * @param arg the execution argument
  380. *
  381. * @return the error code
  382. */
  383. rt_err_t rt_sem_control(rt_sem_t sem, rt_uint8_t cmd, void* arg)
  384. {
  385. return RT_EOK;
  386. }
  387. #endif /* end of RT_USING_SEMAPHORE */
  388. #ifdef RT_USING_MUTEX
  389. /**
  390. * This function will initialize a mutex and put it under control of resource
  391. * management.
  392. *
  393. * @param mutex the mutex object
  394. * @param name the name of mutex
  395. * @param flag the flag of mutex
  396. *
  397. * @return the operation status, RT_EOK on successful
  398. */
  399. rt_err_t rt_mutex_init (rt_mutex_t mutex, const char* name, rt_uint8_t flag)
  400. {
  401. RT_ASSERT(mutex != RT_NULL);
  402. /* init object */
  403. rt_object_init(&(mutex->parent.parent), RT_Object_Class_Mutex, name);
  404. /* init ipc object */
  405. rt_ipc_object_init(&(mutex->parent));
  406. mutex->value = 1;
  407. mutex->owner = RT_NULL;
  408. mutex->original_priority = 0xFF;
  409. mutex->hold = 0;
  410. /* set flag */
  411. mutex->parent.parent.flag = flag;
  412. return RT_EOK;
  413. }
  414. /**
  415. * This function will detach a mutex from resource management
  416. *
  417. * @param mutex the mutex object
  418. *
  419. * @return the operation status, RT_EOK on successful
  420. *
  421. * @see rt_mutex_delete
  422. */
  423. rt_err_t rt_mutex_detach (rt_mutex_t mutex)
  424. {
  425. RT_ASSERT(mutex != RT_NULL);
  426. /* wakeup all suspend threads */
  427. rt_ipc_object_resume_all(&(mutex->parent));
  428. /* detach semaphore object */
  429. rt_object_detach(&(mutex->parent.parent));
  430. return RT_EOK;
  431. }
  432. #ifdef RT_USING_HEAP
  433. /**
  434. * This function will create a mutex from system resource
  435. *
  436. * @param name the name of mutex
  437. * @param flag the flag of mutex
  438. *
  439. * @return the created mutex, RT_NULL on error happen
  440. *
  441. * @see rt_mutex_init
  442. */
  443. rt_mutex_t rt_mutex_create (const char* name, rt_uint8_t flag)
  444. {
  445. struct rt_mutex *mutex;
  446. /* allocate object */
  447. mutex = (rt_mutex_t) rt_object_allocate(RT_Object_Class_Mutex, name);
  448. if (mutex == RT_NULL) return mutex;
  449. /* init ipc object */
  450. rt_ipc_object_init(&(mutex->parent));
  451. mutex->value = 1;
  452. mutex->owner = RT_NULL;
  453. mutex->original_priority = 0xFF;
  454. mutex->hold = 0;
  455. /* set flag */
  456. mutex->parent.parent.flag = flag;
  457. return mutex;
  458. }
  459. /**
  460. * This function will delete a mutex object and release the memory
  461. *
  462. * @param mutex the mutex object
  463. *
  464. * @return the error code
  465. *
  466. * @see rt_mutex_detach
  467. */
  468. rt_err_t rt_mutex_delete (rt_mutex_t mutex)
  469. {
  470. RT_ASSERT(mutex != RT_NULL);
  471. /* wakeup all suspend threads */
  472. rt_ipc_object_resume_all(&(mutex->parent));
  473. /* delete semaphore object */
  474. rt_object_delete(&(mutex->parent.parent));
  475. return RT_EOK;
  476. }
  477. #endif
  478. /**
  479. * This function will take a mutex, if the mutex is unavailable, the
  480. * thread shall wait for a specified time.
  481. *
  482. * @param mutex the mutex object
  483. * @param time the waiting time
  484. *
  485. * @return the error code
  486. */
  487. rt_err_t rt_mutex_take (rt_mutex_t mutex, rt_int32_t time)
  488. {
  489. register rt_base_t temp;
  490. struct rt_thread* thread;
  491. RT_ASSERT(mutex != RT_NULL);
  492. /* disable interrupt */
  493. temp = rt_hw_interrupt_disable();
  494. /* get current thread */
  495. thread = rt_thread_self();
  496. #ifdef RT_USING_HOOK
  497. if (rt_object_trytake_hook != RT_NULL) rt_object_trytake_hook(&(mutex->parent.parent));
  498. #endif
  499. #ifdef RT_IPC_DEBUG
  500. rt_kprintf("mutex_take: current thread %s, mutex value: %d, hold: %d\n",
  501. thread->name, mutex->value, mutex->hold);
  502. #endif
  503. /* reset thread error */
  504. thread->error = RT_EOK;
  505. if (mutex->owner == thread)
  506. {
  507. /* it's the same thread */
  508. mutex->hold ++;
  509. }
  510. else
  511. {
  512. /* in initialization status, the value is 1. Therefore, if the
  513. * value is great than 1, which indicates the mutex is avaible.
  514. */
  515. if (mutex->value > 0)
  516. {
  517. /* mutex is available */
  518. mutex->value --;
  519. /* set mutex owner and original priority */
  520. mutex->owner = thread;
  521. mutex->original_priority = thread->current_priority;
  522. mutex->hold ++;
  523. }
  524. else
  525. {
  526. /* no waiting, return with timeout */
  527. if (time == 0 )
  528. {
  529. /* set error as timeout */
  530. thread->error = -RT_ETIMEOUT;
  531. /* enable interrupt */
  532. rt_hw_interrupt_enable(temp);
  533. return -RT_ETIMEOUT;
  534. }
  535. else
  536. {
  537. /* mutex is unavailable, push to suspend list */
  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. }
  577. }
  578. }
  579. }
  580. /* enable interrupt */
  581. rt_hw_interrupt_enable(temp);
  582. #ifdef RT_USING_HOOK
  583. if (rt_object_take_hook != RT_NULL) rt_object_take_hook(&(mutex->parent.parent));
  584. #endif
  585. return RT_EOK;
  586. }
  587. /**
  588. * This function will release a mutex, if there are threads suspended on mutex,
  589. * it will be waked up.
  590. *
  591. * @param mutex the mutex object
  592. *
  593. * @return the error code
  594. */
  595. rt_err_t rt_mutex_release(rt_mutex_t mutex)
  596. {
  597. register rt_base_t temp;
  598. struct rt_thread* thread;
  599. rt_bool_t need_schedule;
  600. need_schedule = RT_FALSE;
  601. /* get current thread */
  602. thread = rt_thread_self();
  603. /* disable interrupt */
  604. temp = rt_hw_interrupt_disable();
  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. /* wakeup suspended thread */
  632. if (mutex->parent.suspend_thread_count > 0)
  633. {
  634. /* get thread entry */
  635. thread = rt_list_entry(mutex->parent.suspend_thread.next, struct rt_thread, tlist);
  636. #ifdef RT_IPC_DEBUG
  637. rt_kprintf("mutex_release: resume thread: %s\n", thread->name);
  638. #endif
  639. /* set new owner and priority */
  640. mutex->owner = thread;
  641. mutex->original_priority = thread->current_priority;
  642. mutex->hold ++;
  643. /* resume thread */
  644. rt_ipc_object_resume(&(mutex->parent));
  645. need_schedule = RT_TRUE;
  646. }
  647. else
  648. {
  649. /* increase value */
  650. mutex->value ++;
  651. /* clear owner */
  652. mutex->owner = RT_NULL;
  653. mutex->original_priority = 0;
  654. }
  655. }
  656. /* enable interrupt */
  657. rt_hw_interrupt_enable(temp);
  658. /* perform a schedule */
  659. if (need_schedule == RT_TRUE) rt_schedule();
  660. return RT_EOK;
  661. }
  662. /**
  663. * This function can get or set some extra attributions of a mutex object.
  664. *
  665. * @param mutex the mutex object
  666. * @param cmd the execution command
  667. * @param arg the execution argument
  668. *
  669. * @return the error code
  670. */
  671. rt_err_t rt_mutex_control(rt_mutex_t mutex, rt_uint8_t cmd, void* arg)
  672. {
  673. return RT_EOK;
  674. }
  675. #endif /* end of RT_USING_MUTEX */
  676. #ifdef RT_USING_EVENT
  677. /**
  678. * This function will initialize an event and put it under control of resource
  679. * management.
  680. *
  681. * @param event the event object
  682. * @param name the name of event
  683. * @param flag the flag of event
  684. *
  685. * @return the operation status, RT_EOK on successful
  686. */
  687. rt_err_t rt_event_init(rt_event_t event, const char* name, rt_uint8_t flag)
  688. {
  689. RT_ASSERT(event != RT_NULL);
  690. /* init object */
  691. rt_object_init(&(event->parent.parent), RT_Object_Class_Event, name);
  692. /* set parent flag */
  693. event->parent.parent.flag = flag;
  694. /* init ipc object */
  695. rt_ipc_object_init(&(event->parent));
  696. /* init event */
  697. event->set = 0;
  698. return RT_EOK;
  699. }
  700. /**
  701. * This function will detach an event object from resource management
  702. *
  703. * @param event the event object
  704. *
  705. * @return the operation status, RT_EOK on successful
  706. */
  707. rt_err_t rt_event_detach(rt_event_t event)
  708. {
  709. /* parameter check */
  710. RT_ASSERT(event != RT_NULL);
  711. /* resume all suspended thread */
  712. rt_ipc_object_resume_all(&(event->parent));
  713. /* detach event object */
  714. rt_object_detach(&(event->parent.parent));
  715. return RT_EOK;
  716. }
  717. #ifdef RT_USING_HEAP
  718. /**
  719. * This function will create an event object from system resource
  720. *
  721. * @param name the name of event
  722. * @param flag the flag of event
  723. *
  724. * @return the created event, RT_NULL on error happen
  725. */
  726. rt_event_t rt_event_create (const char* name, rt_uint8_t flag)
  727. {
  728. rt_event_t event;
  729. /* allocate object */
  730. event = (rt_event_t) rt_object_allocate(RT_Object_Class_Event, name);
  731. if (event == RT_NULL) return event;
  732. /* set parent */
  733. event->parent.parent.flag = flag;
  734. /* init ipc object */
  735. rt_ipc_object_init(&(event->parent));
  736. /* init event */
  737. event->set = 0;
  738. return event;
  739. }
  740. /**
  741. * This function will delete an event object and release the memory
  742. *
  743. * @param event the event object
  744. *
  745. * @return the error code
  746. */
  747. rt_err_t rt_event_delete (rt_event_t event)
  748. {
  749. /* parameter check */
  750. RT_ASSERT(event != RT_NULL);
  751. /* resume all suspended thread */
  752. rt_ipc_object_resume_all(&(event->parent));
  753. /* delete event object */
  754. rt_object_delete(&(event->parent.parent));
  755. return RT_EOK;
  756. }
  757. #endif
  758. /**
  759. * This function will send an event to the event object, if there are threads
  760. * suspended on event object, it will be waked up.
  761. *
  762. * @param event the event object
  763. * @param set the event set
  764. *
  765. * @return the error code
  766. */
  767. rt_err_t rt_event_send(rt_event_t event, rt_uint32_t set)
  768. {
  769. struct rt_list_node *n;
  770. struct rt_thread *thread;
  771. register rt_ubase_t level;
  772. register rt_base_t status;
  773. rt_bool_t need_schedule;
  774. /* parameter check */
  775. RT_ASSERT(event != RT_NULL);
  776. if (set == 0) return -RT_ERROR;
  777. need_schedule = RT_FALSE;
  778. #ifdef RT_USING_HOOK
  779. if (rt_object_put_hook != RT_NULL) rt_object_put_hook(&(event->parent.parent));
  780. #endif
  781. /* disable interrupt */
  782. level = rt_hw_interrupt_disable();
  783. /* set event */
  784. event->set |= set;
  785. if (event->parent.suspend_thread_count > 0)
  786. {
  787. /* search thread list to resume thread */
  788. n = event->parent.suspend_thread.next;
  789. while (n != &(event->parent.suspend_thread))
  790. {
  791. /* get thread */
  792. thread = rt_list_entry(n, struct rt_thread, tlist);
  793. status = -RT_ERROR;
  794. if (thread->event_info & RT_EVENT_FLAG_AND)
  795. {
  796. if ((thread->event_set & event->set) == thread->event_set)
  797. {
  798. /* received an AND event */
  799. status = RT_EOK;
  800. }
  801. }
  802. else if (thread->event_info & RT_EVENT_FLAG_OR)
  803. {
  804. if (thread->event_set & event->set)
  805. {
  806. /* save recieved event set */
  807. thread->event_set = thread->event_set & event->set;
  808. /* received an OR event */
  809. status = RT_EOK;
  810. }
  811. }
  812. /* move node to the next */
  813. n = n->next;
  814. /* condition is satisfied, resume thread */
  815. if (status == RT_EOK)
  816. {
  817. /* clear event */
  818. if (thread->event_info & RT_EVENT_FLAG_CLEAR)
  819. event->set &= ~thread->event_set;
  820. /* resume thread, and thread list breaks out */
  821. rt_thread_resume(thread);
  822. /* need do a scheduling */
  823. need_schedule = RT_TRUE;
  824. /* decrease suspended thread count */
  825. event->parent.suspend_thread_count--;
  826. }
  827. }
  828. }
  829. /* enable interrupt */
  830. rt_hw_interrupt_enable(level);
  831. /* do a schedule */
  832. if (need_schedule == RT_TRUE)
  833. rt_schedule();
  834. return RT_EOK;
  835. }
  836. /**
  837. * This function will receive an event from event object, if the event is unavailable,
  838. * the thread shall wait for a specified time.
  839. *
  840. * @param event the fast event object
  841. * @param set the interested event set
  842. * @param option the receive option
  843. * @param timeout the waiting time
  844. * @param recved the received event
  845. *
  846. * @return the error code
  847. */
  848. 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)
  849. {
  850. struct rt_thread *thread;
  851. register rt_ubase_t level;
  852. register rt_base_t status;
  853. /* parameter check */
  854. RT_ASSERT(event != RT_NULL);
  855. if (set == 0) return -RT_ERROR;
  856. /* init status */
  857. status = -RT_ERROR;
  858. /* get current thread */
  859. thread = rt_thread_self();
  860. /* reset thread error */
  861. thread->error = RT_EOK;
  862. #ifdef RT_USING_HOOK
  863. if (rt_object_trytake_hook != RT_NULL) rt_object_trytake_hook(&(event->parent.parent));
  864. #endif
  865. /* disable interrupt */
  866. level = rt_hw_interrupt_disable();
  867. /* check event set */
  868. if (option & RT_EVENT_FLAG_AND)
  869. {
  870. if ((event->set & set) == set) status = RT_EOK;
  871. }
  872. else if (option & RT_EVENT_FLAG_OR)
  873. {
  874. if (event->set & set) status = RT_EOK;
  875. }
  876. if (status == RT_EOK)
  877. {
  878. /* set received event */
  879. *recved = (event->set & set);
  880. /* received event */
  881. if (option & RT_EVENT_FLAG_CLEAR) event->set &= ~set;
  882. }
  883. else if (timeout == 0)
  884. {
  885. /* no waiting */
  886. thread->error = -RT_ETIMEOUT;
  887. }
  888. else
  889. {
  890. /* fill thread event info */
  891. thread->event_set = set;
  892. thread->event_info = option;
  893. /* put thread to suspended thread list */
  894. rt_ipc_object_suspend(&(event->parent), thread);
  895. /* if there is a waiting timeout, active thread timer */
  896. if (timeout > 0)
  897. {
  898. /* reset the timeout of thread timer and start it */
  899. rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &timeout);
  900. rt_timer_start(&(thread->thread_timer));
  901. }
  902. /* enable interrupt */
  903. rt_hw_interrupt_enable(level);
  904. /* do a schedule */
  905. rt_schedule();
  906. if (thread->error != RT_EOK)
  907. {
  908. /* decrease suspended thread count */
  909. rt_ipc_object_decrease(&(event->parent));
  910. return thread->error;
  911. }
  912. /* received an event, disable interrupt to protect */
  913. level = rt_hw_interrupt_disable();
  914. /* set received event */
  915. *recved = thread->event_set;
  916. }
  917. /* enable interrupt */
  918. rt_hw_interrupt_enable(level);
  919. #ifdef RT_USING_HOOK
  920. if (rt_object_take_hook != RT_NULL) rt_object_take_hook(&(event->parent.parent));
  921. #endif
  922. return thread->error;
  923. }
  924. /**
  925. * This function can get or set some extra attributions of an event object.
  926. *
  927. * @param event the event object
  928. * @param cmd the execution command
  929. * @param arg the execution argument
  930. *
  931. * @return the error code
  932. */
  933. rt_err_t rt_event_control (rt_event_t event, rt_uint8_t cmd, void* arg)
  934. {
  935. return RT_EOK;
  936. }
  937. #endif /* end of RT_USING_EVENT */
  938. #ifdef RT_USING_MAILBOX
  939. /**
  940. * This function will initialize a mailbox and put it under control of resource
  941. * management.
  942. *
  943. * @param mb the mailbox object
  944. * @param name the name of mailbox
  945. * @param msgpool the begin address of buffer to save received mail
  946. * @param size the size of mailbox
  947. * @param flag the flag of mailbox
  948. *
  949. * @return the operation status, RT_EOK on successful
  950. */
  951. rt_err_t rt_mb_init(rt_mailbox_t mb, const char* name, void* msgpool, rt_size_t size, rt_uint8_t flag)
  952. {
  953. RT_ASSERT(mb != RT_NULL);
  954. /* init object */
  955. rt_object_init(&(mb->parent.parent), RT_Object_Class_MailBox, name);
  956. /* set parent flag */
  957. mb->parent.parent.flag = flag;
  958. /* init ipc object */
  959. rt_ipc_object_init(&(mb->parent));
  960. /* init mailbox */
  961. mb->msg_pool = msgpool;
  962. mb->size = size;
  963. mb->entry = 0;
  964. mb->in_offset = 0;
  965. mb->out_offset = 0;
  966. return RT_EOK;
  967. }
  968. /**
  969. * This function will detach a mailbox from resource management
  970. *
  971. * @param mb the mailbox object
  972. *
  973. * @return the operation status, RT_EOK on successful
  974. */
  975. rt_err_t rt_mb_detach(rt_mailbox_t mb)
  976. {
  977. /* parameter check */
  978. RT_ASSERT(mb != RT_NULL);
  979. /* resume all suspended thread */
  980. rt_ipc_object_resume_all(&(mb->parent));
  981. /* detach mailbox object */
  982. rt_object_detach(&(mb->parent.parent));
  983. return RT_EOK;
  984. }
  985. #ifdef RT_USING_HEAP
  986. /**
  987. * This function will create a mailbox object from system resource
  988. *
  989. * @param name the name of mailbox
  990. * @param size the size of mailbox
  991. * @param flag the flag of mailbox
  992. *
  993. * @return the created mailbox, RT_NULL on error happen
  994. */
  995. rt_mailbox_t rt_mb_create (const char* name, rt_size_t size, rt_uint8_t flag)
  996. {
  997. rt_mailbox_t mb;
  998. /* allocate object */
  999. mb = (rt_mailbox_t) rt_object_allocate(RT_Object_Class_MailBox, name);
  1000. if (mb == RT_NULL) return mb;
  1001. /* set parent */
  1002. mb->parent.parent.flag = flag;
  1003. /* init ipc object */
  1004. rt_ipc_object_init(&(mb->parent));
  1005. /* init mailbox */
  1006. mb->size = size;
  1007. mb->msg_pool = rt_malloc(mb->size * sizeof(rt_uint32_t));
  1008. if (mb->msg_pool == RT_NULL)
  1009. {
  1010. /* delete mailbox object */
  1011. rt_object_delete(&(mb->parent.parent));
  1012. return RT_NULL;
  1013. }
  1014. mb->entry = 0;
  1015. mb->in_offset = 0;
  1016. mb->out_offset = 0;
  1017. return mb;
  1018. }
  1019. /**
  1020. * This function will delete a mailbox object and release the memory
  1021. *
  1022. * @param mb the mailbox object
  1023. *
  1024. * @return the error code
  1025. */
  1026. rt_err_t rt_mb_delete (rt_mailbox_t mb)
  1027. {
  1028. /* parameter check */
  1029. RT_ASSERT(mb != RT_NULL);
  1030. /* resume all suspended thread */
  1031. rt_ipc_object_resume_all(&(mb->parent));
  1032. /* free mailbox pool */
  1033. rt_free(mb->msg_pool);
  1034. /* delete mailbox object */
  1035. rt_object_delete(&(mb->parent.parent));
  1036. return RT_EOK;
  1037. }
  1038. #endif
  1039. /**
  1040. * This function will send a mail to mailbox object, if there are threads suspended
  1041. * on mailbox object, it will be waked up.
  1042. *
  1043. * @param mb the mailbox object
  1044. * @param value the mail
  1045. *
  1046. * @return the error code
  1047. */
  1048. rt_err_t rt_mb_send (rt_mailbox_t mb, rt_uint32_t value)
  1049. {
  1050. register rt_ubase_t temp;
  1051. /* parameter check */
  1052. RT_ASSERT(mb != RT_NULL);
  1053. #ifdef RT_USING_HOOK
  1054. if (rt_object_put_hook != RT_NULL) rt_object_put_hook(&(mb->parent.parent));
  1055. #endif
  1056. /* disable interrupt */
  1057. temp = rt_hw_interrupt_disable();
  1058. /* mailbox is full */
  1059. if (mb->entry == mb->size)
  1060. {
  1061. /* enable interrupt */
  1062. rt_hw_interrupt_enable(temp);
  1063. return -RT_EFULL;
  1064. }
  1065. /* set ptr */
  1066. mb->msg_pool[mb->in_offset] = value;
  1067. /* increase input offset */
  1068. ++ mb->in_offset;
  1069. mb->in_offset %= mb->size;
  1070. /* increase message entry */
  1071. mb->entry ++;
  1072. /* resume suspended thread */
  1073. if (mb->parent.suspend_thread_count > 0)
  1074. {
  1075. rt_ipc_object_resume(&(mb->parent));
  1076. /* enable interrupt */
  1077. rt_hw_interrupt_enable(temp);
  1078. rt_schedule();
  1079. return RT_EOK;
  1080. }
  1081. /* enable interrupt */
  1082. rt_hw_interrupt_enable(temp);
  1083. return RT_EOK;
  1084. }
  1085. /**
  1086. * This function will receive a mail from mailbox object, if there is no mail in
  1087. * mailbox object, the thread shall wait for a specified time.
  1088. *
  1089. * @param mb the mailbox object
  1090. * @param value the received mail will be saved in
  1091. * @param timeout the waiting time
  1092. *
  1093. * @return the error code
  1094. */
  1095. rt_err_t rt_mb_recv (rt_mailbox_t mb, rt_uint32_t* value, rt_int32_t timeout)
  1096. {
  1097. struct rt_thread *thread;
  1098. register rt_ubase_t temp;
  1099. rt_uint32_t tick_delta;
  1100. /* parameter check */
  1101. RT_ASSERT(mb != RT_NULL);
  1102. tick_delta = 0;
  1103. #ifdef RT_USING_HOOK
  1104. if (rt_object_trytake_hook != RT_NULL) rt_object_trytake_hook(&(mb->parent.parent));
  1105. #endif
  1106. /* disable interrupt */
  1107. temp = rt_hw_interrupt_disable();
  1108. /* get current thread */
  1109. thread = rt_thread_self();
  1110. /* mailbox is empty */
  1111. while (mb->entry == 0)
  1112. {
  1113. /* reset error number in thread */
  1114. thread->error = RT_EOK;
  1115. /* no waiting, return timeout */
  1116. if (timeout == 0)
  1117. {
  1118. /* enable interrupt */
  1119. rt_hw_interrupt_enable(temp);
  1120. thread->error = -RT_ETIMEOUT;
  1121. return -RT_ETIMEOUT;
  1122. }
  1123. /* suspend current thread */
  1124. rt_ipc_object_suspend(&(mb->parent), thread);
  1125. /* has waiting time, start thread timer */
  1126. if (timeout > 0)
  1127. {
  1128. /* get the start tick of timer */
  1129. tick_delta = rt_tick_get();
  1130. #ifdef RT_IPC_DEBUG
  1131. rt_kprintf("mb_recv: start timer of thread:%s\n", thread->name);
  1132. #endif
  1133. /* reset the timeout of thread timer and start it */
  1134. rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &timeout);
  1135. rt_timer_start(&(thread->thread_timer));
  1136. }
  1137. /* enable interrupt */
  1138. rt_hw_interrupt_enable(temp);
  1139. /* re-schedule */
  1140. rt_schedule();
  1141. /* resume from suspend state */
  1142. if (thread->error != RT_EOK)
  1143. {
  1144. /* can't recv message, decrease suspended thread count */
  1145. rt_ipc_object_decrease(&(mb->parent));
  1146. return thread->error;
  1147. }
  1148. /* disable interrupt */
  1149. temp = rt_hw_interrupt_disable();
  1150. /* re-calculate timeout tick */
  1151. if (timeout > 0)
  1152. {
  1153. tick_delta = rt_tick_get() - tick_delta;
  1154. timeout -= tick_delta;
  1155. if (timeout < 0) timeout = 0;
  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. if (timeout < 0) timeout = 0;
  1511. }
  1512. }
  1513. /* get message from queue */
  1514. msg = (struct rt_mq_message*) mq->msg_queue_head;
  1515. /* move message queue head */
  1516. mq->msg_queue_head = msg->next;
  1517. /* reach queue tail, set to NULL */
  1518. if (mq->msg_queue_tail == msg) mq->msg_queue_tail = RT_NULL;
  1519. /* copy message */
  1520. rt_memcpy(buffer, msg + 1,
  1521. size > mq->msg_size? mq->msg_size : size);
  1522. /* put message to free list */
  1523. msg->next = (struct rt_mq_message*)mq->msg_queue_free;
  1524. mq->msg_queue_free = msg;
  1525. /* decrease message entry */
  1526. mq->entry --;
  1527. /* enable interrupt */
  1528. rt_hw_interrupt_enable(temp);
  1529. #ifdef RT_USING_HOOK
  1530. if (rt_object_take_hook != RT_NULL) rt_object_take_hook(&(mq->parent.parent));
  1531. #endif
  1532. return RT_EOK;
  1533. }
  1534. /**
  1535. * This function can get or set some extra attributions of a message queue object.
  1536. *
  1537. * @param mq the message queue object
  1538. * @param cmd the execution command
  1539. * @param arg the execution argument
  1540. *
  1541. * @return the error code
  1542. */
  1543. rt_err_t rt_mq_control(rt_mq_t mq, rt_uint8_t cmd, void* arg)
  1544. {
  1545. return RT_EOK;
  1546. }
  1547. #endif /* end of RT_USING_MESSAGEQUEUE */
  1548. /*@}*/