per-state threadpool

master
Daniel Kolesa 2015-10-20 22:05:53 +01:00
parent e348f58108
commit d5ec6eea73
1 changed files with 7 additions and 5 deletions

12
main.cc
View File

@ -25,8 +25,6 @@ using cscript::CsState;
using cscript::TvalRange; using cscript::TvalRange;
using cscript::StackedValue; using cscript::StackedValue;
static ThreadPool tpool;
/* check funcs */ /* check funcs */
static bool ob_check_ts(ConstCharRange tname, const Vector<String> &deps) { static bool ob_check_ts(ConstCharRange tname, const Vector<String> &deps) {
@ -98,6 +96,7 @@ static ConstCharRange ob_compare_subst(ConstCharRange expanded,
struct ObState { struct ObState {
CsState cs; CsState cs;
ConstCharRange progname; ConstCharRange progname;
ThreadPool tpool;
int jobs = 1; int jobs = 1;
bool ignore_env = false; bool ignore_env = false;
@ -170,6 +169,10 @@ struct ObState {
Vector<RuleCounter *> counters; Vector<RuleCounter *> counters;
~ObState() {
tpool.destroy();
}
template<typename ...A> template<typename ...A>
int error(int retcode, ConstCharRange fmt, A &&...args) { int error(int retcode, ConstCharRange fmt, A &&...args) {
ostd::err.write(progname, ": "); ostd::err.write(progname, ": ");
@ -397,13 +400,13 @@ int main(int argc, char **argv) {
} }
} }
tpool.init(os.jobs); os.tpool.init(os.jobs);
os.cs.add_command("shell", "C", [](CsState &cs, ConstCharRange s) { os.cs.add_command("shell", "C", [](CsState &cs, ConstCharRange s) {
auto cnt = ((ObState &)cs).counters.back(); auto cnt = ((ObState &)cs).counters.back();
cnt->incr(); cnt->incr();
char *ds = String(s).disown(); char *ds = String(s).disown();
tpool.push([cnt, ds]() { ((ObState &)cs).tpool.push([cnt, ds]() {
int ret = system(ds); int ret = system(ds);
delete[] ds; delete[] ds;
if (ret && !cnt->result) if (ret && !cnt->result)
@ -449,6 +452,5 @@ int main(int argc, char **argv) {
ret = maincnt.wait_result(os.counters, ret); ret = maincnt.wait_result(os.counters, ret);
if (ret) if (ret)
return ret; return ret;
tpool.destroy();
return 0; return 0;
} }