ipc.c 44 KB

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