154e6dadea
This makes Engines (task execution strategies: serial, thread, fork) pluggable just like most of the rest of ok. It removes the thread and process limits, as I find myself rarely caring about what they are exactly. Instead of limiting to num-cores, we just allow any number of concurrent threads, and any number of concurrent child processes subject to OS limitations. Change-Id: Icef49d86818fe9a4b7380efb60e73e40bc2e6b73 Reviewed-on: https://skia-review.googlesource.com/27140 Reviewed-by: Mike Klein <mtklein@chromium.org> Commit-Queue: Mike Klein <mtklein@chromium.org>
86 lines
2.8 KiB
C++
86 lines
2.8 KiB
C++
/*
|
|
* Copyright 2017 Google Inc.
|
|
*
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
* found in the LICENSE file.
|
|
*/
|
|
|
|
#include "ok.h"
|
|
|
|
struct SerialEngine : Engine {
|
|
static std::unique_ptr<Engine> Factory(Options) {
|
|
SerialEngine engine;
|
|
return move_unique(engine);
|
|
}
|
|
|
|
bool crashproof() override { return false; }
|
|
|
|
std::future<Status> spawn(std::function<Status(void)> fn) override {
|
|
return std::async(std::launch::deferred, fn);
|
|
}
|
|
};
|
|
static Register serial("serial",
|
|
"Run tasks serially on the main thread of a single process.",
|
|
SerialEngine::Factory);
|
|
|
|
struct ThreadEngine : Engine {
|
|
static std::unique_ptr<Engine> Factory(Options) {
|
|
ThreadEngine engine;
|
|
return move_unique(engine);
|
|
}
|
|
|
|
bool crashproof() override { return false; }
|
|
|
|
std::future<Status> spawn(std::function<Status(void)> fn) override {
|
|
return std::async(std::launch::async, fn);
|
|
}
|
|
};
|
|
static Register thread("thread",
|
|
"Run each task on its own thread of a single process.",
|
|
ThreadEngine::Factory);
|
|
|
|
#if !defined(_MSC_VER)
|
|
#include <sys/wait.h>
|
|
#include <unistd.h>
|
|
|
|
struct ForkEngine : Engine {
|
|
static std::unique_ptr<Engine> Factory(Options) {
|
|
ForkEngine engine;
|
|
return move_unique(engine);
|
|
}
|
|
|
|
bool crashproof() override { return true; }
|
|
|
|
std::future<Status> spawn(std::function<Status(void)> fn) override {
|
|
switch (fork()) {
|
|
case 0:
|
|
// We are the spawned child process.
|
|
// Run fn() and exit() with its Status as our return code.
|
|
_exit((int)fn());
|
|
|
|
case -1:
|
|
// The OS won't let us fork() another process right now.
|
|
// We'll need to wait for at least one live task to finish and try again.
|
|
return std::future<Status>();
|
|
|
|
default:
|
|
// We succesfully spawned a child process!
|
|
// This will wait for any spawned process to finish and return its Status.
|
|
return std::async(std::launch::deferred, [] {
|
|
do {
|
|
int status;
|
|
if (wait(&status) > 0) {
|
|
return WIFEXITED(status) ? (Status)WEXITSTATUS(status)
|
|
: Status::Crashed;
|
|
}
|
|
} while (errno == EINTR);
|
|
return Status::None;
|
|
});
|
|
}
|
|
}
|
|
};
|
|
static Register _fork("fork",
|
|
"Run each task in an independent process with fork().",
|
|
ForkEngine::Factory);
|
|
#endif
|