ipc.c 69 KB

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