ipc.c 42 KB

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