Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.fcntl.open
libc.src.fcntl.openat

# poll.h entrypoints
libc.src.poll.poll

# sched.h entrypoints
libc.src.sched.sched_get_priority_max
libc.src.sched.sched_get_priority_min
Expand Down
16 changes: 16 additions & 0 deletions libc/hdr/types/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -342,3 +342,19 @@ add_proxy_header_library(
libc.include.llvm-libc-types.struct_iovec
libc.include.sys_uio
)

add_proxy_header_library(
nfds_t
HDRS
nfds_t.h
FULL_BUILD_DEPENDS
libc.include.llvm-libc-types.nfds_t
)

add_proxy_header_library(
struct_pollfd
HDRS
struct_pollfd.h
FULL_BUILD_DEPENDS
libc.include.llvm-libc-types.struct_pollfd
)
23 changes: 23 additions & 0 deletions libc/hdr/types/nfds_t.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//===-- Definition of nfds_t ----------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_HDR_TYPES_NFDS_T_H
#define LLVM_LIBC_HDR_TYPES_NFDS_T_H

#ifdef LIBC_FULL_BUILD

#include "include/llvm-libc-types/nfds_t.h"

#else // overlay mode

#include <poll.h>

#endif // LLVM_LIBC_FULL_BUILD

#endif // LLVM_LIBC_HDR_TYPES_NFDS_T_H
23 changes: 23 additions & 0 deletions libc/hdr/types/struct_pollfd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//===-- Definition of struct pollfd ---------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_HDR_TYPES_STRUCT_POLLFD_H
#define LLVM_LIBC_HDR_TYPES_STRUCT_POLLFD_H

#ifdef LIBC_FULL_BUILD

#include "include/llvm-libc-types/struct_pollfd.h"

#else // overlay mode

#include <poll.h>

#endif // LLVM_LIBC_FULL_BUILD

#endif // LLVM_LIBC_HDR_TYPES_STRUCT_POLLFD_H
10 changes: 10 additions & 0 deletions libc/include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,16 @@ add_header_macro(
.llvm-libc-types.struct_lconv
)

add_header_macro(
poll
../libc/include/poll.yaml
poll.h
DEPENDS
.llvm-libc-types.struct_pollfd
.llvm-libc-types.nfds_t
.llvm-libc-macros.poll-macros
)

if(NOT LLVM_LIBC_FULL_BUILD)
# We don't install headers in non-fullbuild mode.
return()
Expand Down
6 changes: 6 additions & 0 deletions libc/include/llvm-libc-macros/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -321,3 +321,9 @@ add_macro_header(
HDR
pthread-macros.h
)

add_macro_header(
poll-macros
HDR
poll-macros.h
)
6 changes: 6 additions & 0 deletions libc/include/llvm-libc-macros/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ add_header(
fcntl-macros.h
)

add_header(
poll-macros
HDR
poll-macros.h
)

