diff options
author | Shinichiro Hamaji <shinichiro.hamaji@gmail.com> | 2016-02-26 16:51:50 +0900 |
---|---|---|
committer | Shinichiro Hamaji <shinichiro.hamaji@gmail.com> | 2016-02-26 16:52:05 +0900 |
commit | 8380fb8525adfb580af1df23a54f316ab384be06 (patch) | |
tree | 82465ed6478885df816604af1825be817ae5f47f | |
parent | 3388e696c2c5ec81deb478e446f48b5dc1da2200 (diff) | |
download | kati-8380fb8525adfb580af1df23a54f316ab384be06.tar.gz |
[C++] Use C++11's threading library again
It seems we can use it even for -static build with
-Wl,--whole-archive -lpthread -Wl,--no-whole-archive -ldl
-rw-r--r-- | Makefile.ckati | 3 | ||||
-rw-r--r-- | condvar.cc | 42 | ||||
-rw-r--r-- | condvar.h | 35 | ||||
-rw-r--r-- | mutex.cc | 37 | ||||
-rw-r--r-- | mutex.h | 50 | ||||
-rw-r--r-- | ninja.cc | 4 | ||||
-rw-r--r-- | regen.cc | 8 | ||||
-rw-r--r-- | stats.cc | 12 | ||||
-rw-r--r-- | stats.h | 5 | ||||
-rw-r--r-- | thread.cc | 34 | ||||
-rw-r--r-- | thread.h | 37 | ||||
-rw-r--r-- | thread_pool.cc | 14 |
12 files changed, 21 insertions, 260 deletions
diff --git a/Makefile.ckati b/Makefile.ckati index 38e1feb..377e36d 100644 --- a/Makefile.ckati +++ b/Makefile.ckati @@ -24,7 +24,6 @@ KATI_BIN_PATH ?= . KATI_CXX_SRCS := \ affinity.cc \ command.cc \ - condvar.cc \ dep.cc \ eval.cc \ exec.cc \ @@ -38,7 +37,6 @@ KATI_CXX_SRCS := \ io.cc \ log.cc \ main.cc \ - mutex.cc \ ninja.cc \ parser.cc \ regen.cc \ @@ -49,7 +47,6 @@ KATI_CXX_SRCS := \ stringprintf.cc \ strutil.cc \ symtab.cc \ - thread.cc \ thread_pool.cc \ timeutil.cc \ var.cc diff --git a/condvar.cc b/condvar.cc deleted file mode 100644 index f8b488b..0000000 --- a/condvar.cc +++ /dev/null @@ -1,42 +0,0 @@ -// Copyright 2016 Google Inc. All rights reserved -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "condvar.h" - -#include "log.h" - -condition_variable::condition_variable() { - if (pthread_cond_init(&cond_, NULL) != 0) - PERROR("pthread_cond_init"); -} - -condition_variable::~condition_variable() { - if (pthread_cond_destroy(&cond_) != 0) - PERROR("pthread_cond_destroy"); -} - -void condition_variable::wait(const UniqueLock<Mutex>& mu) { - if (pthread_cond_wait(&cond_, &mu.Mutex()->mu_) != 0) - PERROR("pthread_cond_wait"); -} - -void condition_variable::notify_one() { - if (pthread_cond_signal(&cond_) != 0) - PERROR("pthread_cond_signal"); -} - -void condition_variable::notify_all() { - if (pthread_cond_broadcast(&cond_) != 0) - PERROR("pthread_cond_broadcast"); -} diff --git a/condvar.h b/condvar.h deleted file mode 100644 index b75b9a4..0000000 --- a/condvar.h +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright 2016 Google Inc. All rights reserved -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#ifndef CONDVAR_H_ -#define CONDVAR_H_ - -#include <pthread.h> - -#include "mutex.h" - -class condition_variable { - public: - condition_variable(); - ~condition_variable(); - - void wait(const UniqueLock<Mutex>& mu); - void notify_one(); - void notify_all(); - - private: - pthread_cond_t cond_; -}; - -#endif // CONDVAR_H_ diff --git a/mutex.cc b/mutex.cc deleted file mode 100644 index 986366b..0000000 --- a/mutex.cc +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright 2016 Google Inc. All rights reserved -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "mutex.h" - -#include "log.h" - -Mutex::Mutex() { - if (pthread_mutex_init(&mu_, NULL) != 0) - PERROR("pthread_mutex_init"); -} - -Mutex::~Mutex() { - if (pthread_mutex_destroy(&mu_) != 0) - PERROR("pthread_mutex_destroy"); -} - -void Mutex::lock() { - if (pthread_mutex_lock(&mu_) != 0) - PERROR("pthread_mutex_lock"); -} - -void Mutex::unlock() { - if (pthread_mutex_unlock(&mu_) != 0) - PERROR("pthread_mutex_unlock"); -} diff --git a/mutex.h b/mutex.h deleted file mode 100644 index e730294..0000000 --- a/mutex.h +++ /dev/null @@ -1,50 +0,0 @@ -// Copyright 2016 Google Inc. All rights reserved -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#ifndef MUTEX_H_ -#define MUTEX_H_ - -#include <pthread.h> - -class Mutex { - public: - explicit Mutex(); - ~Mutex(); - - void lock(); - void unlock(); - - private: - pthread_mutex_t mu_; - - friend class condition_variable; -}; - -template<class T> class UniqueLock { - public: - explicit UniqueLock(T& mu) - : mu_(mu) { - mu_.lock(); - } - ~UniqueLock() { - mu_.unlock(); - } - - T* Mutex() const { return &mu_; } - - private: - T& mu_; -}; - -#endif // MUTEX_H_ @@ -569,7 +569,7 @@ class NinjaGenerator { if (use_local_pool) *o << " pool = local_pool\n"; if (node->is_default_target) { - UniqueLock<Mutex> lock(mu_); + unique_lock<mutex> lock(mu_); default_target_ = node; } } @@ -787,7 +787,7 @@ class NinjaGenerator { const double start_time_; vector<NinjaNode*> nodes_; - Mutex mu_; + mutex mu_; const DepNode* default_target_; }; @@ -18,13 +18,13 @@ #include <algorithm> #include <memory> +#include <mutex> #include <vector> #include "fileutil.h" #include "find.h" #include "io.h" #include "log.h" -#include "mutex.h" #include "ninja.h" #include "stats.h" #include "strutil.h" @@ -364,7 +364,7 @@ class StampChecker { // TODO: Make glob cache thread safe and create a task for each glob. for (GlobResult* gr : globs_) { if (CheckGlobResult(gr, &err)) { - UniqueLock<Mutex> lock(mu_); + unique_lock<mutex> lock(mu_); if (!needs_regen_) { needs_regen_ = true; msg_ = err; @@ -378,7 +378,7 @@ class StampChecker { tp->Submit([this, sr]() { string err; if (CheckShellResult(sr, &err)) { - UniqueLock<Mutex> lock(mu_); + unique_lock<mutex> lock(mu_); if (!needs_regen_) { needs_regen_ = true; msg_ = err; @@ -398,7 +398,7 @@ class StampChecker { double gen_time_; vector<GlobResult*> globs_; vector<ShellResult*> commands_; - Mutex mu_; + mutex mu_; bool needs_regen_; string msg_; }; @@ -16,18 +16,18 @@ #include "stats.h" +#include <mutex> #include <vector> #include "flags.h" #include "log.h" -#include "mutex.h" #include "stringprintf.h" #include "thread_local.h" #include "timeutil.h" namespace { -Mutex g_mu; +mutex g_mu; vector<Stats*>* g_stats; DEFINE_THREAD_LOCAL(double, g_start_time); @@ -35,21 +35,21 @@ DEFINE_THREAD_LOCAL(double, g_start_time); Stats::Stats(const char* name) : name_(name), elapsed_(0), cnt_(0) { - UniqueLock<Mutex> lock(g_mu); + unique_lock<mutex> lock(g_mu); if (g_stats == NULL) g_stats = new vector<Stats*>; g_stats->push_back(this); } string Stats::String() const { - UniqueLock<Mutex> lock(mu_); + unique_lock<mutex> lock(mu_); return StringPrintf("%s: %f / %d", name_, elapsed_, cnt_); } void Stats::Start() { CHECK(!TLS_REF(g_start_time)); TLS_REF(g_start_time) = GetTime(); - UniqueLock<Mutex> lock(mu_); + unique_lock<mutex> lock(mu_); cnt_++; } @@ -57,7 +57,7 @@ double Stats::End() { CHECK(TLS_REF(g_start_time)); double e = GetTime() - TLS_REF(g_start_time); TLS_REF(g_start_time) = 0; - UniqueLock<Mutex> lock(mu_); + unique_lock<mutex> lock(mu_); elapsed_ += e; return e; } @@ -15,10 +15,9 @@ #ifndef STATS_H_ #define STATS_H_ +#include <mutex> #include <string> -#include "mutex.h" - using namespace std; class Stats { @@ -36,7 +35,7 @@ class Stats { const char* name_; double elapsed_; int cnt_; - mutable Mutex mu_; + mutable mutex mu_; }; class ScopedStatsRecorder { diff --git a/thread.cc b/thread.cc deleted file mode 100644 index 9733208..0000000 --- a/thread.cc +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright 2016 Google Inc. All rights reserved -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "thread.h" - -#include "log.h" - -thread::thread(const fn_t& fn) { - if (pthread_create(&th_, NULL, &thread::Run, new fn_t(fn)) != 0) - PERROR("pthread_create"); -} - -void thread::join() { - if (pthread_join(th_, NULL) != 0) - PERROR("pthread_join"); -} - -void* thread::Run(void* p) { - fn_t* fn = static_cast<fn_t*>(p); - (*fn)(); - delete fn; - return NULL; -} diff --git a/thread.h b/thread.h deleted file mode 100644 index 440fc5b..0000000 --- a/thread.h +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright 2016 Google Inc. All rights reserved -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#ifndef THREAD_H_ -#define THREAD_H_ - -#include <pthread.h> - -#include <functional> - -using namespace std; - -class thread { - typedef function<void(void)> fn_t; - - public: - explicit thread(const fn_t& fn); - void join(); - - private: - static void* Run(void* p); - - pthread_t th_; -}; - -#endif // THREAD_H_ diff --git a/thread_pool.cc b/thread_pool.cc index c504ef5..0a12bfa 100644 --- a/thread_pool.cc +++ b/thread_pool.cc @@ -14,13 +14,13 @@ #include "thread_pool.h" +#include <condition_variable> +#include <mutex> #include <stack> +#include <thread> #include <vector> #include "affinity.h" -#include "condvar.h" -#include "mutex.h" -#include "thread.h" class ThreadPoolImpl : public ThreadPool { public: @@ -37,14 +37,14 @@ class ThreadPoolImpl : public ThreadPool { } virtual void Submit(function<void(void)> task) override { - UniqueLock<Mutex> lock(mu_); + unique_lock<mutex> lock(mu_); tasks_.push(task); cond_.notify_one(); } virtual void Wait() override { { - UniqueLock<Mutex> lock(mu_); + unique_lock<mutex> lock(mu_); is_waiting_ = true; cond_.notify_all(); } @@ -61,7 +61,7 @@ class ThreadPoolImpl : public ThreadPool { while (true) { function<void(void)> task; { - UniqueLock<Mutex> lock(mu_); + unique_lock<mutex> lock(mu_); if (tasks_.empty()) { if (is_waiting_) return; @@ -79,7 +79,7 @@ class ThreadPoolImpl : public ThreadPool { } vector<thread> threads_; - Mutex mu_; + mutex mu_; condition_variable cond_; stack<function<void(void)>> tasks_; bool is_waiting_; |