ipc.c 42 KB

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