24#ifndef LELY_EV_FIBER_EXEC_HPP_
25#define LELY_EV_FIBER_EXEC_HPP_
38using FiberFlag = util::FiberFlag;
65 ::std::size_t stack_size = 0, ::std::size_t max_unused = 0) {
94 ::std::size_t max_unused,
bool& already) {
98 already = result != 0;
126 other.exec_ =
nullptr;
134 swap(exec_, other.exec_);
149template <
class T,
class E>
169throw_fiber_error(
const char* what_arg,
int ev) {
176 throw ::std::system_error(::std::make_error_code(::std::errc::timed_out),
179 throw ::std::system_error(
180 ::std::make_error_code(::std::errc::resource_unavailable_try_again),
183 throw ::std::system_error(
184 ::std::make_error_code(::std::errc::not_enough_memory), what_arg);
189class FiberMutexBase {
191 FiberMutexBase() =
default;
193 FiberMutexBase(
const FiberMutexBase&) =
delete;
194 FiberMutexBase(FiberMutexBase&& other) =
delete;
196 FiberMutexBase& operator=(
const FiberMutexBase&) =
delete;
197 FiberMutexBase& operator=(FiberMutexBase&& other) =
delete;
220 detail::throw_fiber_error(
"try_lock", ev);
250 FiberRecursiveMutex() {
253 detail::throw_fiber_error(
"FiberRecursiveMutex", ev);
258class FiberConditionVariable {
260 FiberConditionVariable() {
265 FiberConditionVariable(
const FiberConditionVariable&) =
delete;
266 FiberConditionVariable(FiberConditionVariable&& other) =
delete;
268 FiberConditionVariable& operator=(
const FiberConditionVariable&) =
delete;
269 FiberConditionVariable& operator=(FiberConditionVariable&& other) =
delete;
289 wait(std::unique_lock<FiberMutex>& lock) {
295 template <
class Predicate>
297 wait(std::unique_lock<FiberMutex>& lock, Predicate pred) {
298 while (!pred())
wait(lock);
An abstract task executor. This class is a wrapper around #ev_exec_t*.
void notify_all() noexcept
void notify_one() noexcept
void wait(std::unique_lock< FiberMutex > &lock, Predicate pred)
void wait(std::unique_lock< FiberMutex > &lock)
Executor get_inner_executor() const noexcept
FiberExecutor(Executor inner_exec)
Convenience class providing a RAII-style mechanism to ensure the calling thread can be used by fiber ...
FiberThread(FiberFlag flags, ::std::size_t stack_size, ::std::size_t max_unused, bool &already)
Initializes the calling thread for use by fiber executors, if it was not already initialized.
FiberThread(FiberFlag flags=static_cast< FiberFlag >(0), ::std::size_t stack_size=0, ::std::size_t max_unused=0)
Initializes the calling thread for use by fiber executors, if it was not already initialized.
~FiberThread()
Finalizes the calling thread and prevents further use by fiber executors, unless another instance of ...
The base class for mutexes suitable for use in fibers.
void throw_errc(int errc=get_errc())
Throws an std::system_error exception corresponding to the specified or current (thread-specific) nat...
This header file is part of the event library; it contains the C++ interface for the abstract task ex...
This header file is part of the utilities library; it contains the C++ interface for the fiber implem...
This header file is part of the event library; it contains the fiber executor, mutex and condition va...
@ ev_fiber_nomem
Indicates that the requested operation failed because it was unable to allocate memory.
@ ev_fiber_busy
Indicates that the requested operation failed because a resource requested by a test and return funct...
@ ev_fiber_error
Indicates that the requested operation failed.
@ ev_fiber_success
Indicates that the requested operation succeeded.
@ ev_fiber_timedout
Indicates that the time specified in the call was reached without acquiring the requested resource.
int ev_fiber_mtx_lock(ev_fiber_mtx_t *mtx)
Suspends the currently running fiber until it locks the fiber mutex at mtx.
void ev_fiber_thrd_fini(void)
Finalizes the calling thread and prevents further use by fiber executors.
int ev_fiber_mtx_init(ev_fiber_mtx_t *mtx, int type)
Creates a fiber mutex object with properties indicated by type, which must have one of the four value...
int ev_fiber_thrd_init(int flags, size_t stack_size, size_t max_unused)
Initializes the calling thread for use by fiber executors.
int ev_fiber_mtx_unlock(ev_fiber_mtx_t *mtx)
Unlocks the fiber mutex at mtx.
@ ev_fiber_mtx_plain
A fiber mutex type that supports neither timeout nor recursive locking.
@ ev_fiber_mtx_recursive
A fiber mutex type that supports recursive locking.
void ev_fiber_cnd_destroy(ev_fiber_cnd_t *cond)
Releases all resources used by the fiber condition variable at cond.
void ev_fiber_mtx_destroy(ev_fiber_mtx_t *mtx)
Releases any resources used by the fiber mutex at mtx.
int ev_fiber_cnd_signal(ev_fiber_cnd_t *cond)
Unblocks one of the fibers that are blocked on the fiber condition variable at cond at the time of th...
int ev_fiber_cnd_broadcast(ev_fiber_cnd_t *cond)
Unblocks all of the fibers that are blocked on the fiber condition variable at cond at the time of th...
int ev_fiber_cnd_init(ev_fiber_cnd_t *cond)
Creates a fiber condition variable.
void ev_fiber_exec_destroy(ev_exec_t *exec)
Destroys a fiber executor.
void ev_fiber_await(ev_future_t *future)
Suspends a currently running fiber until the specified future becomes ready (or is cancelled).
int ev_fiber_cnd_wait(ev_fiber_cnd_t *cond, ev_fiber_mtx_t *mtx)
Atomically unlocks the fiber mutex at mtx and endeavors to block until the fiber condition variable a...
int ev_fiber_mtx_trylock(ev_fiber_mtx_t *mtx)
Endeavors to lock the fiber mutex at mtx.
ev_exec_t * ev_fiber_exec_create(ev_exec_t *inner_exec)
Creates a fiber executor.
ev_exec_t * ev_fiber_exec_get_inner_exec(const ev_exec_t *exec)
Returns a pointer to the inner executor of a fiber executor.
void fiber_await(Future< T, E > f) noexcept
void fiber_yield() noexcept
Yields a currently running fiber.
This header file is part of the event library; it contains the C++ interface for the futures and prom...
A synchronization primitive (similar to the standard C11 condition variable) that can be used to bloc...
A synchronization primitive (similar to the standard C11 mutex) that can be used to protect shared da...