pipe.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * File : pipe.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2012, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2012-09-30 Bernard first version.
  23. */
  24. #include <rthw.h>
  25. #include <rtthread.h>
  26. #include <rtdevice.h>
  27. static rt_size_t rt_pipe_read(rt_device_t dev,
  28. rt_off_t pos,
  29. void *buffer,
  30. rt_size_t size)
  31. {
  32. rt_uint32_t level;
  33. rt_thread_t thread;
  34. struct rt_pipe_device *pipe;
  35. rt_size_t read_nbytes;
  36. pipe = PIPE_DEVICE(dev);
  37. RT_ASSERT(pipe != RT_NULL);
  38. thread = rt_thread_self();
  39. /* current context checking */
  40. RT_DEBUG_NOT_IN_INTERRUPT;
  41. do
  42. {
  43. level = rt_hw_interrupt_disable();
  44. read_nbytes = rt_ringbuffer_get(&(pipe->ringbuffer), buffer, size);
  45. if (read_nbytes == 0)
  46. {
  47. rt_thread_suspend(thread);
  48. /* waiting on suspended read list */
  49. rt_list_insert_before(&(pipe->suspended_read_list),
  50. &(thread->tlist));
  51. rt_hw_interrupt_enable(level);
  52. rt_schedule();
  53. }
  54. else
  55. {
  56. if (!rt_list_isempty(&pipe->suspended_write_list))
  57. {
  58. /* get suspended thread */
  59. thread = rt_list_entry(pipe->suspended_write_list.next,
  60. struct rt_thread,
  61. tlist);
  62. /* resume the write thread */
  63. rt_thread_resume(thread);
  64. rt_hw_interrupt_enable(level);
  65. rt_schedule();
  66. }
  67. else
  68. {
  69. rt_hw_interrupt_enable(level);
  70. }
  71. break;
  72. }
  73. } while (read_nbytes == 0);
  74. return read_nbytes;
  75. }
  76. struct rt_pipe_device *_pipe = RT_NULL;
  77. static rt_size_t rt_pipe_write(rt_device_t dev,
  78. rt_off_t pos,
  79. const void *buffer,
  80. rt_size_t size)
  81. {
  82. rt_uint32_t level;
  83. rt_thread_t thread;
  84. struct rt_pipe_device *pipe;
  85. rt_size_t write_nbytes;
  86. pipe = PIPE_DEVICE(dev);
  87. RT_ASSERT(pipe != RT_NULL);
  88. if (_pipe == RT_NULL)
  89. _pipe = pipe;
  90. thread = rt_thread_self();
  91. /* current context checking */
  92. RT_DEBUG_NOT_IN_INTERRUPT;
  93. do
  94. {
  95. level = rt_hw_interrupt_disable();
  96. write_nbytes = rt_ringbuffer_put(&(pipe->ringbuffer), buffer, size);
  97. if (write_nbytes == 0)
  98. {
  99. /* pipe full, waiting on suspended write list */
  100. rt_thread_suspend(thread);
  101. /* waiting on suspended read list */
  102. rt_list_insert_before(&(pipe->suspended_write_list),
  103. &(thread->tlist));
  104. rt_hw_interrupt_enable(level);
  105. rt_schedule();
  106. }
  107. else
  108. {
  109. if (!rt_list_isempty(&pipe->suspended_read_list))
  110. {
  111. /* get suspended thread */
  112. thread = rt_list_entry(pipe->suspended_read_list.next,
  113. struct rt_thread,
  114. tlist);
  115. /* resume the read thread */
  116. rt_thread_resume(thread);
  117. rt_hw_interrupt_enable(level);
  118. rt_schedule();
  119. }
  120. else
  121. {
  122. rt_hw_interrupt_enable(level);
  123. }
  124. break;
  125. }
  126. }while (write_nbytes == 0);
  127. return write_nbytes;
  128. }
  129. static rt_err_t rt_pipe_control(rt_device_t dev, rt_uint8_t cmd, void *args)
  130. {
  131. return RT_EOK;
  132. }
  133. rt_err_t rt_pipe_create(const char *name, rt_size_t size)
  134. {
  135. rt_err_t result = RT_EOK;
  136. rt_uint8_t *rb_memptr = RT_NULL;
  137. struct rt_pipe_device *pipe = RT_NULL;
  138. /* get aligned size */
  139. size = RT_ALIGN(size, RT_ALIGN_SIZE);
  140. pipe = (struct rt_pipe_device *)rt_calloc(1, sizeof(struct rt_pipe_device));
  141. if (pipe != RT_NULL)
  142. {
  143. /* create ring buffer of pipe */
  144. rb_memptr = rt_malloc(size);
  145. if (rb_memptr == RT_NULL)
  146. {
  147. result = -RT_ENOMEM;
  148. goto __exit;
  149. }
  150. /* initialize suspended list */
  151. rt_list_init(&pipe->suspended_read_list);
  152. rt_list_init(&pipe->suspended_write_list);
  153. /* initialize ring buffer */
  154. rt_ringbuffer_init(&pipe->ringbuffer, rb_memptr, size);
  155. /* create device */
  156. pipe->parent.type = RT_Device_Class_Char;
  157. pipe->parent.init = RT_NULL;
  158. pipe->parent.open = RT_NULL;
  159. pipe->parent.close = RT_NULL;
  160. pipe->parent.read = rt_pipe_read;
  161. pipe->parent.write = rt_pipe_write;
  162. pipe->parent.control = rt_pipe_control;
  163. return rt_device_register(&(pipe->parent), name, RT_DEVICE_FLAG_RDWR);
  164. }
  165. else
  166. {
  167. result = -RT_ENOMEM;
  168. }
  169. __exit:
  170. if (pipe != RT_NULL)
  171. rt_free(pipe);
  172. if (rb_memptr != RT_NULL)
  173. rt_free(rb_memptr);
  174. return result;
  175. }
  176. RTM_EXPORT(rt_pipe_create);
  177. void rt_pipe_destroy(struct rt_pipe_device *pipe)
  178. {
  179. if (pipe == RT_NULL)
  180. return;
  181. /* un-register pipe device */
  182. rt_device_unregister(&(pipe->parent));
  183. /* release memory */
  184. rt_free(pipe->ringbuffer.buffer_ptr);
  185. rt_free(pipe);
  186. return;
  187. }
  188. RTM_EXPORT(rt_pipe_destroy);