1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 11:07:35 +00:00

Add internal locking to DoubleBuffer.

This commit is contained in:
Andreas Kling 2019-01-15 21:43:38 +01:00
parent e42f090ed3
commit 52c004eb53
2 changed files with 16 additions and 2 deletions

View file

@ -2,6 +2,7 @@
#include <AK/Types.h>
#include <AK/Vector.h>
#include <AK/Lock.h>
class DoubleBuffer {
public:
@ -14,16 +15,20 @@ public:
ssize_t write(const byte*, size_t);
ssize_t read(byte*, size_t);
bool is_empty() const { return m_read_buffer_index >= m_read_buffer->size() && m_write_buffer->is_empty(); }
bool is_empty() const { return m_empty; }
// FIXME: Isn't this racy? What if we get interrupted between getting the buffer pointer and dereferencing it?
size_t bytes_in_write_buffer() const { return m_write_buffer->size(); }
private:
void flip();
void compute_emptiness();
Vector<byte>* m_write_buffer { nullptr };
Vector<byte>* m_read_buffer { nullptr };
Vector<byte> m_buffer1;
Vector<byte> m_buffer2;
size_t m_read_buffer_index { 0 };
bool m_empty { true };
SpinLock m_lock;
};