lwp_ipc.c 34 KB

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