• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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)) {
24   int ret = 0;
25 
26 #ifdef SYS_poll
27   ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_poll, fds, nfds, timeout);
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   }
37   ret =
38       LIBC_NAMESPACE::syscall_impl<int>(SYS_ppoll, fds, nfds, tsp, nullptr, 0);
39 #else
40 // TODO: https://github.com/llvm/llvm-project/issues/125940
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
52