Skip to content

thread: optimize maybe_yield #2574

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

Merged
merged 1 commit into from
Dec 11, 2024
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
10 changes: 8 additions & 2 deletions include/seastar/core/thread.hh
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,19 @@ public:
///
/// Useful where we cannot call yield() immediately because we
/// Need to take some cleanup action first.
static bool should_yield();
static bool should_yield() {
return need_preempt();
}

/// \brief Yield if this thread ought to call yield() now.
///
/// Useful where a code does long running computation and does
/// not want to hog cpu for more then its share
static void maybe_yield();
static void maybe_yield() {
if (should_yield()) [[unlikely]] {
Copy link
Member

Choose a reason for hiding this comment

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

Note: unlikely is unnecessary (but not harmful) since need_preempt() is annotated with __builtin_expect.

yield();
}
}

static bool running_in_thread() {
return thread_impl::get() != nullptr;
Expand Down
11 changes: 0 additions & 11 deletions src/core/thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -354,17 +354,6 @@ void thread::yield() {
thread_impl::get()->yield();
}

bool thread::should_yield() {
return thread_impl::get()->should_yield();
}

void thread::maybe_yield() {
auto tctx = thread_impl::get();
if (tctx->should_yield()) {
tctx->yield();
}
}

}

/// \endcond
Loading