ipc.c 48 KB

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