MT Core (C++)
Core library for replacing C++ standard in project usage
Loading...
Searching...
No Matches
mtcore::thread::Channel< T, Size > Struct Template Reference

Channel is a message-passing primitive between two threads A channel has blocking and non-blocking (or auto timed out for a size of 0) methods Timeout methods are also present A channel will have a buffer size for how many messages can be queued up If there is a buffer size of 0, then a reader and writer must both be accessing the channel simultaneously for messages to be transmitted. More...

#include <channels.hpp>

Public Types

using Message = T
 

Public Member Functions

void close ()
 Closes the channel.
 
Result< void, ChannelTrySendErrorstry_send (const T &msg)
 Tries to send a message Does not block If there is no room or would have to block for locking, will fail If the channel is closed, will fail.
 
Result< void, ChannelSendBlockErrorssend_block (const T &msg)
 Will send a message (unless the channel is closed, then will fail) Will block as long as is needed to send a message.
 
template<typename Clock = std::chrono::steady_clock>
Result< void, ChannelSendBeforeErrorssend_before (const T &msg, const std::chrono::time_point< Clock > &when)
 Will send a message (unless the channel is closed, will fail) Will block up to a certain time point, then will fail if unable to send before that time point.
 
Result< Optional< T >, ChannelTryReceiveErrorstry_receive ()
 Tries to receive a message Will fail if it would have to block If the channel is empty and closed, will return nullopt instead of a message.
 
Optional< Messagereceive_block ()
 Receives a message from the channel Blocks indefinitely.
 
template<typename Clock>
Result< Optional< T >, ChannelReceiveBeforeErrorsreceive_before (const std::chrono::time_point< Clock > &when)
 Receives a message before a time point Blocks until a time point.
 

Detailed Description

template<typename T, size_t Size = 1>
struct mtcore::thread::Channel< T, Size >

Channel is a message-passing primitive between two threads A channel has blocking and non-blocking (or auto timed out for a size of 0) methods Timeout methods are also present A channel will have a buffer size for how many messages can be queued up If there is a buffer size of 0, then a reader and writer must both be accessing the channel simultaneously for messages to be transmitted.

Guaranteed to implement the ChannelLike trait

Locks:

  • 1 lock for entire structure

Condition Variables:

  • 2 condition variables (one to signal sending available, one to signal receiving available)

Non-Blocking Availability:

  • With Failure
Template Parameters
TMessage type
SizeChannel size

Definition at line 117 of file channels.hpp.

Member Typedef Documentation

◆ Message

template<typename T, size_t Size = 1>
using mtcore::thread::Channel< T, Size >::Message = T

Definition at line 128 of file channels.hpp.

Member Function Documentation

◆ close()

template<typename T, size_t Size = 1>
void mtcore::thread::Channel< T, Size >::close ( )
inline

Closes the channel.

Definition at line 133 of file channels.hpp.

