Base module

Synchronizer

Synchronizer enables any thread to communicate with the associated event loop via synchronized callbacks.

#include <icy/synchronizer.h>

class Synchronizer

Defined in src/base/include/icy/synchronizer.h:39

Inherits
Runner

This class inherits the Runner interface and may be used with any implementation that's powered by an asynchronous Runner.

List of all members

NameKindOwner
SynchronizerfunctionDeclared here
SynchronizerfunctionDeclared here
SynchronizerfunctionDeclared here
~SynchronizerfunctionDeclared here
postfunctionDeclared here
startfunctionDeclared here
startfunctionDeclared here
cancelfunctionDeclared here
closefunctionDeclared here
handlefunctionDeclared here
_handlevariableDeclared here
_postMutexvariableDeclared here
asyncfunctionDeclared here
RunnerfunctionInherited from Runner
~RunnerfunctionInherited from Runner
startfunctionInherited from Runner
runningfunctionInherited from Runner
cancelfunctionInherited from Runner
cancelledfunctionInherited from Runner
repeatingfunctionInherited from Runner
setRepeatingfunctionInherited from Runner
asyncfunctionInherited from Runner
tidfunctionInherited from Runner
waitForExitfunctionInherited from Runner
_contextvariableInherited from Runner
RunnerfunctionInherited from Runner
operator=functionInherited from Runner
RunnerfunctionInherited from Runner
operator=functionInherited from Runner

Inherited from Runner

KindNameDescription
functionRunner
function~Runner virtual
functionstart virtualStarts the asynchronous context with the given callback. The callback must remain valid for the lifetime of the Runner.
functionrunning constReturns true if the async context is currently running.
functioncancelSignals the async context to stop at the earliest opportunity.
functioncancelled constReturns true if the context has been cancelled. The implementation is responsible for exiting as soon as possible after cancellation.
functionrepeating constReturns true if the runner is in repeating mode.
functionsetRepeatingEnables or disables repeating mode. When repeating, the target function is invoked repeatedly until cancel() is called. This normalizes behaviour across thread-based and event-loop-based Runner implementations. Must be called before start().
functionasync virtual constReturns true if the implementation is thread-based.
functiontid constReturns the native thread ID of the thread running the async context.
functionwaitForExitBlocks until the async context exits or the timeout elapses. The context should be cancelled before calling this method. Must be called from outside the runner's own thread to avoid deadlock.
variable_contextShared pointer to the internal Context.
functionRunnerNonCopyable and NonMovable.
functionoperator=Deleted assignment operator.
functionRunnerDeleted constructor.
functionoperator=Deleted assignment operator.

Public Methods

ReturnNameDescription
SynchronizerCreates a synchronizer attached to the given event loop without a callback. Call start() separately to register the callback before using post().
SynchronizerCreates and immediately starts a synchronizer with a single callback function. The target is invoked from the event loop context each time post() is called.
Synchronizer inline explicitCreates and immediately starts a synchronizer with a variadic callback.
~Synchronizer virtualDestructor.
voidpostSend a synchronization request to the event loop. Call this each time you want the target method called synchronously. The synchronous method will be called on next iteration. This is not atomic, so do not expect a callback for every request.
voidstart inlineStarts the synchronizer with a variadic callback function. The callback is invoked from the event loop each time post() is called. Throws std::logic_error if already running or if the handle is null.
voidstart virtual overrideStarts the synchronizer with a std::function callback. Overrides Runner::start; delegates to the variadic start template.
voidcancel virtualCancels the synchronizer, signalling the associated callback to stop. A subsequent post() is needed to wake up the event loop so it can process the cancellation.
voidclose virtualCancels the synchronizer and closes the underlying uv_async_t handle. Safe to call multiple times; no-op if already closed.
uv::Handle< uv_async_t > &handleReturns a reference to the underlying libuv async handle.

Synchronizer

Synchronizer(uv::Loop * loop)

Defined in src/base/include/icy/synchronizer.h:47

