1 /* 2 * Copyright 2022 Haiku, Inc. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef _MEMORY_RING_IO_H 6 #define _MEMORY_RING_IO_H 7 8 9 #include <pthread.h> 10 11 #include <DataIO.h> 12 #include <Locker.h> 13 14 15 class BMemoryRingIO : public BDataIO { 16 public: 17 BMemoryRingIO(size_t size); 18 virtual ~BMemoryRingIO(); 19 20 status_t InitCheck() const; 21 22 virtual ssize_t Read(void* buffer, size_t size); 23 virtual ssize_t Write(const void* buffer, size_t size); 24 25 status_t SetSize(size_t size); 26 void Clear(); 27 28 size_t BytesAvailable(); 29 size_t SpaceAvailable(); 30 size_t BufferSize(); 31 32 status_t WaitForRead( 33 bigtime_t timeout = B_INFINITE_TIMEOUT); 34 status_t WaitForWrite( 35 bigtime_t timeout = B_INFINITE_TIMEOUT); 36 37 void SetWriteDisabled(bool disabled); 38 bool WriteDisabled(); 39 40 private: 41 template<typename Condition> 42 status_t _WaitForCondition(bigtime_t timeout); 43 private: 44 pthread_mutex_t fLock; 45 pthread_cond_t fEvent; 46 47 uint8* fBuffer; 48 49 size_t fBufferSize; 50 size_t fWriteAtNext; 51 size_t fReadAtNext; 52 53 bool fBufferFull; 54 bool fWriteDisabled; 55 56 uint32 _reserved[4]; 57 }; 58 59 60 #endif // _MEMORY_RING_IO_H 61