MT Core (C++)
Core library for replacing C++ standard in project usage
Loading...
Searching...
No Matches
mtcore::thread Namespace Reference

Thread-related namespace The methods and classes provided by this class are thread-safe Classes and methods provided outside of this class are not thread-safe (with the exception of malloc_alloc) This requires linking mtcore_thread to your application. More...

Classes

struct  Arc
 Automic Reference Count. More...
 
struct  BeforeOption
 Option to add a timeout on select to ensure a path runs before a timeout If the timeout is hit, a timeout error will be returned from the select Will call a callback on timeout. More...
 
struct  BeforeOptionNoCallback
 Option to add a timeout on select to ensure a path runs before a timeout If the timeout is hit, a timeout error will be returned from the select. More...
 
struct  broadcaster
 A broadcaster is able to send messages to multiple channels, with each channel having its own lock Sending messages requires locking the entire broadcaster. More...
 
struct  BroadcastSubscription
 A subscription to a broadcaster. More...
 
struct  Channel
 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...
 
struct  DebugAllocator
 Allocator state object for shared debug allocation Must live longer (and at the same memory address) than all allocators returned by allocator() More...
 
struct  DynamicArenaAllocator
 Allocator state object for shared dynamic arena allocation Must live longer (and at the same memory address) than all allocators returned by allocator() More...
 
struct  FixedArenaAllocator
 Allocator state object for shared fixed arena allocation Must live longer (and at the same memory address) than all allocators returned by allocator() More...
 
struct  FixedBufferAllocator
 Allocator state object for shared fixed buffer allocation Must live longer (and at the same memory address) than all allocators returned by allocator() More...
 
struct  Future
 Represents a value that will be made available in the future Can wait on the value for when it is ready. More...
 
struct  FutureState
 Represents the state-setter part of a future. More...
 
struct  FutureState< T, void >
 Represents the state-setter part of a future. More...
 
struct  Inbox
 Inter-thread communication primitive to send messages between threads Has a growable Queue. More...
 
struct  Publisher
 Represents a pub/sub publisher which can publish messages of type T Supports blocking and non-blocking publishing. More...
 
struct  ReceiveOption
 Select option to receive a message and calls a callback on receive. More...
 
struct  Select
 Underlying type for a select statement Use the "select" method directly for instantiation. More...
 
struct  Select< CurOption, Options... >
 A stage of a select case Use the "select" method directly for instantiation. More...
 
struct  Select<>
 Base case for a select statement Use the "select" method directly for instantiation. More...
 
struct  SendOption
 Select option to send a message. More...
 
struct  SendOptionNoCallback
 Select option to send a message without calling a callback on send. More...
 
struct  Subscription
 Represents a pub/sub subscription which can receive messages of type T Under the hood, each publisher maintains a message broker which stores all messages Subscribers will hold an Arc to the broker and will retrieve messages from the broker Due to the broker being shared, it does require more locks for both reads and writes This means it has a pretty low throughput when there is lots of contention The primary advantage is it has a much more convenient API for more non-blocking operations or timed blocks The reason this happens is that publishers are given greater insight into each subscription, and can better avoid partial writes. More...
 
struct  WeakArc
 Weak Automic Reference Count. More...
 

Concepts

concept  SelectOption
 Checks if something qualifies as an option to a select statement Allows defining custom options and cases.
 
