ipc.c 57 KB

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