ipc.c 65 KB

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