diff options
author | 2017-08-17 14:56:25 -0700 | |
---|---|---|
committer | 2017-08-17 15:03:24 -0700 | |
commit | b48cfaea2aea3707a33e60c10385a87e37101b95 (patch) | |
tree | 897738a42d520918efcc9bbece881c5c2fcce583 /tensorflow/stream_executor/platform | |
parent | d30537a10529a73b712d805a1f26fd39ce5be609 (diff) |
Make tensorflow::mutex implement a shared (reader/writer) lock, using
open source nsync library.
PiperOrigin-RevId: 165633487
Diffstat (limited to 'tensorflow/stream_executor/platform')
-rw-r--r-- | tensorflow/stream_executor/platform/default/mutex.h | 74 |
1 files changed, 10 insertions, 64 deletions
diff --git a/tensorflow/stream_executor/platform/default/mutex.h b/tensorflow/stream_executor/platform/default/mutex.h index ac2f123d5c..62de0cbce0 100644 --- a/tensorflow/stream_executor/platform/default/mutex.h +++ b/tensorflow/stream_executor/platform/default/mutex.h @@ -16,78 +16,24 @@ limitations under the License. #ifndef TENSORFLOW_STREAM_EXECUTOR_PLATFORM_DEFAULT_MUTEX_H_ #define TENSORFLOW_STREAM_EXECUTOR_PLATFORM_DEFAULT_MUTEX_H_ -#include <chrono> // NOLINT -#include <condition_variable> // NOLINT - -#include "tensorflow/stream_executor/platform/port.h" - -// std::shared_timed_mutex is a C++14 feature. -#if (__cplusplus >= 201402L) -#define STREAM_EXECUTOR_USE_SHARED_MUTEX -#endif // __cplusplus >= 201402L - -#ifdef STREAM_EXECUTOR_USE_SHARED_MUTEX -#include <shared_mutex> // NOLINT -#else -#include <mutex> // NOLINT -#endif +#include "tensorflow/stream_executor/platform/mutex.h" namespace perftools { namespace gputools { #undef mutex_lock -#undef shared_lock - -enum ConditionResult { kCond_Timeout, kCond_MaybeNotified }; - -#ifdef STREAM_EXECUTOR_USE_SHARED_MUTEX -typedef std::shared_timed_mutex BaseMutex; -typedef std::condition_variable_any ConditionVariableForMutex; -#else -typedef std::mutex BaseMutex; -typedef std::condition_variable ConditionVariableForMutex; -#endif - -// A class that wraps around the std::mutex implementation, only adding an -// additional LinkerInitialized constructor interface. -class LOCKABLE mutex : public BaseMutex { - public: - mutex() {} - // The default implementation of std::mutex is safe to use after the linker - // initializations - explicit mutex(LinkerInitialized x) {} +#undef tf_shared_lock - void lock() ACQUIRE() { BaseMutex::lock(); } - void unlock() RELEASE() { BaseMutex::unlock(); } -}; +using tensorflow::ConditionResult; +using tensorflow::WaitForMilliseconds; +using tensorflow::condition_variable; +using tensorflow::mutex; +using tensorflow::mutex_lock; +using tensorflow::tf_shared_lock; -class SCOPED_LOCKABLE mutex_lock : public std::unique_lock<BaseMutex> { - public: - mutex_lock(class mutex& m) ACQUIRE(m) : std::unique_lock<BaseMutex>(m) {} - ~mutex_lock() RELEASE() {} -}; - -// Catch bug where variable name is omitted, e.g. mutex_lock (mu); #define mutex_lock(x) static_assert(0, "mutex_lock_decl_missing_var_name"); - -#ifdef STREAM_EXECUTOR_USE_SHARED_MUTEX -// TODO(vrv): Annotate these with ACQUIRE_SHARED after implementing -// as classes. -typedef std::shared_lock<BaseMutex> shared_lock; -#else -typedef mutex_lock shared_lock; -#endif - -// Catch bug where variable name is omitted, e.g. shared_lock (mu); -#define shared_lock(x) static_assert(0, "shared_lock_decl_missing_var_name"); - -using std::condition_variable; - -inline ConditionResult WaitForMilliseconds(mutex_lock* mu, - ConditionVariableForMutex* cv, int64 ms) { - std::cv_status s = cv->wait_for(*mu, std::chrono::milliseconds(ms)); - return (s == std::cv_status::timeout) ? kCond_Timeout : kCond_MaybeNotified; -} +#define tf_shared_lock(x) \ + static_assert(0, "tf_shared_lock_decl_missing_var_name"); } // namespace gputools } // namespace perftools |