ipc.c 44 KB

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