diff options
author | 2015-11-06 16:27:58 -0800 | |
---|---|---|
committer | 2015-11-06 16:27:58 -0800 | |
commit | f41959ccb2d9d4c722fe8fc3351401d53bcf4900 (patch) | |
tree | ef0ca22cb2a5ac4bdec9d080d8e0788a53ed496d /tensorflow/core/lib/core/blocking_counter.h |
TensorFlow: Initial commit of TensorFlow library.
TensorFlow is an open source software library for numerical computation
using data flow graphs.
Base CL: 107276108
Diffstat (limited to 'tensorflow/core/lib/core/blocking_counter.h')
-rw-r--r-- | tensorflow/core/lib/core/blocking_counter.h | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/tensorflow/core/lib/core/blocking_counter.h b/tensorflow/core/lib/core/blocking_counter.h new file mode 100644 index 0000000000..f141be2c76 --- /dev/null +++ b/tensorflow/core/lib/core/blocking_counter.h @@ -0,0 +1,41 @@ +#ifndef TENSORFLOW_LIB_CORE_BLOCKING_COUNTER_H_ +#define TENSORFLOW_LIB_CORE_BLOCKING_COUNTER_H_ + +#include "tensorflow/core/platform/logging.h" +#include "tensorflow/core/platform/port.h" + +namespace tensorflow { + +class BlockingCounter { + public: + BlockingCounter(int initial_count) : count_(initial_count) { + CHECK_GE(count_, 0); + } + + ~BlockingCounter() {} + + inline void DecrementCount() { + mutex_lock l(mu_); + --count_; + CHECK(count_ >= 0); + if (count_ == 0) { + cond_var_.notify_all(); + } + } + + inline void Wait() { + mutex_lock l(mu_); + while (count_ > 0) { + cond_var_.wait(l); + } + } + + private: + int count_; + mutex mu_; + condition_variable cond_var_; +}; + +} // namespace tensorflow + +#endif // TENSORFLOW_LIB_CORE_BLOCKING_COUNTER_H_ |