lwp_ipc.c 30 KB

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