add_header(
sched_macros
HDR
Expand Down
65 changes: 65 additions & 0 deletions libc/include/llvm-libc-macros/linux/poll-macros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//===-- Macros defined in poll.h header file ------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_MACROS_LINUX_POLL_MACROS_H
#define LLVM_LIBC_MACROS_LINUX_POLL_MACROS_H

// From asm-generic/poll.h, redefined here to avoid redeclaring struct pollfd.
#ifndef POLLIN
#define POLLIN 0x0001
#endif

#ifndef POLLPRI
#define POLLPRI 0x0002
#endif

#ifndef POLLOUT
#define POLLOUT 0x0004
#endif

#ifndef POLLERR
#define POLLERR 0x0008
#endif

#ifndef POLLHUP
#define POLLHUP 0x0010
#endif

#ifndef POLLNVAL
#define POLLNVAL 0x0020
#endif

#ifndef POLLRDNORM
#define POLLRDNORM 0x0040
#endif

#ifndef POLLRDBAND
#define POLLRDBAND 0x0080
#endif

#ifndef POLLWRNORM
#define POLLWRNORM 0x0100
#endif

#ifndef POLLWRBAND
#define POLLWRBAND 0x0200
#endif

#ifndef POLLMSG
#define POLLMSG 0x0400
#endif

#ifndef POLLREMOVE
#define POLLREMOVE 0x1000
#endif

#ifndef POLLRDHUP
#define POLLRDHUP 0x2000
#endif

#endif // LLVM_LIBC_MACROS_LINUX_POLL_MACROS_H
16 changes: 16 additions & 0 deletions libc/include/llvm-libc-macros/poll-macros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//===-- Macros defined in poll.h header file ------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_MACROS_POLL_MACROS_H
#define LLVM_LIBC_MACROS_POLL_MACROS_H

#ifdef __linux__
#include "linux/poll-macros.h"
#endif

#endif // LLVM_LIBC_MACROS_POLL_MACROS_H
10 changes: 6 additions & 4 deletions libc/include/llvm-libc-types/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ add_header(jmp_buf HDR jmp_buf.h)
add_header(mbstate_t HDR mbstate_t.h)
add_header(mode_t HDR mode_t.h)
add_header(mtx_t HDR mtx_t.h DEPENDS .__futex_word .__mutex_type)
add_header(nfds_t HDR nfds_t.h)
add_header(nlink_t HDR nlink_t.h)
add_header(off_t HDR off_t.h)
add_header(once_flag HDR once_flag.h DEPENDS .__futex_word)
Expand All @@ -67,14 +68,15 @@ else()
endif()
add_header(stack_t HDR stack_t.h DEPENDS .size_t)
add_header(suseconds_t HDR suseconds_t.h)
add_header(struct_dirent HDR struct_dirent.h DEPENDS .ino_t .off_t)
add_header(struct_f_owner_ex HDR struct_f_owner_ex.h DEPENDS .pid_t)
add_header(struct_flock HDR struct_flock.h DEPENDS .off_t .pid_t)
add_header(struct_flock64 HDR struct_flock64.h DEPENDS .off64_t .pid_t)
add_header(struct_f_owner_ex HDR struct_f_owner_ex.h DEPENDS .pid_t)
add_header(struct_timeval HDR struct_timeval.h DEPENDS .suseconds_t .time_t)
add_header(struct_pollfd HDR struct_pollfd.h)
add_header(struct_rlimit HDR struct_rlimit.h DEPENDS .rlim_t)
add_header(struct_rusage HDR struct_rusage.h DEPENDS .struct_timeval)
add_header(struct_dirent HDR struct_dirent.h DEPENDS .ino_t .off_t)
add_header(struct_sched_param HDR struct_sched_param.h)
add_header(struct_timeval HDR struct_timeval.h DEPENDS .suseconds_t .time_t)
add_header(struct_rusage HDR struct_rusage.h DEPENDS .struct_timeval)
add_header(union_sigval HDR union_sigval.h)
add_header(siginfo_t HDR siginfo_t.h DEPENDS .union_sigval .pid_t .uid_t .clock_t)
add_header(sig_atomic_t HDR sig_atomic_t.h)
Expand Down
14 changes: 14 additions & 0 deletions libc/include/llvm-libc-types/nfds_t.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//===-- Definition of type nfds_t -----------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_TYPES_NFDS_T_H
#define LLVM_LIBC_TYPES_NFDS_T_H

typedef unsigned int nfds_t;

#endif // LLVM_LIBC_TYPES_NFDS_T_H
18 changes: 18 additions & 0 deletions libc/include/llvm-libc-types/struct_pollfd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===-- Definition of type struct pollfd ----------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_TYPES_STRUCT_POLLFD_H
#define LLVM_LIBC_TYPES_STRUCT_POLLFD_H

struct pollfd {
int fd;
short events;
short revents;
};

#endif // LLVM_LIBC_TYPES_STRUCT_POLLFD_H
16 changes: 16 additions & 0 deletions libc/include/poll.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
header: poll.h
header_template: poll.h.def
macros: []
types:
- type_name: struct_pollfd
- type_name: nfds_t
enums: []
functions:
- name: poll
standards:
- posix
return_type: int
arguments:
- type: struct pollfd *
- type: nfds_t
- type: int
1 change: 1 addition & 0 deletions libc/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ add_subdirectory(unistd)
if(${LIBC_TARGET_OS} STREQUAL "linux")
add_subdirectory(dirent)
add_subdirectory(fcntl)
add_subdirectory(poll)
add_subdirectory(pthread)
add_subdirectory(sched)
add_subdirectory(sys)
Expand Down
14 changes: 14 additions & 0 deletions libc/src/poll/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
add_entrypoint_object(
poll
SRCS
poll.cpp
HDRS
../poll.h
DEPENDS
libc.hdr.types.nfds_t
libc.hdr.types.struct_pollfd
libc.include.poll
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
)
31 changes: 31 additions & 0 deletions libc/src/poll/linux/poll.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//===-- Implementation of poll --------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/poll/poll.h"

#include "hdr/types/nfds_t.h"
#include "hdr/types/struct_pollfd.h"
#include "src/__support/OSUtil/syscall.h" // syscall_impl
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/errno/libc_errno.h"

#include <sys/syscall.h> // SYS_poll

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(int, poll, (struct pollfd * fds, nfds_t nfds, int timeout)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: struct is not needed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

heh, can you tell I still default to C sometimes..?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_poll, fds, nfds, timeout);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like SYS_poll does not exist for all architectures.
https://gpages.juszkiewicz.com.pl/syscalls-table/syscalls.html
I think we can fall back to SYS_ppoll in that case.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, looks like there's a newer ppoll_time64...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a860748. Will omit ppoll_time64 since that will only help riscv32 at the moment.

if (ret < 0) {
libc_errno = -ret;
return -1;
}
return ret;
}

} // namespace LIBC_NAMESPACE_DECL
22 changes: 22 additions & 0 deletions libc/src/poll/poll.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===-- Implementation header for poll ----------------------------*-C++-*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_POLL_POLL_H
#define LLVM_LIBC_SRC_POLL_POLL_H

#include "hdr/types/nfds_t.h"
#include "hdr/types/struct_pollfd.h"
#include "src/__support/macros/config.h"

namespace LIBC_NAMESPACE_DECL {

int poll(struct pollfd *fds, nfds_t nfds, int timeout);

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_POLL_POLL_H
1 change: 1 addition & 0 deletions libc/test/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ add_subdirectory(inttypes)

if(${LIBC_TARGET_OS} STREQUAL "linux")
add_subdirectory(fcntl)
add_subdirectory(poll)
add_subdirectory(sched)
add_subdirectory(sys)
add_subdirectory(termios)
Expand Down
18 changes: 18 additions & 0 deletions libc/test/src/poll/poll_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===-- Unittests for poll ------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/errno/libc_errno.h"
#include "src/poll/poll.h"
#include "test/UnitTest/Test.h"

TEST(LlvmLibcPollTest, SmokeTest) {
LIBC_NAMESPACE::libc_errno = 0;
int ret = LIBC_NAMESPACE::poll(nullptr, 0, 0);
ASSERT_ERRNO_SUCCESS();
ASSERT_EQ(0, ret);
}
Loading