Serenity Operating System
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
27#include <Kernel/DoubleBuffer.h>
28
29namespace Kernel {
30
31inline void DoubleBuffer::compute_lockfree_metadata()
32{
33 InterruptDisabler disabler;
34 m_empty = m_read_buffer_index >= m_read_buffer->size && m_write_buffer->size == 0;
35 m_space_for_writing = m_capacity - m_write_buffer->size;
36}
37
38DoubleBuffer::DoubleBuffer(size_t capacity)
39 : m_write_buffer(&m_buffer1)
40 , m_read_buffer(&m_buffer2)
41 , m_storage(KBuffer::create_with_size(capacity * 2, Region::Access::Read | Region::Access::Write, "DoubleBuffer"))
42 , m_capacity(capacity)
43{
44 m_buffer1.data = m_storage.data();
45 m_buffer1.size = 0;
46 m_buffer2.data = m_storage.data() + capacity;
47 m_buffer2.size = 0;
48 m_space_for_writing = capacity;
49}
50
51void DoubleBuffer::flip()
52{
53 ASSERT(m_read_buffer_index == m_read_buffer->size);
54 swap(m_read_buffer, m_write_buffer);
55 m_write_buffer->size = 0;
56 m_read_buffer_index = 0;
57 compute_lockfree_metadata();
58}
59
60ssize_t DoubleBuffer::write(const u8* data, ssize_t size)
61{
62 if (!size)
63 return 0;
64 ASSERT(size > 0);
65 LOCKER(m_lock);
66 ssize_t bytes_to_write = min(static_cast<size_t>(size), m_space_for_writing);
67 u8* write_ptr = m_write_buffer->data + m_write_buffer->size;
68 m_write_buffer->size += bytes_to_write;
69 compute_lockfree_metadata();
70 memcpy(write_ptr, data, bytes_to_write);
71 return bytes_to_write;
72}
73
74ssize_t DoubleBuffer::read(u8* data, ssize_t size)
75{
76 if (!size)
77 return 0;
78 ASSERT(size > 0);
79 LOCKER(m_lock);
80 if (m_read_buffer_index >= m_read_buffer->size && m_write_buffer->size != 0)
81 flip();
82 if (m_read_buffer_index >= m_read_buffer->size)
83 return 0;
84 ssize_t nread = min((ssize_t)m_read_buffer->size - (ssize_t)m_read_buffer_index, size);
85 memcpy(data, m_read_buffer->data + m_read_buffer_index, nread);
86 m_read_buffer_index += nread;
87 compute_lockfree_metadata();
88 return nread;
89}
90
91}