ipc.c 58 KB

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