lwp_ipc.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327
  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);