2012-07-18 16:01:30 +02:00
|
|
|
/*
|
|
|
|
* \brief poll() implementation
|
|
|
|
* \author Josef Soentgen
|
2019-04-26 12:47:33 +02:00
|
|
|
* \author Emery Hemingway
|
2012-07-18 16:01:30 +02:00
|
|
|
* \date 2012-07-12
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2019-04-26 12:47:33 +02:00
|
|
|
* Copyright (C) 2010-2019 Genode Labs GmbH
|
2012-07-18 16:01:30 +02:00
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
2017-02-20 13:23:52 +01:00
|
|
|
* under the terms of the GNU Affero General Public License version 3.
|
2012-07-18 16:01:30 +02:00
|
|
|
*/
|
|
|
|
|
2017-02-10 22:38:37 +01:00
|
|
|
/* Libc includes */
|
2012-07-18 16:01:30 +02:00
|
|
|
#include <libc-plugin/plugin_registry.h>
|
|
|
|
#include <libc-plugin/plugin.h>
|
|
|
|
#include <sys/poll.h>
|
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
/* internal includes */
|
libc: split task.cc into multiple files
This patch is the first step of re-organizing the internal structure of
the libc. The original version involved many direct calls of global
functions (often with side effects) across compilation units, which
made the control flow (e.g., the initialization sequence) hard to
follow.
The new version replaces those ad-hoc interactions with dedicated
interfaces (like suspend.h, resume.h, select.h, current_time.h). The
underlying facilities are provided by the central Libc::Kernel and
selectively propagated to the various compilation units. The latter is
done by a sequence of 'init_*' calls, which eventually will be replaced
by constructor calls.
The addition of new headers increases the chance for name clashes with
existing (public) headers. To disambiguate libc-internal header files
from public headers, this patch moves the former into a new 'internal/'
subdirectory. This makes the include directives easier to follow and the
libc's source-tree structure more tidy.
There are still a few legacies left, which cannot easily be removed
right now (e.g., because noux relies on them). However, the patch moves
those bad apples to legacy.h and legacy.cc, which highlights the
deprecation of those functions.
Issue #3497
2019-09-18 20:19:10 +02:00
|
|
|
#include <internal/errno.h>
|
|
|
|
#include <internal/file.h>
|
|
|
|
#include <internal/init.h>
|
|
|
|
#include <internal/suspend.h>
|
|
|
|
|
2019-09-19 20:37:17 +02:00
|
|
|
using namespace Libc;
|
libc: split task.cc into multiple files
This patch is the first step of re-organizing the internal structure of
the libc. The original version involved many direct calls of global
functions (often with side effects) across compilation units, which
made the control flow (e.g., the initialization sequence) hard to
follow.
The new version replaces those ad-hoc interactions with dedicated
interfaces (like suspend.h, resume.h, select.h, current_time.h). The
underlying facilities are provided by the central Libc::Kernel and
selectively propagated to the various compilation units. The latter is
done by a sequence of 'init_*' calls, which eventually will be replaced
by constructor calls.
The addition of new headers increases the chance for name clashes with
existing (public) headers. To disambiguate libc-internal header files
from public headers, this patch moves the former into a new 'internal/'
subdirectory. This makes the include directives easier to follow and the
libc's source-tree structure more tidy.
There are still a few legacies left, which cannot easily be removed
right now (e.g., because noux relies on them). However, the patch moves
those bad apples to legacy.h and legacy.cc, which highlights the
deprecation of those functions.
Issue #3497
2019-09-18 20:19:10 +02:00
|
|
|
|
2019-09-19 20:37:17 +02:00
|
|
|
|
|
|
|
static Suspend *_suspend_ptr;
|
libc: split task.cc into multiple files
This patch is the first step of re-organizing the internal structure of
the libc. The original version involved many direct calls of global
functions (often with side effects) across compilation units, which
made the control flow (e.g., the initialization sequence) hard to
follow.
The new version replaces those ad-hoc interactions with dedicated
interfaces (like suspend.h, resume.h, select.h, current_time.h). The
underlying facilities are provided by the central Libc::Kernel and
selectively propagated to the various compilation units. The latter is
done by a sequence of 'init_*' calls, which eventually will be replaced
by constructor calls.
The addition of new headers increases the chance for name clashes with
existing (public) headers. To disambiguate libc-internal header files
from public headers, this patch moves the former into a new 'internal/'
subdirectory. This makes the include directives easier to follow and the
libc's source-tree structure more tidy.
There are still a few legacies left, which cannot easily be removed
right now (e.g., because noux relies on them). However, the patch moves
those bad apples to legacy.h and legacy.cc, which highlights the
deprecation of those functions.
Issue #3497
2019-09-18 20:19:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
void Libc::init_poll(Suspend &suspend)
|
|
|
|
{
|
|
|
|
_suspend_ptr = &suspend;
|
|
|
|
}
|
2012-07-18 16:01:30 +02:00
|
|
|
|
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
extern "C" __attribute__((weak))
|
|
|
|
int poll(struct pollfd fds[], nfds_t nfds, int timeout_ms)
|
2012-07-18 16:01:30 +02:00
|
|
|
{
|
2019-09-19 20:37:17 +02:00
|
|
|
using Genode::uint64_t;
|
2012-07-18 16:01:30 +02:00
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
if (!fds || nfds == 0) return Errno(EINVAL);
|
2018-07-10 14:58:17 +02:00
|
|
|
|
2019-09-19 20:37:17 +02:00
|
|
|
struct Check : Suspend_functor
|
2019-04-26 12:47:33 +02:00
|
|
|
{
|
|
|
|
pollfd *_fds;
|
|
|
|
nfds_t const _nfds;
|
2018-07-10 14:58:17 +02:00
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
int nready { 0 };
|
|
|
|
|
|
|
|
Check(struct pollfd fds[], nfds_t nfds)
|
|
|
|
: _fds(fds), _nfds(nfds) { }
|
|
|
|
|
|
|
|
bool suspend() override
|
|
|
|
{
|
|
|
|
bool polling = false;
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < _nfds; ++i)
|
|
|
|
{
|
|
|
|
pollfd &pfd = _fds[i];
|
|
|
|
File_descriptor *libc_fd = libc_fd_to_fd(pfd.fd, "poll");
|
|
|
|
if (!libc_fd) {
|
|
|
|
pfd.revents |= POLLNVAL;
|
|
|
|
++nready;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!libc_fd->plugin || !libc_fd->plugin->supports_poll()) {
|
2019-09-19 20:37:17 +02:00
|
|
|
warning("poll not supported for file descriptor ", pfd.fd);
|
2019-04-26 12:47:33 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
nready += libc_fd->plugin->poll(*libc_fd, pfd);
|
|
|
|
polling = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (polling && nready == 0);
|
2012-07-18 16:01:30 +02:00
|
|
|
}
|
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
} check (fds, nfds);
|
|
|
|
|
|
|
|
check.suspend();
|
|
|
|
|
|
|
|
if (timeout_ms == 0) {
|
|
|
|
return check.nready;
|
2012-07-18 16:01:30 +02:00
|
|
|
}
|
2019-04-26 12:47:33 +02:00
|
|
|
|
2019-09-19 20:37:17 +02:00
|
|
|
auto suspend = [&] (uint64_t timeout_ms)
|
libc: split task.cc into multiple files
This patch is the first step of re-organizing the internal structure of
the libc. The original version involved many direct calls of global
functions (often with side effects) across compilation units, which
made the control flow (e.g., the initialization sequence) hard to
follow.
The new version replaces those ad-hoc interactions with dedicated
interfaces (like suspend.h, resume.h, select.h, current_time.h). The
underlying facilities are provided by the central Libc::Kernel and
selectively propagated to the various compilation units. The latter is
done by a sequence of 'init_*' calls, which eventually will be replaced
by constructor calls.
The addition of new headers increases the chance for name clashes with
existing (public) headers. To disambiguate libc-internal header files
from public headers, this patch moves the former into a new 'internal/'
subdirectory. This makes the include directives easier to follow and the
libc's source-tree structure more tidy.
There are still a few legacies left, which cannot easily be removed
right now (e.g., because noux relies on them). However, the patch moves
those bad apples to legacy.h and legacy.cc, which highlights the
deprecation of those functions.
Issue #3497
2019-09-18 20:19:10 +02:00
|
|
|
{
|
|
|
|
struct Missing_call_of_init_poll : Exception { };
|
|
|
|
if (!_suspend_ptr)
|
|
|
|
throw Missing_call_of_init_poll();
|
|
|
|
|
|
|
|
return _suspend_ptr->suspend(check, timeout_ms);
|
|
|
|
};
|
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
if (timeout_ms == -1) {
|
|
|
|
while (check.nready == 0) {
|
libc: split task.cc into multiple files
This patch is the first step of re-organizing the internal structure of
the libc. The original version involved many direct calls of global
functions (often with side effects) across compilation units, which
made the control flow (e.g., the initialization sequence) hard to
follow.
The new version replaces those ad-hoc interactions with dedicated
interfaces (like suspend.h, resume.h, select.h, current_time.h). The
underlying facilities are provided by the central Libc::Kernel and
selectively propagated to the various compilation units. The latter is
done by a sequence of 'init_*' calls, which eventually will be replaced
by constructor calls.
The addition of new headers increases the chance for name clashes with
existing (public) headers. To disambiguate libc-internal header files
from public headers, this patch moves the former into a new 'internal/'
subdirectory. This makes the include directives easier to follow and the
libc's source-tree structure more tidy.
There are still a few legacies left, which cannot easily be removed
right now (e.g., because noux relies on them). However, the patch moves
those bad apples to legacy.h and legacy.cc, which highlights the
deprecation of those functions.
Issue #3497
2019-09-18 20:19:10 +02:00
|
|
|
suspend(0);
|
2012-07-18 16:01:30 +02:00
|
|
|
}
|
2019-04-26 12:47:33 +02:00
|
|
|
} else {
|
2019-09-19 20:37:17 +02:00
|
|
|
uint64_t remaining_ms = timeout_ms;
|
2019-04-26 12:47:33 +02:00
|
|
|
while (check.nready == 0 && remaining_ms > 0) {
|
libc: split task.cc into multiple files
This patch is the first step of re-organizing the internal structure of
the libc. The original version involved many direct calls of global
functions (often with side effects) across compilation units, which
made the control flow (e.g., the initialization sequence) hard to
follow.
The new version replaces those ad-hoc interactions with dedicated
interfaces (like suspend.h, resume.h, select.h, current_time.h). The
underlying facilities are provided by the central Libc::Kernel and
selectively propagated to the various compilation units. The latter is
done by a sequence of 'init_*' calls, which eventually will be replaced
by constructor calls.
The addition of new headers increases the chance for name clashes with
existing (public) headers. To disambiguate libc-internal header files
from public headers, this patch moves the former into a new 'internal/'
subdirectory. This makes the include directives easier to follow and the
libc's source-tree structure more tidy.
There are still a few legacies left, which cannot easily be removed
right now (e.g., because noux relies on them). However, the patch moves
those bad apples to legacy.h and legacy.cc, which highlights the
deprecation of those functions.
Issue #3497
2019-09-18 20:19:10 +02:00
|
|
|
remaining_ms = suspend(remaining_ms);
|
2012-07-18 16:01:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
return check.nready;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extern "C" __attribute__((weak, alias("poll")))
|
|
|
|
int __sys_poll(struct pollfd fds[], nfds_t nfds, int timeout_ms);
|
|
|
|
|
|
|
|
|
|
|
|
extern "C" __attribute__((weak, alias("poll")))
|
|
|
|
int _poll(struct pollfd fds[], nfds_t nfds, int timeout_ms);
|
|
|
|
|
|
|
|
|
|
|
|
extern "C" __attribute__((weak))
|
|
|
|
int ppoll(struct pollfd fds[], nfds_t nfds,
|
|
|
|
const struct timespec *timeout,
|
|
|
|
const sigset_t*)
|
|
|
|
{
|
|
|
|
int timeout_ms = timeout->tv_sec * 1000 + timeout->tv_nsec / 1000;
|
|
|
|
return poll(fds, nfds, timeout_ms);
|
2012-07-18 16:01:30 +02:00
|
|
|
}
|
2019-04-26 12:47:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
extern "C" __attribute__((weak, alias("ppoll")))
|
|
|
|
int __sys_ppoll(struct pollfd fds[], nfds_t nfds,
|
|
|
|
const struct timespec *timeout,
|
|
|
|
const sigset_t*);
|