pipe.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * File : pipe.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2012, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2012-09-30 Bernard first version.
  13. */
  14. #include <rthw.h>
  15. #include <rtthread.h>
  16. #include <rtdevice.h>
  17. static rt_size_t rt_pipe_read(rt_device_t dev,
  18. rt_off_t pos,
  19. void *buffer,
  20. rt_size_t size)
  21. {
  22. rt_uint32_t level;
  23. rt_thread_t thread;
  24. struct rt_pipe_device *pipe;
  25. rt_size_t read_nbytes;
  26. pipe = PIPE_DEVICE(dev);
  27. RT_ASSERT(pipe != RT_NULL);
  28. thread = rt_thread_self();
  29. /* current context checking */
  30. RT_DEBUG_NOT_IN_INTERRUPT;
  31. do
  32. {
  33. level = rt_hw_interrupt_disable();
  34. read_nbytes = rt_ringbuffer_get(&(pipe->ringbuffer), buffer, size);
  35. if (read_nbytes == 0)
  36. {
  37. rt_thread_suspend(thread);
  38. /* waiting on suspended read list */
  39. rt_list_insert_before(&(pipe->suspended_read_list),
  40. &(thread->tlist));
  41. rt_hw_interrupt_enable(level);
  42. rt_schedule();
  43. }
  44. else
  45. {
  46. if (!rt_list_isempty(&pipe->suspended_write_list))
  47. {
  48. /* get suspended thread */
  49. thread = rt_list_entry(pipe->suspended_write_list.next,
  50. struct rt_thread,
  51. tlist);
  52. /* resume the write thread */
  53. rt_thread_resume(thread);
  54. rt_hw_interrupt_enable(level);
  55. rt_schedule();
  56. }
  57. else
  58. {
  59. rt_hw_interrupt_enable(level);
  60. }
  61. break;
  62. }
  63. } while (read_nbytes == 0);
  64. return read_nbytes;
  65. }
  66. struct rt_pipe_device *_pipe = RT_NULL;
  67. static rt_size_t rt_pipe_write(rt_device_t dev,
  68. rt_off_t pos,
  69. const void *buffer,
  70. rt_size_t size)
  71. {
  72. rt_uint32_t level;
  73. rt_thread_t thread;
  74. struct rt_pipe_device *pipe;
  75. rt_size_t write_nbytes;
  76. pipe = PIPE_DEVICE(dev);
  77. RT_ASSERT(pipe != RT_NULL);
  78. if (_pipe == RT_NULL)
  79. _pipe = pipe;
  80. thread = rt_thread_self();
  81. /* current context checking */
  82. RT_DEBUG_NOT_IN_INTERRUPT;
  83. do
  84. {
  85. level = rt_hw_interrupt_disable();
  86. write_nbytes = rt_ringbuffer_put(&(pipe->ringbuffer), buffer, size);
  87. if (write_nbytes == 0)
  88. {
  89. /* pipe full, waiting on suspended write list */
  90. rt_thread_suspend(thread);
  91. /* waiting on suspended read list */
  92. rt_list_insert_before(&(pipe->suspended_write_list),
  93. &(thread->tlist));
  94. rt_hw_interrupt_enable(level);
  95. rt_schedule();
  96. }
  97. else
  98. {
  99. if (!rt_list_isempty(&pipe->suspended_read_list))
  100. {
  101. /* get suspended thread */
  102. thread = rt_list_entry(pipe->suspended_read_list.next,
  103. struct rt_thread,
  104. tlist);
  105. /* resume the read thread */
  106. rt_thread_resume(thread);
  107. rt_hw_interrupt_enable(level);
  108. rt_schedule();
  109. }
  110. else
  111. {
  112. rt_hw_interrupt_enable(level);
  113. }
  114. break;
  115. }
  116. }while (write_nbytes == 0);
  117. return write_nbytes;
  118. }
  119. static rt_err_t rt_pipe_control(rt_device_t dev, rt_uint8_t cmd, void *args)
  120. {
  121. return RT_EOK;
  122. }
  123. rt_err_t rt_pipe_create(const char *name, rt_size_t size)
  124. {
  125. rt_err_t result = RT_EOK;
  126. rt_uint8_t *rb_memptr = RT_NULL;
  127. struct rt_pipe_device *pipe = RT_NULL;
  128. /* get aligned size */
  129. size = RT_ALIGN(size, RT_ALIGN_SIZE);
  130. pipe = (struct rt_pipe_device *)rt_calloc(1, sizeof(struct rt_pipe_device));
  131. if (pipe != RT_NULL)
  132. {
  133. /* create ring buffer of pipe */
  134. rb_memptr = rt_malloc(size);
  135. if (rb_memptr == RT_NULL)
  136. {
  137. result = -RT_ENOMEM;
  138. goto __exit;
  139. }
  140. /* initialize suspended list */
  141. rt_list_init(&pipe->suspended_read_list);
  142. rt_list_init(&pipe->suspended_write_list);
  143. /* initialize ring buffer */
  144. rt_ringbuffer_init(&pipe->ringbuffer, rb_memptr, size);
  145. /* create device */
  146. pipe->parent.type = RT_Device_Class_Char;
  147. pipe->parent.init = RT_NULL;
  148. pipe->parent.open = RT_NULL;
  149. pipe->parent.close = RT_NULL;
  150. pipe->parent.read = rt_pipe_read;
  151. pipe->parent.write = rt_pipe_write;
  152. pipe->parent.control = rt_pipe_control;
  153. return rt_device_register(&(pipe->parent), name, RT_DEVICE_FLAG_RDWR);
  154. }
  155. else
  156. {
  157. result = -RT_ENOMEM;
  158. }
  159. __exit:
  160. if (pipe != RT_NULL)
  161. rt_free(pipe);
  162. if (rb_memptr != RT_NULL)
  163. rt_free(rb_memptr);
  164. return result;
  165. }
  166. RTM_EXPORT(rt_pipe_create);
  167. void rt_pipe_destroy(struct rt_pipe_device *pipe)
  168. {
  169. if (pipe == RT_NULL)
  170. return;
  171. /* un-register pipe device */
  172. rt_device_unregister(&(pipe->parent));
  173. /* release memory */
  174. rt_free(pipe->ringbuffer.buffer_ptr);
  175. rt_free(pipe);
  176. return;
  177. }
  178. RTM_EXPORT(rt_pipe_destroy);