poll.cpp 887 B

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