68 Commits

Author SHA1 Message Date
David Herrmann
d3a0358797 eloop: add ev_timer_drain() helper
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>
2012-09-18 16:24:06 +02:00
David Herrmann
de306cee23 shl: move kmscon_hook_* to shl
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>
2012-09-18 10:36:37 +02:00
David Herrmann
d62b367210 shl: move llog to shl
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>
2012-09-18 09:18:50 +02:00
David Herrmann
aed1373bc6 shl: move dlist to shl_dlist.h
Instead of including dlist in the static library, we now move it to SHL.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
2012-09-15 20:11:00 +02:00
David Herrmann
715e247676 shl: move dlist implementation to shl_dlist_*
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>
2012-09-15 19:10:37 +02:00
David Herrmann
246595ab5a eloop: add pre-cbs
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>
2012-09-11 16:37:30 +02:00
David Herrmann
2534b99d9a eloop: call post-cbs on errors during dispatch
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>
2012-09-11 16:36:41 +02:00
David Herrmann
de9111b40a eloop: fix unregistering post-cbs
This is obviously a copy/paste error. This fixes the unregistering to
correctly work now.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
2012-09-11 16:34:56 +02:00
David Herrmann
962dfd8bea eloop: fix registering post-cb hooks
We actually have to register the hook-helper before we can add any hooks.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
2012-09-09 17:36:14 +02:00
David Herrmann
2f6b6a3d07 eloop: improve idle-source logic
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>
2012-09-09 16:49:31 +02:00
David Herrmann
cf318f710e eloop: add post-dispatch hook
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>
2012-09-09 16:14:50 +02:00
David Herrmann
014c468829 eloop: add ev_eloop_get_fd()
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>
2012-09-09 15:01:32 +02:00
David Herrmann
174722b400 build: make eloop independent of libkmscon-static
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>
2012-09-04 19:10:28 +02:00
David Herrmann
7ae88d4ed7 eloop: finish documentation
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>
2012-08-10 18:01:14 +02:00
David Herrmann
1715ec526a eloop: prevent recursive dispatching
We do not support recursive dispatching so prevent it explicitely.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
2012-07-21 20:20:09 +02:00
David Herrmann
f3609f6ed9 eloop: fix invalid memory access during dispatch
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>
2012-07-21 20:14:38 +02:00
David Herrmann
b9ce96941b eloop: fix signal creation
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>
2012-07-01 16:17:04 +02:00
David Herrmann
93b0d3e80c eloop: silence gcc warning
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>
2012-07-01 15:49:36 +02:00
David Herrmann
f1326b2728 static: move statically linked subsystems to static_*
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>
2012-06-24 20:35:22 +02:00
David Herrmann
0f19a8496d eloop: move to llog
Move every use of log_* to llog_* and make all objects aware of llog
objects.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
2012-06-24 18:53:46 +02:00
David Herrmann
43eb459cf6 eloop: require llog function in eloop-creation
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>
2012-06-24 17:57:38 +02:00
David Herrmann
163a9c9424 eloop: turn comment into non-gtkdoc comment
This comment does not use gtk-doc syntax so remove the extra *.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
2012-06-23 20:06:44 +02:00
David Herrmann
d3257fca31 eloop: fix eloop object never being freed
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>
2012-05-20 22:54:08 +02:00
David Herrmann
00ff46b701 eloop: add initial gtk-doc comments
Add gtk-doc compatible comments to most of the functions in eloop.c.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
2012-05-19 17:58:08 +02:00
David Herrmann
0b8da0ce50 eloop: dynamically reallocate dispatch cache
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>
2012-05-19 13:47:55 +02:00
David Herrmann
56d7932523 eloop: add more comments
Add comments describing each source and the nesting features.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
2012-05-19 13:33:05 +02:00
David Herrmann
2eb172acb4 eloop: use counter source for idle events
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>
2012-05-19 13:07:12 +02:00
David Herrmann
f0b92a1366 eloop: fix not returning new counter object
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>
2012-05-19 13:05:49 +02:00
David Herrmann
fb4b087bf2 eloop: forward timer HUP and I/O errors to caller
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>
2012-05-19 12:47:08 +02:00
David Herrmann
c9fe031cb7 eloop: forward EOF and I/O errors of counters to caller
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>
2012-05-19 12:30:05 +02:00
David Herrmann
f10ca03eab eloop: allow enabling/disabling counter sources
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>
2012-05-19 12:26:55 +02:00
David Herrmann
6a0742465b eloop: allow enabling/disabling timer sources
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>
2012-05-19 12:24:28 +02:00
David Herrmann
9703ee3502 eloop: correctly forward error codes
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>
2012-05-19 12:15:51 +02:00
David Herrmann
3ca31d922b eloop: allow enabling/disabling fd sources
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>
2012-05-18 17:35:18 +02:00
David Herrmann
3219b9ccfd eloop: convert idle sources to hooks
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>
2012-05-17 18:35:00 +02:00
David Herrmann
83ed40f089 eloop: move code
Restructure eloop code. This puts stuff more closely together if it is
related.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
2012-05-17 18:17:52 +02:00
David Herrmann
e37f4a9fba eloop: make timers create fd on initialization
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>
2012-05-17 17:40:14 +02:00
David Herrmann
69dcfe8586 eloop: take fd argument for fd-sources at initialization
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>
2012-05-17 17:03:46 +02:00
David Herrmann
31550f3180 eloop: add counter sources
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>
2012-05-17 16:48:43 +02:00
David Herrmann
32baeeec9d eloop: add ev_fd_is_bound() helper
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>
2012-05-17 16:33:47 +02:00
David Herrmann
f7b398a34a eloop: allow flushing an fd
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>
2012-05-05 17:13:42 +02:00
David Herrmann
2010d661dd eloop: fix memory leak for shared signals
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>
2012-04-22 15:47:40 +02:00
David Herrmann
e302a0bd96 eloop: use kmscon_dlist for shared signals
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>
2012-04-22 15:47:00 +02:00
David Herrmann
9a2e025475 eloop: wait for all childs
Move child-waiting into the eloop subsystem so all childs are always
correctly freed.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
2012-03-25 19:02:23 +02:00
David Herrmann
21f6bbb15d eloop: remove old signal sources
Remove the old non-shared signal sources in favor of shared-signals.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
2012-03-25 17:59:58 +02:00
David Herrmann
9e87b853ab use pthread_sigmask instead of sigprocmask
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>
2012-03-25 17:53:16 +02:00
David Herrmann
580b0a7992 eloop: add shared signal callbacks
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>
2012-03-25 17:39:55 +02:00
David Herrmann
ac9df3aac5 eloop: catch EINTR in epoll_wait()
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>
2012-03-25 16:45:39 +02:00
David Herrmann
83dc28fcc7 Fix time calculations
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>
2012-03-23 18:27:29 +01:00
David Herrmann
5f1f1710f9 eloop: correctly set ->exit
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>
2012-03-23 17:58:19 +01:00