5 #include <condition_variable> 17 template <
class,
class,
template<
class>
class,
class>
55 using type = std::tuple<sender_type&, receiver_type&>;
64 return type{sender, receiver};
72 template <
class Actor>
127 this->lock_ = {this->actor->
channel->lock, std::defer_lock};
141 this->lock_.unlock();
148 this->actor->
channel->condition.notify_one();
158 this->actor->
channel->condition.wait(this->lock_);
168 template <
class Pred>
171 this->actor->
channel->condition.wait(
173 std::forward<Pred>(pred)
186 template <
class Rep,
class Period>
187 std::cv_status
waitFor(
const std::chrono::duration<Rep, Period>& time){
189 return this->actor->
channel->condition.wait_for(this->lock_, time);
202 template <
class Rep,
class Period,
class Pred>
203 bool waitFor(
const std::chrono::duration<Rep, Period>& time, Pred&& pred){
205 return this->actor->
channel->condition.wait_for(
208 std::forward<Pred>(pred)
221 template <
class T,
class Lock = std::mutex,
template<
class>
class Guard = std::lock_guard,
class Container = std::deque<T>>
285 template <
size_t I,
class U,
class L,
template<
class>
class G,
class C>
286 friend auto std::get(
Channel<U, L, G, C>& chan) -> META::tuple_element_t<I, typename Channel<U, L, G, C>::transport_type>;
319 return transport_traits::factory(this->sender(), this->receiver());
326 Channel() : lock{}, condition{}, queue{}, sender_{*
this}, receiver_{*
this} {
340 return this->queue.size();
355 return this->size() <= 0;
363 return this->receiver_;
372 return this->sender_;
382 template <
class U = value_type>
384 return this->sender() << std::forward<U>(value);
393 template <
class U = value_type>
395 return this->receiver() >> value;
409 template <
class U,
class T,
class L,
template<
class>
class G,
class C>
411 channel << std::forward<U>(value);
424 template <
class U,
class T,
class L,
template<
class>
class G,
class C>
425 void operator<<(U& value, Channel<T, L, G, C>& channel){
434 template <
class Chan>
501 template <
class... Args>
504 this->channel->
queue.emplace(std::forward<Args>(args)...);
521 template <
class U = value_type>
523 return this->doPush(std::forward<U>(value));
532 template <
class... Args>
534 return this->doPush(std::forward<Args&&>(args)...);
540 template <
class U = value_type>
542 return this->push(std::forward<U>(value));
548 template <
class U = value_type>
550 return this->push(std::forward<U>(value));
556 template <
class U = value_type>
562 template <
class U = value_type>
566 template <
class Chan>
646 return !this->channel->
queue.empty();
649 auto value = this->channel->
queue.front();
650 this->channel->
queue.pop();
662 template <
class Rep,
class Period>
664 bool isEmpty = this->traits.
waitFor(duration, [&]{
665 return !this->channel->
queue.empty();
671 auto value = this->channel->
queue.front();
672 this->channel->
queue.pop();
690 template <
class Rep,
class Period>
692 return this->tryPop(duration);
698 template <
class Rep,
class Period>
700 return this->tryPop(duration);
708 template <
class U = value_type>
710 value = std::move(this->receive());
729 template <
size_t I,
class T,
class Lock,
template<
class>
class Guard,
class Container>
731 return std::get<I>(channel.interface());
734 template <
class T,
class Lock,
template<
class>
class Guard,
class Container>
735 struct tuple_size<
scl::async::Channel<T, Lock, Guard, Container>>
736 : std::tuple_size<typename scl::async::Channel<T, Lock, Guard, Container>::transport_type>{
void lock()
Lock the channel.
size_type size() const
Retrieve the size of the queue.
typename sender_traits::queue_type queue_type
The type of queue used in the channel.
typename channel_type::queue_type queue_type
The type of the queue used in the channel.
bool isEmpty() const
Determine whether or not the channel is currently empty.
optional_type tryPop(const std::chrono::duration< Rep, Period > &duration)
Wait for a given duration before popping.
typename receiver_traits::queue_type queue_type
The type of queue used by the channel.
ChannelReceiver(channel_type &chan)
Construct a receiver from its channel.
ChannelSender & enqueue(U &&value)
Alias for ChannelSender::push.
typename receiver_traits::lock_type lock_type
The type of locks used by the traits.
sender_traits traits
The traits for the sender.
std::unique_lock< lock_type > lock_
The handle to the channel's lock.
channel_type * channel
A (safe) pointer to the channel.
void unlock()
Unlock the channel.
Chan channel_type
The type of the channel.
Global namespace of the SCL.
typename sender_traits::lock_type lock_type
The type of lock used in the sender.
lock_type lock
The lock being used for the channel's queue.
void operator>>(U &&value, Channel< T, L, G, C > &channel)
Forward a value into a channel.
std::mutex lock_type
lock_type The type of locks used in the channel
typename receiver_traits::value_type value_type
The type of values used by the channel.
sender_type & operator<<(U &&value)
Forward a value into the channel.
receiver_type & receiver()
Get a receiver for this channel.
std::lock_guard< L > guard_type
A channel to use for asynchronous communication of data in a queued manner.
Traits for actors that interact with the channel.
Traits for the implementation of the transport of information in a scl::async::Channel.
std::queue< message_type, std::deque< message_type > > queue_type
queue_type The type of the channel's queue
void notify()
Notify the channel.
static type factory(sender_type &sender, receiver_type &receiver)
Create a transport payload from a sender and a receiver.
typename channel_type::template guard_type< L > guard_type
value_type receive()
Alias for ChannelReceiver::receive.
typename queue_type::value_type value_type
The type of values exposed through the channel.
ChannelSender & send(U &&value)
Alias for ChannelSender::push.
std::tuple< sender_type &, receiver_type & > type
The type of transport payload.
channel_actor_traits(actor_type &act)
Construct a trait from a pointer to its actor.
ChannelSender & queue(U &&value)
Alias for ChannelSender::push.
std::cv_status waitFor(const std::chrono::duration< Rep, Period > &time)
Wait for a specific duration for the channel.
std::condition_variable_any condition
The condition used to wait for the channel's queue.
receiver_type receiver_
The receiver for this channel.
void waitUntil(Pred &&pred)
Wait for the channel until a condition is met.
ChannelSender & pushEmplace(Args &&... args)
Construct and push in-place the value.
optional_type tryReceive(const std::chrono::duration< Rep, Period > &duration)
Alias for ChannelReceiver::tryPop.
typename channel_type::receiver_type receiver_type
The type of the receiver.
typename transport_traits::type transport_type
The type of the transport payload.
typename sender_traits::value_type value_type
The type of values used in the channel.
typename channel_type::value_type value_type
The type of values transmitted through channels.
typename queue_type::size_type size_type
The type used to quantize the size of the queue.
typename channel_type::sender_type sender_type
sender_type The type of the sender
constexpr scl::utils::Placeholder _
typename actor_type::channel_type channel_type
The type of the channel.
value_type pop()
Get a value from the channel.
ChannelSender & push(U &&value)
Forward a value into the queue.
ChannelSender & doPush(Args &&... args)
Implementation details to push data onto the channel.
typename sender_traits::template guard_type< L > guard_type
The type of guards used in the sender.
sender_type sender_
The sender for this channel.
transport_type interface()
Implementation details to get a receiver and sender for this channel.
Class used to send data to a Channel.
channel_type * channel
A (safe) pointer to the channel.
actor_type * actor
A (safe) pointer to the actor.
typename channel_type::lock_type lock_type
The type of lock used by the channel.
ChannelSender & operator<<(U &&value)
Alias for ChannelSender::push.
void wait()
Wait for the channel.
Channel()
Construct a channel.
value_type dequeue()
Alias for ChannelReceiver::receive.
size_type length() const
Alias for Channel::size.
ChannelSender(channel_type &chan)
Construct a sender from a (safe) pointer to its channel.
receiver_traits traits
The traits for this receiver.
optional_type tryDeque(const std::chrono::duration< Rep, Period > &duration)
Alias for ChannelReceiver::tryPop.
receiver_type & operator>>(U &value)
Extract a value from the channel.
sender_type & sender()
Get a sender for this channel.
Chan channel_type
The type of the channel.
typename receiver_traits::template guard_type< L > guard_type
ChannelReceiver & operator>>(U &value)
Alias for ChannelReceiver::receive.
queue_type queue
The underlying queue used to store data.
bool waitFor(const std::chrono::duration< Rep, Period > &time, Pred &&pred)
Wait for a given period of time until the predicate is satisfied.