DoubleBuffer.cpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. ASSERT(m_read_buffer_index == m_read_buffer->size);
  50. swap(m_read_buffer, m_write_buffer);
  51. m_write_buffer->size = 0;
  52. m_read_buffer_index = 0;
  53. compute_lockfree_metadata();
  54. }
  55. ssize_t DoubleBuffer::write(const UserOrKernelBuffer& data, size_t size)
  56. {
  57. if (!size)
  58. return 0;
  59. ASSERT(size > 0);
  60. LOCKER(m_lock);
  61. size_t bytes_to_write = min(size, m_space_for_writing);
  62. u8* write_ptr = m_write_buffer->data + m_write_buffer->size;
  63. m_write_buffer->size += bytes_to_write;
  64. compute_lockfree_metadata();
  65. if (!data.read(write_ptr, bytes_to_write))
  66. return -EFAULT;
  67. return (ssize_t)bytes_to_write;
  68. }
  69. ssize_t DoubleBuffer::read(UserOrKernelBuffer& data, size_t size)
  70. {
  71. if (!size)
  72. return 0;
  73. ASSERT(size > 0);
  74. LOCKER(m_lock);
  75. if (m_read_buffer_index >= m_read_buffer->size && m_write_buffer->size != 0)
  76. flip();
  77. if (m_read_buffer_index >= m_read_buffer->size)
  78. return 0;
  79. size_t nread = min(m_read_buffer->size - m_read_buffer_index, size);
  80. if (!data.write(m_read_buffer->data + m_read_buffer_index, nread))
  81. return -EFAULT;
  82. m_read_buffer_index += nread;
  83. compute_lockfree_metadata();
  84. return (ssize_t)nread;
  85. }
  86. }