ipc.c 48 KB

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