ipc.c 42 KB

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