Skip to content

Commit

Permalink
threads: export should_yield / yield into thread_impl
Browse files Browse the repository at this point in the history
Usually we can access static methods of a thread, such as yield, easily by just
calling seastar::thread::yield. Since the earliest commit that introduces
threads, however (cd0ae46), we have a separate "thread_impl" with some
of the methods alternatively exported. Although that is never explicitly
explained in that commit, my understanding is that using the thread methods
directly requires us to import thread.hh, which brings in the reactor, which
simply has no place in the future core machinery.

In preparation to yield from inside the future object, this patch exports
should_yield and yield into thread_impl.

v2: should_yield can now be inline and will not generate an explicit out of line
call.

Signed-off-by: Glauber Costa <[email protected]>
  • Loading branch information
Glauber Costa committed Aug 15, 2016
1 parent 9a1efe0 commit 7a4e706
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions core/thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ thread_context::main() {

namespace thread_impl {

void yield() {
g_current_context->thread->yield();
}

void switch_in(thread_context* to) {
to->switch_in();
}
Expand Down
12 changes: 12 additions & 0 deletions core/thread_impl.hh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/

#pragma once
#include "preempt.hh"
#include <setjmp.h>
#include <chrono>
#include <experimental/optional>
Expand Down Expand Up @@ -51,6 +52,17 @@ inline thread_context* get() {
return g_current_context->thread;
}

inline bool should_yield() {
if (need_preempt()) {
return true;
} else if (g_current_context->yield_at) {
return std::chrono::steady_clock::now() >= *(g_current_context->yield_at);
} else {
return false;
}
}

void yield();
void switch_in(thread_context* to);
void switch_out(thread_context* from);
void init();
Expand Down

0 comments on commit 7a4e706

Please sign in to comment.