1
0

lwp_ipc.c 32 KB

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