ipc.c 69 KB

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