diff --git a/paddle/fluid/framework/rw_lock.h b/paddle/fluid/framework/rw_lock.h index dbf00f3a79f7d1dcf97b346fccfdb68f119d4aa3..dd918fcdfa6675f0f48849eb00ce0723634f24d6 100644 --- a/paddle/fluid/framework/rw_lock.h +++ b/paddle/fluid/framework/rw_lock.h @@ -16,7 +16,9 @@ limitations under the License. */ #if !defined(_WIN32) #include -#endif // !_WIN32 +#else +#include // NOLINT +#endif // !_WIN32 #include "paddle/fluid/platform/enforce.h" @@ -51,9 +53,15 @@ struct RWLock { // https://stackoverflow.com/questions/7125250/making-pthread-rwlock-wrlock-recursive // In windows, rw_lock seems like a hack. Use empty object and do nothing. struct RWLock { - void RDLock() {} - void WRLock() {} - void UNLock() {} + // FIXME(minqiyang): use mutex here to do fake lock + void RDLock() { mutex_.lock(); } + + void WRLock() { mutex_.lock(); } + + void UNLock() { mutex_.unlock(); } + + private: + std::mutex mutex_; }; #endif diff --git a/paddle/fluid/framework/scope.cc b/paddle/fluid/framework/scope.cc index 61416676d631ff84e54388c1e9da21a246a051d1..190a057d9e4baa016acbe09132fad50c827902bd 100644 --- a/paddle/fluid/framework/scope.cc +++ b/paddle/fluid/framework/scope.cc @@ -46,13 +46,10 @@ DEFINE_double( #define SCOPE_READER_LOCK #define SCOPE_WRITER_LOCK #else -// TODO(minqiyang): use reader lock and writer lock in all platforms -#define SCOPE_READER_LOCK -#define SCOPE_WRITER_LOCK -// #define SCOPE_READER_LOCK boost::shared_lock -// lock(mutex_); -// #define SCOPE_WRITER_LOCK boost::unique_lock -// lock(mutex_); +// TODO(minqiyang): use rwlock in all platforms, now rwlock is a fake one +// in _WIN32 platform +#define SCOPE_READER_LOCK RWLockGuard(&rw_lock_, RWLockGuard::Status::kRDLock); +#define SCOPE_WRITER_LOCK RWLockGuard(&rw_lock_, RWLockGuard::Status::kWRLock); #endif namespace paddle { diff --git a/paddle/fluid/framework/scope.h b/paddle/fluid/framework/scope.h index 1901ffbe57e0d85193c3a218f06eba06a0f287a5..c140212c3e44e5af599f74d12dc86e72cac471da 100644 --- a/paddle/fluid/framework/scope.h +++ b/paddle/fluid/framework/scope.h @@ -15,11 +15,11 @@ limitations under the License. */ #pragma once #include -#include // NOLINT #include #include #include +#include "paddle/fluid/framework/rw_lock.h" #include "paddle/fluid/framework/variable.h" #include "paddle/fluid/platform/macros.h" @@ -123,7 +123,7 @@ class Scope { DISABLE_COPY_AND_ASSIGN(Scope); private: - mutable std::mutex mutex_; + mutable RWLock rw_lock_; }; // Generate some debug string about the inherience structure of scope, quite