86fa1c21e1
This commit introduces a simple thread-safe ring buffer implementation based on top of BDataIO. The main use case for this class will be to implement shared buffers between threads for the upcoming refactoring of Services Kit. Change-Id: I526bc044b28c91496ad996fabebe538e75647f2c Reviewed-on: https://review.haiku-os.org/c/haiku/+/2966 Reviewed-by: Jacob Secunda <secundaja@gmail.com> Reviewed-by: waddlesplash <waddlesplash@gmail.com> Reviewed-by: Adrien Destugues <pulkomandy@pulkomandy.tk> Tested-by: Commit checker robot <no-reply+buildbot@haiku-os.org>
61 lines
1.2 KiB
C++
61 lines
1.2 KiB
C++
/*
|
|
* Copyright 2022 Haiku, Inc. All rights reserved.
|
|
* Distributed under the terms of the MIT License.
|
|
*/
|
|
#ifndef _MEMORY_RING_IO_H
|
|
#define _MEMORY_RING_IO_H
|
|
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <DataIO.h>
|
|
#include <Locker.h>
|
|
|
|
|
|
class BMemoryRingIO : public BDataIO {
|
|
public:
|
|
BMemoryRingIO(size_t size);
|
|
virtual ~BMemoryRingIO();
|
|
|
|
status_t InitCheck() const;
|
|
|
|
virtual ssize_t Read(void* buffer, size_t size);
|
|
virtual ssize_t Write(const void* buffer, size_t size);
|
|
|
|
status_t SetSize(size_t size);
|
|
void Clear();
|
|
|
|
size_t BytesAvailable();
|
|
size_t SpaceAvailable();
|
|
size_t BufferSize();
|
|
|
|
status_t WaitForRead(
|
|
bigtime_t timeout = B_INFINITE_TIMEOUT);
|
|
status_t WaitForWrite(
|
|
bigtime_t timeout = B_INFINITE_TIMEOUT);
|
|
|
|
void SetWriteDisabled(bool disabled);
|
|
bool WriteDisabled();
|
|
|
|
private:
|
|
template<typename Condition>
|
|
status_t _WaitForCondition(bigtime_t timeout);
|
|
private:
|
|
pthread_mutex_t fLock;
|
|
pthread_cond_t fEvent;
|
|
|
|
uint8* fBuffer;
|
|
|
|
size_t fBufferSize;
|
|
size_t fWriteAtNext;
|
|
size_t fReadAtNext;
|
|
|
|
bool fBufferFull;
|
|
bool fWriteDisabled;
|
|
|
|
uint32 _reserved[4];
|
|
};
|
|
|
|
|
|
#endif // _MEMORY_RING_IO_H
|