ipc.c 58 KB

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