blob: d7c195878ae1274efc3cc9a6fceebb88f3f18852 [file] [log] [blame]
Nick Desaulniers8e35b3d2025-02-05 21:24:391//===-- Implementation of poll --------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "src/poll/poll.h"
10
11#include "hdr/types/nfds_t.h"
12#include "hdr/types/struct_pollfd.h"
13#include "hdr/types/struct_timespec.h"
14#include "src/__support/OSUtil/syscall.h" // syscall_impl
15#include "src/__support/common.h"
16#include "src/__support/macros/config.h"
17#include "src/errno/libc_errno.h"
18
19#include <sys/syscall.h> // SYS_poll, SYS_ppoll
20
21namespace LIBC_NAMESPACE_DECL {
22
23LLVM_LIBC_FUNCTION(int, poll, (pollfd * fds, nfds_t nfds, int timeout)) {
Nick Desaulniers72aa3882025-02-05 21:56:3024 int ret = 0;
Nick Desaulniers8e35b3d2025-02-05 21:24:3925
26#ifdef SYS_poll
Nick Desaulniers72aa3882025-02-05 21:56:3027 ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_poll, fds, nfds, timeout);
Nick Desaulniers8e35b3d2025-02-05 21:24:3928#elif defined(SYS_ppoll)
29 timespec ts, *tsp;
30 if (timeout >= 0) {
31 ts.tv_sec = timeout / 1000;
32 ts.tv_nsec = (timeout % 1000) * 1000000;
33 tsp = &ts;
34 } else {
35 tsp = nullptr;
36 }
Nick Desaulniers72aa3882025-02-05 21:56:3037 ret =
Nick Desaulniers8e35b3d2025-02-05 21:24:3938 LIBC_NAMESPACE::syscall_impl<int>(SYS_ppoll, fds, nfds, tsp, nullptr, 0);
39#else
Nick Desaulniers72aa3882025-02-05 21:56:3040// TODO: https://github.com/llvm/llvm-project/issues/125940
Nick Desaulniers8e35b3d2025-02-05 21:24:3941#error "SYS_ppoll_time64?"
42#endif
43
44 if (ret < 0) {
45 libc_errno = -ret;
46 return -1;
47 }
48 return ret;
49}
50
51} // namespace LIBC_NAMESPACE_DECL