ipc.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286
  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. RT_ASSERT(mutex != RT_NULL);
  540. /* get current thread */
  541. thread = rt_thread_self();
  542. if (!thread) return RT_EOK; /* return directory if scheduler not started */
  543. /* this function must not be used in interrupt even if time = 0 */
  544. RT_DEBUG_IN_THREAD_CONTEXT;
  545. /* disable interrupt */
  546. temp = rt_hw_interrupt_disable();
  547. RT_OBJECT_HOOK_CALL(rt_object_trytake_hook, (&(mutex->parent.parent)));
  548. RT_DEBUG_LOG(RT_DEBUG_IPC,
  549. ("mutex_take: current thread %s, mutex value: %d, hold: %d\n",
  550. thread->name, mutex->value, mutex->hold));
  551. /* reset thread error */
  552. thread->error = RT_EOK;
  553. if (mutex->owner == thread)
  554. {
  555. /* it's the same thread */
  556. mutex->hold ++;
  557. }
  558. else
  559. {
  560. /* The value of mutex is 1 in initial status. Therefore, if the
  561. * value is great than 0, it indicates the mutex is avaible.
  562. */
  563. if (mutex->value > 0)
  564. {
  565. /* mutex is available */
  566. mutex->value --;
  567. /* set mutex owner and original priority */
  568. mutex->owner = thread;
  569. mutex->original_priority = thread->current_priority;
  570. mutex->hold ++;
  571. }
  572. else
  573. {
  574. /* no waiting, return with timeout */
  575. if (time == 0)
  576. {
  577. /* set error as timeout */
  578. thread->error = -RT_ETIMEOUT;
  579. /* enable interrupt */
  580. rt_hw_interrupt_enable(temp);
  581. return -RT_ETIMEOUT;
  582. }
  583. else
  584. {
  585. /* mutex is unavailable, push to suspend list */
  586. RT_DEBUG_LOG(RT_DEBUG_IPC, ("mutex_take: suspend thread: %s\n",
  587. thread->name));
  588. /* change the owner thread priority of mutex */
  589. if (thread->current_priority < mutex->owner->current_priority)
  590. {
  591. /* change the owner thread priority */
  592. rt_thread_control(mutex->owner,
  593. RT_THREAD_CTRL_CHANGE_PRIORITY,
  594. &thread->current_priority);
  595. }
  596. /* suspend current thread */
  597. rt_ipc_list_suspend(&(mutex->parent.suspend_thread),
  598. thread,
  599. mutex->parent.parent.flag);
  600. /* has waiting time, start thread timer */
  601. if (time > 0)
  602. {
  603. RT_DEBUG_LOG(RT_DEBUG_IPC,
  604. ("mutex_take: start the timer of thread:%s\n",
  605. thread->name));
  606. /* reset the timeout of thread timer and start it */
  607. rt_timer_control(&(thread->thread_timer),
  608. RT_TIMER_CTRL_SET_TIME,
  609. &time);
  610. rt_timer_start(&(thread->thread_timer));
  611. }
  612. /* enable interrupt */
  613. rt_hw_interrupt_enable(temp);
  614. /* do schedule */
  615. rt_schedule();
  616. if (thread->error != RT_EOK)
  617. {
  618. /* return error */
  619. return thread->error;
  620. }
  621. else
  622. {
  623. /* the mutex is taken successfully. */
  624. /* disable interrupt */
  625. temp = rt_hw_interrupt_disable();
  626. }
  627. }
  628. }
  629. }
  630. /* enable interrupt */
  631. rt_hw_interrupt_enable(temp);
  632. RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(mutex->parent.parent)));
  633. return RT_EOK;
  634. }
  635. RTM_EXPORT(rt_mutex_take);
  636. /**
  637. * This function will release a mutex, if there are threads suspended on mutex,
  638. * it will be waked up.
  639. *
  640. * @param mutex the mutex object
  641. *
  642. * @return the error code
  643. */
  644. rt_err_t rt_mutex_release(rt_mutex_t mutex)
  645. {
  646. register rt_base_t temp;
  647. struct rt_thread *thread;
  648. rt_bool_t need_schedule;
  649. need_schedule = RT_FALSE;
  650. /* get current thread */
  651. thread = rt_thread_self();
  652. if (!thread) return RT_EOK;
  653. /* only thread could release mutex because we need test the ownership */
  654. RT_DEBUG_IN_THREAD_CONTEXT;
  655. /* disable interrupt */
  656. temp = rt_hw_interrupt_disable();
  657. RT_DEBUG_LOG(RT_DEBUG_IPC,
  658. ("mutex_release:current thread %s, mutex value: %d, hold: %d\n",
  659. thread->name, mutex->value, mutex->hold));
  660. RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(mutex->parent.parent)));
  661. /* mutex only can be released by owner */
  662. if (thread != mutex->owner)
  663. {
  664. thread->error = -RT_ERROR;
  665. /* enable interrupt */
  666. rt_hw_interrupt_enable(temp);
  667. return -RT_ERROR;
  668. }
  669. /* decrease hold */
  670. mutex->hold --;
  671. /* if no hold */
  672. if (mutex->hold == 0)
  673. {
  674. /* change the owner thread to original priority */
  675. if (mutex->original_priority != mutex->owner->current_priority)
  676. {
  677. rt_thread_control(mutex->owner,
  678. RT_THREAD_CTRL_CHANGE_PRIORITY,
  679. &(mutex->original_priority));
  680. }
  681. /* wakeup suspended thread */
  682. if (!rt_list_isempty(&mutex->parent.suspend_thread))
  683. {
  684. /* get suspended thread */
  685. thread = rt_list_entry(mutex->parent.suspend_thread.next,
  686. struct rt_thread,
  687. tlist);
  688. RT_DEBUG_LOG(RT_DEBUG_IPC, ("mutex_release: resume thread: %s\n",
  689. thread->name));
  690. /* set new owner and priority */
  691. mutex->owner = thread;
  692. mutex->original_priority = thread->current_priority;
  693. mutex->hold ++;
  694. /* resume thread */
  695. rt_ipc_list_resume(&(mutex->parent.suspend_thread));
  696. need_schedule = RT_TRUE;
  697. }
  698. else
  699. {
  700. /* increase value */
  701. mutex->value ++;
  702. /* clear owner */
  703. mutex->owner = RT_NULL;
  704. mutex->original_priority = 0xff;
  705. }
  706. }
  707. /* enable interrupt */
  708. rt_hw_interrupt_enable(temp);
  709. /* perform a schedule */
  710. if (need_schedule == RT_TRUE)
  711. rt_schedule();
  712. return RT_EOK;
  713. }
  714. RTM_EXPORT(rt_mutex_release);
  715. /**
  716. * This function can get or set some extra attributions of a mutex object.
  717. *
  718. * @param mutex the mutex object
  719. * @param cmd the execution command
  720. * @param arg the execution argument
  721. *
  722. * @return the error code
  723. */
  724. rt_err_t rt_mutex_control(rt_mutex_t mutex, rt_uint8_t cmd, void *arg)
  725. {
  726. return -RT_ERROR;
  727. }
  728. RTM_EXPORT(rt_mutex_control);
  729. #endif /* end of RT_USING_MUTEX */
  730. #ifdef RT_USING_EVENT
  731. /**
  732. * This function will initialize an event and put it under control of resource
  733. * management.
  734. *
  735. * @param event the event object
  736. * @param name the name of event
  737. * @param flag the flag of event
  738. *
  739. * @return the operation status, RT_EOK on successful
  740. */
  741. rt_err_t rt_event_init(rt_event_t event, const char *name, rt_uint8_t flag)
  742. {
  743. RT_ASSERT(event != RT_NULL);
  744. /* init object */
  745. rt_object_init(&(event->parent.parent), RT_Object_Class_Event, name);
  746. /* set parent flag */
  747. event->parent.parent.flag = flag;
  748. /* init ipc object */
  749. rt_ipc_object_init(&(event->parent));
  750. /* init event */
  751. event->set = 0;
  752. return RT_EOK;
  753. }
  754. RTM_EXPORT(rt_event_init);
  755. /**
  756. * This function will detach an event object from resource management
  757. *
  758. * @param event the event object
  759. *
  760. * @return the operation status, RT_EOK on successful
  761. */
  762. rt_err_t rt_event_detach(rt_event_t event)
  763. {
  764. /* parameter check */
  765. RT_ASSERT(event != RT_NULL);
  766. /* resume all suspended thread */
  767. rt_ipc_list_resume_all(&(event->parent.suspend_thread));
  768. /* detach event object */
  769. rt_object_detach(&(event->parent.parent));
  770. return RT_EOK;
  771. }
  772. RTM_EXPORT(rt_event_detach);
  773. #ifdef RT_USING_HEAP
  774. /**
  775. * This function will create an event object from system resource
  776. *
  777. * @param name the name of event
  778. * @param flag the flag of event
  779. *
  780. * @return the created event, RT_NULL on error happen
  781. */
  782. rt_event_t rt_event_create(const char *name, rt_uint8_t flag)
  783. {
  784. rt_event_t event;
  785. RT_DEBUG_NOT_IN_INTERRUPT;
  786. /* allocate object */
  787. event = (rt_event_t)rt_object_allocate(RT_Object_Class_Event, name);
  788. if (event == RT_NULL)
  789. return event;
  790. /* set parent */
  791. event->parent.parent.flag = flag;
  792. /* init ipc object */
  793. rt_ipc_object_init(&(event->parent));
  794. /* init event */
  795. event->set = 0;
  796. return event;
  797. }
  798. RTM_EXPORT(rt_event_create);
  799. /**
  800. * This function will delete an event object and release the memory
  801. *
  802. * @param event the event object
  803. *
  804. * @return the error code
  805. */
  806. rt_err_t rt_event_delete(rt_event_t event)
  807. {
  808. /* parameter check */
  809. RT_ASSERT(event != RT_NULL);
  810. RT_DEBUG_NOT_IN_INTERRUPT;
  811. /* resume all suspended thread */
  812. rt_ipc_list_resume_all(&(event->parent.suspend_thread));
  813. /* delete event object */
  814. rt_object_delete(&(event->parent.parent));
  815. return RT_EOK;
  816. }
  817. RTM_EXPORT(rt_event_delete);
  818. #endif
  819. /**
  820. * This function will send an event to the event object, if there are threads
  821. * suspended on event object, it will be waked up.
  822. *
  823. * @param event the event object
  824. * @param set the event set
  825. *
  826. * @return the error code
  827. */
  828. rt_err_t rt_event_send(rt_event_t event, rt_uint32_t set)
  829. {
  830. struct rt_list_node *n;
  831. struct rt_thread *thread;
  832. register rt_ubase_t level;
  833. register rt_base_t status;
  834. rt_bool_t need_schedule;
  835. /* parameter check */
  836. RT_ASSERT(event != RT_NULL);
  837. if (set == 0)
  838. return -RT_ERROR;
  839. need_schedule = RT_FALSE;
  840. RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(event->parent.parent)));
  841. /* disable interrupt */
  842. level = rt_hw_interrupt_disable();
  843. /* set event */
  844. event->set |= set;
  845. if (!rt_list_isempty(&event->parent.suspend_thread))
  846. {
  847. /* search thread list to resume thread */
  848. n = event->parent.suspend_thread.next;
  849. while (n != &(event->parent.suspend_thread))
  850. {
  851. /* get thread */
  852. thread = rt_list_entry(n, struct rt_thread, tlist);
  853. status = -RT_ERROR;
  854. if (thread->event_info & RT_EVENT_FLAG_AND)
  855. {
  856. if ((thread->event_set & event->set) == thread->event_set)
  857. {
  858. /* received an AND event */
  859. status = RT_EOK;
  860. }
  861. }
  862. else if (thread->event_info & RT_EVENT_FLAG_OR)
  863. {
  864. if (thread->event_set & event->set)
  865. {
  866. /* save recieved event set */
  867. thread->event_set = thread->event_set & event->set;
  868. /* received an OR event */
  869. status = RT_EOK;
  870. }
  871. }
  872. /* move node to the next */
  873. n = n->next;
  874. /* condition is satisfied, resume thread */
  875. if (status == RT_EOK)
  876. {
  877. /* clear event */
  878. if (thread->event_info & RT_EVENT_FLAG_CLEAR)
  879. event->set &= ~thread->event_set;
  880. /* resume thread, and thread list breaks out */
  881. rt_thread_resume(thread);
  882. /* need do a scheduling */
  883. need_schedule = RT_TRUE;
  884. }
  885. }
  886. }
  887. /* enable interrupt */
  888. rt_hw_interrupt_enable(level);
  889. /* do a schedule */
  890. if (need_schedule == RT_TRUE)
  891. rt_schedule();
  892. return RT_EOK;
  893. }
  894. RTM_EXPORT(rt_event_send);
  895. /**
  896. * This function will receive an event from event object, if the event is
  897. * unavailable, the thread shall wait for a specified time.
  898. *
  899. * @param event the fast event object
  900. * @param set the interested event set
  901. * @param option the receive option, either RT_EVENT_FLAG_AND or
  902. * RT_EVENT_FLAG_OR should be set.
  903. * @param timeout the waiting time
  904. * @param recved the received event, if you don't care, RT_NULL can be set.
  905. *
  906. * @return the error code
  907. */
  908. rt_err_t rt_event_recv(rt_event_t event,
  909. rt_uint32_t set,
  910. rt_uint8_t option,
  911. rt_int32_t timeout,
  912. rt_uint32_t *recved)
  913. {
  914. struct rt_thread *thread;
  915. register rt_ubase_t level;
  916. register rt_base_t status;
  917. RT_DEBUG_IN_THREAD_CONTEXT;
  918. /* parameter check */
  919. RT_ASSERT(event != RT_NULL);
  920. if (set == 0)
  921. return -RT_ERROR;
  922. /* init status */
  923. status = -RT_ERROR;
  924. /* get current thread */
  925. thread = rt_thread_self();
  926. /* reset thread error */
  927. thread->error = RT_EOK;
  928. RT_OBJECT_HOOK_CALL(rt_object_trytake_hook, (&(event->parent.parent)));
  929. /* disable interrupt */
  930. level = rt_hw_interrupt_disable();
  931. /* check event set */
  932. if (option & RT_EVENT_FLAG_AND)
  933. {
  934. if ((event->set & set) == set)
  935. status = RT_EOK;
  936. }
  937. else if (option & RT_EVENT_FLAG_OR)
  938. {
  939. if (event->set & set)
  940. status = RT_EOK;
  941. }
  942. else
  943. {
  944. /* either RT_EVENT_FLAG_AND or RT_EVENT_FLAG_OR should be set */
  945. RT_ASSERT(0);
  946. }
  947. if (status == RT_EOK)
  948. {
  949. /* set received event */
  950. if (recved)
  951. *recved = (event->set & set);
  952. /* received event */
  953. if (option & RT_EVENT_FLAG_CLEAR)
  954. event->set &= ~set;
  955. }
  956. else if (timeout == 0)
  957. {
  958. /* no waiting */
  959. thread->error = -RT_ETIMEOUT;
  960. }
  961. else
  962. {
  963. /* fill thread event info */
  964. thread->event_set = set;
  965. thread->event_info = option;
  966. /* put thread to suspended thread list */
  967. rt_ipc_list_suspend(&(event->parent.suspend_thread),
  968. thread,
  969. event->parent.parent.flag);
  970. /* if there is a waiting timeout, active thread timer */
  971. if (timeout > 0)
  972. {
  973. /* reset the timeout of thread timer and start it */
  974. rt_timer_control(&(thread->thread_timer),
  975. RT_TIMER_CTRL_SET_TIME,
  976. &timeout);
  977. rt_timer_start(&(thread->thread_timer));
  978. }
  979. /* enable interrupt */
  980. rt_hw_interrupt_enable(level);
  981. /* do a schedule */
  982. rt_schedule();
  983. if (thread->error != RT_EOK)
  984. {
  985. /* return error */
  986. return thread->error;
  987. }
  988. /* received an event, disable interrupt to protect */
  989. level = rt_hw_interrupt_disable();
  990. /* set received event */
  991. if (recved)
  992. *recved = thread->event_set;
  993. }
  994. /* enable interrupt */
  995. rt_hw_interrupt_enable(level);
  996. RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(event->parent.parent)));
  997. return thread->error;
  998. }
  999. RTM_EXPORT(rt_event_recv);
  1000. /**
  1001. * This function can get or set some extra attributions of an event object.
  1002. *
  1003. * @param event the event object
  1004. * @param cmd the execution command
  1005. * @param arg the execution argument
  1006. *
  1007. * @return the error code
  1008. */
  1009. rt_err_t rt_event_control(rt_event_t event, rt_uint8_t cmd, void *arg)
  1010. {
  1011. rt_ubase_t level;
  1012. RT_ASSERT(event != RT_NULL);
  1013. if (cmd == RT_IPC_CMD_RESET)
  1014. {
  1015. /* disable interrupt */
  1016. level = rt_hw_interrupt_disable();
  1017. /* resume all waiting thread */
  1018. rt_ipc_list_resume_all(&event->parent.suspend_thread);
  1019. /* init event set */
  1020. event->set = 0;
  1021. /* enable interrupt */
  1022. rt_hw_interrupt_enable(level);
  1023. rt_schedule();
  1024. return RT_EOK;
  1025. }
  1026. return -RT_ERROR;
  1027. }
  1028. RTM_EXPORT(rt_event_control);
  1029. #endif /* end of RT_USING_EVENT */
  1030. #ifdef RT_USING_MAILBOX
  1031. /**
  1032. * This function will initialize a mailbox and put it under control of resource
  1033. * management.
  1034. *
  1035. * @param mb the mailbox object
  1036. * @param name the name of mailbox
  1037. * @param msgpool the begin address of buffer to save received mail
  1038. * @param size the size of mailbox
  1039. * @param flag the flag of mailbox
  1040. *
  1041. * @return the operation status, RT_EOK on successful
  1042. */
  1043. rt_err_t rt_mb_init(rt_mailbox_t mb,
  1044. const char *name,
  1045. void *msgpool,
  1046. rt_size_t size,
  1047. rt_uint8_t flag)
  1048. {
  1049. RT_ASSERT(mb != RT_NULL);
  1050. /* init object */
  1051. rt_object_init(&(mb->parent.parent), RT_Object_Class_MailBox, name);
  1052. /* set parent flag */
  1053. mb->parent.parent.flag = flag;
  1054. /* init ipc object */
  1055. rt_ipc_object_init(&(mb->parent));
  1056. /* init mailbox */
  1057. mb->msg_pool = msgpool;
  1058. mb->size = size;
  1059. mb->entry = 0;
  1060. mb->in_offset = 0;
  1061. mb->out_offset = 0;
  1062. /* init an additional list of sender suspend thread */
  1063. rt_list_init(&(mb->suspend_sender_thread));
  1064. return RT_EOK;
  1065. }
  1066. RTM_EXPORT(rt_mb_init);
  1067. /**
  1068. * This function will detach a mailbox from resource management
  1069. *
  1070. * @param mb the mailbox object
  1071. *
  1072. * @return the operation status, RT_EOK on successful
  1073. */
  1074. rt_err_t rt_mb_detach(rt_mailbox_t mb)
  1075. {
  1076. /* parameter check */
  1077. RT_ASSERT(mb != RT_NULL);
  1078. /* resume all suspended thread */
  1079. rt_ipc_list_resume_all(&(mb->parent.suspend_thread));
  1080. /* also resume all mailbox private suspended thread */
  1081. rt_ipc_list_resume_all(&(mb->suspend_sender_thread));
  1082. /* detach mailbox object */
  1083. rt_object_detach(&(mb->parent.parent));
  1084. return RT_EOK;
  1085. }
  1086. RTM_EXPORT(rt_mb_detach);
  1087. #ifdef RT_USING_HEAP
  1088. /**
  1089. * This function will create a mailbox object from system resource
  1090. *
  1091. * @param name the name of mailbox
  1092. * @param size the size of mailbox
  1093. * @param flag the flag of mailbox
  1094. *
  1095. * @return the created mailbox, RT_NULL on error happen
  1096. */
  1097. rt_mailbox_t rt_mb_create(const char *name, rt_size_t size, rt_uint8_t flag)
  1098. {
  1099. rt_mailbox_t mb;
  1100. RT_DEBUG_NOT_IN_INTERRUPT;
  1101. /* allocate object */
  1102. mb = (rt_mailbox_t)rt_object_allocate(RT_Object_Class_MailBox, name);
  1103. if (mb == RT_NULL)
  1104. return mb;
  1105. /* set parent */
  1106. mb->parent.parent.flag = flag;
  1107. /* init ipc object */
  1108. rt_ipc_object_init(&(mb->parent));
  1109. /* init mailbox */
  1110. mb->size = size;
  1111. mb->msg_pool = RT_KERNEL_MALLOC(mb->size * sizeof(rt_uint32_t));
  1112. if (mb->msg_pool == RT_NULL)
  1113. {
  1114. /* delete mailbox object */
  1115. rt_object_delete(&(mb->parent.parent));
  1116. return RT_NULL;
  1117. }
  1118. mb->entry = 0;
  1119. mb->in_offset = 0;
  1120. mb->out_offset = 0;
  1121. /* init an additional list of sender suspend thread */
  1122. rt_list_init(&(mb->suspend_sender_thread));
  1123. return mb;
  1124. }
  1125. RTM_EXPORT(rt_mb_create);
  1126. /**
  1127. * This function will delete a mailbox object and release the memory
  1128. *
  1129. * @param mb the mailbox object
  1130. *
  1131. * @return the error code
  1132. */
  1133. rt_err_t rt_mb_delete(rt_mailbox_t mb)
  1134. {
  1135. RT_DEBUG_NOT_IN_INTERRUPT;
  1136. /* parameter check */
  1137. RT_ASSERT(mb != RT_NULL);
  1138. /* resume all suspended thread */
  1139. rt_ipc_list_resume_all(&(mb->parent.suspend_thread));
  1140. /* also resume all mailbox private suspended thread */
  1141. rt_ipc_list_resume_all(&(mb->suspend_sender_thread));
  1142. #if defined(RT_USING_MODULE) && defined(RT_USING_SLAB)
  1143. /* the mb object belongs to an application module */
  1144. if (mb->parent.parent.flag & RT_OBJECT_FLAG_MODULE)
  1145. rt_module_free(mb->parent.parent.module_id, mb->msg_pool);
  1146. else
  1147. #endif
  1148. /* free mailbox pool */
  1149. RT_KERNEL_FREE(mb->msg_pool);
  1150. /* delete mailbox object */
  1151. rt_object_delete(&(mb->parent.parent));
  1152. return RT_EOK;
  1153. }
  1154. RTM_EXPORT(rt_mb_delete);
  1155. #endif
  1156. /**
  1157. * This function will send a mail to mailbox object. If the mailbox is full,
  1158. * current thread will be suspended until timeout.
  1159. *
  1160. * @param mb the mailbox object
  1161. * @param value the mail
  1162. * @param timeout the waiting time
  1163. *
  1164. * @return the error code
  1165. */
  1166. rt_err_t rt_mb_send_wait(rt_mailbox_t mb,
  1167. rt_uint32_t value,
  1168. rt_int32_t timeout)
  1169. {
  1170. struct rt_thread *thread;
  1171. register rt_ubase_t temp;
  1172. rt_uint32_t tick_delta;
  1173. /* parameter check */
  1174. RT_ASSERT(mb != RT_NULL);
  1175. /* initialize delta tick */
  1176. tick_delta = 0;
  1177. /* get current thread */
  1178. thread = rt_thread_self();
  1179. RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(mb->parent.parent)));
  1180. /* disable interrupt */
  1181. temp = rt_hw_interrupt_disable();
  1182. /* for non-blocking call */
  1183. if (mb->entry == mb->size && timeout == 0)
  1184. {
  1185. rt_hw_interrupt_enable(temp);
  1186. return -RT_EFULL;
  1187. }
  1188. /* mailbox is full */
  1189. while (mb->entry == mb->size)
  1190. {
  1191. /* reset error number in thread */
  1192. thread->error = RT_EOK;
  1193. /* no waiting, return timeout */
  1194. if (timeout == 0)
  1195. {
  1196. /* enable interrupt */
  1197. rt_hw_interrupt_enable(temp);
  1198. return -RT_EFULL;
  1199. }
  1200. RT_DEBUG_IN_THREAD_CONTEXT;
  1201. /* suspend current thread */
  1202. rt_ipc_list_suspend(&(mb->suspend_sender_thread),
  1203. thread,
  1204. mb->parent.parent.flag);
  1205. /* has waiting time, start thread timer */
  1206. if (timeout > 0)
  1207. {
  1208. /* get the start tick of timer */
  1209. tick_delta = rt_tick_get();
  1210. RT_DEBUG_LOG(RT_DEBUG_IPC, ("mb_send_wait: start timer of thread:%s\n",
  1211. thread->name));
  1212. /* reset the timeout of thread timer and start it */
  1213. rt_timer_control(&(thread->thread_timer),
  1214. RT_TIMER_CTRL_SET_TIME,
  1215. &timeout);
  1216. rt_timer_start(&(thread->thread_timer));
  1217. }
  1218. /* enable interrupt */
  1219. rt_hw_interrupt_enable(temp);
  1220. /* re-schedule */
  1221. rt_schedule();
  1222. /* resume from suspend state */
  1223. if (thread->error != RT_EOK)
  1224. {
  1225. /* return error */
  1226. return thread->error;
  1227. }
  1228. /* disable interrupt */
  1229. temp = rt_hw_interrupt_disable();
  1230. /* if it's not waiting forever and then re-calculate timeout tick */
  1231. if (timeout > 0)
  1232. {
  1233. tick_delta = rt_tick_get() - tick_delta;
  1234. timeout -= tick_delta;
  1235. if (timeout < 0)
  1236. timeout = 0;
  1237. }
  1238. }
  1239. /* set ptr */
  1240. mb->msg_pool[mb->in_offset] = value;
  1241. /* increase input offset */
  1242. ++ mb->in_offset;
  1243. if (mb->in_offset >= mb->size)
  1244. mb->in_offset = 0;
  1245. /* increase message entry */
  1246. mb->entry ++;
  1247. /* resume suspended thread */
  1248. if (!rt_list_isempty(&mb->parent.suspend_thread))
  1249. {
  1250. rt_ipc_list_resume(&(mb->parent.suspend_thread));
  1251. /* enable interrupt */
  1252. rt_hw_interrupt_enable(temp);
  1253. rt_schedule();
  1254. return RT_EOK;
  1255. }
  1256. /* enable interrupt */
  1257. rt_hw_interrupt_enable(temp);
  1258. return RT_EOK;
  1259. }
  1260. RTM_EXPORT(rt_mb_send_wait);
  1261. /**
  1262. * This function will send a mail to mailbox object, if there are threads
  1263. * suspended on mailbox object, it will be waked up. This function will return
  1264. * immediately, if you want blocking send, use rt_mb_send_wait instead.
  1265. *
  1266. * @param mb the mailbox object
  1267. * @param value the mail
  1268. *
  1269. * @return the error code
  1270. */
  1271. rt_err_t rt_mb_send(rt_mailbox_t mb, rt_uint32_t value)
  1272. {
  1273. return rt_mb_send_wait(mb, value, 0);
  1274. }
  1275. RTM_EXPORT(rt_mb_send);
  1276. /**
  1277. * This function will receive a mail from mailbox object, if there is no mail
  1278. * in mailbox object, the thread shall wait for a specified time.
  1279. *
  1280. * @param mb the mailbox object
  1281. * @param value the received mail will be saved in
  1282. * @param timeout the waiting time
  1283. *
  1284. * @return the error code
  1285. */
  1286. rt_err_t rt_mb_recv(rt_mailbox_t mb, rt_uint32_t *value, rt_int32_t timeout)
  1287. {
  1288. struct rt_thread *thread;
  1289. register rt_ubase_t temp;
  1290. rt_uint32_t tick_delta;
  1291. /* parameter check */
  1292. RT_ASSERT(mb != RT_NULL);
  1293. /* initialize delta tick */
  1294. tick_delta = 0;
  1295. /* get current thread */
  1296. thread = rt_thread_self();
  1297. RT_OBJECT_HOOK_CALL(rt_object_trytake_hook, (&(mb->parent.parent)));
  1298. /* disable interrupt */
  1299. temp = rt_hw_interrupt_disable();
  1300. /* for non-blocking call */
  1301. if (mb->entry == 0 && timeout == 0)
  1302. {
  1303. rt_hw_interrupt_enable(temp);
  1304. return -RT_ETIMEOUT;
  1305. }
  1306. /* mailbox is empty */
  1307. while (mb->entry == 0)
  1308. {
  1309. /* reset error number in thread */
  1310. thread->error = RT_EOK;
  1311. /* no waiting, return timeout */
  1312. if (timeout == 0)
  1313. {
  1314. /* enable interrupt */
  1315. rt_hw_interrupt_enable(temp);
  1316. thread->error = -RT_ETIMEOUT;
  1317. return -RT_ETIMEOUT;
  1318. }
  1319. RT_DEBUG_IN_THREAD_CONTEXT;
  1320. /* suspend current thread */
  1321. rt_ipc_list_suspend(&(mb->parent.suspend_thread),
  1322. thread,
  1323. mb->parent.parent.flag);
  1324. /* has waiting time, start thread timer */
  1325. if (timeout > 0)
  1326. {
  1327. /* get the start tick of timer */
  1328. tick_delta = rt_tick_get();
  1329. RT_DEBUG_LOG(RT_DEBUG_IPC, ("mb_recv: start timer of thread:%s\n",
  1330. thread->name));
  1331. /* reset the timeout of thread timer and start it */
  1332. rt_timer_control(&(thread->thread_timer),
  1333. RT_TIMER_CTRL_SET_TIME,
  1334. &timeout);
  1335. rt_timer_start(&(thread->thread_timer));
  1336. }
  1337. /* enable interrupt */
  1338. rt_hw_interrupt_enable(temp);
  1339. /* re-schedule */
  1340. rt_schedule();
  1341. /* resume from suspend state */
  1342. if (thread->error != RT_EOK)
  1343. {
  1344. /* return error */
  1345. return thread->error;
  1346. }
  1347. /* disable interrupt */
  1348. temp = rt_hw_interrupt_disable();
  1349. /* if it's not waiting forever and then re-calculate timeout tick */
  1350. if (timeout > 0)
  1351. {
  1352. tick_delta = rt_tick_get() - tick_delta;
  1353. timeout -= tick_delta;
  1354. if (timeout < 0)
  1355. timeout = 0;
  1356. }
  1357. }
  1358. /* fill ptr */
  1359. *value = mb->msg_pool[mb->out_offset];
  1360. /* increase output offset */
  1361. ++ mb->out_offset;
  1362. if (mb->out_offset >= mb->size)
  1363. mb->out_offset = 0;
  1364. /* decrease message entry */
  1365. mb->entry --;
  1366. /* resume suspended thread */
  1367. if (!rt_list_isempty(&(mb->suspend_sender_thread)))
  1368. {
  1369. rt_ipc_list_resume(&(mb->suspend_sender_thread));
  1370. /* enable interrupt */
  1371. rt_hw_interrupt_enable(temp);
  1372. RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(mb->parent.parent)));
  1373. rt_schedule();
  1374. return RT_EOK;
  1375. }
  1376. /* enable interrupt */
  1377. rt_hw_interrupt_enable(temp);
  1378. RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(mb->parent.parent)));
  1379. return RT_EOK;
  1380. }
  1381. RTM_EXPORT(rt_mb_recv);
  1382. /**
  1383. * This function can get or set some extra attributions of a mailbox object.
  1384. *
  1385. * @param mb the mailbox object
  1386. * @param cmd the execution command
  1387. * @param arg the execution argument
  1388. *
  1389. * @return the error code
  1390. */
  1391. rt_err_t rt_mb_control(rt_mailbox_t mb, rt_uint8_t cmd, void *arg)
  1392. {
  1393. rt_ubase_t level;
  1394. RT_ASSERT(mb != RT_NULL);
  1395. if (cmd == RT_IPC_CMD_RESET)
  1396. {
  1397. /* disable interrupt */
  1398. level = rt_hw_interrupt_disable();
  1399. /* resume all waiting thread */
  1400. rt_ipc_list_resume_all(&(mb->parent.suspend_thread));
  1401. /* also resume all mailbox private suspended thread */
  1402. rt_ipc_list_resume_all(&(mb->suspend_sender_thread));
  1403. /* re-init mailbox */
  1404. mb->entry = 0;
  1405. mb->in_offset = 0;
  1406. mb->out_offset = 0;
  1407. /* enable interrupt */
  1408. rt_hw_interrupt_enable(level);
  1409. rt_schedule();
  1410. return RT_EOK;
  1411. }
  1412. return -RT_ERROR;
  1413. }
  1414. RTM_EXPORT(rt_mb_control);
  1415. #endif /* end of RT_USING_MAILBOX */
  1416. #ifdef RT_USING_MESSAGEQUEUE
  1417. struct rt_mq_message
  1418. {
  1419. struct rt_mq_message *next;
  1420. };
  1421. /**
  1422. * This function will initialize a message queue and put it under control of
  1423. * resource management.
  1424. *
  1425. * @param mq the message object
  1426. * @param name the name of message queue
  1427. * @param msgpool the beginning address of buffer to save messages
  1428. * @param msg_size the maximum size of message
  1429. * @param pool_size the size of buffer to save messages
  1430. * @param flag the flag of message queue
  1431. *
  1432. * @return the operation status, RT_EOK on successful
  1433. */
  1434. rt_err_t rt_mq_init(rt_mq_t mq,
  1435. const char *name,
  1436. void *msgpool,
  1437. rt_size_t msg_size,
  1438. rt_size_t pool_size,
  1439. rt_uint8_t flag)
  1440. {
  1441. struct rt_mq_message *head;
  1442. register rt_base_t temp;
  1443. /* parameter check */
  1444. RT_ASSERT(mq != RT_NULL);
  1445. /* init object */
  1446. rt_object_init(&(mq->parent.parent), RT_Object_Class_MessageQueue, name);
  1447. /* set parent flag */
  1448. mq->parent.parent.flag = flag;
  1449. /* init ipc object */
  1450. rt_ipc_object_init(&(mq->parent));
  1451. /* set messasge pool */
  1452. mq->msg_pool = msgpool;
  1453. /* get correct message size */
  1454. mq->msg_size = RT_ALIGN(msg_size, RT_ALIGN_SIZE);
  1455. mq->max_msgs = pool_size / (mq->msg_size + sizeof(struct rt_mq_message));
  1456. /* init message list */
  1457. mq->msg_queue_head = RT_NULL;
  1458. mq->msg_queue_tail = RT_NULL;
  1459. /* init message empty list */
  1460. mq->msg_queue_free = RT_NULL;
  1461. for (temp = 0; temp < mq->max_msgs; temp ++)
  1462. {
  1463. head = (struct rt_mq_message *)((rt_uint8_t *)mq->msg_pool +
  1464. temp * (mq->msg_size + sizeof(struct rt_mq_message)));
  1465. head->next = mq->msg_queue_free;
  1466. mq->msg_queue_free = head;
  1467. }
  1468. /* the initial entry is zero */
  1469. mq->entry = 0;
  1470. return RT_EOK;
  1471. }
  1472. RTM_EXPORT(rt_mq_init);
  1473. /**
  1474. * This function will detach a message queue object from resource management
  1475. *
  1476. * @param mq the message queue object
  1477. *
  1478. * @return the operation status, RT_EOK on successful
  1479. */
  1480. rt_err_t rt_mq_detach(rt_mq_t mq)
  1481. {
  1482. /* parameter check */
  1483. RT_ASSERT(mq != RT_NULL);
  1484. /* resume all suspended thread */
  1485. rt_ipc_list_resume_all(&mq->parent.suspend_thread);
  1486. /* detach message queue object */
  1487. rt_object_detach(&(mq->parent.parent));
  1488. return RT_EOK;
  1489. }
  1490. RTM_EXPORT(rt_mq_detach);
  1491. #ifdef RT_USING_HEAP
  1492. /**
  1493. * This function will create a message queue object from system resource
  1494. *
  1495. * @param name the name of message queue
  1496. * @param msg_size the size of message
  1497. * @param max_msgs the maximum number of message in queue
  1498. * @param flag the flag of message queue
  1499. *
  1500. * @return the created message queue, RT_NULL on error happen
  1501. */
  1502. rt_mq_t rt_mq_create(const char *name,
  1503. rt_size_t msg_size,
  1504. rt_size_t max_msgs,
  1505. rt_uint8_t flag)
  1506. {
  1507. struct rt_messagequeue *mq;
  1508. struct rt_mq_message *head;
  1509. register rt_base_t temp;
  1510. RT_DEBUG_NOT_IN_INTERRUPT;
  1511. /* allocate object */
  1512. mq = (rt_mq_t)rt_object_allocate(RT_Object_Class_MessageQueue, name);
  1513. if (mq == RT_NULL)
  1514. return mq;
  1515. /* set parent */
  1516. mq->parent.parent.flag = flag;
  1517. /* init ipc object */
  1518. rt_ipc_object_init(&(mq->parent));
  1519. /* init message queue */
  1520. /* get correct message size */
  1521. mq->msg_size = RT_ALIGN(msg_size, RT_ALIGN_SIZE);
  1522. mq->max_msgs = max_msgs;
  1523. /* allocate message pool */
  1524. mq->msg_pool = RT_KERNEL_MALLOC((mq->msg_size + sizeof(struct rt_mq_message)) * mq->max_msgs);
  1525. if (mq->msg_pool == RT_NULL)
  1526. {
  1527. rt_mq_delete(mq);
  1528. return RT_NULL;
  1529. }
  1530. /* init message list */
  1531. mq->msg_queue_head = RT_NULL;
  1532. mq->msg_queue_tail = RT_NULL;
  1533. /* init message empty list */
  1534. mq->msg_queue_free = RT_NULL;
  1535. for (temp = 0; temp < mq->max_msgs; temp ++)
  1536. {
  1537. head = (struct rt_mq_message *)((rt_uint8_t *)mq->msg_pool +
  1538. temp * (mq->msg_size + sizeof(struct rt_mq_message)));
  1539. head->next = mq->msg_queue_free;
  1540. mq->msg_queue_free = head;
  1541. }
  1542. /* the initial entry is zero */
  1543. mq->entry = 0;
  1544. return mq;
  1545. }
  1546. RTM_EXPORT(rt_mq_create);
  1547. /**
  1548. * This function will delete a message queue object and release the memory
  1549. *
  1550. * @param mq the message queue object
  1551. *
  1552. * @return the error code
  1553. */
  1554. rt_err_t rt_mq_delete(rt_mq_t mq)
  1555. {
  1556. RT_DEBUG_NOT_IN_INTERRUPT;
  1557. /* parameter check */
  1558. RT_ASSERT(mq != RT_NULL);
  1559. /* resume all suspended thread */
  1560. rt_ipc_list_resume_all(&(mq->parent.suspend_thread));
  1561. #if defined(RT_USING_MODULE) && defined(RT_USING_SLAB)
  1562. /* the mq object belongs to an application module */
  1563. if (mq->parent.parent.flag & RT_OBJECT_FLAG_MODULE)
  1564. rt_module_free(mq->parent.parent.module_id, mq->msg_pool);
  1565. else
  1566. #endif
  1567. /* free message queue pool */
  1568. RT_KERNEL_FREE(mq->msg_pool);
  1569. /* delete message queue object */
  1570. rt_object_delete(&(mq->parent.parent));
  1571. return RT_EOK;
  1572. }
  1573. RTM_EXPORT(rt_mq_delete);
  1574. #endif
  1575. /**
  1576. * This function will send a message to message queue object, if there are
  1577. * threads suspended on message queue object, it will be waked up.
  1578. *
  1579. * @param mq the message queue object
  1580. * @param buffer the message
  1581. * @param size the size of buffer
  1582. *
  1583. * @return the error code
  1584. */
  1585. rt_err_t rt_mq_send(rt_mq_t mq, void *buffer, rt_size_t size)
  1586. {
  1587. register rt_ubase_t temp;
  1588. struct rt_mq_message *msg;
  1589. RT_ASSERT(mq != RT_NULL);
  1590. RT_ASSERT(buffer != RT_NULL);
  1591. RT_ASSERT(size != 0);
  1592. /* greater than one message size */
  1593. if (size > mq->msg_size)
  1594. return -RT_ERROR;
  1595. RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(mq->parent.parent)));
  1596. /* disable interrupt */
  1597. temp = rt_hw_interrupt_disable();
  1598. /* get a free list, there must be an empty item */
  1599. msg = (struct rt_mq_message *)mq->msg_queue_free;
  1600. /* message queue is full */
  1601. if (msg == RT_NULL)
  1602. {
  1603. /* enable interrupt */
  1604. rt_hw_interrupt_enable(temp);
  1605. return -RT_EFULL;
  1606. }
  1607. /* move free list pointer */
  1608. mq->msg_queue_free = msg->next;
  1609. /* enable interrupt */
  1610. rt_hw_interrupt_enable(temp);
  1611. /* the msg is the new tailer of list, the next shall be NULL */
  1612. msg->next = RT_NULL;
  1613. /* copy buffer */
  1614. rt_memcpy(msg + 1, buffer, size);
  1615. /* disable interrupt */
  1616. temp = rt_hw_interrupt_disable();
  1617. /* link msg to message queue */
  1618. if (mq->msg_queue_tail != RT_NULL)
  1619. {
  1620. /* if the tail exists, */
  1621. ((struct rt_mq_message *)mq->msg_queue_tail)->next = msg;
  1622. }
  1623. /* set new tail */
  1624. mq->msg_queue_tail = msg;
  1625. /* if the head is empty, set head */
  1626. if (mq->msg_queue_head == RT_NULL)
  1627. mq->msg_queue_head = msg;
  1628. /* increase message entry */
  1629. mq->entry ++;
  1630. /* resume suspended thread */
  1631. if (!rt_list_isempty(&mq->parent.suspend_thread))
  1632. {
  1633. rt_ipc_list_resume(&(mq->parent.suspend_thread));
  1634. /* enable interrupt */
  1635. rt_hw_interrupt_enable(temp);
  1636. rt_schedule();
  1637. return RT_EOK;
  1638. }
  1639. /* enable interrupt */
  1640. rt_hw_interrupt_enable(temp);
  1641. return RT_EOK;
  1642. }
  1643. RTM_EXPORT(rt_mq_send);
  1644. /**
  1645. * This function will send an urgent message to message queue object, which
  1646. * means the message will be inserted to the head of message queue. If there
  1647. * are threads suspended on message queue object, it will be waked up.
  1648. *
  1649. * @param mq the message queue object
  1650. * @param buffer the message
  1651. * @param size the size of buffer
  1652. *
  1653. * @return the error code
  1654. */
  1655. rt_err_t rt_mq_urgent(rt_mq_t mq, void *buffer, rt_size_t size)
  1656. {
  1657. register rt_ubase_t temp;
  1658. struct rt_mq_message *msg;
  1659. RT_ASSERT(mq != RT_NULL);
  1660. RT_ASSERT(buffer != RT_NULL);
  1661. RT_ASSERT(size != 0);
  1662. /* greater than one message size */
  1663. if (size > mq->msg_size)
  1664. return -RT_ERROR;
  1665. RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(mq->parent.parent)));
  1666. /* disable interrupt */
  1667. temp = rt_hw_interrupt_disable();
  1668. /* get a free list, there must be an empty item */
  1669. msg = (struct rt_mq_message *)mq->msg_queue_free;
  1670. /* message queue is full */
  1671. if (msg == RT_NULL)
  1672. {
  1673. /* enable interrupt */
  1674. rt_hw_interrupt_enable(temp);
  1675. return -RT_EFULL;
  1676. }
  1677. /* move free list pointer */
  1678. mq->msg_queue_free = msg->next;
  1679. /* enable interrupt */
  1680. rt_hw_interrupt_enable(temp);
  1681. /* copy buffer */
  1682. rt_memcpy(msg + 1, buffer, size);
  1683. /* disable interrupt */
  1684. temp = rt_hw_interrupt_disable();
  1685. /* link msg to the beginning of message queue */
  1686. msg->next = mq->msg_queue_head;
  1687. mq->msg_queue_head = msg;
  1688. /* if there is no tail */
  1689. if (mq->msg_queue_tail == RT_NULL)
  1690. mq->msg_queue_tail = msg;
  1691. /* increase message entry */
  1692. mq->entry ++;
  1693. /* resume suspended thread */
  1694. if (!rt_list_isempty(&mq->parent.suspend_thread))
  1695. {
  1696. rt_ipc_list_resume(&(mq->parent.suspend_thread));
  1697. /* enable interrupt */
  1698. rt_hw_interrupt_enable(temp);
  1699. rt_schedule();
  1700. return RT_EOK;
  1701. }
  1702. /* enable interrupt */
  1703. rt_hw_interrupt_enable(temp);
  1704. return RT_EOK;
  1705. }
  1706. RTM_EXPORT(rt_mq_urgent);
  1707. /**
  1708. * This function will receive a message from message queue object, if there is
  1709. * no message in message queue object, the thread shall wait for a specified
  1710. * time.
  1711. *
  1712. * @param mq the message queue object
  1713. * @param buffer the received message will be saved in
  1714. * @param size the size of buffer
  1715. * @param timeout the waiting time
  1716. *
  1717. * @return the error code
  1718. */
  1719. rt_err_t rt_mq_recv(rt_mq_t mq,
  1720. void *buffer,
  1721. rt_size_t size,
  1722. rt_int32_t timeout)
  1723. {
  1724. struct rt_thread *thread;
  1725. register rt_ubase_t temp;
  1726. struct rt_mq_message *msg;
  1727. rt_uint32_t tick_delta;
  1728. RT_ASSERT(mq != RT_NULL);
  1729. RT_ASSERT(buffer != RT_NULL);
  1730. RT_ASSERT(size != 0);
  1731. /* initialize delta tick */
  1732. tick_delta = 0;
  1733. /* get current thread */
  1734. thread = rt_thread_self();
  1735. RT_OBJECT_HOOK_CALL(rt_object_trytake_hook, (&(mq->parent.parent)));
  1736. /* disable interrupt */
  1737. temp = rt_hw_interrupt_disable();
  1738. /* for non-blocking call */
  1739. if (mq->entry == 0 && timeout == 0)
  1740. {
  1741. rt_hw_interrupt_enable(temp);
  1742. return -RT_ETIMEOUT;
  1743. }
  1744. /* message queue is empty */
  1745. while (mq->entry == 0)
  1746. {
  1747. RT_DEBUG_IN_THREAD_CONTEXT;
  1748. /* reset error number in thread */
  1749. thread->error = RT_EOK;
  1750. /* no waiting, return timeout */
  1751. if (timeout == 0)
  1752. {
  1753. /* enable interrupt */
  1754. rt_hw_interrupt_enable(temp);
  1755. thread->error = -RT_ETIMEOUT;
  1756. return -RT_ETIMEOUT;
  1757. }
  1758. /* suspend current thread */
  1759. rt_ipc_list_suspend(&(mq->parent.suspend_thread),
  1760. thread,
  1761. mq->parent.parent.flag);
  1762. /* has waiting time, start thread timer */
  1763. if (timeout > 0)
  1764. {
  1765. /* get the start tick of timer */
  1766. tick_delta = rt_tick_get();
  1767. RT_DEBUG_LOG(RT_DEBUG_IPC, ("set thread:%s to timer list\n",
  1768. thread->name));
  1769. /* reset the timeout of thread timer and start it */
  1770. rt_timer_control(&(thread->thread_timer),
  1771. RT_TIMER_CTRL_SET_TIME,
  1772. &timeout);
  1773. rt_timer_start(&(thread->thread_timer));
  1774. }
  1775. /* enable interrupt */
  1776. rt_hw_interrupt_enable(temp);
  1777. /* re-schedule */
  1778. rt_schedule();
  1779. /* recv message */
  1780. if (thread->error != RT_EOK)
  1781. {
  1782. /* return error */
  1783. return thread->error;
  1784. }
  1785. /* disable interrupt */
  1786. temp = rt_hw_interrupt_disable();
  1787. /* if it's not waiting forever and then re-calculate timeout tick */
  1788. if (timeout > 0)
  1789. {
  1790. tick_delta = rt_tick_get() - tick_delta;
  1791. timeout -= tick_delta;
  1792. if (timeout < 0)
  1793. timeout = 0;
  1794. }
  1795. }
  1796. /* get message from queue */
  1797. msg = (struct rt_mq_message *)mq->msg_queue_head;
  1798. /* move message queue head */
  1799. mq->msg_queue_head = msg->next;
  1800. /* reach queue tail, set to NULL */
  1801. if (mq->msg_queue_tail == msg)
  1802. mq->msg_queue_tail = RT_NULL;
  1803. /* decrease message entry */
  1804. mq->entry --;
  1805. /* enable interrupt */
  1806. rt_hw_interrupt_enable(temp);
  1807. /* copy message */
  1808. rt_memcpy(buffer, msg + 1, size > mq->msg_size ? mq->msg_size : size);
  1809. /* disable interrupt */
  1810. temp = rt_hw_interrupt_disable();
  1811. /* put message to free list */
  1812. msg->next = (struct rt_mq_message *)mq->msg_queue_free;
  1813. mq->msg_queue_free = msg;
  1814. /* enable interrupt */
  1815. rt_hw_interrupt_enable(temp);
  1816. RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(mq->parent.parent)));
  1817. return RT_EOK;
  1818. }
  1819. RTM_EXPORT(rt_mq_recv);
  1820. /**
  1821. * This function can get or set some extra attributions of a message queue
  1822. * object.
  1823. *
  1824. * @param mq the message queue object
  1825. * @param cmd the execution command
  1826. * @param arg the execution argument
  1827. *
  1828. * @return the error code
  1829. */
  1830. rt_err_t rt_mq_control(rt_mq_t mq, rt_uint8_t cmd, void *arg)
  1831. {
  1832. rt_ubase_t level;
  1833. struct rt_mq_message *msg;
  1834. RT_ASSERT(mq != RT_NULL);
  1835. if (cmd == RT_IPC_CMD_RESET)
  1836. {
  1837. /* disable interrupt */
  1838. level = rt_hw_interrupt_disable();
  1839. /* resume all waiting thread */
  1840. rt_ipc_list_resume_all(&mq->parent.suspend_thread);
  1841. /* release all message in the queue */
  1842. while (mq->msg_queue_head != RT_NULL)
  1843. {
  1844. /* get message from queue */
  1845. msg = (struct rt_mq_message *)mq->msg_queue_head;
  1846. /* move message queue head */
  1847. mq->msg_queue_head = msg->next;
  1848. /* reach queue tail, set to NULL */
  1849. if (mq->msg_queue_tail == msg)
  1850. mq->msg_queue_tail = RT_NULL;
  1851. /* put message to free list */
  1852. msg->next = (struct rt_mq_message *)mq->msg_queue_free;
  1853. mq->msg_queue_free = msg;
  1854. }
  1855. /* clean entry */
  1856. mq->entry = 0;
  1857. /* enable interrupt */
  1858. rt_hw_interrupt_enable(level);
  1859. rt_schedule();
  1860. return RT_EOK;
  1861. }
  1862. return -RT_ERROR;
  1863. }
  1864. RTM_EXPORT(rt_mq_control);
  1865. #endif /* end of RT_USING_MESSAGEQUEUE */
  1866. /**@}*/