Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(io_context): peek-only as fast path for do_completion_part #74

Merged
merged 7 commits into from
Sep 3, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 14 additions & 2 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ BasedOnStyle: LLVM
IndentWidth: 4
TabWidth: 4
Language: Cpp
# AlignAfterOpenBracket: AlwaysBreak # BlockIndent
AlignAfterOpenBracket: BlockIndent # BlockIndent
AlignArrayOfStructures: Right
AlignConsecutiveBitFields: "Consecutive"
Expand All @@ -14,8 +13,13 @@ AlignTrailingComments: true
AllowAllArgumentsOnNextLine: true
AllowAllConstructorInitializersOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
AlignConsecutiveShortCaseStatements: # clang-format 17
Enabled: true
AcrossEmptyLines: true
AcrossComments: true
AlignCaseColons: false
AllowShortBlocksOnASingleLine: Empty
AllowShortCaseLabelsOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: true
AllowShortEnumsOnASingleLine: true
AllowShortFunctionsOnASingleLine: InlineOnly
AllowShortIfStatementsOnASingleLine: WithoutElse
Expand Down Expand Up @@ -49,7 +53,14 @@ IndentRequires: true # clang-format 14
IndentWrappedFunctionNames: false
InsertBraces: true # clang-format 15
InsertNewlineAtEOF: true # clang-format 16
IntegerLiteralSeparator: # clang-format 16
Binary: 8
Decimal: 0
Hex: 2
HexMinDigits: 8
LambdaBodyIndentation: Signature
LineEnding: LF # clang-format 16
KeepEmptyLinesAtEOF: false # clang-format 17
KeepEmptyLinesAtTheStartOfBlocks: false
MaxEmptyLinesToKeep: 1
NamespaceIndentation: Inner
Expand All @@ -58,6 +69,7 @@ PointerAlignment: Right
QualifierAlignment: Left
ReflowComments: true
RequiresClausePosition: OwnLine # clang-format 15
RequiresExpressionIndentation: OuterScope # clang-format 16
SeparateDefinitionBlocks: Always
ShortNamespaceLines: 5
SortIncludes: "CaseSensitive"
Expand Down
8 changes: 2 additions & 6 deletions example/netcat_timeout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@ using Socket = co_context::socket;

void log_error(int err) {
switch (err) {
case ECANCELED:
log::e("timeout\n");
break;
default:
log::e("%s\n", strerror(err));
break;
case ECANCELED: log::e("timeout\n"); break;
default: log::e("%s\n", strerror(err)); break;
}
}

Expand Down
3 changes: 2 additions & 1 deletion example/sem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ constexpr auto time_tick{10ms};

unsigned rnd() {
static std::uniform_int_distribution<unsigned> distribution{
2U, 9U}; // [delays]
2U, 9U
}; // [delays]
static std::random_device engine;
static std::mt19937 noise{engine()};
return distribution(noise);
Expand Down
3 changes: 2 additions & 1 deletion example/sem_std.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ constexpr auto time_tick{10ms};

