This helper reads the current expiration-count from a timer. This can be
used when waking up from an idle-period or similar to reset the timer.
Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
This is the last static helper that is moved so as a next step we should
get rid of the "static" library entirely.
Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
We might argue whether llog should be kept separate, however, shl is a
loose pile of headers and sources so pushing llog into it seems
reasonable.
Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
Shl (Static helper library) is the new name of all static helpers in
kmscon that might be shared between different applications.
Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
pre-cbs are similar to post-cbs but they are called _before_ the actual
dispatching takes place. It can be used to perform any actions before
going to sleep.
Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
If the dispatch files or is skipped for whatever reason, we should
nevertheless call the post-cbs to avoid any integration errors.
Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
Instead of using an ev_counter object internally, we now directly create a
new eventfd object. This avoids adding/removing the fd from the epoll-set
constantly when adding/removing idle sources. This should increase
idle-source speed considerably.
Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
When integrating other event-sources or event-loops into eloop which do
not provide FD-abstractions, we often want to check for specific
conditions before returning from the dispatch-callback or going to sleep.
Therefore, you can now register a post-dispatch-cb and check whether your
alien event source is firing. If it is, simply push an idle-source into
the event-loop and it will fire during the next dispatch without going to
sleep.
Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
This new helper allows retrieving a single file descriptor which
represents the whole event loop. This is very useful if you want to use
your own event loop in combination with libeloop.
Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
This removes the nasty dependency to libkmscon-static for libeloop.
Otherwise, we might end up with GLESv2 dependencies here which is really
not what we want.
Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
Finish source code documentation. gtk-doc isn't working very well so this
probably needs to be re-done, but not now...
Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
We use a temporary array of "to-be-dispatched" event sources while
dispatching. However, we incorrectly set the "count" variable so sources
may access invalid memory when removing themself from the event loop.
Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
The previous fix incorrectly registered new signals always to the last
found signal which is definitely incorrect. Therefore, we now correctly
traverse the list and register new signals as new signals.
Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
Gcc warns about an uninitialized variable (which technically is correct).
However, there is really no way this can really happen. But to make gcc
happy (and to enhance code readability) we now depend on !sig whether the
list-search was successful.
Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
Small stuff that is not worth putting into a separate library is no moved
into the "static" library which is statically linked into all our
subsystems.
Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
When creating a new eloop object, we now require an lloop function so we
can perform conditional logging.
Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
We must _never_ take a reference to ourself in a constructor. Otherwise,
the refcnt will be >1 which means if the user calls *_unref() the object
will not get freed.
Therefore, do not add the counter object used for idle sources directly to
the event loop. Instead, add it when the first idle source is registered
and remove it when the last source is removed. This will slightly slow
down performance of idle-sources. However, the whole eloop is not
optimized for speed, yet, so we don't care for now.
Reported-by: Ran Benita <ran234@gmail.com>
Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
We do not maintain event caches so we must make sure that every event
source gets dispatched. If we call epoll_wait() and our buffer gets
filled everytime, then there might be an event source that does not get
dispatched because it is always above the buffer range. Therefore, we now
dynamically increase the cache size when it once gets filled up.
This gets critical if we handle thousands of clients or fds, however, our
use case is limited to some system resources and hence does not suffer
here.
Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
epoll has the great feature that the poll object is an fd itself. However,
if we want to use idle-sources, we couldn't add them to the epoll set.
Now, we use a counter source for all idle sources so if we add a single
event-loop as source to another event loop, the idle sources will get
dispatched correctly. Furthermore, we now longer block after handling idle
sources but instead now correctly run idle sources every next round
without sleeping for fd events in between.
Unregister idle sources to avoid hogging the CPU.
Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
We forgot to actually store the pointer to the new counter object in the
\out variable. Fix this now.
Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
Similar to counter callbacks we now call timer callbacks with 0 as
argument on errors and disable the timer source.
Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
Instead of silently dropping read() errors we now disable the counter and
call the user-supplied callback with 0 as argument so they can react on
errors.
Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
Similar to fd and timer sources we now also support disabling counter
sources via similar functions.
Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
Similar to the fd_enable/disable functions we now also allow the same
operations on timer sources.
Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
Instead of ignoring epoll errors we should forward them to the caller. The
caller can then still decide to ignore errors.
Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
We sometimes want to be able to enable/disable an fd-source without
allocating memory (for a short period, for instance). Therefore, introduce
two new functions to enable and disable an fd source.
Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
Instead if implementing complex idle sources we now provide a hook so
other subsystems can register callbacks.
This simplifies the code a lot and doesn't drop any major functionality.
Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
Similar to other event sources we now initialize internal data on timer
creation instead of when the source is added to the loop.
Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
When creating a new fd-source you must supply the file descriptor
directly. You cannot delay this to the time when you add the fd to the
event loop.
This simplifies the logic and allows much smoother handling in the event
loop core.
Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
Counter sources are based on the eventfd syscall of linux. Internally, is
uses a 64bit counter which is initialized to 0 and can be increased by the
caller. Whenever the value is non-zero, the fd is marked readable and we
call our callback. We read the 64bit integer (which resets it to 0) and
pass the current value to the callback.
This can be used to implement cross-process notification methods or to
have idle-sources as valid file-descriptors in an epoll set which could be
exported to other applications that are not compliant to our event loop.
Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
This helper returns true if the fd is bound to an eloop object, otherwise
false is returned.
Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
Sometimes one wants to remove all pending events for an fd. The new
ev_eloop_flush_fd() call allows this in a safe way.
Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
A shared signal owns an eloop_fd object. This has a reference of its
connected eloop. Therefore, we must free a shared signal to drop a
reference to the connected eloop and cannot postpone this to
eloop-destruction. Otherwise, the eloop will never get destroyed.
Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
Move to new list-implementation instead of the single-linked list. This
allows removal of elements in O(1).
Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
pthread is already in our vmem due to our dependencies so link to it
explicitly and use pthread_sigmask to avoid buggy sigprocmask in
multi-threaded applications.
Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
Our current signal source has the problem that a single signal event is
only delivered to one random registered source. This is the design of the
signalfd kernel feature. However, for signals like SIGCHLD it is often
desired to have multiple callbacks. Hence, while keeping the old
signal-source, we now also allow a shared signal source. It is registered
the same way as the old signal source but uses a shared hook.
We may remove the old source so we don't use it in the new implementation.
There are still problems when multiple eloops are used in a single
process and you register the same shared-signum in both eloops. However,
such a design is bad anyway, so we don't care.
Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
epoll_wait() returns EINTR even if using SA_RESTART for signals.
Therefore, catch EINTR and set count to zero.
Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
eloop.c and log.c use struct timeval to calculate time-diffs but
incorrectly use "-" instead of "+" as the value is already negative.
They also use unsigned integers so fix both occurences.
Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
When exiting a child eloop we should forward the exit to the parent. Also,
we should reset ->exit on ev_eloop_run() otherwise we cannot run multiple
times.
Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>