提交 a61eb543 编写于 作者: M minqiyang

Add RWLock to Scope

上级 1b61021c
...@@ -16,7 +16,9 @@ limitations under the License. */ ...@@ -16,7 +16,9 @@ limitations under the License. */
#if !defined(_WIN32) #if !defined(_WIN32)
#include <pthread.h> #include <pthread.h>
#endif // !_WIN32 #else
#include <mutex> // NOLINT
#endif // !_WIN32
#include "paddle/fluid/platform/enforce.h" #include "paddle/fluid/platform/enforce.h"
...@@ -51,9 +53,15 @@ struct RWLock { ...@@ -51,9 +53,15 @@ struct RWLock {
// https://stackoverflow.com/questions/7125250/making-pthread-rwlock-wrlock-recursive // https://stackoverflow.com/questions/7125250/making-pthread-rwlock-wrlock-recursive
// In windows, rw_lock seems like a hack. Use empty object and do nothing. // In windows, rw_lock seems like a hack. Use empty object and do nothing.
struct RWLock { struct RWLock {
void RDLock() {} // FIXME(minqiyang): use mutex here to do fake lock
void WRLock() {} void RDLock() { mutex_.lock(); }
void UNLock() {}
void WRLock() { mutex_.lock(); }
void UNLock() { mutex_.unlock(); }
private:
std::mutex mutex_;
}; };
#endif #endif
......
...@@ -46,13 +46,10 @@ DEFINE_double( ...@@ -46,13 +46,10 @@ DEFINE_double(
#define SCOPE_READER_LOCK #define SCOPE_READER_LOCK
#define SCOPE_WRITER_LOCK #define SCOPE_WRITER_LOCK
#else #else
// TODO(minqiyang): use reader lock and writer lock in all platforms // TODO(minqiyang): use rwlock in all platforms, now rwlock is a fake one
#define SCOPE_READER_LOCK // in _WIN32 platform
#define SCOPE_WRITER_LOCK #define SCOPE_READER_LOCK RWLockGuard(&rw_lock_, RWLockGuard::Status::kRDLock);
// #define SCOPE_READER_LOCK boost::shared_lock<boost::shared_mutex> #define SCOPE_WRITER_LOCK RWLockGuard(&rw_lock_, RWLockGuard::Status::kWRLock);
// lock(mutex_);
// #define SCOPE_WRITER_LOCK boost::unique_lock<boost::shared_mutex>
// lock(mutex_);
#endif #endif
namespace paddle { namespace paddle {
......
...@@ -15,11 +15,11 @@ limitations under the License. */ ...@@ -15,11 +15,11 @@ limitations under the License. */
#pragma once #pragma once
#include <list> #include <list>
#include <mutex> // NOLINT
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#include <vector> #include <vector>
#include "paddle/fluid/framework/rw_lock.h"
#include "paddle/fluid/framework/variable.h" #include "paddle/fluid/framework/variable.h"
#include "paddle/fluid/platform/macros.h" #include "paddle/fluid/platform/macros.h"
...@@ -123,7 +123,7 @@ class Scope { ...@@ -123,7 +123,7 @@ class Scope {
DISABLE_COPY_AND_ASSIGN(Scope); DISABLE_COPY_AND_ASSIGN(Scope);
private: private:
mutable std::mutex mutex_; mutable RWLock rw_lock_;
}; };
// Generate some debug string about the inherience structure of scope, quite // Generate some debug string about the inherience structure of scope, quite
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册