ipc.c 58 KB

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