lwp_ipc.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213
  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 <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. * Get file vnode from fd.
  311. */
  312. static void *_ipc_msg_get_file(int fd)
  313. {
  314. struct dfs_file *d;
  315. d = fd_get(fd);
  316. if (d == RT_NULL)
  317. return RT_NULL;
  318. if (!d->vnode)
  319. return RT_NULL;
  320. d->vnode->ref_count++;
  321. return (void *)d->vnode;
  322. }
  323. /**
  324. * Get fd from file vnode.
  325. */
  326. static int _ipc_msg_fd_new(void *file)
  327. {
  328. int fd;
  329. struct dfs_file *d;
  330. fd = fd_new();
  331. if (fd < 0)
  332. {
  333. return -1;
  334. }
  335. d = fd_get(fd);
  336. if (!d)
  337. {
  338. fd_release(fd);
  339. return -1;
  340. }
  341. d->vnode = (struct dfs_vnode *)file;
  342. d->flags = O_RDWR; /* set flags as read and write */
  343. return fd;
  344. }
  345. /**
  346. * Send data through an IPC channel, wait for the reply or not.
  347. */
  348. 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)
  349. {
  350. rt_ipc_msg_t msg;
  351. struct rt_thread *thread_recv, *thread_send = 0;
  352. register rt_base_t temp;
  353. rt_err_t ret;
  354. void (*old_timeout_func)(void *) = 0;
  355. if (need_reply)
  356. {
  357. RT_DEBUG_NOT_IN_INTERRUPT;
  358. }
  359. if (ch == RT_NULL)
  360. {
  361. return -RT_EIO;
  362. }
  363. temp = rt_hw_interrupt_disable();
  364. if (rt_object_get_type(&ch->parent.parent) != RT_Object_Class_Channel)
  365. {
  366. rt_hw_interrupt_enable(temp);
  367. return -RT_EIO;
  368. }
  369. if (need_reply && time == 0)
  370. {
  371. rt_hw_interrupt_enable(temp);
  372. return -RT_ETIMEOUT;
  373. }
  374. /* allocate an IPC message */
  375. msg = _ipc_msg_alloc();
  376. if (!msg)
  377. {
  378. rt_hw_interrupt_enable(temp);
  379. return -RT_ENOMEM;
  380. }
  381. /* IPC message : file descriptor */
  382. if (data->type == RT_CHANNEL_FD)
  383. {
  384. data->u.fd.file = _ipc_msg_get_file(data->u.fd.fd);
  385. }
  386. rt_ipc_msg_init(msg, data, need_reply);
  387. if (need_reply)
  388. {
  389. thread_send = rt_thread_self();
  390. thread_send->error = RT_EOK;
  391. }
  392. switch (ch->stat)
  393. {
  394. case RT_IPC_STAT_IDLE:
  395. case RT_IPC_STAT_ACTIVE:
  396. if (need_reply)
  397. {
  398. ret = rt_channel_list_suspend(&ch->wait_thread, thread_send);
  399. if (ret != RT_EOK)
  400. {
  401. _ipc_msg_free(msg);
  402. rt_hw_interrupt_enable(temp);
  403. return ret;
  404. }
  405. rt_thread_wakeup_set(thread_send, wakeup_sender_wait_recv, (void*)ch);
  406. if (time > 0)
  407. {
  408. rt_timer_control(&(thread_send->thread_timer),
  409. RT_TIMER_CTRL_GET_FUNC,
  410. &old_timeout_func);
  411. rt_timer_control(&(thread_send->thread_timer),
  412. RT_TIMER_CTRL_SET_FUNC,
  413. sender_timeout);
  414. /* reset the timeout of thread timer and start it */
  415. rt_timer_control(&(thread_send->thread_timer),
  416. RT_TIMER_CTRL_SET_TIME,
  417. &time);
  418. rt_timer_start(&(thread_send->thread_timer));
  419. }
  420. }
  421. /*
  422. * If there is no thread waiting for messages, chain the message
  423. * into the list.
  424. */
  425. rt_list_insert_before(&ch->wait_msg, &msg->mlist);
  426. break;
  427. case RT_IPC_STAT_WAIT:
  428. /*
  429. * If there are suspended receivers on the IPC channel, transfer the
  430. * pointer of the message to the first receiver directly and wake it
  431. * up.
  432. */
  433. RT_ASSERT(ch->parent.suspend_thread.next != &ch->parent.suspend_thread);
  434. if (need_reply)
  435. {
  436. ret = rt_channel_list_suspend(&ch->wait_thread, thread_send);
  437. if (ret != RT_EOK)
  438. {
  439. _ipc_msg_free(msg);
  440. rt_hw_interrupt_enable(temp);
  441. return ret;
  442. }
  443. ch->reply = thread_send; /* record the current waiting sender */
  444. ch->stat = RT_IPC_STAT_ACTIVE;
  445. rt_thread_wakeup_set(thread_send, wakeup_sender_wait_reply, (void*)ch);
  446. if (time > 0)
  447. {
  448. rt_timer_control(&(thread_send->thread_timer),
  449. RT_TIMER_CTRL_GET_FUNC,
  450. &old_timeout_func);
  451. rt_timer_control(&(thread_send->thread_timer),
  452. RT_TIMER_CTRL_SET_FUNC,
  453. sender_timeout);
  454. /* reset the timeout of thread timer and start it */
  455. rt_timer_control(&(thread_send->thread_timer),
  456. RT_TIMER_CTRL_SET_TIME,
  457. &time);
  458. rt_timer_start(&(thread_send->thread_timer));
  459. }
  460. }
  461. else
  462. {
  463. ch->stat = RT_IPC_STAT_IDLE;
  464. }
  465. thread_recv = rt_list_entry(ch->parent.suspend_thread.next, struct rt_thread, tlist);
  466. thread_recv->msg_ret = msg; /* to the first suspended receiver */
  467. thread_recv->error = RT_EOK;
  468. rt_channel_list_resume(&ch->parent.suspend_thread);
  469. break;
  470. default:
  471. break;
  472. }
  473. if ( ch->stat == RT_IPC_STAT_IDLE)
  474. {
  475. _rt_channel_check_wq_wakup(ch);
  476. }
  477. rt_hw_interrupt_enable(temp);
  478. /* reschedule in order to let the potential receivers run */
  479. rt_schedule();
  480. if (need_reply)
  481. {
  482. temp = rt_hw_interrupt_disable();
  483. if (old_timeout_func)
  484. {
  485. rt_timer_control(&(thread_send->thread_timer),
  486. RT_TIMER_CTRL_SET_FUNC,
  487. old_timeout_func);
  488. }
  489. ret = thread_send->error;
  490. rt_hw_interrupt_enable(temp);
  491. if (ret != RT_EOK)
  492. {
  493. return ret;
  494. }
  495. /* If the sender gets the chance to run, the requested reply must be valid. */
  496. RT_ASSERT(data_ret != RT_NULL);
  497. *data_ret = ((rt_ipc_msg_t)(thread_send->msg_ret))->msg; /* extract data */
  498. temp = rt_hw_interrupt_disable();
  499. _ipc_msg_free(thread_send->msg_ret); /* put back the message to kernel */
  500. rt_hw_interrupt_enable(temp);
  501. thread_send->msg_ret = RT_NULL;
  502. }
  503. return RT_EOK;
  504. }
  505. /**
  506. * Send data through an IPC channel with no reply.
  507. */
  508. rt_err_t rt_raw_channel_send(rt_channel_t ch, rt_channel_msg_t data)
  509. {
  510. return _rt_raw_channel_send_recv_timeout(ch, data, 0, 0, RT_WAITING_FOREVER);
  511. }
  512. /**
  513. * Send data through an IPC channel and wait for the relpy.
  514. */
  515. rt_err_t rt_raw_channel_send_recv(rt_channel_t ch, rt_channel_msg_t data, rt_channel_msg_t data_ret)
  516. {
  517. return _rt_raw_channel_send_recv_timeout(ch, data, 1, data_ret, RT_WAITING_FOREVER);
  518. }
  519. /**
  520. * Send data through an IPC channel and wait for the relpy.
  521. */
  522. 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)
  523. {
  524. return _rt_raw_channel_send_recv_timeout(ch, data, 1, data_ret, time);
  525. }
  526. /**
  527. * Reply to the waiting sender and wake it up.
  528. */
  529. rt_err_t rt_raw_channel_reply(rt_channel_t ch, rt_channel_msg_t data)
  530. {
  531. rt_ipc_msg_t msg;
  532. struct rt_thread *thread;
  533. register rt_base_t temp;
  534. if (ch == RT_NULL)
  535. {
  536. return -RT_EIO;
  537. }
  538. temp = rt_hw_interrupt_disable();
  539. if (rt_object_get_type(&ch->parent.parent) != RT_Object_Class_Channel)
  540. {
  541. rt_hw_interrupt_enable(temp);
  542. return -RT_EIO;
  543. }
  544. if (ch->stat != RT_IPC_STAT_ACTIVE)
  545. {
  546. rt_hw_interrupt_enable(temp);
  547. return -RT_ERROR;
  548. }
  549. if (ch->reply == RT_NULL)
  550. {
  551. rt_hw_interrupt_enable(temp);
  552. return -RT_ERROR;
  553. }
  554. /* allocate an IPC message */
  555. msg = _ipc_msg_alloc();
  556. if (!msg)
  557. {
  558. rt_hw_interrupt_enable(temp);
  559. return -RT_ENOMEM;
  560. }
  561. rt_ipc_msg_init(msg, data, 0);
  562. thread = ch->reply;
  563. thread->msg_ret = msg; /* transfer the reply to the sender */
  564. rt_thread_resume(thread); /* wake up the sender */
  565. ch->stat = RT_IPC_STAT_IDLE;
  566. ch->reply = RT_NULL;
  567. _rt_channel_check_wq_wakup(ch);
  568. rt_hw_interrupt_enable(temp);
  569. rt_schedule();
  570. return RT_EOK;
  571. }
  572. static rt_err_t wakeup_receiver(void *object, struct rt_thread *thread)
  573. {
  574. rt_channel_t ch;
  575. rt_err_t ret;
  576. ch = (rt_channel_t)object;
  577. ch->stat = RT_IPC_STAT_IDLE;
  578. thread->error = -RT_EINTR;
  579. ret = rt_channel_list_resume(&ch->parent.suspend_thread);
  580. _rt_channel_check_wq_wakup(ch);
  581. return ret;
  582. }
  583. static void receiver_timeout(void *parameter)
  584. {
  585. struct rt_thread *thread = (struct rt_thread*)parameter;
  586. rt_channel_t ch;
  587. ch = (rt_channel_t)(thread->wakeup.user_data);
  588. ch->stat = RT_IPC_STAT_IDLE;
  589. thread->error = -RT_ETIMEOUT;
  590. thread->wakeup.func = RT_NULL;
  591. rt_list_remove(&(thread->tlist));
  592. /* insert to schedule ready list */
  593. rt_schedule_insert_thread(thread);
  594. _rt_channel_check_wq_wakup(ch);
  595. /* do schedule */
  596. rt_schedule();
  597. }
  598. /**
  599. * Fetch a message from the specified IPC channel.
  600. */
  601. static rt_err_t _rt_raw_channel_recv_timeout(rt_channel_t ch, rt_channel_msg_t data, rt_int32_t time)
  602. {
  603. struct rt_thread *thread;
  604. rt_ipc_msg_t msg_ret;
  605. register rt_base_t temp;
  606. rt_err_t ret;
  607. void (*old_timeout_func)(void *) = 0;
  608. RT_DEBUG_NOT_IN_INTERRUPT;
  609. if (ch == RT_NULL)
  610. {
  611. return -RT_EIO;
  612. }
  613. temp = rt_hw_interrupt_disable();
  614. if (rt_object_get_type(&ch->parent.parent) != RT_Object_Class_Channel)
  615. {
  616. rt_hw_interrupt_enable(temp);
  617. return -RT_EIO;
  618. }
  619. if (ch->stat != RT_IPC_STAT_IDLE)
  620. {
  621. rt_hw_interrupt_enable(temp);
  622. return -RT_ERROR;
  623. }
  624. if (ch->wait_msg.next != &ch->wait_msg) /* there exist unhandled messages */
  625. {
  626. msg_ret = rt_list_entry(ch->wait_msg.next, struct rt_ipc_msg, mlist);
  627. rt_list_remove(ch->wait_msg.next); /* remove the message from the channel */
  628. if (msg_ret->need_reply)
  629. {
  630. RT_ASSERT(ch->wait_thread.next != &ch->wait_thread);
  631. thread = rt_list_entry(ch->wait_thread.next, struct rt_thread, tlist);
  632. rt_list_remove(ch->wait_thread.next);
  633. ch->reply = thread; /* record the waiting sender */
  634. ch->stat = RT_IPC_STAT_ACTIVE; /* no valid suspened receivers */
  635. }
  636. *data = msg_ret->msg; /* extract the transferred data */
  637. if (data->type == RT_CHANNEL_FD)
  638. {
  639. data->u.fd.fd = _ipc_msg_fd_new(data->u.fd.file);
  640. }
  641. _ipc_msg_free(msg_ret); /* put back the message to kernel */
  642. }
  643. else
  644. {
  645. if (time == 0)
  646. {
  647. rt_hw_interrupt_enable(temp);
  648. return -RT_ETIMEOUT;
  649. }
  650. /* no valid message, we must wait */
  651. thread = rt_thread_self();
  652. ret = rt_channel_list_suspend(&ch->parent.suspend_thread, thread);
  653. if (ret != RT_EOK)
  654. {
  655. rt_hw_interrupt_enable(temp);
  656. return ret;
  657. }
  658. rt_thread_wakeup_set(thread, wakeup_receiver, (void*)ch);
  659. ch->stat = RT_IPC_STAT_WAIT;/* no valid suspended senders */
  660. thread->error = RT_EOK;
  661. if (time > 0)
  662. {
  663. rt_timer_control(&(thread->thread_timer),
  664. RT_TIMER_CTRL_GET_FUNC,
  665. &old_timeout_func);
  666. rt_timer_control(&(thread->thread_timer),
  667. RT_TIMER_CTRL_SET_FUNC,
  668. receiver_timeout);
  669. /* reset the timeout of thread timer and start it */
  670. rt_timer_control(&(thread->thread_timer),
  671. RT_TIMER_CTRL_SET_TIME,
  672. &time);
  673. rt_timer_start(&(thread->thread_timer));
  674. }
  675. rt_hw_interrupt_enable(temp);
  676. rt_schedule(); /* let the senders run */
  677. temp = rt_hw_interrupt_disable();
  678. if (old_timeout_func)
  679. {
  680. rt_timer_control(&(thread->thread_timer),
  681. RT_TIMER_CTRL_SET_FUNC,
  682. old_timeout_func);
  683. }
  684. ret = thread->error;
  685. if ( ret != RT_EOK)
  686. {
  687. rt_hw_interrupt_enable(temp);
  688. return ret;
  689. }
  690. /* If waked up, the received message has been store into the thread. */
  691. *data = ((rt_ipc_msg_t)(thread->msg_ret))->msg; /* extract data */
  692. if (data->type == RT_CHANNEL_FD)
  693. {
  694. data->u.fd.fd = _ipc_msg_fd_new(data->u.fd.file);
  695. }
  696. _ipc_msg_free(thread->msg_ret); /* put back the message to kernel */
  697. thread->msg_ret = RT_NULL;
  698. }
  699. rt_hw_interrupt_enable(temp);
  700. return RT_EOK;
  701. }
  702. rt_err_t rt_raw_channel_recv(rt_channel_t ch, rt_channel_msg_t data)
  703. {
  704. return _rt_raw_channel_recv_timeout(ch, data, RT_WAITING_FOREVER);
  705. }
  706. rt_err_t rt_raw_channel_recv_timeout(rt_channel_t ch, rt_channel_msg_t data, rt_int32_t time)
  707. {
  708. return _rt_raw_channel_recv_timeout(ch, data, time);
  709. }
  710. /**
  711. * Peek a message from the specified IPC channel.
  712. */
  713. rt_err_t rt_raw_channel_peek(rt_channel_t ch, rt_channel_msg_t data)
  714. {
  715. return _rt_raw_channel_recv_timeout(ch, data, 0);
  716. }
  717. /* for API */
  718. static int lwp_fd_new(int fdt_type)
  719. {
  720. struct dfs_fdtable *fdt;
  721. if (fdt_type)
  722. {
  723. fdt = dfs_fdtable_get_global();
  724. }
  725. else
  726. {
  727. fdt = dfs_fdtable_get();
  728. }
  729. return fdt_fd_new(fdt);
  730. }
  731. static struct dfs_file *lwp_fd_get(int fdt_type, int fd)
  732. {
  733. struct dfs_fdtable *fdt;
  734. if (fdt_type)
  735. {
  736. fdt = dfs_fdtable_get_global();
  737. }
  738. else
  739. {
  740. fdt = dfs_fdtable_get();
  741. }
  742. return fdt_fd_get(fdt, fd);
  743. }
  744. static void lwp_fd_release(int fdt_type, int fd)
  745. {
  746. struct dfs_fdtable *fdt;
  747. if (fdt_type)
  748. {
  749. fdt = dfs_fdtable_get_global();
  750. }
  751. else
  752. {
  753. fdt = dfs_fdtable_get();
  754. }
  755. fdt_fd_release(fdt, fd);
  756. }
  757. static int _chfd_alloc(int fdt_type)
  758. {
  759. /* create a BSD socket */
  760. int fd;
  761. /* allocate a fd */
  762. fd = lwp_fd_new(fdt_type);
  763. if (fd < 0)
  764. {
  765. return -1;
  766. }
  767. return fd;
  768. }
  769. static void _chfd_free(int fd, int fdt_type)
  770. {
  771. struct dfs_file *d;
  772. d = lwp_fd_get(fdt_type, fd);
  773. if (d == RT_NULL)
  774. {
  775. return;
  776. }
  777. lwp_fd_release(fdt_type, fd);
  778. }
  779. /* for fops */
  780. static int channel_fops_poll(struct dfs_file *file, struct rt_pollreq *req)
  781. {
  782. int mask = POLLOUT;
  783. rt_channel_t ch;
  784. ch = (rt_channel_t)file->vnode->data;
  785. rt_poll_add(&(ch->reader_queue), req);
  786. if (ch->stat != RT_IPC_STAT_IDLE)
  787. {
  788. return mask;
  789. }
  790. if (!rt_list_isempty(&ch->wait_msg))
  791. {
  792. mask |= POLLIN;
  793. }
  794. return mask;
  795. }
  796. static int channel_fops_close(struct dfs_file *file)
  797. {
  798. rt_channel_t ch;
  799. rt_base_t level;
  800. level = rt_hw_interrupt_disable();
  801. ch = (rt_channel_t)file->vnode->data;
  802. if (file->vnode->ref_count == 1)
  803. {
  804. ch->ref--;
  805. if (ch->ref == 0)
  806. {
  807. /* wakeup all the suspended receivers and senders */
  808. rt_channel_list_resume_all(&ch->parent.suspend_thread);
  809. rt_channel_list_resume_all(&ch->wait_thread);
  810. /* all ipc msg will lost */
  811. rt_list_init(&ch->wait_msg);
  812. rt_object_delete(&ch->parent.parent); /* release the IPC channel structure */
  813. }
  814. }
  815. rt_hw_interrupt_enable(level);
  816. return 0;
  817. }
  818. static const struct dfs_file_ops channel_fops =
  819. {
  820. NULL, /* open */
  821. channel_fops_close,
  822. NULL,
  823. NULL,
  824. NULL,
  825. NULL,
  826. NULL, /* lseek */
  827. NULL, /* getdents */
  828. channel_fops_poll,
  829. };
  830. int lwp_channel_open(int fdt_type, const char *name, int flags)
  831. {
  832. int fd;
  833. rt_channel_t ch = RT_NULL;
  834. struct dfs_file *d;
  835. fd = _chfd_alloc(fdt_type); /* allocate an IPC channel descriptor */
  836. if (fd == -1)
  837. {
  838. goto quit;
  839. }
  840. d = lwp_fd_get(fdt_type, fd);
  841. d->vnode = (struct dfs_vnode *)rt_malloc(sizeof(struct dfs_vnode));
  842. if (!d->vnode)
  843. {
  844. _chfd_free(fd, fdt_type);
  845. fd = -1;
  846. goto quit;
  847. }
  848. ch = rt_raw_channel_open(name, flags);
  849. if (ch)
  850. {
  851. rt_memset(d->vnode, 0, sizeof(struct dfs_vnode));
  852. rt_list_init(&d->vnode->list);
  853. d->vnode->type = FT_USER;
  854. d->vnode->path = NULL;
  855. d->vnode->fullpath = NULL;
  856. d->vnode->fops = &channel_fops;
  857. d->flags = O_RDWR; /* set flags as read and write */
  858. d->vnode->size = 0;
  859. d->pos = 0;
  860. d->vnode->ref_count = 1;
  861. /* set socket to the data of dfs_file */
  862. d->vnode->data = (void *)ch;
  863. }
  864. else
  865. {
  866. rt_free(d->vnode);
  867. d->vnode = RT_NULL;
  868. _chfd_free(fd, fdt_type);
  869. fd = -1;
  870. }
  871. quit:
  872. return fd;
  873. }
  874. static rt_channel_t fd_2_channel(int fdt_type, int fd)
  875. {
  876. struct dfs_file *d;
  877. d = lwp_fd_get(fdt_type, fd);
  878. if (d)
  879. {
  880. rt_channel_t ch;
  881. ch = (rt_channel_t)d->vnode->data;
  882. if (ch)
  883. {
  884. return ch;
  885. }
  886. }
  887. return RT_NULL;
  888. }
  889. rt_err_t lwp_channel_close(int fdt_type, int fd)
  890. {
  891. rt_channel_t ch;
  892. struct dfs_file *d;
  893. struct dfs_vnode *vnode;
  894. d = lwp_fd_get(fdt_type, fd);
  895. if (!d)
  896. {
  897. return -RT_EIO;
  898. }
  899. vnode = d->vnode;
  900. if (!vnode)
  901. {
  902. return -RT_EIO;
  903. }
  904. ch = fd_2_channel(fdt_type, fd);
  905. if (!ch)
  906. {
  907. return -RT_EIO;
  908. }
  909. _chfd_free(fd, fdt_type);
  910. if (vnode->ref_count == 1)
  911. {
  912. rt_free(vnode);
  913. return rt_raw_channel_close(ch);
  914. }
  915. return 0;
  916. }
  917. rt_err_t lwp_channel_send(int fdt_type, int fd, rt_channel_msg_t data)
  918. {
  919. rt_channel_t ch;
  920. ch = fd_2_channel(fdt_type, fd);
  921. if (ch)
  922. {
  923. return rt_raw_channel_send(ch, data);
  924. }
  925. return -RT_EIO;
  926. }
  927. 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)
  928. {
  929. rt_channel_t ch;
  930. ch = fd_2_channel(fdt_type, fd);
  931. if (ch)
  932. {
  933. return rt_raw_channel_send_recv_timeout(ch, data, data_ret, time);
  934. }
  935. return -RT_EIO;
  936. }
  937. rt_err_t lwp_channel_reply(int fdt_type, int fd, rt_channel_msg_t data)
  938. {
  939. rt_channel_t ch;
  940. ch = fd_2_channel(fdt_type, fd);
  941. if (ch)
  942. {
  943. return rt_raw_channel_reply(ch, data);
  944. }
  945. return -RT_EIO;
  946. }
  947. rt_err_t lwp_channel_recv_timeout(int fdt_type, int fd, rt_channel_msg_t data, rt_int32_t time)
  948. {
  949. rt_channel_t ch;
  950. ch = fd_2_channel(fdt_type, fd);
  951. if (ch)
  952. {
  953. return rt_raw_channel_recv_timeout(ch, data, time);
  954. }
  955. return -RT_EIO;
  956. }
  957. int rt_channel_open(const char *name, int flags)
  958. {
  959. return lwp_channel_open(FDT_TYPE_KERNEL, name, flags);
  960. }
  961. rt_err_t rt_channel_close(int fd)
  962. {
  963. return lwp_channel_close(FDT_TYPE_KERNEL, fd);
  964. }
  965. rt_err_t rt_channel_send(int fd, rt_channel_msg_t data)
  966. {
  967. return lwp_channel_send(FDT_TYPE_KERNEL, fd, data);
  968. }
  969. 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)
  970. {
  971. return lwp_channel_send_recv_timeout(FDT_TYPE_KERNEL, fd, data, data_ret, time);
  972. }
  973. rt_err_t rt_channel_send_recv(int fd, rt_channel_msg_t data, rt_channel_msg_t data_ret)
  974. {
  975. return lwp_channel_send_recv_timeout(FDT_TYPE_KERNEL, fd, data, data_ret, RT_WAITING_FOREVER);
  976. }
  977. rt_err_t rt_channel_reply(int fd, rt_channel_msg_t data)
  978. {
  979. return lwp_channel_reply(FDT_TYPE_KERNEL, fd, data);
  980. }
  981. rt_err_t rt_channel_recv_timeout(int fd, rt_channel_msg_t data, rt_int32_t time)
  982. {
  983. return lwp_channel_recv_timeout(FDT_TYPE_KERNEL, fd, data, time);
  984. }
  985. rt_err_t rt_channel_recv(int fd, rt_channel_msg_t data)
  986. {
  987. return lwp_channel_recv_timeout(FDT_TYPE_KERNEL, fd, data, RT_WAITING_FOREVER);
  988. }
  989. rt_err_t rt_channel_peek(int fd, rt_channel_msg_t data)
  990. {
  991. return lwp_channel_recv_timeout(FDT_TYPE_KERNEL, fd, data, 0);
  992. }
  993. #ifdef RT_USING_FINSH
  994. static int list_channel(void)
  995. {
  996. rt_base_t level;
  997. rt_channel_t *channels;
  998. rt_ubase_t index, count;
  999. struct rt_object *object;
  1000. struct rt_list_node *node;
  1001. struct rt_object_information *information;
  1002. const char* stat_strs[] = {"idle", "wait", "active"};
  1003. information = rt_object_get_information(RT_Object_Class_Channel);
  1004. RT_ASSERT(information != RT_NULL);
  1005. count = 0;
  1006. level = rt_hw_interrupt_disable();
  1007. /* get the count of IPC channels */
  1008. for (node = information->object_list.next;
  1009. node != &(information->object_list);
  1010. node = node->next)
  1011. {
  1012. count ++;
  1013. }
  1014. rt_hw_interrupt_enable(level);
  1015. if (count == 0) return 0;
  1016. channels = (rt_channel_t *) rt_calloc(count, sizeof(rt_channel_t));
  1017. if (channels == RT_NULL) return 0; /* out of memory */
  1018. index = 0;
  1019. level = rt_hw_interrupt_disable();
  1020. /* retrieve pointer of IPC channels */
  1021. for (node = information->object_list.next;
  1022. node != &(information->object_list);
  1023. node = node->next)
  1024. {
  1025. object = rt_list_entry(node, struct rt_object, list);
  1026. channels[index] = (rt_channel_t)object;
  1027. index ++;
  1028. }
  1029. rt_hw_interrupt_enable(level);
  1030. rt_kprintf(" channel state\n");
  1031. rt_kprintf("-------- -------\n");
  1032. for (index = 0; index < count; index ++)
  1033. {
  1034. if (channels[index] != RT_NULL)
  1035. {
  1036. rt_kprintf("%-*.s", RT_NAME_MAX, channels[index]->parent.parent.name);
  1037. if (channels[index]->stat < 3)
  1038. rt_kprintf(" %s\n", stat_strs[channels[index]->stat]);
  1039. }
  1040. }
  1041. rt_free(channels);
  1042. return 0;
  1043. }
  1044. MSH_CMD_EXPORT(list_channel, list IPC channel information);
  1045. #endif