2018-12-03 03:51:44 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Types.h>
|
|
|
|
#include <AK/Vector.h>
|
2019-03-16 15:18:22 +03:00
|
|
|
#include <Kernel/Lock.h>
|
2018-12-03 03:51:44 +03:00
|
|
|
|
|
|
|
class DoubleBuffer {
|
|
|
|
public:
|
|
|
|
DoubleBuffer()
|
|
|
|
: m_write_buffer(&m_buffer1)
|
|
|
|
, m_read_buffer(&m_buffer2)
|
2019-02-07 13:12:23 +03:00
|
|
|
, m_lock("DoubleBuffer")
|
2018-12-03 03:51:44 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-02-25 23:19:57 +03:00
|
|
|
ssize_t write(const byte*, ssize_t);
|
|
|
|
ssize_t read(byte*, ssize_t);
|
2018-12-03 03:51:44 +03:00
|
|
|
|
2019-01-15 23:43:38 +03:00
|
|
|
bool is_empty() const { return m_empty; }
|
2018-12-03 03:51:44 +03:00
|
|
|
|
2019-01-15 23:43:38 +03:00
|
|
|
// FIXME: Isn't this racy? What if we get interrupted between getting the buffer pointer and dereferencing it?
|
2019-02-25 23:19:57 +03:00
|
|
|
ssize_t bytes_in_write_buffer() const { return (ssize_t)m_write_buffer->size(); }
|
2019-01-15 11:17:22 +03:00
|
|
|
|
2018-12-03 03:51:44 +03:00
|
|
|
private:
|
|
|
|
void flip();
|
2019-01-15 23:43:38 +03:00
|
|
|
void compute_emptiness();
|
2018-12-03 03:51:44 +03:00
|
|
|
|
|
|
|
Vector<byte>* m_write_buffer { nullptr };
|
|
|
|
Vector<byte>* m_read_buffer { nullptr };
|
|
|
|
Vector<byte> m_buffer1;
|
|
|
|
Vector<byte> m_buffer2;
|
2019-02-25 23:19:57 +03:00
|
|
|
ssize_t m_read_buffer_index { 0 };
|
2019-01-15 23:43:38 +03:00
|
|
|
bool m_empty { true };
|
2019-01-17 18:25:02 +03:00
|
|
|
Lock m_lock;
|
2018-12-03 03:51:44 +03:00
|
|
|
};
|