Move skip_eintr to detail.

GitOrigin-RevId: f9898af5691b7fda7e0036de4ede6f17e281c657
This commit is contained in:
levlam 2018-09-10 02:16:42 +03:00
parent cfea83b4c5
commit 5260fa4ef9
9 changed files with 29 additions and 49 deletions

View File

@ -316,7 +316,7 @@ Status Fd::get_pending_error() {
Result<size_t> Fd::write_unsafe(Slice slice) { Result<size_t> Fd::write_unsafe(Slice slice) {
int native_fd = get_native_fd(); int native_fd = get_native_fd();
auto write_res = skip_eintr([&] { return ::write(native_fd, slice.begin(), slice.size()); }); auto write_res = detail::skip_eintr([&] { return ::write(native_fd, slice.begin(), slice.size()); });
auto write_errno = errno; auto write_errno = errno;
if (write_res >= 0) { if (write_res >= 0) {
return narrow_cast<size_t>(write_res); return narrow_cast<size_t>(write_res);
@ -326,7 +326,7 @@ Result<size_t> Fd::write_unsafe(Slice slice) {
Result<size_t> Fd::write(Slice slice) { Result<size_t> Fd::write(Slice slice) {
int native_fd = get_native_fd(); int native_fd = get_native_fd();
auto write_res = skip_eintr([&] { return ::write(native_fd, slice.begin(), slice.size()); }); auto write_res = detail::skip_eintr([&] { return ::write(native_fd, slice.begin(), slice.size()); });
auto write_errno = errno; auto write_errno = errno;
if (write_res >= 0) { if (write_res >= 0) {
return narrow_cast<size_t>(write_res); return narrow_cast<size_t>(write_res);
@ -372,7 +372,7 @@ Result<size_t> Fd::read(MutableSlice slice) {
} }
int native_fd = get_native_fd(); int native_fd = get_native_fd();
CHECK(slice.size() > 0); CHECK(slice.size() > 0);
auto read_res = skip_eintr([&] { return ::read(native_fd, slice.begin(), slice.size()); }); auto read_res = detail::skip_eintr([&] { return ::read(native_fd, slice.begin(), slice.size()); });
auto read_errno = errno; auto read_errno = errno;
if (read_res >= 0) { if (read_res >= 0) {
if (read_res == 0) { if (read_res == 0) {

View File

@ -195,28 +195,6 @@ class Fd {
#endif #endif
}; };
#if TD_PORT_POSIX
template <class F>
auto skip_eintr(F &&f) {
decltype(f()) res;
static_assert(std::is_integral<decltype(res)>::value, "integral type expected");
do {
errno = 0; // just in case
res = f();
} while (res < 0 && errno == EINTR);
return res;
}
template <class F>
auto skip_eintr_cstr(F &&f) {
char *res;
do {
errno = 0; // just in case
res = f();
} while (res == nullptr && errno == EINTR);
return res;
}
#endif
template <class FdT> template <class FdT>
bool can_read(const FdT &fd) { bool can_read(const FdT &fd) {
return (fd.get_flags() & (Fd::Read | Fd::Error)) != 0; return (fd.get_flags() & (Fd::Read | Fd::Error)) != 0;

View File

@ -129,7 +129,7 @@ Result<FileFd> FileFd::open(CSlice filepath, int32 flags, int32 mode) {
native_flags |= O_APPEND; native_flags |= O_APPEND;
} }
int native_fd = skip_eintr([&] { return ::open(filepath.c_str(), native_flags, static_cast<mode_t>(mode)); }); int native_fd = detail::skip_eintr([&] { return ::open(filepath.c_str(), native_flags, static_cast<mode_t>(mode)); });
if (native_fd < 0) { if (native_fd < 0) {
return OS_ERROR(PSLICE() << "File \"" << filepath << "\" can't be " << PrintFlags{flags}); return OS_ERROR(PSLICE() << "File \"" << filepath << "\" can't be " << PrintFlags{flags});
} }
@ -202,7 +202,7 @@ FileFd FileFd::from_native_fd(NativeFd native_fd) {
Result<size_t> FileFd::write(Slice slice) { Result<size_t> FileFd::write(Slice slice) {
#if TD_PORT_POSIX #if TD_PORT_POSIX
auto native_fd = get_native_fd().fd(); auto native_fd = get_native_fd().fd();
auto write_res = skip_eintr([&] { return ::write(native_fd, slice.begin(), slice.size()); }); auto write_res = detail::skip_eintr([&] { return ::write(native_fd, slice.begin(), slice.size()); });
if (write_res >= 0) { if (write_res >= 0) {
return narrow_cast<size_t>(write_res); return narrow_cast<size_t>(write_res);
} }
@ -231,7 +231,7 @@ Result<size_t> FileFd::write(Slice slice) {
Result<size_t> FileFd::read(MutableSlice slice) { Result<size_t> FileFd::read(MutableSlice slice) {
#if TD_PORT_POSIX #if TD_PORT_POSIX
auto native_fd = get_native_fd().fd(); auto native_fd = get_native_fd().fd();
auto read_res = skip_eintr([&] { return ::read(native_fd, slice.begin(), slice.size()); }); auto read_res = detail::skip_eintr([&] { return ::read(native_fd, slice.begin(), slice.size()); });
auto read_errno = errno; auto read_errno = errno;
if (read_res >= 0) { if (read_res >= 0) {
@ -271,7 +271,7 @@ Result<size_t> FileFd::pwrite(Slice slice, int64 offset) {
#if TD_PORT_POSIX #if TD_PORT_POSIX
auto native_fd = get_native_fd().fd(); auto native_fd = get_native_fd().fd();
TRY_RESULT(offset_off_t, narrow_cast_safe<off_t>(offset)); TRY_RESULT(offset_off_t, narrow_cast_safe<off_t>(offset));
auto pwrite_res = skip_eintr([&] { return ::pwrite(native_fd, slice.begin(), slice.size(), offset_off_t); }); auto pwrite_res = detail::skip_eintr([&] { return ::pwrite(native_fd, slice.begin(), slice.size(), offset_off_t); });
if (pwrite_res >= 0) { if (pwrite_res >= 0) {
return narrow_cast<size_t>(pwrite_res); return narrow_cast<size_t>(pwrite_res);
} }
@ -309,7 +309,7 @@ Result<size_t> FileFd::pread(MutableSlice slice, int64 offset) {
#if TD_PORT_POSIX #if TD_PORT_POSIX
auto native_fd = get_native_fd().fd(); auto native_fd = get_native_fd().fd();
TRY_RESULT(offset_off_t, narrow_cast_safe<off_t>(offset)); TRY_RESULT(offset_off_t, narrow_cast_safe<off_t>(offset));
auto pread_res = skip_eintr([&] { return ::pread(native_fd, slice.begin(), slice.size(), offset_off_t); }); auto pread_res = detail::skip_eintr([&] { return ::pread(native_fd, slice.begin(), slice.size(), offset_off_t); });
if (pread_res >= 0) { if (pread_res >= 0) {
return narrow_cast<size_t>(pread_res); return narrow_cast<size_t>(pread_res);
} }
@ -481,7 +481,7 @@ Status FileFd::seek(int64 position) {
CHECK(!empty()); CHECK(!empty());
#if TD_PORT_POSIX #if TD_PORT_POSIX
TRY_RESULT(position_off_t, narrow_cast_safe<off_t>(position)); TRY_RESULT(position_off_t, narrow_cast_safe<off_t>(position));
if (skip_eintr([&] { return ::lseek(get_native_fd().fd(), position_off_t, SEEK_SET); }) < 0) { if (detail::skip_eintr([&] { return ::lseek(get_native_fd().fd(), position_off_t, SEEK_SET); }) < 0) {
#elif TD_PORT_WINDOWS #elif TD_PORT_WINDOWS
LARGE_INTEGER offset; LARGE_INTEGER offset;
offset.QuadPart = position; offset.QuadPart = position;
@ -496,7 +496,7 @@ Status FileFd::truncate_to_current_position(int64 current_position) {
CHECK(!empty()); CHECK(!empty());
#if TD_PORT_POSIX #if TD_PORT_POSIX
TRY_RESULT(current_position_off_t, narrow_cast_safe<off_t>(current_position)); TRY_RESULT(current_position_off_t, narrow_cast_safe<off_t>(current_position));
if (skip_eintr([&] { return ::ftruncate(get_native_fd().fd(), current_position_off_t); }) < 0) { if (detail::skip_eintr([&] { return ::ftruncate(get_native_fd().fd(), current_position_off_t); }) < 0) {
#elif TD_PORT_WINDOWS #elif TD_PORT_WINDOWS
if (SetEndOfFile(get_native_fd().io_handle()) == 0) { if (SetEndOfFile(get_native_fd().io_handle()) == 0) {
#endif #endif

View File

@ -215,7 +215,7 @@ class ServerSocketFdImpl {
sockaddr_storage addr; sockaddr_storage addr;
socklen_t addr_len = sizeof(addr); socklen_t addr_len = sizeof(addr);
int native_fd = get_native_fd().fd(); int native_fd = get_native_fd().fd();
int r_fd = skip_eintr([&] { return ::accept(native_fd, reinterpret_cast<sockaddr *>(&addr), &addr_len); }); int r_fd = detail::skip_eintr([&] { return ::accept(native_fd, reinterpret_cast<sockaddr *>(&addr), &addr_len); });
auto accept_errno = errno; auto accept_errno = errno;
if (r_fd >= 0) { if (r_fd >= 0) {
return SocketFd::from_native_fd(NativeFd(r_fd)); return SocketFd::from_native_fd(NativeFd(r_fd));

View File

@ -331,7 +331,7 @@ class SocketFdImpl {
} }
Result<size_t> write(Slice slice) { Result<size_t> write(Slice slice) {
int native_fd = get_native_fd().fd(); int native_fd = get_native_fd().fd();
auto write_res = skip_eintr([&] { return ::write(native_fd, slice.begin(), slice.size()); }); auto write_res = detail::skip_eintr([&] { return ::write(native_fd, slice.begin(), slice.size()); });
auto write_errno = errno; auto write_errno = errno;
if (write_res >= 0) { if (write_res >= 0) {
return narrow_cast<size_t>(write_res); return narrow_cast<size_t>(write_res);
@ -376,7 +376,7 @@ class SocketFdImpl {
} }
int native_fd = get_native_fd().fd(); int native_fd = get_native_fd().fd();
CHECK(slice.size() > 0); CHECK(slice.size() > 0);
auto read_res = skip_eintr([&] { return ::read(native_fd, slice.begin(), slice.size()); }); auto read_res = detail::skip_eintr([&] { return ::read(native_fd, slice.begin(), slice.size()); });
auto read_errno = errno; auto read_errno = errno;
if (read_res >= 0) { if (read_res >= 0) {
if (read_res == 0) { if (read_res == 0) {

View File

@ -503,7 +503,7 @@ class UdpSocketFdImpl {
helper.to_native(message, message_header); helper.to_native(message, message_header);
auto native_fd = get_native_fd().fd(); auto native_fd = get_native_fd().fd();
auto recvmsg_res = skip_eintr([&] { return recvmsg(native_fd, &message_header, flags); }); auto recvmsg_res = detail::skip_eintr([&] { return recvmsg(native_fd, &message_header, flags); });
auto recvmsg_errno = errno; auto recvmsg_errno = errno;
if (recvmsg_res >= 0) { if (recvmsg_res >= 0) {
helper.from_native(message_header, recvmsg_res, message); helper.from_native(message_header, recvmsg_res, message);
@ -558,7 +558,7 @@ class UdpSocketFdImpl {
helper.to_native(message, message_header); helper.to_native(message, message_header);
auto native_fd = get_native_fd().fd(); auto native_fd = get_native_fd().fd();
auto sendmsg_res = skip_eintr([&] { return sendmsg(native_fd, &message_header, 0); }); auto sendmsg_res = detail::skip_eintr([&] { return sendmsg(native_fd, &message_header, 0); });
auto sendmsg_errno = errno; auto sendmsg_errno = errno;
if (sendmsg_res >= 0) { if (sendmsg_res >= 0) {
is_sent = true; is_sent = true;
@ -668,7 +668,7 @@ class UdpSocketFdImpl {
auto native_fd = get_native_fd().fd(); auto native_fd = get_native_fd().fd();
auto sendmmsg_res = auto sendmmsg_res =
skip_eintr([&] { return sendmmsg(native_fd, headers.data(), narrow_cast<unsigned int>(to_send), 0); }); detail::skip_eintr([&] { return sendmmsg(native_fd, headers.data(), narrow_cast<unsigned int>(to_send), 0); });
auto sendmmsg_errno = errno; auto sendmmsg_errno = errno;
if (sendmmsg_res >= 0) { if (sendmmsg_res >= 0) {
cnt = sendmmsg_res; cnt = sendmmsg_res;
@ -718,7 +718,7 @@ class UdpSocketFdImpl {
} }
auto native_fd = get_native_fd().fd(); auto native_fd = get_native_fd().fd();
auto recvmmsg_res = skip_eintr( auto recvmmsg_res = detail::skip_eintr(
[&] { return recvmmsg(native_fd, headers.data(), narrow_cast<unsigned int>(to_receive), flags, nullptr); }); [&] { return recvmmsg(native_fd, headers.data(), narrow_cast<unsigned int>(to_receive), flags, nullptr); });
auto recvmmsg_errno = errno; auto recvmmsg_errno = errno;
if (recvmmsg_res >= 0) { if (recvmmsg_res >= 0) {

View File

@ -61,7 +61,7 @@ void EventFdLinux::release() {
auto native_fd = impl_->info.native_fd().fd(); auto native_fd = impl_->info.native_fd().fd();
auto result = [&]() -> Result<size_t> { auto result = [&]() -> Result<size_t> {
auto write_res = skip_eintr([&] { return ::write(native_fd, slice.begin(), slice.size()); }); auto write_res = detail::skip_eintr([&] { return ::write(native_fd, slice.begin(), slice.size()); });
auto write_errno = errno; auto write_errno = errno;
if (write_res >= 0) { if (write_res >= 0) {
return narrow_cast<size_t>(write_res); return narrow_cast<size_t>(write_res);
@ -84,7 +84,7 @@ void EventFdLinux::acquire() {
auto native_fd = impl_->info.native_fd().fd(); auto native_fd = impl_->info.native_fd().fd();
auto result = [&]() -> Result<size_t> { auto result = [&]() -> Result<size_t> {
CHECK(slice.size() > 0); CHECK(slice.size() > 0);
auto read_res = skip_eintr([&] { return ::read(native_fd, slice.begin(), slice.size()); }); auto read_res = detail::skip_eintr([&] { return ::read(native_fd, slice.begin(), slice.size()); });
auto read_errno = errno; auto read_errno = errno;
if (read_res >= 0) { if (read_res >= 0) {
CHECK(read_res != 0); CHECK(read_res != 0);

View File

@ -324,6 +324,7 @@ inline const NativeFd &PollableFd::native_fd() const {
} }
#if TD_PORT_POSIX #if TD_PORT_POSIX
namespace detail {
template <class F> template <class F>
auto skip_eintr(F &&f) { auto skip_eintr(F &&f) {
decltype(f()) res; decltype(f()) res;
@ -343,6 +344,7 @@ auto skip_eintr_cstr(F &&f) {
} while (res == nullptr && errno == EINTR); } while (res == nullptr && errno == EINTR);
return res; return res;
} }
} // namespace detail
#endif #endif
template <class FdT> template <class FdT>

View File

@ -77,7 +77,7 @@ Status rmrf(CSlice path) {
#if TD_PORT_POSIX #if TD_PORT_POSIX
Status mkdir(CSlice dir, int32 mode) { Status mkdir(CSlice dir, int32 mode) {
int mkdir_res = skip_eintr([&] { return ::mkdir(dir.c_str(), static_cast<mode_t>(mode)); }); int mkdir_res = detail::skip_eintr([&] { return ::mkdir(dir.c_str(), static_cast<mode_t>(mode)); });
if (mkdir_res == 0) { if (mkdir_res == 0) {
return Status::OK(); return Status::OK();
} }
@ -90,7 +90,7 @@ Status mkdir(CSlice dir, int32 mode) {
} }
Status rename(CSlice from, CSlice to) { Status rename(CSlice from, CSlice to) {
int rename_res = skip_eintr([&] { return ::rename(from.c_str(), to.c_str()); }); int rename_res = detail::skip_eintr([&] { return ::rename(from.c_str(), to.c_str()); });
if (rename_res < 0) { if (rename_res < 0) {
return OS_ERROR(PSLICE() << "Can't rename \"" << from << "\" to \"" << to << '\"'); return OS_ERROR(PSLICE() << "Can't rename \"" << from << "\" to \"" << to << '\"');
} }
@ -100,7 +100,7 @@ Status rename(CSlice from, CSlice to) {
Result<string> realpath(CSlice slice, bool ignore_access_denied) { Result<string> realpath(CSlice slice, bool ignore_access_denied) {
char full_path[PATH_MAX + 1]; char full_path[PATH_MAX + 1];
string res; string res;
char *err = skip_eintr_cstr([&] { return ::realpath(slice.c_str(), full_path); }); char *err = detail::skip_eintr_cstr([&] { return ::realpath(slice.c_str(), full_path); });
if (err != full_path) { if (err != full_path) {
if (ignore_access_denied && (errno == EACCES || errno == EPERM)) { if (ignore_access_denied && (errno == EACCES || errno == EPERM)) {
res = slice.str(); res = slice.str();
@ -122,7 +122,7 @@ Result<string> realpath(CSlice slice, bool ignore_access_denied) {
} }
Status chdir(CSlice dir) { Status chdir(CSlice dir) {
int chdir_res = skip_eintr([&] { return ::chdir(dir.c_str()); }); int chdir_res = detail::skip_eintr([&] { return ::chdir(dir.c_str()); });
if (chdir_res) { if (chdir_res) {
return OS_ERROR(PSLICE() << "Can't change directory to \"" << dir << '"'); return OS_ERROR(PSLICE() << "Can't change directory to \"" << dir << '"');
} }
@ -130,7 +130,7 @@ Status chdir(CSlice dir) {
} }
Status rmdir(CSlice dir) { Status rmdir(CSlice dir) {
int rmdir_res = skip_eintr([&] { return ::rmdir(dir.c_str()); }); int rmdir_res = detail::skip_eintr([&] { return ::rmdir(dir.c_str()); });
if (rmdir_res) { if (rmdir_res) {
return OS_ERROR(PSLICE() << "Can't delete directory \"" << dir << '"'); return OS_ERROR(PSLICE() << "Can't delete directory \"" << dir << '"');
} }
@ -138,7 +138,7 @@ Status rmdir(CSlice dir) {
} }
Status unlink(CSlice path) { Status unlink(CSlice path) {
int unlink_res = skip_eintr([&] { return ::unlink(path.c_str()); }); int unlink_res = detail::skip_eintr([&] { return ::unlink(path.c_str()); });
if (unlink_res) { if (unlink_res) {
return OS_ERROR(PSLICE() << "Can't unlink \"" << path << '"'); return OS_ERROR(PSLICE() << "Can't unlink \"" << path << '"');
} }
@ -185,7 +185,7 @@ Result<std::pair<FileFd, string>> mkstemp(CSlice dir) {
} }
file_pattern += "tmpXXXXXXXXXX"; file_pattern += "tmpXXXXXXXXXX";
int fd = skip_eintr([&] { return ::mkstemp(&file_pattern[0]); }); int fd = detail::skip_eintr([&] { return ::mkstemp(&file_pattern[0]); });
if (fd == -1) { if (fd == -1) {
return OS_ERROR(PSLICE() << "Can't create temporary file \"" << file_pattern << '"'); return OS_ERROR(PSLICE() << "Can't create temporary file \"" << file_pattern << '"');
} }
@ -217,7 +217,7 @@ Result<string> mkdtemp(CSlice dir, Slice prefix) {
dir_pattern.append(prefix.begin(), prefix.size()); dir_pattern.append(prefix.begin(), prefix.size());
dir_pattern += "XXXXXX"; dir_pattern += "XXXXXX";
char *result = skip_eintr_cstr([&] { return ::mkdtemp(&dir_pattern[0]); }); char *result = detail::skip_eintr_cstr([&] { return ::mkdtemp(&dir_pattern[0]); });
if (result == nullptr) { if (result == nullptr) {
return OS_ERROR(PSLICE() << "Can't create temporary directory \"" << dir_pattern << '"'); return OS_ERROR(PSLICE() << "Can't create temporary directory \"" << dir_pattern << '"');
} }