lwp_ipc.c 31 KB

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