ipc.c 67 KB

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