ipc.c 42 KB

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