lwp_ipc.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  1. /*
  2. * Copyright (c) 2006-2020, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2019-10-12 Jesven first version
  9. */
  10. #include <rtthread.h>
  11. #include <rthw.h>
  12. #include <lwp.h>
  13. #include "lwp_ipc.h"
  14. #include "lwp_ipc_internal.h"
  15. #include <dfs_file.h>
  16. #include <dfs_poll.h>
  17. /**
  18. * the IPC channel states
  19. */
  20. enum
  21. {
  22. RT_IPC_STAT_IDLE, /* no suspended threads */
  23. RT_IPC_STAT_WAIT, /* suspended receivers exist */
  24. RT_IPC_STAT_ACTIVE, /* suspended senders exist */
  25. };
  26. /**
  27. * IPC message structure.
  28. *
  29. * They are allocated and released in the similar way like 'rt_chfd'.
  30. */
  31. struct rt_ipc_msg
  32. {
  33. struct rt_channel_msg msg; /**< the payload of msg */
  34. rt_list_t mlist; /**< the msg list */
  35. rt_uint8_t need_reply; /**< whether msg wait reply*/
  36. };
  37. typedef struct rt_ipc_msg *rt_ipc_msg_t;
  38. static rt_ipc_msg_t _ipc_msg_free_list = (rt_ipc_msg_t)RT_NULL; /* released chain */
  39. static int rt_ipc_msg_used = 0; /* first unallocated entry */
  40. static struct rt_ipc_msg ipc_msg_pool[RT_CH_MSG_MAX_NR]; /* initial message array */
  41. /**
  42. * Allocate an IPC message from the statically-allocated array.
  43. */
  44. static rt_ipc_msg_t _ipc_msg_alloc(void)
  45. {
  46. rt_ipc_msg_t p = (rt_ipc_msg_t)RT_NULL;
  47. if (_ipc_msg_free_list) /* use the released chain first */
  48. {
  49. p = _ipc_msg_free_list;
  50. _ipc_msg_free_list = (rt_ipc_msg_t)p->msg.sender; /* emtry payload as a pointer */
  51. }
  52. else if (rt_ipc_msg_used < RT_CH_MSG_MAX_NR)
  53. {
  54. p = &ipc_msg_pool[rt_ipc_msg_used];
  55. rt_ipc_msg_used++;
  56. }
  57. return p;
  58. }
  59. /**
  60. * Put a released IPC message back to the released chain.
  61. */
  62. static void _ipc_msg_free(rt_ipc_msg_t p_msg)
  63. {
  64. p_msg->msg.sender = (void*)_ipc_msg_free_list;
  65. _ipc_msg_free_list = p_msg;
  66. }
  67. /**
  68. * Initialized the IPC message.
  69. */
  70. static void rt_ipc_msg_init(rt_ipc_msg_t msg, struct rt_channel_msg *data, rt_uint8_t need_reply)
  71. {
  72. RT_ASSERT(msg != RT_NULL);
  73. msg->need_reply = need_reply;
  74. msg->msg = *data;
  75. msg->msg.sender = (void*)rt_thread_self();
  76. rt_list_init(&msg->mlist);
  77. }
  78. /**
  79. * Initialized the list of the waiting receivers on the IPC channel.
  80. */
  81. rt_inline rt_err_t rt_channel_object_init(struct rt_ipc_object *ipc)
  82. {
  83. rt_list_init(&(ipc->suspend_thread)); /* receiver list */
  84. return RT_EOK;
  85. }
  86. /**
  87. * Wakeup the first suspened thread in the list.
  88. */
  89. rt_inline rt_err_t rt_channel_list_resume(rt_list_t *list)
  90. {
  91. struct rt_thread *thread;
  92. /* get the first thread entry waiting for sending */
  93. thread = rt_list_entry(list->next, struct rt_thread, tlist);
  94. rt_thread_resume(thread);
  95. return RT_EOK;
  96. }
  97. /**
  98. * Wakeup all the suspended threads in the list.
  99. */
  100. rt_inline rt_err_t rt_channel_list_resume_all(rt_list_t *list)
  101. {
  102. struct rt_thread *thread;
  103. register rt_ubase_t temp;
  104. /* wakeup all suspended threads for sending */
  105. while (!rt_list_isempty(list))
  106. {
  107. temp = rt_hw_interrupt_disable();
  108. thread = rt_list_entry(list->next, struct rt_thread, tlist);
  109. thread->error = -RT_ERROR;
  110. rt_thread_resume(thread);
  111. rt_hw_interrupt_enable(temp);
  112. }
  113. return RT_EOK;
  114. }
  115. /**
  116. * Suspend the thread and chain it into the end of the list.
  117. */
  118. rt_inline rt_err_t rt_channel_list_suspend(rt_list_t *list, struct rt_thread *thread)
  119. {
  120. /* suspend thread */
  121. rt_err_t ret = rt_thread_suspend_with_flag(thread, RT_INTERRUPTIBLE);
  122. if (ret == RT_EOK)
  123. {
  124. rt_list_insert_before(list, &(thread->tlist)); /* list end */
  125. }
  126. return ret;
  127. }
  128. static void _rt_channel_check_wq_wakup(rt_channel_t ch)
  129. {
  130. rt_base_t level;
  131. level = rt_hw_interrupt_disable();
  132. if (rt_list_isempty(&ch->wait_msg))
  133. {
  134. rt_hw_interrupt_enable(level);
  135. return;
  136. }
  137. rt_wqueue_wakeup(&ch->reader_queue, 0);
  138. rt_hw_interrupt_enable(level);
  139. }
  140. /**
  141. * Create a new or open an existing IPC channel.
  142. */
  143. rt_channel_t rt_raw_channel_open(const char *name, int flags)
  144. {
  145. register rt_ubase_t temp = 0;
  146. rt_channel_t ch = RT_NULL;
  147. struct rt_object *object;
  148. struct rt_list_node *node;
  149. struct rt_object_information *information;
  150. temp = rt_hw_interrupt_disable();
  151. information = rt_object_get_information(RT_Object_Class_Channel);
  152. RT_ASSERT(information != RT_NULL);
  153. /* retrieve the existing IPC channels */
  154. for (node = information->object_list.next;
  155. node != &(information->object_list);
  156. node = node->next)
  157. {
  158. object = rt_list_entry(node, struct rt_object, list);
  159. if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)
  160. {
  161. if ((flags & O_CREAT) && (flags & O_EXCL))
  162. {
  163. goto quit;
  164. }
  165. /* find the IPC channel with the specific name */
  166. ch = (rt_channel_t)object;
  167. ch->ref++; /* increase the reference count */
  168. break;
  169. }
  170. }
  171. if (!ch) /* create a new IPC channel */
  172. {
  173. if (flags & O_CREAT)
  174. {
  175. RT_DEBUG_NOT_IN_INTERRUPT;
  176. /* allocate a real IPC channel structure */
  177. ch = (rt_channel_t)rt_object_allocate(RT_Object_Class_Channel, name);
  178. }
  179. if (!ch)
  180. {
  181. goto quit;
  182. }
  183. rt_channel_object_init(&ch->parent); /* suspended receivers */
  184. rt_list_init(&ch->wait_msg); /* unhandled messages */
  185. rt_list_init(&ch->wait_thread); /* suspended senders */
  186. rt_wqueue_init(&ch->reader_queue); /* reader poll queue */
  187. ch->reply = RT_NULL;
  188. ch->stat = RT_IPC_STAT_IDLE; /* no suspended threads */
  189. ch->ref = 1;
  190. }
  191. quit:
  192. rt_hw_interrupt_enable(temp);
  193. return ch;
  194. }
  195. /**
  196. * Close an existiong IPC channel, release the resources.
  197. */
  198. rt_err_t rt_raw_channel_close(rt_channel_t ch)
  199. {
  200. register rt_ubase_t temp;
  201. RT_DEBUG_NOT_IN_INTERRUPT;
  202. if (ch == RT_NULL)
  203. {
  204. return -RT_EIO;
  205. }
  206. temp = rt_hw_interrupt_disable();
  207. if (rt_object_get_type(&ch->parent.parent) != RT_Object_Class_Channel)
  208. {
  209. rt_hw_interrupt_enable(temp);
  210. return -RT_EIO;
  211. }
  212. if (rt_object_is_systemobject(&ch->parent.parent) != RT_FALSE)
  213. {
  214. rt_hw_interrupt_enable(temp);
  215. return -RT_EIO;
  216. }
  217. if (ch->ref == 0)
  218. {
  219. rt_hw_interrupt_enable(temp);
  220. return -RT_EIO;
  221. }
  222. ch->ref--;
  223. if (ch->ref == 0)
  224. {
  225. /* wakeup all the suspended receivers and senders */
  226. rt_channel_list_resume_all(&ch->parent.suspend_thread);
  227. rt_channel_list_resume_all(&ch->wait_thread);
  228. /* all ipc msg will lost */
  229. rt_list_init(&ch->wait_msg);
  230. rt_object_delete(&ch->parent.parent); /* release the IPC channel structure */
  231. }
  232. rt_hw_interrupt_enable(temp);
  233. return RT_EOK;
  234. }
  235. static rt_err_t wakeup_sender_wait_recv(void *object, struct rt_thread *thread)
  236. {
  237. rt_channel_t ch;
  238. ch = (rt_channel_t)object;
  239. if (ch->stat == RT_IPC_STAT_ACTIVE && ch->reply == thread)
  240. {
  241. ch->stat = RT_IPC_STAT_IDLE;
  242. ch->reply = RT_NULL;
  243. }
  244. else
  245. {
  246. rt_ipc_msg_t msg;
  247. rt_list_t *l;
  248. l = ch->wait_msg.next;
  249. while (l != &ch->wait_msg)
  250. {
  251. msg = rt_list_entry(l, struct rt_ipc_msg, mlist);
  252. if (msg->need_reply && msg->msg.sender == thread)
  253. {
  254. rt_list_remove(&msg->mlist); /* remove the msg from the channel */
  255. _ipc_msg_free(msg);
  256. break;
  257. }
  258. l = l->next;
  259. }
  260. }
  261. thread->error = -RT_EINTR;
  262. return rt_thread_resume(thread); /* wake up the sender */
  263. }
  264. static rt_err_t wakeup_sender_wait_reply(void *object, struct rt_thread *thread)
  265. {
  266. rt_channel_t ch;
  267. ch = (rt_channel_t)object;
  268. RT_ASSERT(ch->stat == RT_IPC_STAT_ACTIVE && ch->reply == thread);
  269. ch->stat = RT_IPC_STAT_IDLE;
  270. ch->reply = RT_NULL;
  271. thread->error = -RT_EINTR;
  272. return rt_thread_resume(thread); /* wake up the sender */
  273. }
  274. static void sender_timeout(void *parameter)
  275. {
  276. struct rt_thread *thread = (struct rt_thread*)parameter;
  277. rt_channel_t ch;
  278. ch = (rt_channel_t)(thread->wakeup.user_data);
  279. if (ch->stat == RT_IPC_STAT_ACTIVE && ch->reply == thread)
  280. {
  281. ch->stat = RT_IPC_STAT_IDLE;
  282. ch->reply = RT_NULL;
  283. }
  284. else
  285. {
  286. rt_ipc_msg_t msg;
  287. rt_list_t *l;
  288. l = ch->wait_msg.next;
  289. while (l != &ch->wait_msg)
  290. {
  291. msg = rt_list_entry(l, struct rt_ipc_msg, mlist);
  292. if (msg->need_reply && msg->msg.sender == thread)
  293. {
  294. rt_list_remove(&msg->mlist); /* remove the msg from the channel */
  295. _ipc_msg_free(msg);
  296. break;
  297. }
  298. l = l->next;
  299. }
  300. }
  301. thread->error = -RT_ETIMEOUT;
  302. thread->wakeup.func = RT_NULL;
  303. rt_list_remove(&(thread->tlist));
  304. /* insert to schedule ready list */
  305. rt_schedule_insert_thread(thread);
  306. /* do schedule */
  307. rt_schedule();
  308. }
  309. /**
  310. * Send data through an IPC channel, wait for the reply or not.
  311. */
  312. static rt_err_t _rt_raw_channel_send_recv_timeout(rt_channel_t ch, rt_channel_msg_t data, int need_reply, rt_channel_msg_t data_ret, rt_int32_t time)
  313. {
  314. rt_ipc_msg_t msg;
  315. struct rt_thread *thread_recv, *thread_send = 0;
  316. register rt_base_t temp;
  317. rt_err_t ret;
  318. void (*old_timeout_func)(void *) = 0;
  319. if (need_reply)
  320. {
  321. RT_DEBUG_NOT_IN_INTERRUPT;
  322. }
  323. if (ch == RT_NULL)
  324. {
  325. return -RT_EIO;
  326. }
  327. temp = rt_hw_interrupt_disable();
  328. if (rt_object_get_type(&ch->parent.parent) != RT_Object_Class_Channel)
  329. {
  330. rt_hw_interrupt_enable(temp);
  331. return -RT_EIO;
  332. }
  333. if (need_reply && time == 0)
  334. {
  335. rt_hw_interrupt_enable(temp);
  336. return -RT_ETIMEOUT;
  337. }
  338. /* allocate an IPC message */
  339. msg = _ipc_msg_alloc();
  340. if (!msg)
  341. {
  342. rt_hw_interrupt_enable(temp);
  343. return -RT_ENOMEM;
  344. }
  345. rt_ipc_msg_init(msg, data, need_reply);
  346. if (need_reply)
  347. {
  348. thread_send = rt_thread_self();
  349. thread_send->error = RT_EOK;
  350. }
  351. switch (ch->stat)
  352. {
  353. case RT_IPC_STAT_IDLE:
  354. case RT_IPC_STAT_ACTIVE:
  355. if (need_reply)
  356. {
  357. ret = rt_channel_list_suspend(&ch->wait_thread, thread_send);
  358. if (ret != RT_EOK)
  359. {
  360. _ipc_msg_free(msg);
  361. rt_hw_interrupt_enable(temp);
  362. return ret;
  363. }
  364. rt_thread_wakeup_set(thread_send, wakeup_sender_wait_recv, (void*)ch);
  365. if (time > 0)
  366. {
  367. rt_timer_control(&(thread_send->thread_timer),
  368. RT_TIMER_CTRL_GET_FUNC,
  369. &old_timeout_func);
  370. rt_timer_control(&(thread_send->thread_timer),
  371. RT_TIMER_CTRL_SET_FUNC,
  372. sender_timeout);
  373. /* reset the timeout of thread timer and start it */
  374. rt_timer_control(&(thread_send->thread_timer),
  375. RT_TIMER_CTRL_SET_TIME,
  376. &time);
  377. rt_timer_start(&(thread_send->thread_timer));
  378. }
  379. }
  380. /*
  381. * If there is no thread waiting for messages, chain the message
  382. * into the list.
  383. */
  384. rt_list_insert_before(&ch->wait_msg, &msg->mlist);
  385. break;
  386. case RT_IPC_STAT_WAIT:
  387. /*
  388. * If there are suspended receivers on the IPC channel, transfer the
  389. * pointer of the message to the first receiver directly and wake it
  390. * up.
  391. */
  392. RT_ASSERT(ch->parent.suspend_thread.next != &ch->parent.suspend_thread);
  393. if (need_reply)
  394. {
  395. ret = rt_channel_list_suspend(&ch->wait_thread, thread_send);
  396. if (ret != RT_EOK)
  397. {
  398. _ipc_msg_free(msg);
  399. rt_hw_interrupt_enable(temp);
  400. return ret;
  401. }
  402. ch->reply = thread_send; /* record the current waiting sender */
  403. ch->stat = RT_IPC_STAT_ACTIVE;
  404. rt_thread_wakeup_set(thread_send, wakeup_sender_wait_reply, (void*)ch);
  405. if (time > 0)
  406. {
  407. rt_timer_control(&(thread_send->thread_timer),
  408. RT_TIMER_CTRL_GET_FUNC,
  409. &old_timeout_func);
  410. rt_timer_control(&(thread_send->thread_timer),
  411. RT_TIMER_CTRL_SET_FUNC,
  412. sender_timeout);
  413. /* reset the timeout of thread timer and start it */
  414. rt_timer_control(&(thread_send->thread_timer),
  415. RT_TIMER_CTRL_SET_TIME,
  416. &time);
  417. rt_timer_start(&(thread_send->thread_timer));
  418. }
  419. }
  420. else
  421. {
  422. ch->stat = RT_IPC_STAT_IDLE;
  423. }
  424. thread_recv = rt_list_entry(ch->parent.suspend_thread.next, struct rt_thread, tlist);
  425. thread_recv->msg_ret = msg; /* to the first suspended receiver */
  426. thread_recv->error = RT_EOK;
  427. rt_channel_list_resume(&ch->parent.suspend_thread);
  428. break;
  429. default:
  430. break;
  431. }
  432. if ( ch->stat == RT_IPC_STAT_IDLE)
  433. {
  434. _rt_channel_check_wq_wakup(ch);
  435. }
  436. rt_hw_interrupt_enable(temp);
  437. /* reschedule in order to let the potential receivers run */
  438. rt_schedule();
  439. if (need_reply)
  440. {
  441. temp = rt_hw_interrupt_disable();
  442. if (old_timeout_func)
  443. {
  444. rt_timer_control(&(thread_send->thread_timer),
  445. RT_TIMER_CTRL_SET_FUNC,
  446. old_timeout_func);
  447. }
  448. ret = thread_send->error;
  449. rt_hw_interrupt_enable(temp);
  450. if (ret != RT_EOK)
  451. {
  452. return ret;
  453. }
  454. /* If the sender gets the chance to run, the requested reply must be valid. */
  455. RT_ASSERT(data_ret != RT_NULL);
  456. *data_ret = ((rt_ipc_msg_t)(thread_send->msg_ret))->msg; /* extract data */
  457. temp = rt_hw_interrupt_disable();
  458. _ipc_msg_free(thread_send->msg_ret); /* put back the message to kernel */
  459. rt_hw_interrupt_enable(temp);
  460. thread_send->msg_ret = RT_NULL;
  461. }
  462. return RT_EOK;
  463. }
  464. /**
  465. * Send data through an IPC channel with no reply.
  466. */
  467. rt_err_t rt_raw_channel_send(rt_channel_t ch, rt_channel_msg_t data)
  468. {
  469. return _rt_raw_channel_send_recv_timeout(ch, data, 0, 0, RT_WAITING_FOREVER);
  470. }
  471. /**
  472. * Send data through an IPC channel and wait for the relpy.
  473. */
  474. rt_err_t rt_raw_channel_send_recv(rt_channel_t ch, rt_channel_msg_t data, rt_channel_msg_t data_ret)
  475. {
  476. return _rt_raw_channel_send_recv_timeout(ch, data, 1, data_ret, RT_WAITING_FOREVER);
  477. }
  478. /**
  479. * Send data through an IPC channel and wait for the relpy.
  480. */
  481. rt_err_t rt_raw_channel_send_recv_timeout(rt_channel_t ch, rt_channel_msg_t data, rt_channel_msg_t data_ret, rt_int32_t time)
  482. {
  483. return _rt_raw_channel_send_recv_timeout(ch, data, 1, data_ret, time);
  484. }
  485. /**
  486. * Reply to the waiting sender and wake it up.
  487. */
  488. rt_err_t rt_raw_channel_reply(rt_channel_t ch, rt_channel_msg_t data)
  489. {
  490. rt_ipc_msg_t msg;
  491. struct rt_thread *thread;
  492. register rt_base_t temp;
  493. if (ch == RT_NULL)
  494. {
  495. return -RT_EIO;
  496. }
  497. temp = rt_hw_interrupt_disable();
  498. if (rt_object_get_type(&ch->parent.parent) != RT_Object_Class_Channel)
  499. {
  500. rt_hw_interrupt_enable(temp);
  501. return -RT_EIO;
  502. }
  503. if (ch->stat != RT_IPC_STAT_ACTIVE)
  504. {
  505. rt_hw_interrupt_enable(temp);
  506. return -RT_ERROR;
  507. }
  508. if (ch->reply == RT_NULL)
  509. {
  510. rt_hw_interrupt_enable(temp);
  511. return -RT_ERROR;
  512. }
  513. /* allocate an IPC message */
  514. msg = _ipc_msg_alloc();
  515. if (!msg)
  516. {
  517. rt_hw_interrupt_enable(temp);
  518. return -RT_ENOMEM;
  519. }
  520. rt_ipc_msg_init(msg, data, 0);
  521. thread = ch->reply;
  522. thread->msg_ret = msg; /* transfer the reply to the sender */
  523. rt_thread_resume(thread); /* wake up the sender */
  524. ch->stat = RT_IPC_STAT_IDLE;
  525. ch->reply = RT_NULL;
  526. _rt_channel_check_wq_wakup(ch);
  527. rt_hw_interrupt_enable(temp);
  528. rt_schedule();
  529. return RT_EOK;
  530. }
  531. static rt_err_t wakeup_receiver(void *object, struct rt_thread *thread)
  532. {
  533. rt_channel_t ch;
  534. rt_err_t ret;
  535. ch = (rt_channel_t)object;
  536. ch->stat = RT_IPC_STAT_IDLE;
  537. thread->error = -RT_EINTR;
  538. ret = rt_channel_list_resume(&ch->parent.suspend_thread);
  539. _rt_channel_check_wq_wakup(ch);
  540. return ret;
  541. }
  542. static void receiver_timeout(void *parameter)
  543. {
  544. struct rt_thread *thread = (struct rt_thread*)parameter;
  545. rt_channel_t ch;
  546. ch = (rt_channel_t)(thread->wakeup.user_data);
  547. ch->stat = RT_IPC_STAT_IDLE;
  548. thread->error = -RT_ETIMEOUT;
  549. thread->wakeup.func = RT_NULL;
  550. rt_list_remove(&(thread->tlist));
  551. /* insert to schedule ready list */
  552. rt_schedule_insert_thread(thread);
  553. _rt_channel_check_wq_wakup(ch);
  554. /* do schedule */
  555. rt_schedule();
  556. }
  557. /**
  558. * Fetch a message from the specified IPC channel.
  559. */
  560. static rt_err_t _rt_raw_channel_recv_timeout(rt_channel_t ch, rt_channel_msg_t data, rt_int32_t time)
  561. {
  562. struct rt_thread *thread;
  563. rt_ipc_msg_t msg_ret;
  564. register rt_base_t temp;
  565. rt_err_t ret;
  566. void (*old_timeout_func)(void *) = 0;
  567. RT_DEBUG_NOT_IN_INTERRUPT;
  568. if (ch == RT_NULL)
  569. {
  570. return -RT_EIO;
  571. }
  572. temp = rt_hw_interrupt_disable();
  573. if (rt_object_get_type(&ch->parent.parent) != RT_Object_Class_Channel)
  574. {
  575. rt_hw_interrupt_enable(temp);
  576. return -RT_EIO;
  577. }
  578. if (ch->stat != RT_IPC_STAT_IDLE)
  579. {
  580. rt_hw_interrupt_enable(temp);
  581. return -RT_ERROR;
  582. }
  583. if (ch->wait_msg.next != &ch->wait_msg) /* there exist unhandled messages */
  584. {
  585. msg_ret = rt_list_entry(ch->wait_msg.next, struct rt_ipc_msg, mlist);
  586. rt_list_remove(ch->wait_msg.next); /* remove the message from the channel */
  587. if (msg_ret->need_reply)
  588. {
  589. RT_ASSERT(ch->wait_thread.next != &ch->wait_thread);
  590. thread = rt_list_entry(ch->wait_thread.next, struct rt_thread, tlist);
  591. rt_list_remove(ch->wait_thread.next);
  592. ch->reply = thread; /* record the waiting sender */
  593. ch->stat = RT_IPC_STAT_ACTIVE; /* no valid suspened receivers */
  594. }
  595. *data = msg_ret->msg; /* extract the transferred data */
  596. _ipc_msg_free(msg_ret); /* put back the message to kernel */
  597. }
  598. else
  599. {
  600. if (time == 0)
  601. {
  602. rt_hw_interrupt_enable(temp);
  603. return -RT_ETIMEOUT;
  604. }
  605. /* no valid message, we must wait */
  606. thread = rt_thread_self();
  607. ret = rt_channel_list_suspend(&ch->parent.suspend_thread, thread);
  608. if (ret != RT_EOK)
  609. {
  610. rt_hw_interrupt_enable(temp);
  611. return ret;
  612. }
  613. rt_thread_wakeup_set(thread, wakeup_receiver, (void*)ch);
  614. ch->stat = RT_IPC_STAT_WAIT;/* no valid suspended senders */
  615. thread->error = RT_EOK;
  616. if (time > 0)
  617. {
  618. rt_timer_control(&(thread->thread_timer),
  619. RT_TIMER_CTRL_GET_FUNC,
  620. &old_timeout_func);
  621. rt_timer_control(&(thread->thread_timer),
  622. RT_TIMER_CTRL_SET_FUNC,
  623. receiver_timeout);
  624. /* reset the timeout of thread timer and start it */
  625. rt_timer_control(&(thread->thread_timer),
  626. RT_TIMER_CTRL_SET_TIME,
  627. &time);
  628. rt_timer_start(&(thread->thread_timer));
  629. }
  630. rt_hw_interrupt_enable(temp);
  631. rt_schedule(); /* let the senders run */
  632. temp = rt_hw_interrupt_disable();
  633. if (old_timeout_func)
  634. {
  635. rt_timer_control(&(thread->thread_timer),
  636. RT_TIMER_CTRL_SET_FUNC,
  637. old_timeout_func);
  638. }
  639. ret = thread->error;
  640. if ( ret != RT_EOK)
  641. {
  642. rt_hw_interrupt_enable(temp);
  643. return ret;
  644. }
  645. /* If waked up, the received message has been store into the thread. */
  646. *data = ((rt_ipc_msg_t)(thread->msg_ret))->msg; /* extract data */
  647. _ipc_msg_free(thread->msg_ret); /* put back the message to kernel */
  648. thread->msg_ret = RT_NULL;
  649. }
  650. rt_hw_interrupt_enable(temp);
  651. return RT_EOK;
  652. }
  653. rt_err_t rt_raw_channel_recv(rt_channel_t ch, rt_channel_msg_t data)
  654. {
  655. return _rt_raw_channel_recv_timeout(ch, data, RT_WAITING_FOREVER);
  656. }
  657. rt_err_t rt_raw_channel_recv_timeout(rt_channel_t ch, rt_channel_msg_t data, rt_int32_t time)
  658. {
  659. return _rt_raw_channel_recv_timeout(ch, data, time);
  660. }
  661. /**
  662. * Peek a message from the specified IPC channel.
  663. */
  664. rt_err_t rt_raw_channel_peek(rt_channel_t ch, rt_channel_msg_t data)
  665. {
  666. return _rt_raw_channel_recv_timeout(ch, data, 0);
  667. }
  668. /* for API */
  669. static int lwp_fd_new(int fdt_type)
  670. {
  671. struct dfs_fdtable *fdt;
  672. if (fdt_type)
  673. {
  674. fdt = dfs_fdtable_get_global();
  675. }
  676. else
  677. {
  678. fdt = dfs_fdtable_get();
  679. }
  680. return fdt_fd_new(fdt);
  681. }
  682. static struct dfs_fd *lwp_fd_get(int fdt_type, int fd)
  683. {
  684. struct dfs_fdtable *fdt;
  685. if (fdt_type)
  686. {
  687. fdt = dfs_fdtable_get_global();
  688. }
  689. else
  690. {
  691. fdt = dfs_fdtable_get();
  692. }
  693. return fdt_fd_get(fdt, fd);
  694. }
  695. static void lwp_fd_put(int fdt_type, struct dfs_fd *fd)
  696. {
  697. struct dfs_fdtable *fdt;
  698. if (fdt_type)
  699. {
  700. fdt = dfs_fdtable_get_global();
  701. }
  702. else
  703. {
  704. fdt = dfs_fdtable_get();
  705. }
  706. fdt_fd_put(fdt, fd);
  707. }
  708. static int _chfd_alloc(int fdt_type)
  709. {
  710. /* create a BSD socket */
  711. int fd;
  712. /* allocate a fd */
  713. fd = lwp_fd_new(fdt_type);
  714. if (fd < 0)
  715. {
  716. return -1;
  717. }
  718. return fd;
  719. }
  720. static void _chfd_free(int fd, int fdt_type)
  721. {
  722. struct dfs_fd *d;
  723. d = lwp_fd_get(fdt_type, fd);
  724. if (d == RT_NULL)
  725. {
  726. return;
  727. }
  728. lwp_fd_put(fdt_type, d);
  729. lwp_fd_put(fdt_type, d);
  730. }
  731. /* for fops */
  732. static int channel_fops_poll(struct dfs_fd *file, struct rt_pollreq *req)
  733. {
  734. int mask = POLLOUT;
  735. rt_channel_t ch;
  736. ch = (rt_channel_t)file->data;
  737. rt_poll_add(&(ch->reader_queue), req);
  738. if (ch->stat != RT_IPC_STAT_IDLE)
  739. {
  740. return mask;
  741. }
  742. if (!rt_list_isempty(&ch->wait_msg))
  743. {
  744. mask |= POLLIN;
  745. }
  746. return mask;
  747. }
  748. static int channel_fops_close(struct dfs_fd *file)
  749. {
  750. rt_channel_t ch;
  751. rt_base_t level;
  752. level = rt_hw_interrupt_disable();
  753. ch = (rt_channel_t)file->data;
  754. ch->ref--;
  755. if (ch->ref == 0)
  756. {
  757. /* wakeup all the suspended receivers and senders */
  758. rt_channel_list_resume_all(&ch->parent.suspend_thread);
  759. rt_channel_list_resume_all(&ch->wait_thread);
  760. /* all ipc msg will lost */
  761. rt_list_init(&ch->wait_msg);
  762. rt_object_delete(&ch->parent.parent); /* release the IPC channel structure */
  763. }
  764. rt_hw_interrupt_enable(level);
  765. return 0;
  766. }
  767. static const struct dfs_file_ops channel_fops =
  768. {
  769. NULL, /* open */
  770. channel_fops_close,
  771. NULL,
  772. NULL,
  773. NULL,
  774. NULL,
  775. NULL, /* lseek */
  776. NULL, /* getdents */
  777. channel_fops_poll,
  778. };
  779. int lwp_channel_open(int fdt_type, const char *name, int flags)
  780. {
  781. int fd;
  782. rt_channel_t ch = RT_NULL;
  783. fd = _chfd_alloc(fdt_type); /* allocate an IPC channel descriptor */
  784. if (fd == -1)
  785. {
  786. goto quit;
  787. }
  788. ch = rt_raw_channel_open(name, flags);
  789. if (ch)
  790. {
  791. struct dfs_fd *d;
  792. d = lwp_fd_get(fdt_type, fd);
  793. d->type = FT_USER;
  794. d->path = NULL;
  795. d->fops = &channel_fops;
  796. d->flags = O_RDWR; /* set flags as read and write */
  797. d->size = 0;
  798. d->pos = 0;
  799. /* set socket to the data of dfs_fd */
  800. d->data = (void *)ch;
  801. lwp_fd_put(fdt_type, d);
  802. }
  803. else
  804. {
  805. _chfd_free(fd, fdt_type);
  806. fd = -1;
  807. }
  808. quit:
  809. return fd;
  810. }
  811. static rt_channel_t fd_2_channel(int fdt_type, int fd)
  812. {
  813. struct dfs_fd *d;
  814. d = lwp_fd_get(fdt_type, fd);
  815. if (d)
  816. {
  817. rt_channel_t ch;
  818. ch = (rt_channel_t)d->data;
  819. lwp_fd_put(fdt_type, d);
  820. if (ch)
  821. {
  822. return ch;
  823. }
  824. }
  825. return RT_NULL;
  826. }
  827. rt_err_t lwp_channel_close(int fdt_type, int fd)
  828. {
  829. rt_channel_t ch;
  830. ch = fd_2_channel(fdt_type, fd);
  831. if (ch)
  832. {
  833. _chfd_free(fd, fdt_type);
  834. return rt_raw_channel_close(ch);
  835. }
  836. return -RT_EIO;
  837. }
  838. rt_err_t lwp_channel_send(int fdt_type, int fd, rt_channel_msg_t data)
  839. {
  840. rt_channel_t ch;
  841. ch = fd_2_channel(fdt_type, fd);
  842. if (ch)
  843. {
  844. return rt_raw_channel_send(ch, data);
  845. }
  846. return -RT_EIO;
  847. }
  848. rt_err_t lwp_channel_send_recv_timeout(int fdt_type, int fd, rt_channel_msg_t data, rt_channel_msg_t data_ret, rt_int32_t time)
  849. {
  850. rt_channel_t ch;
  851. ch = fd_2_channel(fdt_type, fd);
  852. if (ch)
  853. {
  854. return rt_raw_channel_send_recv_timeout(ch, data, data_ret, time);
  855. }
  856. return -RT_EIO;
  857. }
  858. rt_err_t lwp_channel_reply(int fdt_type, int fd, rt_channel_msg_t data)
  859. {
  860. rt_channel_t ch;
  861. ch = fd_2_channel(fdt_type, fd);
  862. if (ch)
  863. {
  864. return rt_raw_channel_reply(ch, data);
  865. }
  866. return -RT_EIO;
  867. }
  868. rt_err_t lwp_channel_recv_timeout(int fdt_type, int fd, rt_channel_msg_t data, rt_int32_t time)
  869. {
  870. rt_channel_t ch;
  871. ch = fd_2_channel(fdt_type, fd);
  872. if (ch)
  873. {
  874. return rt_raw_channel_recv_timeout(ch, data, time);
  875. }
  876. return -RT_EIO;
  877. }
  878. int rt_channel_open(const char *name, int flags)
  879. {
  880. return lwp_channel_open(FDT_TYPE_KERNEL, name, flags);
  881. }
  882. rt_err_t rt_channel_close(int fd)
  883. {
  884. return lwp_channel_close(FDT_TYPE_KERNEL, fd);
  885. }
  886. rt_err_t rt_channel_send(int fd, rt_channel_msg_t data)
  887. {
  888. return lwp_channel_send(FDT_TYPE_KERNEL, fd, data);
  889. }
  890. rt_err_t rt_channel_send_recv_timeout(int fd, rt_channel_msg_t data, rt_channel_msg_t data_ret, rt_int32_t time)
  891. {
  892. return lwp_channel_send_recv_timeout(FDT_TYPE_KERNEL, fd, data, data_ret, time);
  893. }
  894. rt_err_t rt_channel_send_recv(int fd, rt_channel_msg_t data, rt_channel_msg_t data_ret)
  895. {
  896. return lwp_channel_send_recv_timeout(FDT_TYPE_KERNEL, fd, data, data_ret, RT_WAITING_FOREVER);
  897. }
  898. rt_err_t rt_channel_reply(int fd, rt_channel_msg_t data)
  899. {
  900. return lwp_channel_reply(FDT_TYPE_KERNEL, fd, data);
  901. }
  902. rt_err_t rt_channel_recv_timeout(int fd, rt_channel_msg_t data, rt_int32_t time)
  903. {
  904. return lwp_channel_recv_timeout(FDT_TYPE_KERNEL, fd, data, time);
  905. }
  906. rt_err_t rt_channel_recv(int fd, rt_channel_msg_t data)
  907. {
  908. return lwp_channel_recv_timeout(FDT_TYPE_KERNEL, fd, data, RT_WAITING_FOREVER);
  909. }
  910. rt_err_t rt_channel_peek(int fd, rt_channel_msg_t data)
  911. {
  912. return lwp_channel_recv_timeout(FDT_TYPE_KERNEL, fd, data, 0);
  913. }
  914. #ifdef RT_USING_FINSH
  915. static int list_channel(void)
  916. {
  917. rt_base_t level;
  918. rt_channel_t *channels;
  919. rt_ubase_t index, count;
  920. struct rt_object *object;
  921. struct rt_list_node *node;
  922. struct rt_object_information *information;
  923. const char* stat_strs[] = {"idle", "wait", "active"};
  924. information = rt_object_get_information(RT_Object_Class_Channel);
  925. RT_ASSERT(information != RT_NULL);
  926. count = 0;
  927. level = rt_hw_interrupt_disable();
  928. /* get the count of IPC channels */
  929. for (node = information->object_list.next;
  930. node != &(information->object_list);
  931. node = node->next)
  932. {
  933. count ++;
  934. }
  935. rt_hw_interrupt_enable(level);
  936. if (count == 0) return 0;
  937. channels = (rt_channel_t *) rt_calloc(count, sizeof(rt_channel_t));
  938. if (channels == RT_NULL) return 0; /* out of memory */
  939. index = 0;
  940. level = rt_hw_interrupt_disable();
  941. /* retrieve pointer of IPC channels */
  942. for (node = information->object_list.next;
  943. node != &(information->object_list);
  944. node = node->next)
  945. {
  946. object = rt_list_entry(node, struct rt_object, list);
  947. channels[index] = (rt_channel_t)object;
  948. index ++;
  949. }
  950. rt_hw_interrupt_enable(level);
  951. rt_kprintf(" channel state\n");
  952. rt_kprintf("-------- -------\n");
  953. for (index = 0; index < count; index ++)
  954. {
  955. if (channels[index] != RT_NULL)
  956. {
  957. rt_kprintf("%-*.s", RT_NAME_MAX, channels[index]->parent.parent.name);
  958. if (channels[index]->stat < 3)
  959. rt_kprintf(" %s\n", stat_strs[channels[index]->stat]);
  960. }
  961. }
  962. rt_free(channels);
  963. return 0;
  964. }
  965. MSH_CMD_EXPORT(list_channel, list IPC channel information);
  966. #endif