Fix VarHandle return bug. (#13354)

update-install-command
gongweibao 7 years ago committed by GitHub
parent 7c8730824a
commit 239a83860b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -56,7 +56,7 @@ class VarHandle {
const std::string& name, const std::string& name,
const platform::DeviceContext* p_ctx = nullptr, const platform::DeviceContext* p_ctx = nullptr,
const framework::Scope* p_scope = nullptr) const framework::Scope* p_scope = nullptr)
: ok_(kVarHandleDefaultState) { : status_(kDefaultState) {
ep_ = ep; ep_ = ep;
ctx_ = p_ctx; ctx_ = p_ctx;
scope_ = p_scope; scope_ = p_scope;
@ -68,18 +68,20 @@ class VarHandle {
public: public:
bool Wait() { bool Wait() {
int ret = kDefaultState;
{ {
std::unique_lock<std::mutex> lk(sync_mutex_); std::unique_lock<std::mutex> lk(sync_mutex_);
wait_cond_.wait(lk, [this] { return ok_ != kVarHandleDefaultState; }); wait_cond_.wait(lk, [this] { return status_ != kDefaultState; });
ret = status_;
} }
VLOG(7) << "VarHandle wait:" << ok_; VLOG(7) << "VarHandle wait:" << ret;
return ok_ != 0; return ret != kErrorState;
} }
void Finish(bool ok) { void Finish(bool ok) {
{ {
std::unique_lock<std::mutex> lk(sync_mutex_); std::unique_lock<std::mutex> lk(sync_mutex_);
ok_ = ok; status_ = ok ? kFinishState : kErrorState;
} }
VLOG(7) << "VarHandle finish:" << ok; VLOG(7) << "VarHandle finish:" << ok;
wait_cond_.notify_all(); wait_cond_.notify_all();
@ -87,8 +89,8 @@ class VarHandle {
std::string String() const { std::string String() const {
std::ostringstream s; std::ostringstream s;
s << method_ << " name:[" << name_ << "], ep:[" << ep_ << "], ok:[" << ok_ s << method_ << " name:[" << name_ << "], ep:[" << ep_ << "], status:["
<< "]"; << status_ << "]";
return s.str(); return s.str();
} }
@ -111,9 +113,13 @@ class VarHandle {
protected: protected:
std::mutex sync_mutex_; std::mutex sync_mutex_;
std::condition_variable wait_cond_; std::condition_variable wait_cond_;
int ok_;
static const int kVarHandleDefaultState = -1; enum VarHandleStatus {
kDefaultState = -1,
kErrorState = 0,
kFinishState = 1,
};
VarHandleStatus status_;
private: private:
DISABLE_COPY_AND_ASSIGN(VarHandle); DISABLE_COPY_AND_ASSIGN(VarHandle);

Loading…
Cancel
Save