C/C++ API Reference
Loading...
Searching...
No Matches
pw::stream::MpscWriter Class Reference

Overview

Writer for a multi-producer, single consumer stream.

This class has a default constructor that only produces disconnected writers. To connect writers, use CreateMpscStream(). Additional connected writers can be created by copying an existing one.

Each thread should have its own dedicated writer. This class is thread-safe with respect to the reader, but not with respect to itself. In particular, attempting to call Write() concurrently on different threads may cause result in a failure.

Inheritance diagram for pw::stream::MpscWriter:
pw::stream::NonSeekableWriter pw::containers::future::IntrusiveList< T >::Item pw::stream::Writer pw::stream::Stream

Classes

struct  Request
 

Public Types

using duration = std::optional< chrono::SystemClock::duration >
 
- Public Types inherited from pw::stream::Stream
enum  Whence : uint8_t { kBeginning = 0b001 , kCurrent = 0b010 , kEnd = 0b100 }
 Positions from which to seek. More...
 

Public Member Functions

 MpscWriter (const MpscWriter &other)
 
MpscWriteroperator= (const MpscWriter &other)
 
 MpscWriter (MpscWriter &&other)
 
MpscWriteroperator= (MpscWriter &&other)
 
bool connected () const
 Returns whether this object is connected to a reader.
 
size_t last_write () const
 Indicates how much data was sent in the last call to Write().
 
const duration & timeout () const
 Returns the optional maximum time elapsed before a Write() fails.
 
void SetTimeout (const duration &timeout)
 
void SetLimit (size_t limit)
 
void Close ()
 
- Public Member Functions inherited from pw::stream::Stream
constexpr bool readable () const
 
constexpr bool writable () const
 
constexpr bool seekable () const
 
constexpr bool seekable (Whence origin) const
 True if the stream supports seeking from the specified origin.
 
Result< ByteSpanRead (ByteSpan dest)
 
Result< ByteSpanRead (void *dest, size_t size_bytes)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
Result< ByteSpanReadExact (ByteSpan const buffer)
 
Status Write (ConstByteSpan data)
 
Status Write (const void *data, size_t size_bytes)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
Status Write (const std::byte b)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
Status Seek (ptrdiff_t offset, Whence origin=kBeginning)
 
size_t Tell ()
 
size_t ConservativeReadLimit () const
 
size_t ConservativeWriteLimit () const
 

Private Member Functions

size_t ConservativeLimit (LimitType type) const override
 
Status DoWrite (ConstByteSpan data) override
 Virtual Write() function implemented by derived classes.
 

Friends

void CreateMpscStream (MpscReader &, MpscWriter &)
 

Additional Inherited Members

- Static Public Attributes inherited from pw::stream::Stream
static constexpr size_t kUnlimited = std::numeric_limits<size_t>::max()
 Value returned from read/write limit if unlimited.
 
static constexpr size_t kUnknownPosition = std::numeric_limits<size_t>::max()
 Returned by Tell() if getting the position is not supported.
 
- Protected Types inherited from pw::stream::Stream
enum class  LimitType : bool { kRead , kWrite }
 
- Protected Member Functions inherited from pw::containers::future::IntrusiveList< T >::Item
constexpr Item ()=default
 

Member Function Documentation

◆ Close()

void pw::stream::MpscWriter::Close ( )

Disconnects this writer from its reader.

This method does nothing if the writer is not connected.

◆ ConservativeLimit()

size_t pw::stream::MpscWriter::ConservativeLimit ( LimitType  type) const
overrideprivatevirtual

Virtual function optionally implemented by derived classes that is used for ConservativeReadLimit() and ConservativeWriteLimit().

The default implementation returns kUnlimited or 0 depending on whether the stream is readable/writable.

Reimplemented from pw::stream::Stream.

◆ DoWrite()

Status pw::stream::MpscWriter::DoWrite ( ConstByteSpan  data)
overrideprivatevirtual

Virtual Write() function implemented by derived classes.

This method is not thread-safe with respect to itself. If multiple threads attempt to write concurrently using the same writer, those calls may fail. Instead, each thread should have its own writer.

Precondition
No other thread has called Write() on this object.

Implements pw::stream::Stream.

◆ SetLimit()

void pw::stream::MpscWriter::SetLimit ( size_t  limit)

Sets the maximum amount that can be written by this writer.

By default, writers can write an unlimited amount of data. This method can be used to set a limit, or remove it by providing a value of Stream::kUnlimited.

If a limit is set, the writer will automatically close once it has written that much data. The current number of bytes remaining until the limit is reached can be retrieved using ConservativeWriteLimit().

Parameters
[in]limitThe maximum amount that can be written by this writer.

◆ SetTimeout()

void pw::stream::MpscWriter::SetTimeout ( const duration &  timeout)

Set the timeout for writing to this stream.

After setting a timeout, if the given duration elapses while making a call to Write(),

embed:rst:inline :c:enumerator:`RESOURCE_EXHAUSTED` 

will be returned. If desired, a timeout should be set before calling Write(). Setting a timeout when a writer is awaiting notification from a reader will not affect the duration of that wait.

Note that setting a write timeout makes partial writes possible. For example, if a call to Write() of some length corresponds to 2 calls to Read() of half that length with an sufficient delay between the calls will result in the first half being written and read, but not the second. This differs from Stream::Write() which stipulates that no data is written on failure. If this happens, the length of the data written can be retrieved using last_write().

Generally, callers should use one of three approaches:

  1. Do not set a write timeout, and let writers block arbitrarily long until space is available or the reader is disconnected.
  2. Use only a single writer, and use last_write() to resend data.
  3. Structure the data being sent so that the reader can always read complete messages and avoid blocking or performing complex work mid-message.
Parameters
[in]timeoutThe duration to wait before returning an error.

Friends And Related Function Documentation

◆ CreateMpscStream

void CreateMpscStream ( MpscReader ,
MpscWriter  
)
friend

Creates a multi-producer, single consumer stream.

This method creates a stream by associating a reader and writer. Both are reset before being connected. This is the only way to connect a reader. Additional writers may be connected by copying the given writer after it is connected.

This method is thread-safe with respect to other MpscReader and MpscWriter methods. It is not thread-safe with respect to itself, i.e. callers must not make concurrent calls to CreateMpscStream() from different threads with the same objects.

Parameters
[out]readerThe reader to connect.
[out]writerThe writer to connect.

The documentation for this class was generated from the following file: