DoubleBuffer.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/StringView.h>
  27. #include <Kernel/DoubleBuffer.h>
  28. namespace Kernel {
  29. inline void DoubleBuffer::compute_lockfree_metadata()
  30. {
  31. InterruptDisabler disabler;
  32. m_empty = m_read_buffer_index >= m_read_buffer->size && m_write_buffer->size == 0;
  33. m_space_for_writing = m_capacity - m_write_buffer->size;
  34. }
  35. DoubleBuffer::DoubleBuffer(size_t capacity)
  36. : m_write_buffer(&m_buffer1)
  37. , m_read_buffer(&m_buffer2)
  38. , m_storage(KBuffer::create_with_size(capacity * 2, Region::Access::Read | Region::Access::Write, "DoubleBuffer"))
  39. , m_capacity(capacity)
  40. {
  41. m_buffer1.data = m_storage.data();
  42. m_buffer1.size = 0;
  43. m_buffer2.data = m_storage.data() + capacity;
  44. m_buffer2.size = 0;
  45. m_space_for_writing = capacity;
  46. }
  47. void DoubleBuffer::flip()
  48. {
  49. if (m_storage.is_null())
  50. return;
  51. VERIFY(m_read_buffer_index == m_read_buffer->size);
  52. swap(m_read_buffer, m_write_buffer);
  53. m_write_buffer->size = 0;
  54. m_read_buffer_index = 0;
  55. compute_lockfree_metadata();
  56. }
  57. ssize_t DoubleBuffer::write(const UserOrKernelBuffer& data, size_t size)
  58. {
  59. if (!size || m_storage.is_null())
  60. return 0;
  61. VERIFY(size > 0);
  62. LOCKER(m_lock);
  63. size_t bytes_to_write = min(size, m_space_for_writing);
  64. u8* write_ptr = m_write_buffer->data + m_write_buffer->size;
  65. m_write_buffer->size += bytes_to_write;
  66. compute_lockfree_metadata();
  67. if (!data.read(write_ptr, bytes_to_write))
  68. return -EFAULT;
  69. if (m_unblock_callback && !m_empty)
  70. m_unblock_callback();
  71. return (ssize_t)bytes_to_write;
  72. }
  73. ssize_t DoubleBuffer::read(UserOrKernelBuffer& data, size_t size)
  74. {
  75. if (!size || m_storage.is_null())
  76. return 0;
  77. VERIFY(size > 0);
  78. LOCKER(m_lock);
  79. if (m_read_buffer_index >= m_read_buffer->size && m_write_buffer->size != 0)
  80. flip();
  81. if (m_read_buffer_index >= m_read_buffer->size)
  82. return 0;
  83. size_t nread = min(m_read_buffer->size - m_read_buffer_index, size);
  84. if (!data.write(m_read_buffer->data + m_read_buffer_index, nread))
  85. return -EFAULT;
  86. m_read_buffer_index += nread;
  87. compute_lockfree_metadata();
  88. if (m_unblock_callback && m_space_for_writing > 0)
  89. m_unblock_callback();
  90. return (ssize_t)nread;
  91. }
  92. }