Creates a synchronizer attached to the given event loop without a callback. Call start() separately to register the callback before using post().

Parameters

ParameterTypeDescription
loopuv::Loop *Event loop to attach the async handle to.

Synchronizer

Synchronizer(std::function< void()> target, uv::Loop * loop = uv::defaultLoop())

Defined in src/base/include/icy/synchronizer.h:53

Creates and immediately starts a synchronizer with a single callback function. The target is invoked from the event loop context each time post() is called.

Parameters

ParameterTypeDescription
targetstd::function< void()>Callback to invoke on each event loop wakeup.
loopuv::Loop *Event loop to attach the async handle to.

Synchronizer

inline explicit

template<typename Function, typename... Args> inline explicit Synchronizer(Function && func, Args &&... args, uv::Loop * loop = uv::defaultLoop())

Defined in src/base/include/icy/synchronizer.h:62

Creates and immediately starts a synchronizer with a variadic callback.

Parameters

ParameterTypeDescription
funcFunction &&Callable to invoke on each event loop wakeup.
argsArgs &&...Arguments forwarded to func.
loopuv::Loop *Event loop to attach the async handle to.

~Synchronizer

virtual

virtual ~Synchronizer()

Defined in src/base/include/icy/synchronizer.h:70

Destructor.


post

void post()

Defined in src/base/include/icy/synchronizer.h:76

Send a synchronization request to the event loop. Call this each time you want the target method called synchronously. The synchronous method will be called on next iteration. This is not atomic, so do not expect a callback for every request.


start

inline

template<typename Function, typename... Args> inline void start(Function && func, Args &&... args)

Defined in src/base/include/icy/synchronizer.h:86

Starts the synchronizer with a variadic callback function. The callback is invoked from the event loop each time post() is called. Throws std::logic_error if already running or if the handle is null.

Parameters

ParameterTypeDescription
funcFunction &&Callable to invoke on each event loop wakeup.
argsArgs &&...Arguments forwarded to func.

start

virtual override

virtual void start(std::function< void()> func) override

Defined in src/base/include/icy/synchronizer.h:132

Starts the synchronizer with a std::function callback. Overrides Runner::start; delegates to the variadic start template.

Reimplements

Parameters

ParameterTypeDescription
funcstd::function< void()>Callback invoked from the event loop on each post() call.

cancel

virtual

virtual void cancel()

Defined in src/base/include/icy/synchronizer.h:136

Cancels the synchronizer, signalling the associated callback to stop. A subsequent post() is needed to wake up the event loop so it can process the cancellation.


close

virtual

virtual void close()

Defined in src/base/include/icy/synchronizer.h:140

Cancels the synchronizer and closes the underlying uv_async_t handle. Safe to call multiple times; no-op if already closed.


handle

uv::Handle< uv_async_t > & handle()

Defined in src/base/include/icy/synchronizer.h:144

Returns a reference to the underlying libuv async handle.

Returns

Reference to the uv::Handle<uv_async_t>.

Protected Attributes

ReturnNameDescription
uv::Handle< uv_async_t >_handle
std::mutex_postMutexSerializes post() (any thread) against close(). The handle context uses a non-atomic intrusive refcount owned by the loop thread, so post() must never copy the context pointer; it reads the raw handle under this mutex instead.

_handle

uv::Handle< uv_async_t > _handle

Type: uv::Handle< uv_async_t >

Defined in src/base/include/icy/synchronizer.h:149


_postMutex

std::mutex _postMutex

Defined in src/base/include/icy/synchronizer.h:155

Serializes post() (any thread) against close(). The handle context uses a non-atomic intrusive refcount owned by the loop thread, so post() must never copy the context pointer; it reads the raw handle under this mutex instead.

Protected Methods

ReturnNameDescription
boolasync virtual const overrideReturns true if the implementation is thread-based.

async

virtual const override

virtual bool async() const override

Defined in src/base/include/icy/synchronizer.h:147

Returns true if the implementation is thread-based.

Returns

True for thread-backed runners, false for event-loop-driven runners.

Reimplements