ipc.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105
  1. /*
  2. * File : ipc.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2011, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2006-03-14 Bernard the first version
  13. * 2006-04-25 Bernard implement semaphore
  14. * 2006-05-03 Bernard add RT_IPC_DEBUG
  15. * modify the type of IPC waiting time to rt_int32_t
  16. * 2006-05-10 Bernard fix the semaphore take bug and add IPC object
  17. * 2006-05-12 Bernard implement mailbox and message queue
  18. * 2006-05-20 Bernard implement mutex
  19. * 2006-05-23 Bernard implement fast event
  20. * 2006-05-24 Bernard implement event
  21. * 2006-06-03 Bernard fix the thread timer init bug
  22. * 2006-06-05 Bernard fix the mutex release bug
  23. * 2006-06-07 Bernard fix the message queue send bug
  24. * 2006-08-04 Bernard add hook support
  25. * 2009-05-21 Yi.qiu fix the sem release bug
  26. * 2009-07-18 Bernard fix the event clear bug
  27. * 2009-09-09 Bernard remove fast event and fix ipc release bug
  28. * 2009-10-10 Bernard change semaphore and mutex value to unsigned value
  29. * 2009-10-25 Bernard change the mb/mq receive timeout to 0 if the
  30. * re-calculated delta tick is a negative number.
  31. * 2009-12-16 Bernard fix the rt_ipc_object_suspend issue when IPC flag
  32. * is RT_IPC_FLAG_PRIO
  33. * 2010-01-20 mbbill remove rt_ipc_object_decrease function.
  34. * 2010-04-20 Bernard move memcpy outside interrupt disable in mq
  35. * 2010-10-26 yi.qiu add module support in rt_mp_delete and rt_mq_delete
  36. * 2010-11-10 Bernard add IPC reset command implementation.
  37. */
  38. #include <rtthread.h>
  39. #include <rthw.h>
  40. #include "kservice.h"
  41. #ifdef RT_USING_HOOK
  42. extern void (*rt_object_trytake_hook)(struct rt_object *object);
  43. extern void (*rt_object_take_hook)(struct rt_object *object);
  44. extern void (*rt_object_put_hook)(struct rt_object *object);
  45. #endif
  46. /**
  47. * @addtogroup IPC
  48. */
  49. /*@{*/
  50. /**
  51. * This function will initialize an IPC object
  52. *
  53. * @param ipc the IPC object
  54. *
  55. * @return the operation status, RT_EOK on successful
  56. */
  57. rt_inline rt_err_t rt_ipc_object_init(struct rt_ipc_object *ipc)
  58. {
  59. /* init ipc object */
  60. rt_list_init(&(ipc->suspend_thread));
  61. return RT_EOK;
  62. }
  63. /**
  64. * This function will suspend a thread to a specified list. IPC object or some double-queue
  65. * object (mailbox etc.) contains this kind of list.
  66. *
  67. * @param ipc the IPC object
  68. * @param thread the thread object to be suspended
  69. *
  70. * @return the operation status, RT_EOK on successful
  71. */
  72. rt_inline rt_err_t rt_ipc_list_suspend(rt_list_t *list, struct rt_thread *thread, rt_uint8_t flag)
  73. {
  74. /* suspend thread */
  75. rt_thread_suspend(thread);
  76. switch (flag)
  77. {
  78. case RT_IPC_FLAG_FIFO:
  79. rt_list_insert_before(list, &(thread->tlist));
  80. break;
  81. case RT_IPC_FLAG_PRIO:
  82. {
  83. struct rt_list_node *n;
  84. struct rt_thread *sthread;
  85. /* find a suitable position */
  86. for (n = list->next; n != list; n = n->next)
  87. {
  88. sthread = rt_list_entry(n, struct rt_thread, tlist);
  89. /* find out */
  90. if (thread->current_priority < sthread->current_priority)
  91. {
  92. /* insert this thread before the sthread */
  93. rt_list_insert_before(&(sthread->tlist), &(thread->tlist));
  94. break;
  95. }
  96. }
  97. /* not found a suitable position, append to the end of suspend_thread list */
  98. if (n == list)
  99. rt_list_insert_before(list, &(thread->tlist));
  100. }
  101. break;
  102. }
  103. return RT_EOK;
  104. }
  105. /**
  106. * This function will resume the first thread in the list of a IPC object:
  107. * - remove the thread from suspend queue of IPC object
  108. * - put the thread into system ready queue
  109. *
  110. * @param list the thread list
  111. *
  112. * @return the operation status, RT_EOK on successful
  113. */
  114. rt_inline rt_err_t rt_ipc_list_resume(rt_list_t *list)
  115. {
  116. struct rt_thread *thread;
  117. /* get thread entry */
  118. thread = rt_list_entry(list->next, struct rt_thread, tlist);
  119. RT_DEBUG_LOG(RT_DEBUG_IPC, ("resume thread:%s\n", thread->name));
  120. /* resume it */
  121. rt_thread_resume(thread);
  122. return RT_EOK;
  123. }
  124. /**
  125. * This function will resume all suspended threads in a list, including
  126. * suspend list of IPC object and private list of mailbox etc.
  127. *
  128. * @param list of the threads to resume
  129. *
  130. * @return the operation status, RT_EOK on successful
  131. */
  132. rt_inline rt_err_t rt_ipc_list_resume_all(rt_list_t *list)
  133. {
  134. struct rt_thread *thread;
  135. register rt_ubase_t temp;
  136. /* wakeup all suspend threads */
  137. while (!rt_list_isempty(list))
  138. {
  139. /* disable interrupt */
  140. temp = rt_hw_interrupt_disable();
  141. /* get next suspend thread */
  142. thread = rt_list_entry(list->next, struct rt_thread, tlist);
  143. /* set error code to RT_ERROR */
  144. thread->error = -RT_ERROR;
  145. /*
  146. * resume thread
  147. * In rt_thread_resume function, it will remove current thread from
  148. * suspend list
  149. */
  150. rt_thread_resume(thread);
  151. /* enable interrupt */
  152. rt_hw_interrupt_enable(temp);
  153. }
  154. return RT_EOK;
  155. }
  156. #ifdef RT_USING_SEMAPHORE
  157. /**
  158. * This function will initialize a semaphore and put it under control of resource
  159. * management.
  160. *
  161. * @param sem the semaphore object
  162. * @param name the name of semaphore
  163. * @param value the init value of semaphore
  164. * @param flag the flag of semaphore
  165. *
  166. * @return the operation status, RT_EOK on successful
  167. */
  168. rt_err_t rt_sem_init(rt_sem_t sem, const char *name, rt_uint32_t value, rt_uint8_t flag)
  169. {
  170. RT_ASSERT(sem != RT_NULL);
  171. /* init object */
  172. rt_object_init(&(sem->parent.parent), RT_Object_Class_Semaphore, name);
  173. /* init ipc object */
  174. rt_ipc_object_init(&(sem->parent));
  175. /* set init value */
  176. sem->value = value;
  177. /* set parent */
  178. sem->parent.parent.flag = flag;
  179. return RT_EOK;
  180. }
  181. /**
  182. * This function will detach a semaphore from resource management
  183. *
  184. * @param sem the semaphore object
  185. *
  186. * @return the operation status, RT_EOK on successful
  187. *
  188. * @see rt_sem_delete
  189. */
  190. rt_err_t rt_sem_detach(rt_sem_t sem)
  191. {
  192. RT_ASSERT(sem != RT_NULL);
  193. /* wakeup all suspend threads */
  194. rt_ipc_list_resume_all(&(sem->parent.suspend_thread));
  195. /* detach semaphore object */
  196. rt_object_detach(&(sem->parent.parent));
  197. return RT_EOK;
  198. }
  199. #ifdef RT_USING_HEAP
  200. /**
  201. * This function will create a semaphore from system resource
  202. *
  203. * @param name the name of semaphore
  204. * @param value the init value of semaphore
  205. * @param flag the flag of semaphore
  206. *
  207. * @return the created semaphore, RT_NULL on error happen
  208. *
  209. * @see rt_sem_init
  210. */
  211. rt_sem_t rt_sem_create(const char *name, rt_uint32_t value, rt_uint8_t flag)
  212. {
  213. rt_sem_t sem;
  214. RT_DEBUG_NOT_IN_INTERRUPT;
  215. /* allocate object */
  216. sem = (rt_sem_t)rt_object_allocate(RT_Object_Class_Semaphore, name);
  217. if (sem == RT_NULL) return sem;
  218. /* init ipc object */
  219. rt_ipc_object_init(&(sem->parent));
  220. /* set init value */
  221. sem->value = value;
  222. /* set parent */
  223. sem->parent.parent.flag = flag;
  224. return sem;
  225. }
  226. /**
  227. * This function will delete a semaphore object and release the memory
  228. *
  229. * @param sem the semaphore object
  230. *
  231. * @return the error code
  232. *
  233. * @see rt_sem_detach
  234. */
  235. rt_err_t rt_sem_delete(rt_sem_t sem)
  236. {
  237. RT_DEBUG_NOT_IN_INTERRUPT;
  238. RT_ASSERT(sem != RT_NULL);
  239. /* wakeup all suspend threads */
  240. rt_ipc_list_resume_all(&(sem->parent.suspend_thread));
  241. /* delete semaphore object */
  242. rt_object_delete(&(sem->parent.parent));
  243. return RT_EOK;
  244. }
  245. #endif
  246. /**
  247. * This function will take a semaphore, if the semaphore is unavailable, the
  248. * thread shall wait for a specified time.
  249. *
  250. * @param sem the semaphore object
  251. * @param time the waiting time
  252. *
  253. * @return the error code
  254. */
  255. rt_err_t rt_sem_take(rt_sem_t sem, rt_int32_t time)
  256. {
  257. register rt_base_t temp;
  258. struct rt_thread *thread;
  259. RT_ASSERT(sem != RT_NULL);
  260. RT_OBJECT_HOOK_CALL(rt_object_trytake_hook, (&(sem->parent.parent)));
  261. /* disable interrupt */
  262. temp = rt_hw_interrupt_disable();
  263. RT_DEBUG_LOG(RT_DEBUG_IPC,
  264. ("thread %s take sem:%s, which value is: %d\n", rt_thread_self()->name,
  265. ((struct rt_object *)sem)->name, sem->value));
  266. if (sem->value > 0)
  267. {
  268. /* semaphore is available */
  269. sem->value --;
  270. /* enable interrupt */
  271. rt_hw_interrupt_enable(temp);
  272. }
  273. else
  274. {
  275. /* no waiting, return with timeout */
  276. if (time == 0 )
  277. {
  278. rt_hw_interrupt_enable(temp);
  279. return -RT_ETIMEOUT;
  280. }
  281. else
  282. {
  283. /* current context checking */
  284. RT_DEBUG_NOT_IN_INTERRUPT;
  285. /* semaphore is unavailable, push to suspend list */
  286. /* get current thread */
  287. thread = rt_thread_self();
  288. /* reset thread error number */
  289. thread->error = RT_EOK;
  290. RT_DEBUG_LOG(RT_DEBUG_IPC, ("sem take: suspend thread - %s\n", thread->name));
  291. /* suspend thread */
  292. rt_ipc_list_suspend(&(sem->parent.suspend_thread),
  293. thread, sem->parent.parent.flag);
  294. /* has waiting time, start thread timer */
  295. if (time > 0)
  296. {
  297. RT_DEBUG_LOG(RT_DEBUG_IPC, ("set thread:%s to timer list\n", thread->name));
  298. /* reset the timeout of thread timer and start it */
  299. rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &time);
  300. rt_timer_start(&(thread->thread_timer));
  301. }
  302. /* enable interrupt */
  303. rt_hw_interrupt_enable(temp);
  304. /* do schedule */
  305. rt_schedule();
  306. if (thread->error != RT_EOK)
  307. {
  308. return thread->error;
  309. }
  310. }
  311. }
  312. RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(sem->parent.parent)));
  313. return RT_EOK;
  314. }
  315. /**
  316. * This function will try to take a semaphore and immediately return
  317. *
  318. * @param sem the semaphore object
  319. *
  320. * @return the error code
  321. */
  322. rt_err_t rt_sem_trytake(rt_sem_t sem)
  323. {
  324. return rt_sem_take(sem, 0);
  325. }
  326. /**
  327. * This function will release a semaphore, if there are threads suspended on
  328. * semaphore, it will be waked up.
  329. *
  330. * @param sem the semaphore object
  331. *
  332. * @return the error code
  333. */
  334. rt_err_t rt_sem_release(rt_sem_t sem)
  335. {
  336. register rt_base_t temp;
  337. register rt_bool_t need_schedule;
  338. RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(sem->parent.parent)));
  339. need_schedule = RT_FALSE;
  340. /* disable interrupt */
  341. temp = rt_hw_interrupt_disable();
  342. RT_DEBUG_LOG(RT_DEBUG_IPC,
  343. ("thread %s releases sem:%s, which value is: %d\n", rt_thread_self()->name,
  344. ((struct rt_object *)sem)->name, sem->value));
  345. if (!rt_list_isempty(&sem->parent.suspend_thread))
  346. {
  347. /* resume the suspended thread */
  348. rt_ipc_list_resume(&(sem->parent.suspend_thread));
  349. need_schedule = RT_TRUE;
  350. }
  351. else sem->value ++; /* increase value */
  352. /* enable interrupt */
  353. rt_hw_interrupt_enable(temp);
  354. /* resume a thread, re-schedule */
  355. if (need_schedule == RT_TRUE) rt_schedule();
  356. return RT_EOK;
  357. }
  358. /**
  359. * This function can get or set some extra attributions of a semaphore object.
  360. *
  361. * @param sem the semaphore object
  362. * @param cmd the execution command
  363. * @param arg the execution argument
  364. *
  365. * @return the error code
  366. */
  367. rt_err_t rt_sem_control(rt_sem_t sem, rt_uint8_t cmd, void *arg)
  368. {
  369. rt_ubase_t level;
  370. RT_ASSERT(sem != RT_NULL);
  371. if (cmd == RT_IPC_CMD_RESET)
  372. {
  373. rt_uint32_t value;
  374. /* get value */
  375. value = (rt_uint32_t)arg;
  376. /* disable interrupt */
  377. level = rt_hw_interrupt_disable();
  378. /* resume all waiting thread */
  379. rt_ipc_list_resume_all(&sem->parent.suspend_thread);
  380. /* set new value */
  381. sem->value = (rt_uint16_t)value;
  382. /* enable interrupt */
  383. rt_hw_interrupt_enable(level);
  384. return RT_EOK;
  385. }
  386. return -RT_ERROR;
  387. }
  388. #endif /* end of RT_USING_SEMAPHORE */
  389. #ifdef RT_USING_MUTEX
  390. /**
  391. * This function will initialize a mutex and put it under control of resource
  392. * management.
  393. *
  394. * @param mutex the mutex object
  395. * @param name the name of mutex
  396. * @param flag the flag of mutex
  397. *
  398. * @return the operation status, RT_EOK on successful
  399. */
  400. rt_err_t rt_mutex_init(rt_mutex_t mutex, const char *name, rt_uint8_t flag)
  401. {
  402. RT_ASSERT(mutex != RT_NULL);
  403. /* init object */
  404. rt_object_init(&(mutex->parent.parent), RT_Object_Class_Mutex, name);
  405. /* init ipc object */
  406. rt_ipc_object_init(&(mutex->parent));
  407. mutex->value = 1;
  408. mutex->owner = RT_NULL;
  409. mutex->original_priority = 0xFF;
  410. mutex->hold = 0;
  411. /* set flag */
  412. mutex->parent.parent.flag = flag;
  413. return RT_EOK;
  414. }
  415. /**
  416. * This function will detach a mutex from resource management
  417. *
  418. * @param mutex the mutex object
  419. *
  420. * @return the operation status, RT_EOK on successful
  421. *
  422. * @see rt_mutex_delete
  423. */
  424. rt_err_t rt_mutex_detach(rt_mutex_t mutex)
  425. {
  426. RT_ASSERT(mutex != RT_NULL);
  427. /* wakeup all suspend threads */
  428. rt_ipc_list_resume_all(&(mutex->parent.suspend_thread));
  429. /* detach semaphore object */
  430. rt_object_detach(&(mutex->parent.parent));
  431. return RT_EOK;
  432. }
  433. #ifdef RT_USING_HEAP
  434. /**
  435. * This function will create a mutex from system resource
  436. *
  437. * @param name the name of mutex
  438. * @param flag the flag of mutex
  439. *
  440. * @return the created mutex, RT_NULL on error happen
  441. *
  442. * @see rt_mutex_init
  443. */
  444. rt_mutex_t rt_mutex_create(const char *name, rt_uint8_t flag)
  445. {
  446. struct rt_mutex *mutex;
  447. RT_DEBUG_NOT_IN_INTERRUPT;
  448. /* allocate object */
  449. mutex = (rt_mutex_t)rt_object_allocate(RT_Object_Class_Mutex, name);
  450. if (mutex == RT_NULL) return mutex;
  451. /* init ipc object */
  452. rt_ipc_object_init(&(mutex->parent));
  453. mutex->value = 1;
  454. mutex->owner = RT_NULL;
  455. mutex->original_priority = 0xFF;
  456. mutex->hold = 0;
  457. /* set flag */
  458. mutex->parent.parent.flag = flag;
  459. return mutex;
  460. }
  461. /**
  462. * This function will delete a mutex object and release the memory
  463. *
  464. * @param mutex the mutex object
  465. *
  466. * @return the error code
  467. *
  468. * @see rt_mutex_detach
  469. */
  470. rt_err_t rt_mutex_delete(rt_mutex_t mutex)
  471. {
  472. RT_DEBUG_NOT_IN_INTERRUPT;
  473. RT_ASSERT(mutex != RT_NULL);
  474. /* wakeup all suspend threads */
  475. rt_ipc_list_resume_all(&(mutex->parent.suspend_thread));
  476. /* delete semaphore object */
  477. rt_object_delete(&(mutex->parent.parent));
  478. return RT_EOK;
  479. }
  480. #endif
  481. /**
  482. * This function will take a mutex, if the mutex is unavailable, the
  483. * thread shall wait for a specified time.
  484. *
  485. * @param mutex the mutex object
  486. * @param time the waiting time
  487. *
  488. * @return the error code
  489. */
  490. rt_err_t rt_mutex_take(rt_mutex_t mutex, rt_int32_t time)
  491. {
  492. register rt_base_t temp;
  493. struct rt_thread *thread;
  494. /* this function must not be used in interrupt even if time = 0 */
  495. RT_DEBUG_NOT_IN_INTERRUPT;
  496. RT_ASSERT(mutex != RT_NULL);
  497. /* disable interrupt */
  498. temp = rt_hw_interrupt_disable();
  499. /* get current thread */
  500. thread = rt_thread_self();
  501. RT_OBJECT_HOOK_CALL(rt_object_trytake_hook, (&(mutex->parent.parent)));
  502. RT_DEBUG_LOG(RT_DEBUG_IPC,
  503. ("mutex_take: current thread %s, mutex value: %d, hold: %d\n",
  504. thread->name, mutex->value, mutex->hold));
  505. /* reset thread error */
  506. thread->error = RT_EOK;
  507. if (mutex->owner == thread)
  508. {
  509. /* it's the same thread */
  510. mutex->hold ++;
  511. }
  512. else
  513. {
  514. /* The value of mutex is 1 in initial status. Therefore, if the
  515. * value is great than 0, it indicates the mutex is avaible.
  516. */
  517. if (mutex->value > 0)
  518. {
  519. /* mutex is available */
  520. mutex->value --;
  521. /* set mutex owner and original priority */
  522. mutex->owner = thread;
  523. mutex->original_priority = thread->current_priority;
  524. mutex->hold ++;
  525. }
  526. else
  527. {
  528. /* no waiting, return with timeout */
  529. if (time == 0)
  530. {
  531. /* set error as timeout */
  532. thread->error = -RT_ETIMEOUT;
  533. /* enable interrupt */
  534. rt_hw_interrupt_enable(temp);
  535. return -RT_ETIMEOUT;
  536. }
  537. else
  538. {
  539. /* mutex is unavailable, push to suspend list */
  540. RT_DEBUG_LOG(RT_DEBUG_IPC,
  541. ("mutex_take: suspend thread: %s\n", thread->name));
  542. /* change the owner thread priority of mutex */
  543. if (thread->current_priority < mutex->owner->current_priority)
  544. {
  545. /* change the owner thread priority */
  546. rt_thread_control(mutex->owner, RT_THREAD_CTRL_CHANGE_PRIORITY,
  547. &thread->current_priority);
  548. }
  549. /* suspend current thread */
  550. rt_ipc_list_suspend(&(mutex->parent.suspend_thread),
  551. thread, mutex->parent.parent.flag);
  552. /* has waiting time, start thread timer */
  553. if (time > 0)
  554. {
  555. RT_DEBUG_LOG(RT_DEBUG_IPC,
  556. ("mutex_take: start the timer of thread:%s\n", thread->name));
  557. /* reset the timeout of thread timer and start it */
  558. rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &time);
  559. rt_timer_start(&(thread->thread_timer));
  560. }
  561. /* enable interrupt */
  562. rt_hw_interrupt_enable(temp);
  563. /* do schedule */
  564. rt_schedule();
  565. if (thread->error != RT_EOK)
  566. {
  567. /* return error */
  568. return thread->error;
  569. }
  570. else
  571. {
  572. /* the mutex is taken successfully. */
  573. /* disable interrupt */
  574. temp = rt_hw_interrupt_disable();
  575. }
  576. }
  577. }
  578. }
  579. /* enable interrupt */
  580. rt_hw_interrupt_enable(temp);
  581. RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(mutex->parent.parent)));
  582. return RT_EOK;
  583. }
  584. /**
  585. * This function will release a mutex, if there are threads suspended on mutex,
  586. * it will be waked up.
  587. *
  588. * @param mutex the mutex object
  589. *
  590. * @return the error code
  591. */
  592. rt_err_t rt_mutex_release(rt_mutex_t mutex)
  593. {
  594. register rt_base_t temp;
  595. struct rt_thread *thread;
  596. rt_bool_t need_schedule;
  597. need_schedule = RT_FALSE;
  598. /* get current thread */
  599. thread = rt_thread_self();
  600. /* disable interrupt */
  601. temp = rt_hw_interrupt_disable();
  602. RT_DEBUG_LOG(RT_DEBUG_IPC,
  603. ("mutex_release:current thread %s, mutex value: %d, hold: %d\n",
  604. thread->name, mutex->value, mutex->hold));
  605. RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(mutex->parent.parent)));
  606. /* mutex only can be released by owner */
  607. if (thread != mutex->owner)
  608. {
  609. thread->error = -RT_ERROR;
  610. /* enable interrupt */
  611. rt_hw_interrupt_enable(temp);
  612. return -RT_ERROR;
  613. }
  614. /* decrease hold */
  615. mutex->hold --;
  616. /* if no hold */
  617. if (mutex->hold == 0)
  618. {
  619. /* change the owner thread to original priority */
  620. if (mutex->original_priority != mutex->owner->current_priority)
  621. {
  622. rt_thread_control(mutex->owner, RT_THREAD_CTRL_CHANGE_PRIORITY,
  623. &(mutex->original_priority));
  624. }
  625. /* wakeup suspended thread */
  626. if (!rt_list_isempty(&mutex->parent.suspend_thread))
  627. {
  628. /* get suspended thread */
  629. thread = rt_list_entry(mutex->parent.suspend_thread.next, struct rt_thread, tlist);
  630. RT_DEBUG_LOG(RT_DEBUG_IPC, ("mutex_release: resume thread: %s\n", thread->name));
  631. /* set new owner and priority */
  632. mutex->owner = thread;
  633. mutex->original_priority = thread->current_priority;
  634. mutex->hold ++;
  635. /* resume thread */
  636. rt_ipc_list_resume(&(mutex->parent.suspend_thread));
  637. need_schedule = RT_TRUE;
  638. }
  639. else
  640. {
  641. /* increase value */
  642. mutex->value ++;
  643. /* clear owner */
  644. mutex->owner = RT_NULL;
  645. mutex->original_priority = 0xff;
  646. }
  647. }
  648. /* enable interrupt */
  649. rt_hw_interrupt_enable(temp);
  650. /* perform a schedule */
  651. if (need_schedule == RT_TRUE) rt_schedule();
  652. return RT_EOK;
  653. }
  654. /**
  655. * This function can get or set some extra attributions of a mutex object.
  656. *
  657. * @param mutex the mutex object
  658. * @param cmd the execution command
  659. * @param arg the execution argument
  660. *
  661. * @return the error code
  662. */
  663. rt_err_t rt_mutex_control(rt_mutex_t mutex, rt_uint8_t cmd, void *arg)
  664. {
  665. return -RT_ERROR;
  666. }
  667. #endif /* end of RT_USING_MUTEX */
  668. #ifdef RT_USING_EVENT
  669. /**
  670. * This function will initialize an event and put it under control of resource
  671. * management.
  672. *
  673. * @param event the event object
  674. * @param name the name of event
  675. * @param flag the flag of event
  676. *
  677. * @return the operation status, RT_EOK on successful
  678. */
  679. rt_err_t rt_event_init(rt_event_t event, const char *name, rt_uint8_t flag)
  680. {
  681. RT_ASSERT(event != RT_NULL);
  682. /* init object */
  683. rt_object_init(&(event->parent.parent), RT_Object_Class_Event, name);
  684. /* set parent flag */
  685. event->parent.parent.flag = flag;
  686. /* init ipc object */
  687. rt_ipc_object_init(&(event->parent));
  688. /* init event */
  689. event->set = 0;
  690. return RT_EOK;
  691. }
  692. /**
  693. * This function will detach an event object from resource management
  694. *
  695. * @param event the event object
  696. *
  697. * @return the operation status, RT_EOK on successful
  698. */
  699. rt_err_t rt_event_detach(rt_event_t event)
  700. {
  701. /* parameter check */
  702. RT_ASSERT(event != RT_NULL);
  703. /* resume all suspended thread */
  704. rt_ipc_list_resume_all(&(event->parent.suspend_thread));
  705. /* detach event object */
  706. rt_object_detach(&(event->parent.parent));
  707. return RT_EOK;
  708. }
  709. #ifdef RT_USING_HEAP
  710. /**
  711. * This function will create an event object from system resource
  712. *
  713. * @param name the name of event
  714. * @param flag the flag of event
  715. *
  716. * @return the created event, RT_NULL on error happen
  717. */
  718. rt_event_t rt_event_create(const char *name, rt_uint8_t flag)
  719. {
  720. rt_event_t event;
  721. RT_DEBUG_NOT_IN_INTERRUPT;
  722. /* allocate object */
  723. event = (rt_event_t)rt_object_allocate(RT_Object_Class_Event, name);
  724. if (event == RT_NULL) return event;
  725. /* set parent */
  726. event->parent.parent.flag = flag;
  727. /* init ipc object */
  728. rt_ipc_object_init(&(event->parent));
  729. /* init event */
  730. event->set = 0;
  731. return event;
  732. }
  733. /**
  734. * This function will delete an event object and release the memory
  735. *
  736. * @param event the event object
  737. *
  738. * @return the error code
  739. */
  740. rt_err_t rt_event_delete(rt_event_t event)
  741. {
  742. /* parameter check */
  743. RT_ASSERT(event != RT_NULL);
  744. RT_DEBUG_NOT_IN_INTERRUPT;
  745. /* resume all suspended thread */
  746. rt_ipc_list_resume_all(&(event->parent.suspend_thread));
  747. /* delete event object */
  748. rt_object_delete(&(event->parent.parent));
  749. return RT_EOK;
  750. }
  751. #endif
  752. /**
  753. * This function will send an event to the event object, if there are threads
  754. * suspended on event object, it will be waked up.
  755. *
  756. * @param event the event object
  757. * @param set the event set
  758. *
  759. * @return the error code
  760. */
  761. rt_err_t rt_event_send(rt_event_t event, rt_uint32_t set)
  762. {
  763. struct rt_list_node *n;
  764. struct rt_thread *thread;
  765. register rt_ubase_t level;
  766. register rt_base_t status;
  767. rt_bool_t need_schedule;
  768. /* parameter check */
  769. RT_ASSERT(event != RT_NULL);
  770. if (set == 0) return -RT_ERROR;
  771. need_schedule = RT_FALSE;
  772. RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(event->parent.parent)));
  773. /* disable interrupt */
  774. level = rt_hw_interrupt_disable();
  775. /* set event */
  776. event->set |= set;
  777. if (!rt_list_isempty(&event->parent.suspend_thread))
  778. {
  779. /* search thread list to resume thread */
  780. n = event->parent.suspend_thread.next;
  781. while (n != &(event->parent.suspend_thread))
  782. {
  783. /* get thread */
  784. thread = rt_list_entry(n, struct rt_thread, tlist);
  785. status = -RT_ERROR;
  786. if (thread->event_info & RT_EVENT_FLAG_AND)
  787. {
  788. if ((thread->event_set & event->set) == thread->event_set)
  789. {
  790. /* received an AND event */
  791. status = RT_EOK;
  792. }
  793. }
  794. else if (thread->event_info & RT_EVENT_FLAG_OR)
  795. {
  796. if (thread->event_set & event->set)
  797. {
  798. /* save recieved event set */
  799. thread->event_set = thread->event_set & event->set;
  800. /* received an OR event */
  801. status = RT_EOK;
  802. }
  803. }
  804. /* move node to the next */
  805. n = n->next;
  806. /* condition is satisfied, resume thread */
  807. if (status == RT_EOK)
  808. {
  809. /* clear event */
  810. if (thread->event_info & RT_EVENT_FLAG_CLEAR)
  811. event->set &= ~thread->event_set;
  812. /* resume thread, and thread list breaks out */
  813. rt_thread_resume(thread);
  814. /* need do a scheduling */
  815. need_schedule = RT_TRUE;
  816. }
  817. }
  818. }
  819. /* enable interrupt */
  820. rt_hw_interrupt_enable(level);
  821. /* do a schedule */
  822. if (need_schedule == RT_TRUE)
  823. rt_schedule();
  824. return RT_EOK;
  825. }
  826. /**
  827. * This function will receive an event from event object, if the event is unavailable,
  828. * the thread shall wait for a specified time.
  829. *
  830. * @param event the fast event object
  831. * @param set the interested event set
  832. * @param option the receive option
  833. * @param timeout the waiting time
  834. * @param recved the received event
  835. *
  836. * @return the error code
  837. */
  838. rt_err_t rt_event_recv(rt_event_t event, rt_uint32_t set, rt_uint8_t option, rt_int32_t timeout, rt_uint32_t *recved)
  839. {
  840. struct rt_thread *thread;
  841. register rt_ubase_t level;
  842. register rt_base_t status;
  843. RT_DEBUG_NOT_IN_INTERRUPT;
  844. /* parameter check */
  845. RT_ASSERT(event != RT_NULL);
  846. if (set == 0) return -RT_ERROR;
  847. /* init status */
  848. status = -RT_ERROR;
  849. /* get current thread */
  850. thread = rt_thread_self();
  851. /* reset thread error */
  852. thread->error = RT_EOK;
  853. RT_OBJECT_HOOK_CALL(rt_object_trytake_hook, (&(event->parent.parent)));
  854. /* disable interrupt */
  855. level = rt_hw_interrupt_disable();
  856. /* check event set */
  857. if (option & RT_EVENT_FLAG_AND)
  858. {
  859. if ((event->set & set) == set) status = RT_EOK;
  860. }
  861. else if (option & RT_EVENT_FLAG_OR)
  862. {
  863. if (event->set & set) status = RT_EOK;
  864. }
  865. if (status == RT_EOK)
  866. {
  867. /* set received event */
  868. *recved = (event->set & set);
  869. /* received event */
  870. if (option & RT_EVENT_FLAG_CLEAR) event->set &= ~set;
  871. }
  872. else if (timeout == 0)
  873. {
  874. /* no waiting */
  875. thread->error = -RT_ETIMEOUT;
  876. }
  877. else
  878. {
  879. /* fill thread event info */
  880. thread->event_set = set;
  881. thread->event_info = option;
  882. /* put thread to suspended thread list */
  883. rt_ipc_list_suspend(&(event->parent.suspend_thread), thread, event->parent.parent.flag);
  884. /* if there is a waiting timeout, active thread timer */
  885. if (timeout > 0)
  886. {
  887. /* reset the timeout of thread timer and start it */
  888. rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &timeout);
  889. rt_timer_start(&(thread->thread_timer));
  890. }
  891. /* enable interrupt */
  892. rt_hw_interrupt_enable(level);
  893. /* do a schedule */
  894. rt_schedule();
  895. if (thread->error != RT_EOK)
  896. {
  897. /* return error */
  898. return thread->error;
  899. }
  900. /* received an event, disable interrupt to protect */
  901. level = rt_hw_interrupt_disable();
  902. /* set received event */
  903. *recved = thread->event_set;
  904. }
  905. /* enable interrupt */
  906. rt_hw_interrupt_enable(level);
  907. RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(event->parent.parent)));
  908. return thread->error;
  909. }
  910. /**
  911. * This function can get or set some extra attributions of an event object.
  912. *
  913. * @param event the event object
  914. * @param cmd the execution command
  915. * @param arg the execution argument
  916. *
  917. * @return the error code
  918. */
  919. rt_err_t rt_event_control(rt_event_t event, rt_uint8_t cmd, void *arg)
  920. {
  921. rt_ubase_t level;
  922. RT_ASSERT(event != RT_NULL);
  923. if (cmd == RT_IPC_CMD_RESET)
  924. {
  925. /* disable interrupt */
  926. level = rt_hw_interrupt_disable();
  927. /* resume all waiting thread */
  928. rt_ipc_list_resume_all(&event->parent.suspend_thread);
  929. /* init event set */
  930. event->set = 0;
  931. /* enable interrupt */
  932. rt_hw_interrupt_enable(level);
  933. return RT_EOK;
  934. }
  935. return -RT_ERROR;
  936. }
  937. #endif /* end of RT_USING_EVENT */
  938. #ifdef RT_USING_MAILBOX
  939. /**
  940. * This function will initialize a mailbox and put it under control of resource
  941. * management.
  942. *
  943. * @param mb the mailbox object
  944. * @param name the name of mailbox
  945. * @param msgpool the begin address of buffer to save received mail
  946. * @param size the size of mailbox
  947. * @param flag the flag of mailbox
  948. *
  949. * @return the operation status, RT_EOK on successful
  950. */
  951. rt_err_t rt_mb_init(rt_mailbox_t mb, const char *name, void *msgpool, rt_size_t size, rt_uint8_t flag)
  952. {
  953. RT_ASSERT(mb != RT_NULL);
  954. /* init object */
  955. rt_object_init(&(mb->parent.parent), RT_Object_Class_MailBox, name);
  956. /* set parent flag */
  957. mb->parent.parent.flag = flag;
  958. /* init ipc object */
  959. rt_ipc_object_init(&(mb->parent));
  960. /* init mailbox */
  961. mb->msg_pool = msgpool;
  962. mb->size = size;
  963. mb->entry = 0;
  964. mb->in_offset = 0;
  965. mb->out_offset = 0;
  966. /* init an additional list of sender suspend thread */
  967. rt_list_init(&(mb->suspend_sender_thread));
  968. return RT_EOK;
  969. }
  970. /**
  971. * This function will detach a mailbox from resource management
  972. *
  973. * @param mb the mailbox object
  974. *
  975. * @return the operation status, RT_EOK on successful
  976. */
  977. rt_err_t rt_mb_detach(rt_mailbox_t mb)
  978. {
  979. /* parameter check */
  980. RT_ASSERT(mb != RT_NULL);
  981. /* resume all suspended thread */
  982. rt_ipc_list_resume_all(&(mb->parent.suspend_thread));
  983. /* also resume all mailbox private suspended thread */
  984. rt_ipc_list_resume_all(&(mb->suspend_sender_thread));
  985. /* detach mailbox object */
  986. rt_object_detach(&(mb->parent.parent));
  987. return RT_EOK;
  988. }
  989. #ifdef RT_USING_HEAP
  990. /**
  991. * This function will create a mailbox object from system resource
  992. *
  993. * @param name the name of mailbox
  994. * @param size the size of mailbox
  995. * @param flag the flag of mailbox
  996. *
  997. * @return the created mailbox, RT_NULL on error happen
  998. */
  999. rt_mailbox_t rt_mb_create(const char *name, rt_size_t size, rt_uint8_t flag)
  1000. {
  1001. rt_mailbox_t mb;
  1002. RT_DEBUG_NOT_IN_INTERRUPT;
  1003. /* allocate object */
  1004. mb = (rt_mailbox_t)rt_object_allocate(RT_Object_Class_MailBox, name);
  1005. if (mb == RT_NULL) return mb;
  1006. /* set parent */
  1007. mb->parent.parent.flag = flag;
  1008. /* init ipc object */
  1009. rt_ipc_object_init(&(mb->parent));
  1010. /* init mailbox */
  1011. mb->size = size;
  1012. mb->msg_pool = rt_malloc(mb->size * sizeof(rt_uint32_t));
  1013. if (mb->msg_pool == RT_NULL)
  1014. {
  1015. /* delete mailbox object */
  1016. rt_object_delete(&(mb->parent.parent));
  1017. return RT_NULL;
  1018. }
  1019. mb->entry = 0;
  1020. mb->in_offset = 0;
  1021. mb->out_offset = 0;
  1022. /* init an additional list of sender suspend thread */
  1023. rt_list_init(&(mb->suspend_sender_thread));
  1024. return mb;
  1025. }
  1026. /**
  1027. * This function will delete a mailbox object and release the memory
  1028. *
  1029. * @param mb the mailbox object
  1030. *
  1031. * @return the error code
  1032. */
  1033. rt_err_t rt_mb_delete(rt_mailbox_t mb)
  1034. {
  1035. RT_DEBUG_NOT_IN_INTERRUPT;
  1036. /* parameter check */
  1037. RT_ASSERT(mb != RT_NULL);
  1038. /* resume all suspended thread */
  1039. rt_ipc_list_resume_all(&(mb->parent.suspend_thread));
  1040. /* also resume all mailbox private suspended thread */
  1041. rt_ipc_list_resume_all(&(mb->suspend_sender_thread));
  1042. #if defined(RT_USING_MODULE) && defined(RT_USING_SLAB)
  1043. /* the mb object belongs to an application module */
  1044. if (mb->parent.parent.flag & RT_OBJECT_FLAG_MODULE)
  1045. rt_module_free(mb->parent.parent.module_id, mb->msg_pool);
  1046. else
  1047. #endif
  1048. /* free mailbox pool */
  1049. rt_free(mb->msg_pool);
  1050. /* delete mailbox object */
  1051. rt_object_delete(&(mb->parent.parent));
  1052. return RT_EOK;
  1053. }
  1054. #endif
  1055. /**
  1056. * This function will send a mail to mailbox object. If the mailbox is full,
  1057. * current thread will be suspended until timeout.
  1058. *
  1059. * @param mb the mailbox object
  1060. * @param value the mail
  1061. * @param timeout the waiting time
  1062. *
  1063. * @return the error code
  1064. */
  1065. rt_err_t rt_mb_send_wait(rt_mailbox_t mb, rt_uint32_t value, rt_int32_t timeout)
  1066. {
  1067. struct rt_thread *thread;
  1068. register rt_ubase_t temp;
  1069. rt_uint32_t tick_delta;
  1070. /* parameter check */
  1071. RT_ASSERT(mb != RT_NULL);
  1072. /* initialize delta tick */
  1073. tick_delta = 0;
  1074. /* get current thread */
  1075. thread = rt_thread_self();
  1076. RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(mb->parent.parent)));
  1077. /* disable interrupt */
  1078. temp = rt_hw_interrupt_disable();
  1079. /* for non-blocking call */
  1080. if (mb->entry == mb->size && timeout == 0)
  1081. {
  1082. rt_hw_interrupt_enable(temp);
  1083. return -RT_EFULL;
  1084. }
  1085. /* mailbox is full */
  1086. while (mb->entry == mb->size)
  1087. {
  1088. /* reset error number in thread */
  1089. thread->error = RT_EOK;
  1090. /* no waiting, return timeout */
  1091. if (timeout == 0)
  1092. {
  1093. /* enable interrupt */
  1094. rt_hw_interrupt_enable(temp);
  1095. return -RT_EFULL;
  1096. }
  1097. RT_DEBUG_NOT_IN_INTERRUPT;
  1098. /* suspend current thread */
  1099. rt_ipc_list_suspend(&(mb->suspend_sender_thread), thread, mb->parent.parent.flag);
  1100. /* has waiting time, start thread timer */
  1101. if (timeout > 0)
  1102. {
  1103. /* get the start tick of timer */
  1104. tick_delta = rt_tick_get();
  1105. RT_DEBUG_LOG(RT_DEBUG_IPC,
  1106. ("mb_send_wait: start timer of thread:%s\n", thread->name));
  1107. /* reset the timeout of thread timer and start it */
  1108. rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &timeout);
  1109. rt_timer_start(&(thread->thread_timer));
  1110. }
  1111. /* enable interrupt */
  1112. rt_hw_interrupt_enable(temp);
  1113. /* re-schedule */
  1114. rt_schedule();
  1115. /* resume from suspend state */
  1116. if (thread->error != RT_EOK)
  1117. {
  1118. /* return error */
  1119. return thread->error;
  1120. }
  1121. /* disable interrupt */
  1122. temp = rt_hw_interrupt_disable();
  1123. /* if it's not waiting forever and then re-calculate timeout tick */
  1124. if (timeout > 0)
  1125. {
  1126. tick_delta = rt_tick_get() - tick_delta;
  1127. timeout -= tick_delta;
  1128. if (timeout < 0) timeout = 0;
  1129. }
  1130. }
  1131. /* set ptr */
  1132. mb->msg_pool[mb->in_offset] = value;
  1133. /* increase input offset */
  1134. ++ mb->in_offset;
  1135. if (mb->in_offset >= mb->size) mb->in_offset = 0;
  1136. /* increase message entry */
  1137. mb->entry ++;
  1138. /* resume suspended thread */
  1139. if (!rt_list_isempty(&mb->parent.suspend_thread))
  1140. {
  1141. rt_ipc_list_resume(&(mb->parent.suspend_thread));
  1142. /* enable interrupt */
  1143. rt_hw_interrupt_enable(temp);
  1144. rt_schedule();
  1145. return RT_EOK;
  1146. }
  1147. /* enable interrupt */
  1148. rt_hw_interrupt_enable(temp);
  1149. return RT_EOK;
  1150. }
  1151. /**
  1152. * This function will send a mail to mailbox object, if there are threads suspended
  1153. * on mailbox object, it will be waked up. This function will return immediately, if
  1154. * you want blocking send, use rt_mb_send_wait instead.
  1155. *
  1156. * @param mb the mailbox object
  1157. * @param value the mail
  1158. *
  1159. * @return the error code
  1160. */
  1161. rt_err_t rt_mb_send(rt_mailbox_t mb, rt_uint32_t value)
  1162. {
  1163. return rt_mb_send_wait(mb, value, 0);
  1164. }
  1165. /**
  1166. * This function will receive a mail from mailbox object, if there is no mail in
  1167. * mailbox object, the thread shall wait for a specified time.
  1168. *
  1169. * @param mb the mailbox object
  1170. * @param value the received mail will be saved in
  1171. * @param timeout the waiting time
  1172. *
  1173. * @return the error code
  1174. */
  1175. rt_err_t rt_mb_recv(rt_mailbox_t mb, rt_uint32_t *value, rt_int32_t timeout)
  1176. {
  1177. struct rt_thread *thread;
  1178. register rt_ubase_t temp;
  1179. rt_uint32_t tick_delta;
  1180. /* parameter check */
  1181. RT_ASSERT(mb != RT_NULL);
  1182. /* initialize delta tick */
  1183. tick_delta = 0;
  1184. /* get current thread */
  1185. thread = rt_thread_self();
  1186. RT_OBJECT_HOOK_CALL(rt_object_trytake_hook, (&(mb->parent.parent)));
  1187. /* disable interrupt */
  1188. temp = rt_hw_interrupt_disable();
  1189. /* for non-blocking call */
  1190. if (mb->entry == 0 && timeout == 0)
  1191. {
  1192. rt_hw_interrupt_enable(temp);
  1193. return -RT_ETIMEOUT;
  1194. }
  1195. /* mailbox is empty */
  1196. while (mb->entry == 0)
  1197. {
  1198. /* reset error number in thread */
  1199. thread->error = RT_EOK;
  1200. /* no waiting, return timeout */
  1201. if (timeout == 0)
  1202. {
  1203. /* enable interrupt */
  1204. rt_hw_interrupt_enable(temp);
  1205. thread->error = -RT_ETIMEOUT;
  1206. return -RT_ETIMEOUT;
  1207. }
  1208. RT_DEBUG_NOT_IN_INTERRUPT;
  1209. /* suspend current thread */
  1210. rt_ipc_list_suspend(&(mb->parent.suspend_thread), thread, mb->parent.parent.flag);
  1211. /* has waiting time, start thread timer */
  1212. if (timeout > 0)
  1213. {
  1214. /* get the start tick of timer */
  1215. tick_delta = rt_tick_get();
  1216. RT_DEBUG_LOG(RT_DEBUG_IPC,
  1217. ("mb_recv: start timer of thread:%s\n", thread->name));
  1218. /* reset the timeout of thread timer and start it */
  1219. rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &timeout);
  1220. rt_timer_start(&(thread->thread_timer));
  1221. }
  1222. /* enable interrupt */
  1223. rt_hw_interrupt_enable(temp);
  1224. /* re-schedule */
  1225. rt_schedule();
  1226. /* resume from suspend state */
  1227. if (thread->error != RT_EOK)
  1228. {
  1229. /* return error */
  1230. return thread->error;
  1231. }
  1232. /* disable interrupt */
  1233. temp = rt_hw_interrupt_disable();
  1234. /* if it's not waiting forever and then re-calculate timeout tick */
  1235. if (timeout > 0)
  1236. {
  1237. tick_delta = rt_tick_get() - tick_delta;
  1238. timeout -= tick_delta;
  1239. if (timeout < 0) timeout = 0;
  1240. }
  1241. }
  1242. /* fill ptr */
  1243. *value = mb->msg_pool[mb->out_offset];
  1244. /* increase output offset */
  1245. ++ mb->out_offset;
  1246. if (mb->out_offset >= mb->size) mb->out_offset = 0;
  1247. /* decrease message entry */
  1248. mb->entry --;
  1249. /* resume suspended thread */
  1250. if (!rt_list_isempty(&(mb->suspend_sender_thread)))
  1251. {
  1252. rt_ipc_list_resume(&(mb->suspend_sender_thread));
  1253. /* enable interrupt */
  1254. rt_hw_interrupt_enable(temp);
  1255. RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(mb->parent.parent)));
  1256. rt_schedule();
  1257. return RT_EOK;
  1258. }
  1259. /* enable interrupt */
  1260. rt_hw_interrupt_enable(temp);
  1261. RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(mb->parent.parent)));
  1262. return RT_EOK;
  1263. }
  1264. /**
  1265. * This function can get or set some extra attributions of a mailbox object.
  1266. *
  1267. * @param mb the mailbox object
  1268. * @param cmd the execution command
  1269. * @param arg the execution argument
  1270. *
  1271. * @return the error code
  1272. */
  1273. rt_err_t rt_mb_control(rt_mailbox_t mb, rt_uint8_t cmd, void *arg)
  1274. {
  1275. rt_ubase_t level;
  1276. RT_ASSERT(mb != RT_NULL);
  1277. if (cmd == RT_IPC_CMD_RESET)
  1278. {
  1279. /* disable interrupt */
  1280. level = rt_hw_interrupt_disable();
  1281. /* resume all waiting thread */
  1282. rt_ipc_list_resume_all(&(mb->parent.suspend_thread));
  1283. /* also resume all mailbox private suspended thread */
  1284. rt_ipc_list_resume_all(&(mb->suspend_sender_thread));
  1285. /* re-init mailbox */
  1286. mb->entry = 0;
  1287. mb->in_offset = 0;
  1288. mb->out_offset = 0;
  1289. /* enable interrupt */
  1290. rt_hw_interrupt_enable(level);
  1291. return RT_EOK;
  1292. }
  1293. return -RT_ERROR;
  1294. }
  1295. #endif /* end of RT_USING_MAILBOX */
  1296. #ifdef RT_USING_MESSAGEQUEUE
  1297. struct rt_mq_message
  1298. {
  1299. struct rt_mq_message *next;
  1300. };
  1301. /**
  1302. * This function will initialize a message queue and put it under control of resource
  1303. * management.
  1304. *
  1305. * @param mq the message object
  1306. * @param name the name of message queue
  1307. * @param msgpool the beginning address of buffer to save messages
  1308. * @param msg_size the maximum size of message
  1309. * @param pool_size the size of buffer to save messages
  1310. * @param flag the flag of message queue
  1311. *
  1312. * @return the operation status, RT_EOK on successful
  1313. */
  1314. rt_err_t rt_mq_init(rt_mq_t mq, const char *name, void *msgpool, rt_size_t msg_size, rt_size_t pool_size, rt_uint8_t flag)
  1315. {
  1316. struct rt_mq_message *head;
  1317. register rt_base_t temp;
  1318. /* parameter check */
  1319. RT_ASSERT(mq != RT_NULL);
  1320. /* init object */
  1321. rt_object_init(&(mq->parent.parent), RT_Object_Class_MessageQueue, name);
  1322. /* set parent flag */
  1323. mq->parent.parent.flag = flag;
  1324. /* init ipc object */
  1325. rt_ipc_object_init(&(mq->parent));
  1326. /* set messasge pool */
  1327. mq->msg_pool = msgpool;
  1328. /* get correct message size */
  1329. mq->msg_size = RT_ALIGN(msg_size, RT_ALIGN_SIZE);
  1330. mq->max_msgs = pool_size / (mq->msg_size + sizeof(struct rt_mq_message));
  1331. /* init message list */
  1332. mq->msg_queue_head = RT_NULL;
  1333. mq->msg_queue_tail = RT_NULL;
  1334. /* init message empty list */
  1335. mq->msg_queue_free = RT_NULL;
  1336. for (temp = 0; temp < mq->max_msgs; temp ++)
  1337. {
  1338. head = (struct rt_mq_message *)((rt_uint8_t *)mq->msg_pool +
  1339. temp * (mq->msg_size + sizeof(struct rt_mq_message)));
  1340. head->next = mq->msg_queue_free;
  1341. mq->msg_queue_free = head;
  1342. }
  1343. /* the initial entry is zero */
  1344. mq->entry = 0;
  1345. return RT_EOK;
  1346. }
  1347. /**
  1348. * This function will detach a message queue object from resource management
  1349. *
  1350. * @param mq the message queue object
  1351. *
  1352. * @return the operation status, RT_EOK on successful
  1353. */
  1354. rt_err_t rt_mq_detach(rt_mq_t mq)
  1355. {
  1356. /* parameter check */
  1357. RT_ASSERT(mq != RT_NULL);
  1358. /* resume all suspended thread */
  1359. rt_ipc_list_resume_all(&mq->parent.suspend_thread);
  1360. /* detach message queue object */
  1361. rt_object_detach(&(mq->parent.parent));
  1362. return RT_EOK;
  1363. }
  1364. #ifdef RT_USING_HEAP
  1365. /**
  1366. * This function will create a message queue object from system resource
  1367. *
  1368. * @param name the name of message queue
  1369. * @param msg_size the size of message
  1370. * @param max_msgs the maximum number of message in queue
  1371. * @param flag the flag of message queue
  1372. *
  1373. * @return the created message queue, RT_NULL on error happen
  1374. */
  1375. rt_mq_t rt_mq_create(const char *name, rt_size_t msg_size, rt_size_t max_msgs, rt_uint8_t flag)
  1376. {
  1377. struct rt_messagequeue *mq;
  1378. struct rt_mq_message *head;
  1379. register rt_base_t temp;
  1380. RT_DEBUG_NOT_IN_INTERRUPT;
  1381. /* allocate object */
  1382. mq = (rt_mq_t)rt_object_allocate(RT_Object_Class_MessageQueue, name);
  1383. if (mq == RT_NULL) return mq;
  1384. /* set parent */
  1385. mq->parent.parent.flag = flag;
  1386. /* init ipc object */
  1387. rt_ipc_object_init(&(mq->parent));
  1388. /* init message queue */
  1389. /* get correct message size */
  1390. mq->msg_size = RT_ALIGN(msg_size, RT_ALIGN_SIZE);
  1391. mq->max_msgs = max_msgs;
  1392. /* allocate message pool */
  1393. mq->msg_pool = rt_malloc((mq->msg_size + sizeof(struct rt_mq_message))* mq->max_msgs);
  1394. if (mq->msg_pool == RT_NULL)
  1395. {
  1396. rt_mq_delete(mq);
  1397. return RT_NULL;
  1398. }
  1399. /* init message list */
  1400. mq->msg_queue_head = RT_NULL;
  1401. mq->msg_queue_tail = RT_NULL;
  1402. /* init message empty list */
  1403. mq->msg_queue_free = RT_NULL;
  1404. for (temp = 0; temp < mq->max_msgs; temp ++)
  1405. {
  1406. head = (struct rt_mq_message*)((rt_uint8_t*)mq->msg_pool +
  1407. temp * (mq->msg_size + sizeof(struct rt_mq_message)));
  1408. head->next = mq->msg_queue_free;
  1409. mq->msg_queue_free = head;
  1410. }
  1411. /* the initial entry is zero */
  1412. mq->entry = 0;
  1413. return mq;
  1414. }
  1415. /**
  1416. * This function will delete a message queue object and release the memory
  1417. *
  1418. * @param mq the message queue object
  1419. *
  1420. * @return the error code
  1421. */
  1422. rt_err_t rt_mq_delete(rt_mq_t mq)
  1423. {
  1424. RT_DEBUG_NOT_IN_INTERRUPT;
  1425. /* parameter check */
  1426. RT_ASSERT(mq != RT_NULL);
  1427. /* resume all suspended thread */
  1428. rt_ipc_list_resume_all(&(mq->parent.suspend_thread));
  1429. #if defined(RT_USING_MODULE) && defined(RT_USING_SLAB)
  1430. /* the mq object belongs to an application module */
  1431. if (mq->parent.parent.flag & RT_OBJECT_FLAG_MODULE)
  1432. rt_module_free(mq->parent.parent.module_id, mq->msg_pool);
  1433. else
  1434. #endif
  1435. /* free message queue pool */
  1436. rt_free(mq->msg_pool);
  1437. /* delete message queue object */
  1438. rt_object_delete(&(mq->parent.parent));
  1439. return RT_EOK;
  1440. }
  1441. #endif
  1442. /**
  1443. * This function will send a message to message queue object, if there are threads
  1444. * suspended on message queue object, it will be waked up.
  1445. *
  1446. * @param mq the message queue object
  1447. * @param buffer the message
  1448. * @param size the size of buffer
  1449. *
  1450. * @return the error code
  1451. */
  1452. rt_err_t rt_mq_send(rt_mq_t mq, void *buffer, rt_size_t size)
  1453. {
  1454. register rt_ubase_t temp;
  1455. struct rt_mq_message *msg;
  1456. /* greater than one message size */
  1457. if (size > mq->msg_size) return -RT_ERROR;
  1458. RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(mq->parent.parent)));
  1459. /* disable interrupt */
  1460. temp = rt_hw_interrupt_disable();
  1461. /* get a free list, there must be an empty item */
  1462. msg = (struct rt_mq_message*)mq->msg_queue_free;
  1463. /* message queue is full */
  1464. if (msg == RT_NULL)
  1465. {
  1466. /* enable interrupt */
  1467. rt_hw_interrupt_enable(temp);
  1468. return -RT_EFULL;
  1469. }
  1470. /* move free list pointer */
  1471. mq->msg_queue_free = msg->next;
  1472. /* enable interrupt */
  1473. rt_hw_interrupt_enable(temp);
  1474. /* the msg is the new tailer of list, the next shall be NULL */
  1475. msg->next = RT_NULL;
  1476. /* copy buffer */
  1477. rt_memcpy(msg + 1, buffer, size);
  1478. /* disable interrupt */
  1479. temp = rt_hw_interrupt_disable();
  1480. /* link msg to message queue */
  1481. if (mq->msg_queue_tail != RT_NULL)
  1482. {
  1483. /* if the tail exists, */
  1484. ((struct rt_mq_message *)mq->msg_queue_tail)->next = msg;
  1485. }
  1486. /* set new tail */
  1487. mq->msg_queue_tail = msg;
  1488. /* if the head is empty, set head */
  1489. if (mq->msg_queue_head == RT_NULL)mq->msg_queue_head = msg;
  1490. /* increase message entry */
  1491. mq->entry ++;
  1492. /* resume suspended thread */
  1493. if (!rt_list_isempty(&mq->parent.suspend_thread))
  1494. {
  1495. rt_ipc_list_resume(&(mq->parent.suspend_thread));
  1496. /* enable interrupt */
  1497. rt_hw_interrupt_enable(temp);
  1498. rt_schedule();
  1499. return RT_EOK;
  1500. }
  1501. /* enable interrupt */
  1502. rt_hw_interrupt_enable(temp);
  1503. return RT_EOK;
  1504. }
  1505. /**
  1506. * This function will send urgently a message to message queue object, which means
  1507. * the message will be inserted to the head of message queue. If there are threads
  1508. * suspended on message queue object, it will be waked up.
  1509. *
  1510. * @param mq the message queue object
  1511. * @param buffer the message
  1512. * @param size the size of buffer
  1513. *
  1514. * @return the error code
  1515. */
  1516. rt_err_t rt_mq_urgent(rt_mq_t mq, void *buffer, rt_size_t size)
  1517. {
  1518. register rt_ubase_t temp;
  1519. struct rt_mq_message *msg;
  1520. /* greater than one message size */
  1521. if (size > mq->msg_size) return -RT_ERROR;
  1522. RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(mq->parent.parent)));
  1523. /* disable interrupt */
  1524. temp = rt_hw_interrupt_disable();
  1525. /* get a free list, there must be an empty item */
  1526. msg = (struct rt_mq_message *)mq->msg_queue_free;
  1527. /* message queue is full */
  1528. if (msg == RT_NULL)
  1529. {
  1530. /* enable interrupt */
  1531. rt_hw_interrupt_enable(temp);
  1532. return -RT_EFULL;
  1533. }
  1534. /* move free list pointer */
  1535. mq->msg_queue_free = msg->next;
  1536. /* enable interrupt */
  1537. rt_hw_interrupt_enable(temp);
  1538. /* copy buffer */
  1539. rt_memcpy(msg + 1, buffer, size);
  1540. /* disable interrupt */
  1541. temp = rt_hw_interrupt_disable();
  1542. /* link msg to the beginning of message queue */
  1543. msg->next = mq->msg_queue_head;
  1544. mq->msg_queue_head = msg;
  1545. /* if there is no tail */
  1546. if (mq->msg_queue_tail == RT_NULL) mq->msg_queue_tail = msg;
  1547. /* increase message entry */
  1548. mq->entry ++;
  1549. /* resume suspended thread */
  1550. if (!rt_list_isempty(&mq->parent.suspend_thread))
  1551. {
  1552. rt_ipc_list_resume(&(mq->parent.suspend_thread));
  1553. /* enable interrupt */
  1554. rt_hw_interrupt_enable(temp);
  1555. rt_schedule();
  1556. return RT_EOK;
  1557. }
  1558. /* enable interrupt */
  1559. rt_hw_interrupt_enable(temp);
  1560. return RT_EOK;
  1561. }
  1562. /**
  1563. * This function will receive a message from message queue object, if there is no
  1564. * message in message queue object, the thread shall wait for a specified time.
  1565. *
  1566. * @param mq the message queue object
  1567. * @param buffer the received message will be saved in
  1568. * @param size the size of buffer
  1569. * @param timeout the waiting time
  1570. *
  1571. * @return the error code
  1572. */
  1573. rt_err_t rt_mq_recv(rt_mq_t mq, void *buffer, rt_size_t size, rt_int32_t timeout)
  1574. {
  1575. struct rt_thread *thread;
  1576. register rt_ubase_t temp;
  1577. struct rt_mq_message *msg;
  1578. rt_uint32_t tick_delta;
  1579. /* initialize delta tick */
  1580. tick_delta = 0;
  1581. /* get current thread */
  1582. thread = rt_thread_self();
  1583. RT_OBJECT_HOOK_CALL(rt_object_trytake_hook, (&(mq->parent.parent)));
  1584. /* disable interrupt */
  1585. temp = rt_hw_interrupt_disable();
  1586. /* for non-blocking call */
  1587. if (mq->entry == 0 && timeout == 0)
  1588. {
  1589. rt_hw_interrupt_enable(temp);
  1590. return -RT_ETIMEOUT;
  1591. }
  1592. /* message queue is empty */
  1593. while (mq->entry == 0)
  1594. {
  1595. RT_DEBUG_NOT_IN_INTERRUPT;
  1596. /* reset error number in thread */
  1597. thread->error = RT_EOK;
  1598. /* no waiting, return timeout */
  1599. if (timeout == 0)
  1600. {
  1601. /* enable interrupt */
  1602. rt_hw_interrupt_enable(temp);
  1603. thread->error = -RT_ETIMEOUT;
  1604. return -RT_ETIMEOUT;
  1605. }
  1606. /* suspend current thread */
  1607. rt_ipc_list_suspend(&(mq->parent.suspend_thread), thread, mq->parent.parent.flag);
  1608. /* has waiting time, start thread timer */
  1609. if (timeout > 0)
  1610. {
  1611. /* get the start tick of timer */
  1612. tick_delta = rt_tick_get();
  1613. RT_DEBUG_LOG(RT_DEBUG_IPC, ("set thread:%s to timer list\n", thread->name));
  1614. /* reset the timeout of thread timer and start it */
  1615. rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &timeout);
  1616. rt_timer_start(&(thread->thread_timer));
  1617. }
  1618. /* enable interrupt */
  1619. rt_hw_interrupt_enable(temp);
  1620. /* re-schedule */
  1621. rt_schedule();
  1622. /* recv message */
  1623. if (thread->error != RT_EOK)
  1624. {
  1625. /* return error */
  1626. return thread->error;
  1627. }
  1628. /* disable interrupt */
  1629. temp = rt_hw_interrupt_disable();
  1630. /* if it's not waiting forever and then re-calculate timeout tick */
  1631. if (timeout > 0)
  1632. {
  1633. tick_delta = rt_tick_get() - tick_delta;
  1634. timeout -= tick_delta;
  1635. if (timeout < 0) timeout = 0;
  1636. }
  1637. }
  1638. /* get message from queue */
  1639. msg = (struct rt_mq_message *)mq->msg_queue_head;
  1640. /* move message queue head */
  1641. mq->msg_queue_head = msg->next;
  1642. /* reach queue tail, set to NULL */
  1643. if (mq->msg_queue_tail == msg) mq->msg_queue_tail = RT_NULL;
  1644. /* decrease message entry */
  1645. mq->entry --;
  1646. /* enable interrupt */
  1647. rt_hw_interrupt_enable(temp);
  1648. /* copy message */
  1649. rt_memcpy(buffer, msg + 1, size > mq->msg_size ? mq->msg_size : size);
  1650. /* disable interrupt */
  1651. temp = rt_hw_interrupt_disable();
  1652. /* put message to free list */
  1653. msg->next = (struct rt_mq_message *)mq->msg_queue_free;
  1654. mq->msg_queue_free = msg;
  1655. /* enable interrupt */
  1656. rt_hw_interrupt_enable(temp);
  1657. RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(mq->parent.parent)));
  1658. return RT_EOK;
  1659. }
  1660. /**
  1661. * This function can get or set some extra attributions of a message queue object.
  1662. *
  1663. * @param mq the message queue object
  1664. * @param cmd the execution command
  1665. * @param arg the execution argument
  1666. *
  1667. * @return the error code
  1668. */
  1669. rt_err_t rt_mq_control(rt_mq_t mq, rt_uint8_t cmd, void *arg)
  1670. {
  1671. rt_ubase_t level;
  1672. struct rt_mq_message *msg;
  1673. RT_ASSERT(mq != RT_NULL);
  1674. if (cmd == RT_IPC_CMD_RESET)
  1675. {
  1676. /* disable interrupt */
  1677. level = rt_hw_interrupt_disable();
  1678. /* resume all waiting thread */
  1679. rt_ipc_list_resume_all(&mq->parent.suspend_thread);
  1680. /* release all message in the queue */
  1681. while (mq->msg_queue_head != RT_NULL)
  1682. {
  1683. /* get message from queue */
  1684. msg = (struct rt_mq_message *)mq->msg_queue_head;
  1685. /* move message queue head */
  1686. mq->msg_queue_head = msg->next;
  1687. /* reach queue tail, set to NULL */
  1688. if (mq->msg_queue_tail == msg) mq->msg_queue_tail = RT_NULL;
  1689. /* put message to free list */
  1690. msg->next = (struct rt_mq_message *)mq->msg_queue_free;
  1691. mq->msg_queue_free = msg;
  1692. }
  1693. /* clean entry */
  1694. mq->entry = 0;
  1695. /* enable interrupt */
  1696. rt_hw_interrupt_enable(level);
  1697. return RT_EOK;
  1698. }
  1699. return -RT_ERROR;
  1700. }
  1701. #endif /* end of RT_USING_MESSAGEQUEUE */
  1702. /*@}*/