ipc.c 61 KB

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