Nick Desaulniers | 8e35b3d | 2025-02-05 21:24:39 | [diff] [blame] | 1 | //===-- 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 | |
| 21 | namespace LIBC_NAMESPACE_DECL { |
| 22 | |
| 23 | LLVM_LIBC_FUNCTION(int, poll, (pollfd * fds, nfds_t nfds, int timeout)) { |
Nick Desaulniers | 72aa388 | 2025-02-05 21:56:30 | [diff] [blame] | 24 | int ret = 0; |
Nick Desaulniers | 8e35b3d | 2025-02-05 21:24:39 | [diff] [blame] | 25 | |
| 26 | #ifdef SYS_poll |
Nick Desaulniers | 72aa388 | 2025-02-05 21:56:30 | [diff] [blame] | 27 | ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_poll, fds, nfds, timeout); |
Nick Desaulniers | 8e35b3d | 2025-02-05 21:24:39 | [diff] [blame] | 28 | #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 Desaulniers | 72aa388 | 2025-02-05 21:56:30 | [diff] [blame] | 37 | ret = |
Nick Desaulniers | 8e35b3d | 2025-02-05 21:24:39 | [diff] [blame] | 38 | LIBC_NAMESPACE::syscall_impl<int>(SYS_ppoll, fds, nfds, tsp, nullptr, 0); |
| 39 | #else |
Nick Desaulniers | 72aa388 | 2025-02-05 21:56:30 | [diff] [blame] | 40 | // TODO: https://github.com/llvm/llvm-project/issues/125940 |
Nick Desaulniers | 8e35b3d | 2025-02-05 21:24:39 | [diff] [blame] | 41 | #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 |