19#ifndef MTCORE_MTTHREAD_FUTURE_HPP
20#define MTCORE_MTTHREAD_FUTURE_HPP
24#include <condition_variable>
29 template<
typename T,
typename E =
void>
49 template<
typename T,
typename E =
void>
58 auto res =
state.acquire(alloc);
62 return Future{.state = res.value()};
89 std::condition_variable
cv = {};
94 auto l = std::unique_lock{
mux};
95 ensure(
value.empty(),
"CANNOT SET FUTURE VALUE MULTIPLE TIMES! CONSIDER USING A CANNEL OR INBOX!");
102 auto l = std::unique_lock{
mux};
103 while (
value.empty()) {
120 if (
auto err = state.
init(alloc); err.is_error()) {
123 auto futureArc = state.
acquire(alloc);
124 if (futureArc.is_error()) {
140 template<
typename T,
typename E>
143 std::condition_variable
cv = {};
148 auto l = std::unique_lock{
mux};
149 ensure(
value.empty(),
"CANNOT SET FUTURE VALUE MULTIPLE TIMES! CONSIDER USING A "
157 auto l = std::unique_lock{
mux};
158 while (
value.empty()) {
185 if (
auto err = state.
init(alloc); err.is_error()) {
189 if (
extra.is_error()) {
191 return extra.error();
194 state->extra =
extra.value();
195 auto futureArc = state.
acquire(alloc);
196 if (futureArc.is_error()) {
constexpr auto nullopt
Placeholder value for an empty Optional.
AllocationError
Error indicating failed allocation.
#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.
Thread-related namespace The methods and classes provided by this class are thread-safe Classes and m...
Represents a memory allocator Exact behavior depends on the underlying VTable used Should use the a_*...
Represents a value that may or may not exist (an "Optional" value) Similar concept to std::optional,...
Represents a Result that may have an error (error code) or a success value A type of "void" means the...
Result< Arc, ArcError > acquire(Allocator &alloc)
Acquires a new Arc reference (increments ref count by 1)
void deinit(Allocator &alloc)
Result< void, AllocationError > init(Allocator &alloc, Args... args)
Tries to initialize an ARC to point to a new object.
T wait()
Called by the future, don't call this directly.
void set_val(const T &v)
Sets the value of the associated future.
static Result< make_res, AllocationError > make(Allocator &alloc)
Makes a future and associated future state.
std::condition_variable cv
Represents the state-setter part of a future.
T wait()
Called by the future, don't call this directly.
static Result< make_res, AllocationError > make(Allocator &alloc, std::function< Result< Arc< E >, AllocationError >(Allocator &alloc, Arc< FutureState > &)> getExtra)
Makes a future and associated future state.
void deinit(Allocator &alloc)
std::condition_variable cv
Represents a value that will be made available in the future Can wait on the value for when it is rea...
T wait()
Blocks to wait for result.
Arc< FutureState< T, E > > state
Result< Future, ArcError > acquire(Allocator &alloc)
Acquires a new reference to the future (if you want multiple parts of your code to read it)
void deinit(Allocator &alloc)
Cleans up future.