1/*
2 * Copyright 2009, Haiku, Inc.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		Michael Lotz <mmlr@mlotz.ch>
7 */
8#ifndef STREAMING_RING_BUFFER_H
9#define STREAMING_RING_BUFFER_H
10
11#include <OS.h>
12#include <SupportDefs.h>
13#include <Locker.h>
14
15class StreamingRingBuffer {
16public:
17								StreamingRingBuffer(size_t bufferSize);
18								~StreamingRingBuffer();
19
20		status_t				InitCheck();
21
22		// blocking read and write
23		int32					Read(void *buffer, size_t length,
24									bool onlyBlockOnNoData = false);
25		status_t				Write(const void *buffer, size_t length);
26
27private:
28		bool					_Lock();
29		void					_Unlock();
30
31		bool					fReaderWaiting;
32		bool					fWriterWaiting;
33		sem_id					fReaderNotifier;
34		sem_id					fWriterNotifier;
35
36		BLocker					fReaderLocker;
37		BLocker					fWriterLocker;
38		BLocker					fDataLocker;
39
40		uint8 *					fBuffer;
41		size_t					fBufferSize;
42		size_t					fReadable;
43		int32					fReadPosition;
44		int32					fWritePosition;
45};
46
47#endif // STREAMING_RING_BUFFER_H
48