ipc.c 70 KB

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