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