133 {
134 auto lock = std::unique_lock(channelMux);
135 closed = true;
136 read_cond.notify_all();
137 send_cond.notify_all();
138 }
Channel is a message-passing primitive between two threads A channel has blocking and non-blocking (o...
Definition channels.hpp:117

◆ receive_before()

template<typename T, size_t Size = 1>
template<typename Clock>
Result< Optional< T >, ChannelReceiveBeforeErrors > mtcore::thread::Channel< T, Size >::receive_before ( const std::chrono::time_point< Clock > & when)
inlinenodiscard

Receives a message before a time point Blocks until a time point.

If does not receive before then, will fail

Template Parameters
ClockClock type for the time point
Parameters
whenWhen should it timeout
Returns
Message received or nullopt if channel is closed, or error if timed out

Definition at line 265 of file channels.hpp.

265 {
266 auto lock = std::unique_lock(channelMux, when);
267
268 if (!lock.owns_lock()) {
270 }
271
272 while (!closed && q.is_empty()) {
273 if (read_cond.wait_until(lock, when) == std::cv_status::timeout) {
275 }
276 }
277
278 ensure(lock.owns_lock(), "DID NOT LOCK");
279 return readImpl();
280 }
#define ensure(check,...)
Ensures that a check holds true, aborts the program if not true Will print error if the condition is ...
Error< Underlying > error(Underlying err)
Creates an error.
Definition result.hpp:425
Here is the call graph for this function:

◆ receive_block()

template<typename T, size_t Size = 1>
Optional< Message > mtcore::thread::Channel< T, Size >::receive_block ( )
inlinenodiscard

Receives a message from the channel Blocks indefinitely.

Returns
Returns message received, or nullopt if channel is closed

Definition at line 244 of file channels.hpp.

244 {
245 auto lock = std::unique_lock(channelMux);
246
247 while (!closed && q.is_empty()) {
248 read_cond.wait(lock);
249 }
250
251 ensure(lock.owns_lock(), "DID NOT LOCK");
252 return readImpl();
253 }

◆ send_before()

template<typename T, size_t Size = 1>
template<typename Clock = std::chrono::steady_clock>
Result< void, ChannelSendBeforeErrors > mtcore::thread::Channel< T, Size >::send_before ( const T & msg,
const std::chrono::time_point< Clock > & when )
inlinenodiscard

Will send a message (unless the channel is closed, will fail) Will block up to a certain time point, then will fail if unable to send before that time point.

Template Parameters
ClockClock for tracking time with
Parameters
msgMessage to send
whenWhen should there be a timeout
Returns

Definition at line 192 of file channels.hpp.

193 {
194 auto lock = std::unique_lock(channelMux, when);
195
196 if (!lock.owns_lock()) {
198 }
199
200 while (!closed && q.is_full()) {
201 if (send_cond.wait_until(lock, when) == std::cv_status::timeout) {
203 }
204 }
205
206 ensure(lock.owns_lock(), "DID NOT LOCK!");
207 if (sendImpl(msg).is_error()) {
209 }
210 return success();
211 }
Success< void > success()
Creates a successful void Result object.
Definition result.hpp:398
Here is the call graph for this function:

◆ send_block()

template<typename T, size_t Size = 1>
Result< void, ChannelSendBlockErrors > mtcore::thread::Channel< T, Size >::send_block ( const T & msg)
inlinenodiscard

Will send a message (unless the channel is closed, then will fail) Will block as long as is needed to send a message.

Parameters
msgMessage to send
Returns
success or failure

Definition at line 172 of file channels.hpp.

172 {
173 auto lock = std::unique_lock(channelMux);
174 while (!closed && q.is_full()) {
175 send_cond.wait(lock);
176 }
177
178 ensure(lock.owns_lock(), "DID NOT LOCK!");
179 return sendImpl(msg);
180 }

◆ try_receive()

template<typename T, size_t Size = 1>
Result< Optional< T >, ChannelTryReceiveErrors > mtcore::thread::Channel< T, Size >::try_receive ( )
inlinenodiscard

Tries to receive a message Will fail if it would have to block If the channel is empty and closed, will return nullopt instead of a message.

Returns
Message, nullopt, or failure. Message if available, nullopt if closed, failure if would have to block

Definition at line 221 of file channels.hpp.

221 {
222 auto lock = std::unique_lock(channelMux, std::try_to_lock_t{});
223
224 if (!lock.owns_lock()) {
226 }
227
228 if (q.is_empty()) {
229 if (closed) {
230 return readImpl();
231 }
233 }
234
235 ensure(lock.owns_lock(), "DID NOT LOCK!");
236 return readImpl();
237 }
Here is the call graph for this function:

◆ try_send()

template<typename T, size_t Size = 1>
Result< void, ChannelTrySendErrors > mtcore::thread::Channel< T, Size >::try_send ( const T & msg)
inlinenodiscard

Tries to send a message Does not block If there is no room or would have to block for locking, will fail If the channel is closed, will fail.

Parameters
msgMessage to send
Returns
success or failure

Definition at line 148 of file channels.hpp.

148 {
149 auto lock = std::unique_lock(channelMux, std::try_to_lock_t{});
150
151 if (!lock.owns_lock()) {
153 }
154
155 if (q.is_full()) {
157 }
158
159 ensure(lock.owns_lock(), "DID NOT LOCK!");
160 if (sendImpl(msg).is_error()) {
162 }
163 return success();
164 }
Here is the call graph for this function:

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