Skip to content

Commit

Permalink
Use Boost.Context instead of SerialExecutor
Browse files Browse the repository at this point in the history
Summary: Implement `StackExecutor` with `Boost.Context`.

Differential Revision: D66214582
  • Loading branch information
tmikov authored and facebook-github-bot committed Dec 6, 2024
1 parent e4b62b1 commit 6d62f45
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ add_subdirectory(Sema)
add_library(hermescompiler STATIC)
target_link_libraries(hermescompiler PUBLIC
LLVHSupport_obj
${BOOST_CONTEXT_LIB}
dtoa_obj
hermesADT_obj
hermesAST2JS_obj
Expand All @@ -118,6 +119,7 @@ target_link_libraries(hermescompiler PUBLIC
add_library(hermesvm_a STATIC)
target_link_libraries(hermesvm_a PUBLIC
LLVHSupport_obj
${BOOST_CONTEXT_LIB}
dtoa_obj
hermesapi_obj
hermesADT_obj
Expand Down Expand Up @@ -147,6 +149,7 @@ target_link_libraries(hermesvm_a PUBLIC
add_library(hermesvmlean_a STATIC)
target_link_libraries(hermesvmlean_a PUBLIC
LLVHSupport_obj
${BOOST_CONTEXT_LIB}
dtoa_obj
hermesapiLean_obj
hermesADT_obj
Expand Down
6 changes: 5 additions & 1 deletion lib/Support/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ if (ANDROID)
set(LINK_LIBS log)
endif()

if (HERMES_USE_BOOST_CONTEXT)
include_directories(${PROJECT_SOURCE_DIR}/external/boost/boost_1_86_0)
endif()

add_hermes_library(hermesSupport
Allocator.cpp
Base64.cpp
Expand Down Expand Up @@ -41,6 +45,6 @@ add_hermes_library(hermesSupport
LEB128.cpp
MemorySizeParser.cpp
RandomSeedParser.cpp
LINK_OBJLIBS ${LINK_OBJLIBS}
LINK_OBJLIBS ${LINK_OBJLIBS} ${BOOST_CONTEXT_LIB}
LINK_LIBS ${LINK_LIBS}
)
76 changes: 76 additions & 0 deletions lib/Support/StackExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,81 @@

#include "hermes/Support/StackExecutor.h"

#if defined(HERMES_USE_BOOST_CONTEXT) && HERMES_USE_BOOST_CONTEXT
#include "llvh/Support/Debug.h"
#include "llvh/Support/raw_ostream.h"

#include <boost/context/fiber.hpp>
#include <boost/context/protected_fixedsize_stack.hpp>

#include <cassert>

#define DEBUG_TYPE "StackExecutor"

namespace ctx = boost::context;

namespace hermes {

class StackExecutor {
/// The argument to be passed to the callee.
void *arg_ = nullptr;
/// The callee to be invoked by the fiber each time it is resumed. If it is
/// nullptr when the fiber is resumed, the fiber terminates.
void (*fn_)(void *) = nullptr;
/// The fiber with its own stack.
ctx::fiber fiber_;

public:
explicit StackExecutor(size_t stackSize = 0)
: fiber_(
std::allocator_arg,
ctx::protected_fixedsize_stack(
stackSize ? stackSize : ctx::stack_traits::default_size()),
[this](ctx::fiber &&next) {
// Loop until the callee becomes nullptr.
while (fn_) {
LLVM_DEBUG(llvh::dbgs() << "StackExecutor running function\n");
(*fn_)(arg_);
LLVM_DEBUG(llvh::dbgs() << "StackExecutor function done\n");
next = std::move(next).resume();
}
LLVM_DEBUG(llvh::dbgs() << "Ending StackExecutor fiber\n");
// The std::move() here is required by older compilers.
return std::move(next);
}) {}

/// Execute the specified function in the fiber.
void exec(void *arg, void (*fn)(void *arg)) {
assert(fn_ == nullptr && "StackExecutor recursive call??");
arg_ = arg;
fn_ = fn;
fiber_ = std::move(fiber_).resume();
arg_ = nullptr;
fn_ = nullptr;
}

~StackExecutor() {
LLVM_DEBUG(llvh::dbgs() << "~StackExecutor");
assert(fn_ == nullptr && "StackExecutor recursive call??");
// Tell the fiber to terminate.
fiber_ = std::move(fiber_).resume();
}
};

std::shared_ptr<StackExecutor> newStackExecutor(
size_t stackSize,
std::chrono::milliseconds /*timeout*/) {
return std::make_shared<StackExecutor>(stackSize);
}

void executeInStack(StackExecutor &exec, void *arg, void (*func)(void *arg)) {
exec.exec(arg, func);
}

} // namespace hermes

#else

#include "hermes/Support/SerialExecutor.h"

#include <future>
Expand Down Expand Up @@ -35,3 +110,4 @@ void executeInStack(StackExecutor &exec, void *arg, void (*func)(void *arg)) {
}

} // namespace hermes
#endif

0 comments on commit 6d62f45

Please sign in to comment.