ipc.c 58 KB

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