Commit Graph
28 Commits
Author SHA1 Message Date
Tobias Brunner 705a20619f watcher: Make sure to re-activate the correct entry after a callback
Since the same FD may be added multiple times (e.g. with separate
callbacks for WATCHER_READ and WATCHER_WRITE), the previous check
might not have found the correct entry.  As the entry can't be removed
while in a callback, the pointer comparison is fine.
2023-04-27 13:52:34 +02:00
Tobias Brunner 34e9cdbcac watcher: Log when watched FDs are added, removed or updated 2023-04-27 13:52:27 +02:00
Tobias Brunner 0a806d9717 watcher: Only log number of managed FDs
Adding the internal notify FD might be confusing.
2023-04-27 13:45:32 +02:00
Tobias Brunner 7c657e78ff watcher: Avoid logging on level 1 while holding the mutex
This could be problematic in case loggers in some way rely on watcher_t
themselves.  This particular log message should rarely occur if at all,
but still avoid holding the mutex.
2023-04-27 13:45:32 +02:00
Tobias Brunner 19ef2aec15 Update copyright headers after acquisition by secunet 2022-06-28 10:22:56 +02:00
Tobias Brunner f6aafb3005 Fixed some typos, courtesy of codespell
Main change is the conversion from the British cancelling/-ed to the
American canceling/-ed.
2021-06-25 11:32:29 +02:00
Tobias Brunner 0d08959a04 watcher: Don't notify watcher if removed FD was not found
This can happen if a stream is used blocking exclusively (the FD is
never registered with watcher, but is removed in the stream's destructor
just in case it ever was - doing this conditionally would require an
additional flag in streams).  There may be no thread reading from
the read end of the notify pipe (e.g. in starter), causing the write
to the notify pipe to block after it's full.  Anyway, doing a relatively
expensive FD update is unnecessary if there were no changes.

Fixes #1453.
2017-10-10 11:13:53 +02:00
Tobias Brunner b27663399b watcher: Avoid allocations due to enumerators
Since the FD set could get rebuilt quite often this change avoids having
to allocate memory just to enumerate the registered FDs.
2016-10-04 10:12:18 +02:00
Martin Willi 8b0c9cf155 watcher: Check for cancellation if poll() fails with EINTR
With LinuxThreads, poll() is unfortunately no cancellation point. It seems
that poll gets woken up after cancellation, but we actively must check
for cancellation before re-entering poll to properly shut down the watcher
thread.
2015-12-04 08:01:15 +01:00
Martin Willi 015fb3134d watcher: Proper handle poll() POLLHUP/NVAL signaling
poll() may return POLLHUP or POLLNVAL for given file descriptors. To handle
these properly, we signal them to the EXCEPT watcher state, if registered. If
not, we call the read/write callbacks, so they can properly fail when trying
to read from or write to the file descriptor.
2014-11-28 12:50:34 +01:00
Martin Willi 6e1d3f3615 watcher: Use Windows read/write(2) wrappers instead of compile-conditions 2014-11-21 12:02:08 +01:00
Martin Willi 03bccc240c watcher: Use poll(2) instead of select 2014-11-21 12:02:07 +01:00
Martin Willi 5ea627bdfa watcher: Add a method to query the watcher state
This allows a user to check if the watcher is actually running, and potentially
perform read operations directly instead of relying on watcher.
2014-09-24 11:19:59 +02:00
Martin Willi bc1139480f watcher: Prevent race condition spawning multiple watcher threads
If file descriptors get added and removed in rapid succession, the active
watcher thread might not take notice of it and continues running. However, add()
spawns a watcher thread whenever a file descriptor is added to an empty set.
This could result in multiple watcher threads, which is fixed by a proper
check for running watchers.
2014-06-17 15:19:47 +02:00
Martin Willi 93f78d8225 watcher: Add Windows support
Instead of a pipe we use a TCP socketpair (can't select() a _pipe()), and
Windsock2 send/recv functions instead of read/write.

Currently supported (and required) are file descriptors provided by Winsock
only; we might use a separate mechanism for traditional file handles if
required (or switch to Windows events and WaitForMultipleObjects) for a future
version.
2014-06-04 15:52:59 +02:00
Martin Willi d2b07d3187 watcher: Don't wait for running callback once watcher thread cancelled
During shutdown, waiting for callbacks might never complete, as queued
callbacks might not get executed under certain conditions. Not the clean fix,
but works good enough for now.

Seen on Windows in vici tests.
2014-05-07 14:13:34 +02:00
Martin Willi d16d5a245f watcher: Avoid queueing multiple watcher callbacks at the same time
While we don't add FDs with an active callback to the watched FDSET, we still
can get notifications for callbacks active due the asynchronous processing
of the same.

To avoid queue multiple callbacks, we check for queued callbacks before
activating new ones.
2014-05-07 14:13:34 +02:00
Martin Willi 027cf7ddcf watcher: Don't complain if select() syscall got interrupted 2014-01-22 15:34:53 +01:00
Martin Willi 5ce3c9b15a watcher: Rebuild fdset when select() fails
This should make sure we refresh the fdset if a user closes an FD it just
removed. Some selects() seem to complain about the bad FD before signaling the
notification pipe.
2013-10-24 15:37:20 +02:00
Tobias Brunner 1f2d9c7688 watcher: Made notify array initialization compatible with older GCC versions 2013-07-25 16:57:42 +02:00
Martin Willi ea009869e9 watcher: use processors new execute_job() to notify FDs
Just queueing is problematic, as all threads might be busy waiting for events
that the queued (but never executed) job delivers.
2013-07-18 16:00:31 +02:00
Martin Willi 55240835b0 watcher: properly support multiple watch callback types for the same FD 2013-07-18 16:00:31 +02:00
Martin Willi d0c25a3f23 watcher: read multiple notifications if available
Use non-blocking I/O on the read end of the notify pipe. This also makes sure
the read does not block should select() signal data while there is none.
2013-07-18 16:00:31 +02:00
Martin Willi 58d0dadddc watcher: add some debugging statements 2013-07-18 16:00:29 +02:00
Martin Willi 91a2ae644c watcher: if the processor has no threads, execute the job with watcher thread
This is important during shutdown, where we might need to signal some FDs while
all idle threads are gone already.
2013-07-18 16:00:29 +02:00
Martin Willi a0e3a7363f watcher: unregister a watcher FD if its thread gets cancelled 2013-07-18 16:00:29 +02:00
Martin Willi a558ba16f3 watcher: release threads waiting in remove() when watcher thread gets cancelled
During daemon shutdown, users might call remove() after processor.set_threads(0)
has been called. This gets problematic, as a watch event might be unable
to signal completion when no threads are available anymore. Work around this
issue by cancelling waiters once processor.cancel() has been called.
2013-07-18 16:00:29 +02:00
Martin Willi 32b2a5e04b watcher: add a centralized an generic facility to monitor file descriptors 2013-07-18 16:00:27 +02:00