From 5bd9f470f4cb4649def5439aff56fd33f87248b3 Mon Sep 17 00:00:00 2001 From: q66 Date: Tue, 2 Aug 2016 19:54:05 +0100 Subject: [PATCH] fixes and cleanups --- main.cc | 48 ++++++++++++++++++++++++++++-------------------- obuild.cfg | 7 ++----- 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/main.cc b/main.cc index 644d853..466a0a5 100644 --- a/main.cc +++ b/main.cc @@ -36,12 +36,13 @@ using cscript::Bytecode; /* thread pool */ struct ThreadPool { - ThreadPool() {} + ThreadPool(): + cond(), mtx(), thrs(), tasks(nullptr), last_task(nullptr), + running(false) + {} ~ThreadPool() { - if (running) { - destroy(); - } + destroy(); } static void *thr_func(void *ptr) { @@ -63,6 +64,10 @@ struct ThreadPool { void destroy() { mtx.lock(); + if (!running) { + mtx.unlock(); + return; + } running = false; mtx.unlock(); cond.broadcast(); @@ -235,32 +240,35 @@ struct ObState: CsState { Map> cache; struct RuleCounter { - RuleCounter(): cond(), mtx(), counter(0), result(0) {} + RuleCounter(): p_cond(), p_mtx(), p_counter(0), p_result(0) {} void wait() { - UniqueLock l(mtx); - while (counter) { - cond.wait(l); + UniqueLock l(p_mtx); + while (p_counter) { + p_cond.wait(l); } } void incr() { - UniqueLock l(mtx); - ++counter; + UniqueLock l(p_mtx); + ++p_counter; } void decr() { - UniqueLock l(mtx); - if (!--counter) { + UniqueLock l(p_mtx); + if (!--p_counter) { l.unlock(); - cond.broadcast(); + p_cond.broadcast(); } } - Condition cond; - Mutex mtx; - int volatile counter; - ostd::AtomicInt result; + ostd::AtomicInt &get_result() { return p_result; } + +private: + Condition p_cond; + Mutex p_mtx; + int p_counter; + ostd::AtomicInt p_result; }; Vector counters; @@ -275,7 +283,7 @@ struct ObState: CsState { return ret; } ctr.wait(); - return ctr.result; + return ctr.get_result(); } template @@ -575,8 +583,8 @@ int main(int argc, char **argv) { cnt->incr(); tpool.push([cnt, ds = String(args[0].get_strr())]() { int ret = system(ds.data()); - if (ret && !cnt->result) { - cnt->result = ret; + if (ret && !cnt->get_result()) { + cnt->get_result() = ret; } cnt->decr(); }); diff --git a/obuild.cfg b/obuild.cfg index 0036762..1eb2d81 100644 --- a/obuild.cfg +++ b/obuild.cfg @@ -5,13 +5,13 @@ OB_CXXFLAGS = "-g -Wall -Wextra -Wshadow -Wold-style-cast -O2" CS_PATH = "../libcubescript" OS_PATH = "../octastd" -FILES = [main_ob.o globs_ob.o cubescript_ob.o] +FILES = [main_ob.o globs_ob.o] OB_CXXFLAGS = [@OB_CXXFLAGS -std=c++14 -I. -I@CS_PATH -I@OS_PATH -pthread] rule obuild $FILES [ echo " LD" $target - shell $CXX $OB_CXXFLAGS -o obuild_ob $sources + shell $CXX $OB_CXXFLAGS -o obuild_ob $sources [@CS_PATH/libcubescript.a] ] rule %_ob.o %.cc [ @@ -19,8 +19,6 @@ rule %_ob.o %.cc [ shell $CXX $OB_CXXFLAGS -c -o $target $source ] -duprule cubescript_ob.o %_ob.o [@CS_PATH/cubescript.cc] - action clean [ echo " CLEAN" $FILES obuild_ob shell rm -f $FILES obuild_ob @@ -28,6 +26,5 @@ action clean [ depend main_ob.o [@CS_PATH/cubescript.hh] depend globs_ob.o [@CS_PATH/cubescript.hh] -depend cubescript_ob.o [@CS_PATH/cubescript.hh] rule default obuild