ipc.c 61 KB

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