17 IN = ASE_SYSVAL_POLLINIT[0],
18 PRI = ASE_SYSVAL_POLLINIT[1],
19 OUT = ASE_SYSVAL_POLLINIT[2],
25 ERR = ASE_SYSVAL_POLLINIT[7],
26 HUP = ASE_SYSVAL_POLLINIT[8],
27 NVAL = ASE_SYSVAL_POLLINIT[9],
38class DispatcherSource;
49#if defined __G_LIB_H__ || defined DOXYGEN
50typedef ::GMainContext GlibGMainContext;
52struct GlibGMainContext;
66 int16 dispatch_priority_;
72 bool has_primary_L (
void);
74 void kill_sources_Lm (
void);
75 void unpoll_sources_U ();
106 bool flag_primary (
bool on);
108 template<
class BoolVo
idFunctor>
110 template<
class BoolVo
idFunctor>
113 template<
class BoolVo
idFunctor>
124 template<
class BoolVo
idFunctor>
127 template<
class BoolVo
idPollFunctor>
144 GlibGMainContext *gcontext_;
145 bool finishable_L ();
149 void kill_loops_Lm ();
150 bool iterate_loops_Lm (
LoopState&,
bool b,
bool d);
158 void quit (
int quit_code = 0);
170 enum Phase { NONE, COLLECT, PREPARE, CHECK, DISPATCH, DESTROY };
191 uint may_recurse_ : 1;
192 uint dispatching_ : 1;
193 uint was_dispatching_ : 1;
197 uint source_id () {
return loop_ ? id_ : 0; }
201 int64 *timeout_usecs_p) = 0;
204 virtual void destroy ();
209 void primary (
bool is_primary);
227 virtual void destroy ();
231 {
return make_shared (slot); }
239 int8 signum_ = 0, index_ = 0, shift_ = 0;
246 virtual void destroy ();
251 {
return make_shared (signum, slot); }
252 static void install_sigaction (
int8);
268 virtual void destroy ();
272 {
return make_shared (pid, slot); }
281 uint interval_msecs_;
282 bool first_interval_;
298 {
return make_shared (slot, initial_interval_msecs, repeat_interval_msecs); }
300 {
return make_shared (slot, initial_interval_msecs, repeat_interval_msecs); }
309 void construct (
const String &mode);
314 virtual void destroy ();
316 uint never_close_ : 1;
318 const uint oneshot_ : 1;
328 {
return make_shared (slot, fd, mode); }
330 {
return make_shared (slot, fd, mode); }
334template<
class BoolVo
idFunctor>
uint
337 typedef decltype (bvf()) ReturnType;
340 tsource->primary (
true);
344template<
class BoolVo
idFunctor>
uint
347 typedef decltype (bvf()) ReturnType;
349 return add (TimedSource::create (slot), priority);
352template<
class BoolVo
idFunctor>
uint
355 typedef decltype (bvf()) ReturnType;
361EventLoop::exec_dispatcher (
const DispatcherSlot &slot,
int priority)
363 return add (DispatcherSource::create (slot), priority);
369 return add (USignalSource::create (signum, slot), priority);
375 return add (SigchldSource::create (pid, slot), priority);
378template<
class BoolVo
idFunctor>
uint
381 typedef decltype (bvf()) ReturnType;
383 return add (TimedSource::create (slot, delay_ms, repeat_ms < 0 ? delay_ms : repeat_ms), priority);
386template<
class BoolVo
idPollFunctor>
uint
391 return add (PollFDSource::create (slot, fd, mode), priority);
EventLoop source for handler execution.
virtual bool dispatch(const LoopState &state)
Dispatch source, returns if it should be kept alive.
virtual bool prepare(const LoopState &state, int64 *timeout_usecs_p)
Prepare the source for dispatching (true return) or polling (false).
virtual bool check(const LoopState &state)
Check the source and its PollFD descriptors for dispatching (true return).
Loop object, polling for events and executing callbacks in accordance.
bool has_primary(void)
Indicates whether loop contains primary sources.
void wakeup()
Wakeup loop from polling.
bool clear_source(uint *id_pointer)
Remove source if id_pointer and *id_pointer are valid.
uint exec_idle(BoolVoidFunctor &&bvf)
Execute a callback with priority "idle", returning true repeats callback.
void remove(uint id)
Removes a source from loop, the source must be present.
uint exec_usignal(int8 signum, const USignalSlot &sl, int priority=PRIORITY_NOW -1)
Execute a single dispatcher callback for prepare, check, dispatch.
static const int16 PRIORITY_NORMAL
Normal importantance, GUI event processing, RPC.
uint exec_sigchld(int64_t pid, const SigchldSlot &vfunc, int priority=PRIORITY_NORMAL)
Execute a signal callback for prepare, check, dispatch.
static const int16 PRIORITY_CEILING
Internal upper limit, don't use.
uint exec_io_handler(BoolVoidPollFunctor &&bvf, int fd, const String &mode, int priority=PRIORITY_NORMAL)
Execute a callback after polling for mode on fd, returning true repeats callback.
bool exec_once(uint delay_ms, uint *once_id, const VoidSlot &vfunc, int priority=PRIORITY_NORMAL)
Execute a callback once on SIGCHLD for pid.
static const int16 PRIORITY_UPDATE
Mildly important, used for GUI updates or user information.
uint exec_timer(BoolVoidFunctor &&bvf, uint delay_ms, int64 repeat_ms=-1, int priority=PRIORITY_NORMAL)
Execute a callback after a specified timeout with adjustable initial timeout, returning true repeats ...
static const int16 PRIORITY_HIGH
Very important, used for timers or IO handlers.
MainLoop * main_loop() const
Get the main loop for this loop.
uint exec_now(BoolVoidFunctor &&bvf)
Execute a callback as primary source with priority "now" (highest), returning true repeats callback.
uint exec_callback(BoolVoidFunctor &&bvf, int priority=PRIORITY_NORMAL)
Execute a callback at user defined priority returning true repeats callback.
static const int16 PRIORITY_IDLE
Mildly important, used for background tasks.
static const int16 PRIORITY_ASCENT
Threshold for priorization across different loops.
bool try_remove(uint id)
Tries to remove a source, returns if successfull.
uint add(EventSourceP loop_source, int priority=PRIORITY_NORMAL)
Adds a new source to the loop with custom priority.
static const int16 PRIORITY_NEXT
Important, used for async operations and callbacks.
static const int16 PRIORITY_LOW
Unimportant, used when everything else done.
static const int16 PRIORITY_NOW
Most important, used for immediate async execution.
EventLoop source for callback execution.
bool primary() const
Indicate whether this source is primary.
void add_poll(PollFD *const pfd)
Add a PollFD descriptors for poll(2) and check().
void loop_remove()
Remove this source from its event loop if any.
MainLoop * main_loop() const
Get the main loop for this source.
void remove_poll(PollFD *const pfd)
Remove a previously added PollFD.
virtual bool dispatch(const LoopState &state)=0
Dispatch source, returns if it should be kept alive.
virtual bool check(const LoopState &state)=0
Check the source and its PollFD descriptors for dispatching (true return).
virtual bool prepare(const LoopState &state, int64 *timeout_usecs_p)=0
Prepare the source for dispatching (true return) or polling (false).
bool recursion() const
Indicates wether the source is currently in recursion.
bool may_recurse() const
Indicates if this source may recurse.
An EventLoop implementation that offers public API for running the loop.
EventLoopP create_sub_loop()
Creates a new event loop that is run as part of this main loop.
void quit(int quit_code=0)
Cause run() to return with quit_code.
static MainLoopP create()
Create a MainLoop shared pointer handle.
bool pending()
Check if iterate() needs to be called for dispatching.
int run()
Run loop iterations until a call to quit() or finishable becomes true.
bool set_g_main_context(GlibGMainContext *glib_main_context)
Set context to integrate with a GLib GMainContext loop.
void iterate_pending()
Call iterate() until no immediate dispatching is needed.
bool running()
Indicates if quit() has been called already.
bool finishable()
Indicates wether this loop has no primary sources left to process.
std::mutex & mutex()
Provide access to the mutex associated with this main loop.
bool iterate(bool block)
Perform one loop iteration and return whether more iterations are needed.
EventLoop source for IO callbacks.
virtual bool dispatch(const LoopState &state)
Dispatch source, returns if it should be kept alive.
virtual bool prepare(const LoopState &state, int64 *timeout_usecs_p)
Prepare the source for dispatching (true return) or polling (false).
virtual bool check(const LoopState &state)
Check the source and its PollFD descriptors for dispatching (true return).
EventLoop source for handler execution.
virtual bool dispatch(const LoopState &state)
Dispatch source, returns if it should be kept alive.
virtual bool prepare(const LoopState &state, int64 *timeout_usecs_p)
Prepare the source for dispatching (true return) or polling (false).
virtual bool check(const LoopState &state)
Check the source and its PollFD descriptors for dispatching (true return).
EventLoop source for timer execution.
virtual bool check(const LoopState &state)
Check the source and its PollFD descriptors for dispatching (true return).
virtual bool prepare(const LoopState &state, int64 *timeout_usecs_p)
Prepare the source for dispatching (true return) or polling (false).
virtual bool dispatch(const LoopState &state)
Dispatch source, returns if it should be kept alive.
EventLoop source for handler execution.
virtual bool dispatch(const LoopState &state)
Dispatch source, returns if it should be kept alive.
virtual bool prepare(const LoopState &state, int64 *timeout_usecs_p)
Prepare the source for dispatching (true return) or polling (false).
static void raise(int8 signum)
Flag a unix signal being raised, this function may be called from any thread at any time.
virtual bool check(const LoopState &state)
Check the source and its PollFD descriptors for dispatching (true return).
#define ASE_DEFINE_MAKE_SHARED(CLASS)
#define ASE_CLASS_NON_COPYABLE(ClassName)
Delete copy ctor and assignment operator.
The Anklang C++ API namespace.
uint64_t uint64
A 64-bit unsigned integer, use PRI*64 in format strings.
int16_t int16
A 16-bit signed integer.
uint8_t uint8
An 8-bit unsigned integer.
int8_t int8
An 8-bit signed integer.
int64_t int64
A 64-bit unsigned integer, use PRI*64 in format strings.
uint16_t uint16
A 16-bit unsigned integer.
uint32_t uint
Provide 'uint' as convenience type.
bool seen_primary
Useful as hint for primary source presence, MainLoop::finishable() checks exhaustively.
uint64 current_time_usecs
Equals timestamp_realtime() as of prepare() and check().
int64 timeout_usecs
Maximum timeout for poll, queried during prepare().
Mirrors struct pollfd for poll(3posix)
@ RDNORM
reading data will not block
@ HUP
file descriptor closed
@ WRNORM
writing data will not block
@ PRI
urgent data available
@ RDBAND
reading priority data will not block
@ OUT
writing data will not block
@ WRBAND
writing priority data will not block