roo_io
API Documentation for roo_io
Loading...
Searching...
No Matches
ringpipe.h
Go to the documentation of this file.
1#pragma once
2
4#include "roo_io/status.h"
5#include "roo_threads.h"
6#include "roo_threads/condition_variable.h"
7#include "roo_threads/mutex.h"
8
9namespace roo_io {
10
11// A thread-safe pipe backed by a fixed-size in-memory buffer.
12class RingPipe {
13 public:
14 // Constructs a pipe with the given capacity.
15 RingPipe(size_t capacity);
16
17 // Writes up to 'len' bytes, but at least one byte, to the pipe. Blocks if
18 // necessary until space is available. Returns the count of bytes written.
19 // If the input or output end has been closed, returns zero.
20 size_t write(const byte* data, size_t len);
21
22 // Attempts to write exactly 'len' bytes to the pipe. Blocks if necessary
23 // until space is available. Returns the count of bytes written. The count
24 // may be less than 'len' if the input or output end gets closed.
25 size_t writeFully(const byte* data, size_t len);
26
27 // Returns the number of bytes that can be written without blocking. If the
28 // input or output end has been closed, returns zero.
29 size_t availableForWrite();
30
31 // Writes up to 'len' bytes, but no more than the current value of
32 // availableForWrite(), to the pipe. Returns the count of bytes written,
33 // which may be zero. If the input or output end has been closed, returns
34 // zero.
35 size_t tryWrite(const byte* data, size_t len);
36
37 // Reads up to 'len' bytes, but at least one byte, from the pipe. Blocks if
38 // necessary until data is available. Returns the count of bytes read. If the
39 // output end has been closed and no more data is available, returns zero.
40 // If the input end has been closed, returns zero.
41 size_t read(byte* data, size_t len);
42
43 // Returns the number of bytes that can be read without blocking. If the
44 // input end has been closed, returns zero.
45 size_t availableForRead();
46
47 // Reads up to 'len' bytes, but no more than the current value of
48 // availableForRead(), from the pipe. Returns the count of bytes read, which
49 // may be zero.
50 size_t tryRead(byte* data, size_t len);
51
52 // Returns the current status of the input end of the pipe. If the input end
53 // has been closed, returns kClosed. If the output end has been closed and
54 // no more data is available, returns kEndOfStream. Otherwise, returns kOk.
55 Status inputStatus() const;
56
57 // Returns the current status of the output end of the pipe. If the output end
58 // has been closed, returns kClosed. If the input end has been closed, returns
59 // kBrokenPipe. Otherwise, returns kOk.
60 Status outputStatus() const;
61
62 // Closes the input end of the pipe. Further write() or tryWrite() calls, as
63 // well as read() or tryRead() calls, will return zero. If there is a
64 // write() call blocked waiting for data, it will be unblocked and return
65 // zero.
66 void closeInput();
67
68 // Closes the output end of the pipe. Further write() or tryWrite() calls will
69 // return zero. If there is a read() call blocked waiting for data, it will be
70 // unblocked and return zero.
71 void closeOutput();
72
73 private:
74 RingBuffer buffer_;
75 mutable roo::mutex mutex_;
76 roo::condition_variable not_empty_;
77 roo::condition_variable not_full_;
78 bool input_closed_;
79 bool output_closed_;
80};
81
82} // namespace roo_io
size_t write(const byte *data, size_t len)
Definition ringpipe.cpp:8
size_t tryRead(byte *data, size_t len)
Definition ringpipe.cpp:67
size_t tryWrite(const byte *data, size_t len)
Definition ringpipe.cpp:38
Status inputStatus() const
Definition ringpipe.cpp:77
Status outputStatus() const
Definition ringpipe.cpp:88
size_t availableForRead()
Definition ringpipe.cpp:62
size_t read(byte *data, size_t len)
Definition ringpipe.cpp:48
size_t availableForWrite()
Definition ringpipe.cpp:33
size_t writeFully(const byte *data, size_t len)
Definition ringpipe.cpp:21
Definition byte.h:6
roo::basic_string_view< CharT, Traits > basic_string_view
Definition string_view.h:8
Status
Definition status.h:7