concept  Closeable
 Checks if something is closeable (e.g.
 
concept  Sender
 Checks if messages can be sent to something (e.g.
 
concept  Receiver
 Checks if messages can be received from something (e.g.
 
concept  SenderBlock
 Checks if messages can be sent in a blocking manner.
 
concept  ReceiverBlock
 Checks if messages can be received in a blocking manner.
 
concept  SenderBefore
 Checks if messages can be sent in a blocking manner with a timeout.
 
concept  ReceiverBefore
 Checks if messages can be received in a blocking manner with a timeout.
 
concept  SenderGrow
 Checks if messages can be sent to something (e.g.
 
concept  SenderBlockGrow
 Checks if messages can be sent to something (e.g.
 
concept  SenderBeforeGrow
 Checks if messages can be sent to something (e.g.
 
concept  HasClosedError
 Checks if an error type has a "Has Closed" error called CHANNEL_CLOSED.
 
concept  SenderReceiver
 Checks if non-blocking sends and receives are supported.
 
concept  SenderReceiverBlock
 Checks if blocking sends and receives are supported.
 
concept  SenderReceiverBefore
 Checks if blocking with timeout sends and receives are supported.
 
concept  SenderReceiverGrow
 Checks if non-blocking sends and receives are supported with allocation to grow.
 
concept  SenderReceiverBlockGrow
 Checks if blocking sends and receives are supported with allocation to grow.
 
concept  SenderReceiverBeforeGrow
 Checks if blocking with timeout sends and receives are supported with allocation to grow.
 
concept  ChannelLike
 Checks if something qualifies as a channel Channels can:
 
concept  ChannelLikeGrow
 Checks if something qualifies as a growing channel Growing channels can:
 

Enumerations

enum class  ArcError { ArcError::CannotAcquire = -1 }
 Error when cannot acquire an ARC. More...
 
enum class  BroadcastSubscribeError { BroadcastSubscribeError::CHANNEL_CLOSED , BroadcastSubscribeError::ALLOCATION_FAILED , BroadcastSubscribeError::CANNOT_ACQUIRE }
 Errors when getting a broadcast subscription. More...
 
enum class  ChannelSendBlockErrors { ChannelSendBlockErrors::CHANNEL_CLOSED }
 Errors for when blocking sends fail. More...
 
enum class  ChannelTrySendErrors { ChannelTrySendErrors::CHANNEL_CLOSED , ChannelTrySendErrors::COULD_NOT_LOCK , ChannelTrySendErrors::NO_ROOM }
 Errors for when non-blocking sends fail. More...
 
enum class  ChannelSendBeforeErrors { ChannelSendBeforeErrors::CHANNEL_CLOSED , ChannelSendBeforeErrors::TIMEOUT }
 Errors for when timed blocking sends fail. More...
 
enum class  ChannelTryReceiveErrors { ChannelTryReceiveErrors::COULD_NOT_LOCK , ChannelTryReceiveErrors::MESSAGE_NOT_READY }
 Errors for when non-blocking receives fail Note: If the channel is closed, a nullopt will be returned instead of an error. More...
 
enum class  ChannelReceiveBeforeErrors { ChannelReceiveBeforeErrors::TIMEOUT }
 Errors for when timed receives fail Note: If the channel is closed, a nullopt will be returned instead of an error. More...
 
enum class  ChannelGrowTrySendErrors { ChannelGrowTrySendErrors::CHANNEL_CLOSED , ChannelGrowTrySendErrors::COULD_NOT_LOCK , ChannelGrowTrySendErrors::NO_ROOM , ChannelGrowTrySendErrors::ALLOCATION_FAILED }
 Errors for when trying to do non-blocking send (with possible allocation) fails. More...
 
enum class  ChannelGrowSendBlockErrors { ChannelGrowSendBlockErrors::CHANNEL_CLOSED , ChannelGrowSendBlockErrors::ALLOCATION_FAILED }
 Errors for when trying to do blocking send (with possible allocation) fails. More...
 
enum class  ChannelGrowSendBeforeErrors { ChannelGrowSendBeforeErrors::CHANNEL_CLOSED , ChannelGrowSendBeforeErrors::TIMEOUT , ChannelGrowSendBeforeErrors::ALLOCATION_FAILED }
 Errors for when trying to do timed blocking send (with possible allocation) fails. More...
 
enum class  PubSubSendBlockGrowErrors { PubSubSendBlockGrowErrors::CHANNEL_CLOSED , PubSubSendBlockGrowErrors::PARTIAL_SEND , PubSubSendBlockGrowErrors::ALLOCATION_FAILED }
 Errors for when blocking publishing fails. More...
 
enum class  PubSubSendBeforeGrowsError { PubSubSendBeforeGrowsError::CHANNEL_CLOSED , PubSubSendBeforeGrowsError::TIMEOUT , PubSubSendBeforeGrowsError::PARTIAL_SEND , PubSubSendBeforeGrowsError::ALLOCATION_FAILED }
 Errors for when timed blocking publishing fails. More...
 
enum class  PubSubTrySendGrowsError {
  PubSubTrySendGrowsError::CHANNEL_CLOSED , PubSubTrySendGrowsError::COULD_NOT_LOCK , PubSubTrySendGrowsError::NO_ROOM , PubSubTrySendGrowsError::PARTIAL_SEND ,
  PubSubTrySendGrowsError::ALLOCATION_FAILED
}
 Errors for when non-blocking publishing fails. More...
 
enum class  SelectErrors { SelectErrors::SELECT_TIMEOUT , SelectErrors::DEADLOCK_DETECTED , SelectErrors::NOT_READY }
 Errors for when a select statement fails SELECT_TIMEOUT and DEADLOCK_DETECTED are the only ones which will be bubbled up NOT_READY is an internal error to indicate loop continuation. More...
 
enum class  SelectOptionErrors { SelectOptionErrors::ATTEMPT_FAILED , SelectOptionErrors::SKIP , SelectOptionErrors::TIMEOUT }
 Errors indicating control flow from a select option ATTEMPT_FAILED indicates to try the next option SKIP indicates to forever skip this option TIMEOUT indicates that a timeout error should be raised and select execution should stop. More...
 

Functions

DynamicArenaAllocator dynamic_arena (Allocator *root, size_t elementSizeHint, size_t elementCountHint) noexcept
 Creates a dynamically growing arena allocator This arena allocator will grow if it runs out of space The allocator will clean all pages on deinit, and reallocate them again when the next allocation happens It does iterate through all pages, so it's \(O(p)\) to allocate Use maxElementHint (max element size) and maxCountHint (max number of elements per page) to hint at page size.
 
FixedArenaAllocator fixed_arena (Slice< u8 > bytes) noexcept
 Creates basic arena allocator which just pushes more state onto the end Calling deinit will simply reset the stack pointer.
 
FixedBufferAllocator fixed_buff (Slice< u8 > bytes) noexcept
 Creates an allocator that works on a fixed buffer of bytes.
 
DebugAllocator debug_alloc () noexcept
 Creates a memory allocator using the standard's malloc under the hood This allocator will do basic tracking of memory allocations Once all instances of std_alloc are de-initialized, it will then check to see if any memory is being leakd.
 
template<SelectOption... Options>
Result< void, SelectErrorsselect (Options... opts)
 Creates a "select statement" similar to Go's select statement, except this statement runs on thread-based primitives instead of coroutine-based primitives.
 
template<Sender S, typename Callback>
SendOption< S, Callback > send_to (S &to, typename S::Message msg, const Callback &callback)
 Creates a select case which will try to send a message to a sender Option will call a callback on successful send.
 
template<Sender S>
SendOptionNoCallback< S > send_to (S &to, typename S::Message msg)
 Creates a select case which will try to send a message to a sender.
 
template<typename Clock, typename Duration, typename Callback>
BeforeOption< Clock, Duration, Callback > before (const std::chrono::time_point< Clock, Duration > &tp, const Callback &callback)
 The before option will timeout a select if an option is not hit before the target time point.
 
template<typename Clock, typename Duration>
BeforeOptionNoCallback< Clock, Duration > before (const std::chrono::time_point< Clock, Duration > &tp)
 The before option will timeout a select if an option is not hit before the target time point.
 
template<typename Rep, typename Per, typename Callback>
auto timeout (const std::chrono::duration< Rep, Per > &tp, const Callback &callback)
 Does a timeout relative to current time (e.g.
 
template<typename Rep, typename Per>
auto timeout (const std::chrono::duration< Rep, Per > &tp)
 Does a timeout relative to current time (e.g.
 
template<Receiver R, typename Callback>
ReceiveOption< R, Callback > receive_from (R &from, const Callback &callback)
 Creates a receive option for select which will execute when a message is ready.
 

Variables

template<typename T>
constexpr bool has_closed_error = impl::IsHasClosedError<T>::value
 Checks if an error type has a recognized closed error.
 
template<typename T>
constexpr bool is_closeable = impl::IsCloseable<T>::value
 Checks if a type is closeable.
 
template<typename T>
constexpr bool is_sender = impl::IsSender<T>::value
 Checks if a type can be sent to.
 
template<typename T>
constexpr bool is_sender_grow = impl::IsSenderGrow<T>::value
 Checks if a type can be sent to with growing.
 
template<typename T>
constexpr bool is_sender_block = impl::IsSenderBlock<T>::value
 Checks if a type can be sent to (blocking)
 
template<typename T>
constexpr bool is_sender_block_grow = impl::IsSenderBlockGrow<T>::value
 Checks if a type can be sent to (blocking) with growing.
 
template<typename T>
constexpr bool is_sender_before = impl::IsSenderBefore<T>::value
 Checks if a type can be sent to (blocking, timeout)
 
template<typename T>
constexpr bool is_sender_before_grow = impl::IsSenderBeforeGrow<T>::value
 Checks if a type can be sent to (blocking, timeout) with growing.
 
template<typename T>
constexpr bool is_receiver = impl::IsReceiver<T>::value
 Checks if a type can be received from.
 
template<typename T>
constexpr bool is_receiver_block = impl::IsReceiverBlock<T>::value
 Checks if a type can be received from (blocking)
 
template<typename T>
constexpr bool is_receiver_before = impl::IsReceiverBefore<T>::value
 Checks if a type can be received from (blocking, timeout)
 
template<typename T>
constexpr bool is_sender_receiver = impl::IsSenderReceiver<T>::value
 Checks if a type can be sent to and received from.
 
template<typename T>
constexpr bool is_sender_receiver_grow = impl::IsSenderReceiverGrow<T>::value
 Checks if a type can be sent to and received from with growing send.
 
template<typename T>
constexpr bool is_sender_receiver_block = impl::IsSenderReceiverBlock<T>::value
 Checks if a type can be sent to and received from (blocking)
 
template<typename T>
constexpr bool is_sender_receiver_block_grow = impl::IsSenderReceiverBlockGrow<T>::value
 Checks if a type can be sent to and received from (blocking) with growing send.
 
template<typename T>
constexpr bool is_sender_receiver_before = impl::IsSenderReceiverBefore<T>::value
 Checks if a type can be sent to and received from (blocking, timeout)
 
template<typename T>
constexpr bool is_sender_receiver_before_grow = impl::IsSenderReceiverBeforeGrow<T>::value
 Checks if a type can be sent to and received from (blocking, timeout) with growing send.
 
template<typename T>
constexpr bool is_channel_like = impl::IsChannelLike<T>::value
 Checks if a type is channel-like.
 
template<typename T>
constexpr bool is_channel_like_grow = impl::IsChannelLikeGrow<T>::value
 Checks if a type is growing channel-like.
 
template<typename T>
constexpr bool is_select_option = impl::IsSelectOption<T>::value
 Checks if a type can be used in a select statement as an option.
 

Detailed Description

Thread-related namespace The methods and classes provided by this class are thread-safe Classes and methods provided outside of this class are not thread-safe (with the exception of malloc_alloc) This requires linking mtcore_thread to your application.

Not available for WASM builds