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
27		void					MakeEmpty();
28
29private:
30		bool					fReaderWaiting;
31		bool					fWriterWaiting;
32		bool					fCancelRead;
33		bool					fCancelWrite;
34		sem_id					fReaderNotifier;
35		sem_id					fWriterNotifier;
36
37		BLocker					fReaderLocker;
38		BLocker					fWriterLocker;
39		BLocker					fDataLocker;
40
41		uint8 *					fBuffer;
42		size_t					fBufferSize;
43		size_t					fReadable;
44		int32					fReadPosition;
45		int32					fWritePosition;
46};
47
48#endif // STREAMING_RING_BUFFER_H
49