ipc.c 47 KB

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