unsigned rnd() {
static std::uniform_int_distribution<unsigned> distribution{
2U, 9U}; // [delays]
2U, 9U
}; // [delays]
static std::random_device engine;
static std::mt19937 noise{engine()};
return distribution(noise);
Expand Down
7 changes: 4 additions & 3 deletions include/co_context/co/stop_token.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ class stop_token {
bool *m_destroyed = nullptr;
std::binary_semaphore m_done{0};

[[__gnu__::__nonnull__]] explicit stop_cb(cb_type *cb)
[[__gnu__::__nonnull__]]
explicit stop_cb(cb_type *cb)
: m_callback(cb) {}

void run() noexcept { m_callback(this); }
Expand Down Expand Up @@ -352,8 +353,8 @@ class stop_token {
other.m_ptr = nullptr;
}

stop_state_ref &operator= /*NOLINT*/ (const stop_state_ref &other
) noexcept {
stop_state_ref &operator= /*NOLINT*/ (const stop_state_ref
& other) noexcept {
if (auto *ptr = other.m_ptr; ptr != m_ptr) {
if (ptr) {
ptr->add_owner();
Expand Down
3 changes: 2 additions & 1 deletion include/co_context/co/when_any.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ task<detail::any_return_type<Ts...>> any(task<Ts> &&...node) {
co_return meta_ptr->idx;
} else {
co_return detail::any_tuple<Ts...>{
meta_ptr->idx, std::move(meta_ptr->buffer)};
meta_ptr->idx, std::move(meta_ptr->buffer)
};
}
}

Expand Down
2 changes: 1 addition & 1 deletion include/co_context/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ namespace config {
* missed than to ensure accuracy, it is recommended to set bias to 0.
*/
// inline constexpr int64_t timeout_bias_nanosecond = 0;
inline constexpr int64_t timeout_bias_nanosecond = -60'000;
inline constexpr int64_t timeout_bias_nanosecond = -30'000;
// ========================================================================

} // namespace config
Expand Down
55 changes: 22 additions & 33 deletions include/co_context/detail/lazy_io_awaiter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,18 +210,16 @@ struct lazy_recvmsg : lazy_awaiter {

#if LIBURINGCXX_IS_KERNEL_REACH(5, 20)
struct lazy_recvmsg_multishot : lazy_awaiter {
inline lazy_recvmsg_multishot(
int fd, msghdr *msg, unsigned flags
) noexcept {
inline
lazy_recvmsg_multishot(int fd, msghdr *msg, unsigned flags) noexcept {
sqe->prep_recvmsg_multishot(fd, msg, flags);
}
};
#endif

struct lazy_sendmsg : lazy_awaiter {
inline lazy_sendmsg(
int fd, const msghdr *msg, unsigned int flags
) noexcept {
inline
lazy_sendmsg(int fd, const msghdr *msg, unsigned int flags) noexcept {
sqe->prep_sendmsg(fd, msg, flags);
}
};
Expand Down Expand Up @@ -499,9 +497,8 @@ struct lazy_link_timeout
};

struct lazy_connect : lazy_awaiter {
inline lazy_connect(
int sockfd, const sockaddr *addr, socklen_t addrlen
) noexcept {
inline
lazy_connect(int sockfd, const sockaddr *addr, socklen_t addrlen) noexcept {
sqe->prep_connect(sockfd, addr, addrlen);
}
};
Expand All @@ -513,17 +510,15 @@ struct lazy_files_update : lazy_awaiter {
};

struct lazy_fallocate : lazy_awaiter {
inline lazy_fallocate(
int fd, int mode, uint64_t offset, uint64_t len
) noexcept {
inline
lazy_fallocate(int fd, int mode, uint64_t offset, uint64_t len) noexcept {
sqe->prep_fallocate(fd, mode, offset, len);
}
};

struct lazy_openat : lazy_awaiter {
inline lazy_openat(
int dfd, const char *path, int flags, mode_t mode
) noexcept {
inline
lazy_openat(int dfd, const char *path, int flags, mode_t mode) noexcept {
sqe->prep_openat(dfd, path, flags, mode);
}
};
Expand Down Expand Up @@ -554,9 +549,8 @@ struct lazy_read : lazy_awaiter {
};

struct lazy_write : lazy_awaiter {
inline lazy_write(
int fd, std::span<const char> buf, uint64_t offset
) noexcept {
inline
lazy_write(int fd, std::span<const char> buf, uint64_t offset) noexcept {
sqe->prep_write(fd, buf, offset);
}
};
Expand All @@ -574,9 +568,8 @@ struct lazy_statx : lazy_awaiter {
};

struct lazy_fadvise : lazy_awaiter {
inline lazy_fadvise(
int fd, uint64_t offset, off_t len, int advice
) noexcept {
inline
lazy_fadvise(int fd, uint64_t offset, off_t len, int advice) noexcept {
sqe->prep_fadvise(fd, offset, len, advice);
}
};
Expand All @@ -588,9 +581,8 @@ struct lazy_madvise : lazy_awaiter {
};

struct lazy_send : lazy_awaiter {
inline lazy_send(
int sockfd, std::span<const char> buf, int flags
) noexcept {
inline
lazy_send(int sockfd, std::span<const char> buf, int flags) noexcept {
sqe->prep_send(sockfd, buf, flags);
}
};
Expand Down Expand Up @@ -631,9 +623,8 @@ struct lazy_recv : lazy_awaiter {

#if LIBURINGCXX_IS_KERNEL_REACH(5, 20)
struct lazy_recv_multishot : lazy_awaiter {
inline lazy_recv_multishot(
int sockfd, std::span<char> buf, int flags
) noexcept {
inline
lazy_recv_multishot(int sockfd, std::span<char> buf, int flags) noexcept {
sqe->prep_recv_multishot(sockfd, buf, flags);
}
};
Expand Down Expand Up @@ -757,9 +748,8 @@ struct lazy_linkat : lazy_awaiter {
};

struct lazy_link : lazy_awaiter {
inline lazy_link(
const char *oldpath, const char *newpath, int flags
) noexcept {
inline
lazy_link(const char *oldpath, const char *newpath, int flags) noexcept {
sqe->prep_link(oldpath, newpath, flags);
}
};
Expand Down Expand Up @@ -807,9 +797,8 @@ struct lazy_setxattr : lazy_awaiter {
};

struct lazy_fgetxattr : lazy_awaiter {
inline lazy_fgetxattr(
int fd, const char *name, char *value, size_t len
) noexcept {
inline
lazy_fgetxattr(int fd, const char *name, char *value, size_t len) noexcept {
sqe->prep_fgetxattr(fd, name, value, len);
}
};
Expand Down
3 changes: 2 additions & 1 deletion include/co_context/detail/trival_task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ class trival_task {

trival_task get_return_object() noexcept {
return trival_task{
std::coroutine_handle<promise_type>::from_promise(*this)};
std::coroutine_handle<promise_type>::from_promise(*this)
};
}

constexpr void unhandled_exception() noexcept {}
Expand Down
1 change: 1 addition & 0 deletions include/co_context/detail/user_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ enum class user_data_type : uint8_t {
task_info_ptr,
coroutine_handle,
task_info_ptr__link_sqe,
msg_ring,
none
};

Expand Down
10 changes: 9 additions & 1 deletion include/co_context/detail/worker_meta.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ struct worker_meta final {

void wait_uring() noexcept;

bool peek_uring() noexcept;

[[nodiscard]]
cur_t number_to_schedule() const noexcept {
const auto &cur = this->reap_cur;
Expand Down Expand Up @@ -208,7 +210,7 @@ inline void worker_meta::co_spawn_safe_msg_ring(std::coroutine_handle<> handle
);
auto *const sqe = from.get_free_sqe();
auto user_data = reinterpret_cast<uint64_t>(handle.address())
| uint8_t(user_data_type::coroutine_handle);
| uint8_t(user_data_type::msg_ring);
sqe->prep_msg_ring(ring_fd, 0, user_data, 0);
sqe->set_data(uint64_t(reserved_user_data::nop));
#if LIBURINGCXX_IS_KERNEL_REACH(5, 17)
Expand Down Expand Up @@ -253,4 +255,10 @@ inline void worker_meta::wait_uring() noexcept {
ring.wait_cq_entry(_);
}

inline bool worker_meta::peek_uring() noexcept {
[[maybe_unused]] const liburingcxx::cq_entry *cqe;
ring.peek_cq_entry(cqe);
return cqe != nullptr;
}

} // namespace co_context::detail
9 changes: 6 additions & 3 deletions include/co_context/generator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,8 @@ class _Gen_promise_base {
}
}(std::allocator_arg, _Elem.allocator,
::std::ranges::begin(_Elem.range),
::std::ranges::end(_Elem.range))};
::std::ranges::end(_Elem.range))
};
}

#pragma GCC diagnostic pop
Expand Down Expand Up @@ -567,7 +568,8 @@ class generator
generator get_return_object() noexcept {
return generator{
_Gen_secret_tag{},
std::coroutine_handle<promise_type>::from_promise(*this)};
std::coroutine_handle<promise_type>::from_promise(*this)
};
}
};

Expand Down Expand Up @@ -600,7 +602,8 @@ class generator
return _Gen_iter<_Value, _Ref>{
_Gen_secret_tag{},
std::coroutine_handle<_Gen_promise_base<_Gen_yield_t<_Ref>>>::
from_address(_Coro.address())};
from_address(_Coro.address())
};
}

[[nodiscard]]
Expand Down
12 changes: 8 additions & 4 deletions include/co_context/lazy_io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ inline namespace lazy {
unsigned flags
) noexcept {
return detail::lazy_poll_update{
old_user_data, new_user_data, poll_mask, flags};
old_user_data, new_user_data, poll_mask, flags
};
}

[[CO_CONTEXT_AWAIT_HINT]]
Expand Down Expand Up @@ -400,7 +401,8 @@ inline namespace lazy {
unsigned buf_index
) noexcept {
return detail::lazy_send_zc_fixed{
sockfd, buf, flags, zc_flags, buf_index};
sockfd, buf, flags, zc_flags, buf_index
};
}

[[CO_CONTEXT_AWAIT_HINT]]
Expand Down Expand Up @@ -553,7 +555,8 @@ inline namespace lazy {
uint32_t cqe_flags
) noexcept {
return detail::lazy_msg_ring_cqe_flags{
fd, cqe_res, cqe_user_data, flags, cqe_flags};
fd, cqe_res, cqe_user_data, flags, cqe_flags
};
}
#endif

Expand Down Expand Up @@ -600,7 +603,8 @@ inline namespace lazy {
unsigned int flags
) noexcept {
return detail::lazy_socket_direct{
domain, type, protocol, file_index, flags};
domain, type, protocol, file_index, flags
};
}

[[CO_CONTEXT_AWAIT_HINT]]
Expand Down
Loading
Loading