Skip to content

Commit c501826

Browse files
committed
use framework::RWLock
1 parent 1f36a4c commit c501826

File tree

2 files changed

+76
-9
lines changed

2 files changed

+76
-9
lines changed

paddle/fluid/framework/rw_lock.h

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,70 @@ struct RWLock {
5656
};
5757
#endif
5858

59+
class RWLockGuard {
60+
public:
61+
enum Status { kUnLock, kWRLock, kRDLock };
62+
63+
RWLockGuard(RWLock* rw_lock, Status init_status)
64+
: lock_(rw_lock), status_(Status::kUnLock) {
65+
switch (init_status) {
66+
case Status::kRDLock: {
67+
RDLock();
68+
break;
69+
}
70+
case Status::kWRLock: {
71+
WRLock();
72+
break;
73+
}
74+
}
75+
}
76+
77+
void WRLock() {
78+
switch (status_) {
79+
case Status::kUnLock: {
80+
lock_->WRLock();
81+
break;
82+
}
83+
case Status::kWRLock: {
84+
break;
85+
}
86+
case Status::kRDLock: {
87+
PADDLE_THROW(
88+
"Please unlock read lock first before invoking write lock.");
89+
break;
90+
}
91+
}
92+
}
93+
94+
void RDLock() {
95+
switch (status_) {
96+
case Status::kUnLock: {
97+
lock_->RDLock();
98+
break;
99+
}
100+
case Status::kRDLock: {
101+
break;
102+
}
103+
case Status::kWRLock: {
104+
PADDLE_THROW(
105+
"Please unlock write lock first before invoking read lock.");
106+
break;
107+
}
108+
}
109+
}
110+
111+
void UnLock() {
112+
if (status_ != Status::kUnLock) {
113+
lock_->UNLock();
114+
}
115+
}
116+
117+
~RWLockGuard() { UnLock(); }
118+
119+
private:
120+
RWLock* lock_;
121+
Status status_;
122+
};
123+
59124
} // namespace framework
60125
} // namespace paddle

paddle/fluid/platform/device_context.cc

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@ limitations under the License. */
1515
#include <unordered_set>
1616
#include <vector>
1717

18+
#include "paddle/fluid/memory/memory.h"
1819
#ifdef PADDLE_WITH_CUDA
19-
#include <boost\thread\thread.hpp>
20+
#include "paddle/fluid/framework/rw_lock.h"
2021
#endif
2122

22-
#include "paddle/fluid/memory/memory.h"
23-
2423
namespace paddle {
2524
namespace platform {
2625

@@ -158,19 +157,22 @@ class CudnnHolder {
158157

159158
void RunFunc(const std::function<void(void*)>& cudnn_func,
160159
size_t required_workspace_len) {
161-
boost::upgrade_lock<boost::shared_mutex> shared_lock(mtx_);
160+
framework::RWLockGuard lock_guard(&rw_lock_,
161+
framework::RWLockGuard::Status::kRDLock);
162162
if (required_workspace_len > workspace_len_) {
163-
ReallocateWorkspace(required_workspace_len, &shared_lock);
163+
lock_guard.UnLock();
164+
lock_guard.WRLock();
165+
ReallocateWorkspace(required_workspace_len);
166+
lock_guard.UnLock();
167+
lock_guard.RDLock();
164168
}
165169
cudnn_func(workspace_);
166170
}
167171

168172
~CudnnHolder() { PADDLE_ENFORCE(dynload::cudnnDestroy(cudnn_handle_)); }
169173

170174
private:
171-
void ReallocateWorkspace(size_t required_workspace_len,
172-
boost::upgrade_lock<boost::shared_mutex>* lock) {
173-
boost::upgrade_to_unique_lock<boost::shared_mutex> unique_lock(*lock);
175+
void ReallocateWorkspace(size_t required_workspace_len) {
174176
if (required_workspace_len <= workspace_len_) {
175177
return;
176178
}
@@ -192,7 +194,7 @@ class CudnnHolder {
192194
const cudaStream_t* stream_; // not owned;
193195
const CUDAPlace place_;
194196

195-
boost::shared_mutex mtx_;
197+
framework::RWLock rw_lock_;
196198
};
197199

198200
CUDADeviceContext::CUDADeviceContext(CUDAPlace place)

0 commit comments

Comments
 (0)