ipc.c 44 KB

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