From 83dd8f91a7c4ba827a08db262c3aaa5f9413b14f Mon Sep 17 00:00:00 2001 From: q66 Date: Tue, 24 Jan 2017 00:32:16 +0100 Subject: [PATCH] use standard c++ threading --- main.cc | 42 +++++++++++++++--------------- tpool.hh | 78 +++++++++++++++++++++++++++----------------------------- 2 files changed, 59 insertions(+), 61 deletions(-) diff --git a/main.cc b/main.cc index b206afb..7e1b5a9 100644 --- a/main.cc +++ b/main.cc @@ -1,16 +1,19 @@ +#include +#include +#include +#include +#include + #include +#include #include #include -#include #include -#include #include #include #include #include #include -#include -#include #include @@ -21,9 +24,6 @@ using ostd::Vector; using ostd::Map; using ostd::String; using ostd::slice_until; -using ostd::UniqueLock; -using ostd::Mutex; -using ostd::Condition; using cscript::CsState; using cscript::CsValueRange; @@ -294,32 +294,32 @@ struct ObState: CsState { RuleCounter(): p_cond(), p_mtx(), p_counter(0), p_result(0) {} void wait() { - UniqueLock l(p_mtx); + std::unique_lock l(p_mtx); while (p_counter) { p_cond.wait(l); } } void incr() { - UniqueLock l(p_mtx); + std::unique_lock l(p_mtx); ++p_counter; } void decr() { - UniqueLock l(p_mtx); + std::unique_lock l(p_mtx); if (!--p_counter) { l.unlock(); - p_cond.broadcast(); + p_cond.notify_all(); } } - ostd::AtomicInt &get_result() { return p_result; } + std::atomic_int &get_result() { return p_result; } private: - Condition p_cond; - Mutex p_mtx; + std::condition_variable p_cond; + std::mutex p_mtx; int p_counter; - ostd::AtomicInt p_result; + std::atomic_int p_result; }; Vector counters; @@ -409,7 +409,7 @@ struct ObState: CsState { auto dsv = ostd::appender(); ostd::concat(dsv, subdeps); - sourcesv.set_str(ostd::move(dsv.get())); + sourcesv.set_str(std::move(dsv.get())); sourcesv.push(); } @@ -590,7 +590,7 @@ int main(int argc, char **argv) { os.init_libs(); - int ncpus = ostd::Thread::hardware_concurrency(); + int ncpus = std::thread::hardware_concurrency(); os.new_ivar("numcpus", 4096, 1, ncpus); ConstCharRange fcont; @@ -637,7 +637,7 @@ int main(int argc, char **argv) { } os.new_ivar("numjobs", 4096, 1, jobs); - ThreadPool tpool; + thread_pool tpool; tpool.init(jobs); os.register_rulecmds(); @@ -664,7 +664,7 @@ int main(int argc, char **argv) { res.set_cstr(""); return; } - res.set_str(ostd::move( + res.set_str(std::move( ostd::env_get(args[0].get_str()).value_or(args[1].get_str()) )); }); @@ -696,7 +696,7 @@ int main(int argc, char **argv) { ret += it; } } - res.set_str(ostd::move(ret)); + res.set_str(std::move(ret)); }); os.new_command("invoke", "s", [&os](auto &, auto args, auto &res) { @@ -709,7 +709,7 @@ int main(int argc, char **argv) { while (p.parse()) { ob_expand_glob(ret, p.get_item()); } - res.set_str(ostd::move(ret)); + res.set_str(std::move(ret)); }); if ((!fcont.empty() && !os.run_bool(fcont)) || !os.run_file(deffile)) { diff --git a/tpool.hh b/tpool.hh index b87d1ad..d58c5d4 100644 --- a/tpool.hh +++ b/tpool.hh @@ -1,37 +1,36 @@ #ifndef OBUILD_TPOOL_HH #define OBUILD_TPOOL_HH -#include +#include +#include +#include +#include +#include +#include -using ostd::Thread; -using ostd::UniqueLock; -using ostd::Mutex; -using ostd::Condition; -using ostd::Vector; - -struct ThreadPool { - ThreadPool(): +struct thread_pool { + thread_pool(): cond(), mtx(), thrs(), tasks(nullptr), last_task(nullptr), running(false) {} - ~ThreadPool() { + ~thread_pool() { destroy(); } static void *thr_func(void *ptr) { - static_cast(ptr)->run(); + static_cast(ptr)->run(); return nullptr; } - bool init(ostd::Size size) { + bool init(size_t size) { running = true; - for (ostd::Size i = 0; i < size; ++i) { - Thread tid([this]() { run(); }); - if (!tid) { + for (size_t i = 0; i < size; ++i) { + std::thread tid{[this]() { run(); }}; + if (!tid.joinable()) { return false; } - thrs.push(ostd::move(tid)); + thrs.push_back(std::move(tid)); } return true; } @@ -44,24 +43,23 @@ struct ThreadPool { } running = false; mtx.unlock(); - cond.broadcast(); - for (Thread &tid: thrs.iter()) { + cond.notify_all(); + for (std::thread &tid: thrs) { tid.join(); - cond.broadcast(); + cond.notify_all(); } } void run() { for (;;) { - UniqueLock l(mtx); + std::unique_lock l(mtx); while (running && (tasks == nullptr)) { cond.wait(l); } if (!running) { - l.unlock(); - ostd::this_thread::exit(); + return; } - Task *t = tasks; + task *t = tasks; tasks = t->next; if (last_task == t) { last_task = nullptr; @@ -72,9 +70,9 @@ struct ThreadPool { } } - void push(ostd::Function func) { + void push(std::function func) { mtx.lock(); - Task *t = new Task(ostd::move(func)); + task *t = new task(std::move(func)); if (last_task) { last_task->next = t; } @@ -82,27 +80,27 @@ struct ThreadPool { if (!tasks) { tasks = t; } - cond.signal(); + cond.notify_one(); mtx.unlock(); } private: - struct Task { - ostd::Function cb; - Task *next = nullptr; - Task() = delete; - Task(Task const &) = delete; - Task(Task &&) = delete; - Task(ostd::Function &&cbf): cb(ostd::move(cbf)) {} - Task &operator=(Task const &) = delete; - Task &operator=(Task &&) = delete; + struct task { + std::function cb; + task *next = nullptr; + task() = delete; + task(task const &) = delete; + task(task &&) = delete; + task(std::function &&cbf): cb(ostd::move(cbf)) {} + task &operator=(task const &) = delete; + task &operator=(task &&) = delete; }; - Condition cond; - Mutex mtx; - Vector thrs; - Task *tasks; - Task *last_task; + std::condition_variable cond; + std::mutex mtx; + std::vector thrs; + task *tasks; + task *last_task; bool volatile running; };