ipc.c 70 KB

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