Skip to content

Instantly share code, notes, and snippets.

@rightfold

rightfold/vm.cpp Secret

Created December 19, 2014 20:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rightfold/4bd64407e62526baa808 to your computer and use it in GitHub Desktop.
Save rightfold/4bd64407e62526baa808 to your computer and use it in GitHub Desktop.
Styx::VM::VM() : run(true), work(ioService) {
auto nthreads = std::thread::hardware_concurrency();
if (nthreads == 0) {
nthreads = 1;
}
for (decltype(nthreads) i = 0; i < nthreads; ++i) {
threads.create_thread([this] {
while (run) {
Fiber* fiber;
scheduledFibers.pop(fiber);
resume(*fiber);
}
});
}
threads.create_thread([this] {
ioService.run();
});
}
Styx::VM::~VM() {
run = false;
ioService.stop();
threads.join_all();
}
class VM {
public:
VM();
~VM();
std::future<GCPtr<>> call(GCPtr<Function> function, GCPtr<> arg);
private:
std::atomic<bool> run;
public:
GC gc;
boost::asio::io_service ioService;
private:
Fiber& spawn(GCPtr<Function> function);
void resume(Fiber& fiber);
boost::asio::io_service::work work;
std::vector<std::unique_ptr<Fiber>> fibers;
tbb::concurrent_bounded_queue<Fiber*> scheduledFibers;
boost::thread_group threads;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment