ipc.c 42 KB

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