Skip to content

Commit

Permalink
StdSync::Poll: Use range instead of std::span
Browse files Browse the repository at this point in the history
  • Loading branch information
Fulgen301 committed May 18, 2023
1 parent 460202e commit cac0ab1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
17 changes: 0 additions & 17 deletions src/StdSync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,23 +76,6 @@ bool CStdEvent::WaitFor(const std::uint32_t milliseconds)

#else

int StdSync::Poll(const std::span<pollfd> fds, const std::uint32_t timeout)
{
const auto clampedTimeout = std::clamp(static_cast<int>(timeout), static_cast<int>(Infinite), std::numeric_limits<int>::max());

for (;;)
{
const int result{poll(fds.data(), static_cast<nfds_t>(fds.size()), clampedTimeout)};

if (result == -1 && (errno == EINTR || errno == EAGAIN || errno == ENOMEM))
{
continue;
}

return result;
}
}

[[noreturn]] static void ThrowError(const char *const message)
{
throw std::runtime_error{std::string{message} + ": " + std::strerror(errno)};
Expand Down
19 changes: 18 additions & 1 deletion src/StdSync.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <condition_variable>

#ifndef _WIN32
#include <ranges>
#include <span>

#include <poll.h>
Expand All @@ -37,7 +38,23 @@ namespace StdSync
static inline constexpr auto Infinite = std::numeric_limits<std::uint32_t>::max();

#ifndef _WIN32
int Poll(std::span<pollfd> fds, std::uint32_t timeout);
template<typename T> requires std::ranges::contiguous_range<T> && std::ranges::sized_range<T>
int Poll(T &&fds, const std::uint32_t timeout)
{
const auto clampedTimeout = std::clamp(static_cast<int>(timeout), static_cast<int>(Infinite), std::numeric_limits<int>::max());

for (;;)
{
const int result{poll(std::ranges::data(fds), static_cast<nfds_t>(std::ranges::size(fds)), clampedTimeout)};

if (result == -1 && (errno == EINTR || errno == EAGAIN || errno == ENOMEM))
{
continue;
}

return result;
}
}
#endif
}

Expand Down

0 comments on commit cac0ab1

Please sign in to comment.