Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include <bits/pthread_cancel.h>
8#include <errno.h>
9#include <poll.h>
10#include <sys/time.h>
11#include <syscall.h>
12
13extern "C" {
14
15// https://pubs.opengroup.org/onlinepubs/9699919799/functions/poll.html
16int poll(pollfd* fds, nfds_t nfds, int timeout_ms)
17{
18 __pthread_maybe_cancel();
19
20 timespec timeout;
21 timespec* timeout_ts = &timeout;
22 if (timeout_ms < 0)
23 timeout_ts = nullptr;
24 else
25 timeout = { timeout_ms / 1000, (timeout_ms % 1000) * 1'000'000 };
26 return ppoll(fds, nfds, timeout_ts, nullptr);
27}
28
29int ppoll(pollfd* fds, nfds_t nfds, timespec const* timeout, sigset_t const* sigmask)
30{
31 Syscall::SC_poll_params params { fds, nfds, timeout, sigmask };
32 int rc = syscall(SC_poll, ¶ms);
33 __RETURN_WITH_ERRNO(rc, rc, -1);
34}
35}