|
|
|
@ -14,6 +14,7 @@
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include "glog/logging.h"
|
|
|
|
|
#include "paddle/fluid/platform/enforce.h"
|
|
|
|
|
|
|
|
|
|
namespace paddle {
|
|
|
|
@ -22,27 +23,24 @@ namespace details {
|
|
|
|
|
|
|
|
|
|
class ExceptionHolder {
|
|
|
|
|
public:
|
|
|
|
|
void Catch(const platform::EnforceNotMet& exp) {
|
|
|
|
|
std::lock_guard<std::mutex> lock(mu_);
|
|
|
|
|
exception_.reset(new platform::EnforceNotMet(exp));
|
|
|
|
|
type_ = kEnforceNotMet;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Catch(const platform::EOFException& exp) {
|
|
|
|
|
std::lock_guard<std::mutex> lock(mu_);
|
|
|
|
|
// EOFException will not cover up existing EnforceNotMet.
|
|
|
|
|
if (exception_.get() == nullptr) {
|
|
|
|
|
exception_.reset(new platform::EOFException(exp));
|
|
|
|
|
type_ = kEOF;
|
|
|
|
|
void Catch(std::exception_ptr eptr) {
|
|
|
|
|
try {
|
|
|
|
|
std::rethrow_exception(eptr);
|
|
|
|
|
} catch (platform::EOFException exp) {
|
|
|
|
|
Catch(exp);
|
|
|
|
|
} catch (platform::EnforceNotMet exp) {
|
|
|
|
|
Catch(exp);
|
|
|
|
|
} catch (...) {
|
|
|
|
|
LOG(FATAL) << "Unknown exception caught";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ExceptionCatched() const {
|
|
|
|
|
bool IsCaught() const {
|
|
|
|
|
std::lock_guard<std::mutex> lock(mu_);
|
|
|
|
|
return exception_.get() != nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Throw() {
|
|
|
|
|
void ReThrow() {
|
|
|
|
|
std::lock_guard<std::mutex> lock(mu_);
|
|
|
|
|
switch (type_) {
|
|
|
|
|
case kNone:
|
|
|
|
@ -50,27 +48,41 @@ class ExceptionHolder {
|
|
|
|
|
case kEnforceNotMet: {
|
|
|
|
|
auto e = *static_cast<platform::EnforceNotMet*>(exception_.get());
|
|
|
|
|
throw e;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case kEOF: {
|
|
|
|
|
auto e = *static_cast<platform::EOFException*>(exception_.get());
|
|
|
|
|
throw e;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
LOG(FATAL) << "Unknown exception.";
|
|
|
|
|
}
|
|
|
|
|
exception_.reset();
|
|
|
|
|
type_ = kNone;
|
|
|
|
|
ClearImpl();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Clear() {
|
|
|
|
|
std::lock_guard<std::mutex> lock(mu_);
|
|
|
|
|
ClearImpl();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
void ClearImpl() {
|
|
|
|
|
exception_.reset();
|
|
|
|
|
type_ = kNone;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
void Catch(const platform::EnforceNotMet& exp) {
|
|
|
|
|
std::lock_guard<std::mutex> lock(mu_);
|
|
|
|
|
exception_.reset(new platform::EnforceNotMet(exp));
|
|
|
|
|
type_ = kEnforceNotMet;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Catch(const platform::EOFException& exp) {
|
|
|
|
|
std::lock_guard<std::mutex> lock(mu_);
|
|
|
|
|
// EOFException will not cover up existing EnforceNotMet.
|
|
|
|
|
if (exception_.get() == nullptr) {
|
|
|
|
|
exception_.reset(new platform::EOFException(exp));
|
|
|
|
|
type_ = kEOF;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enum ExceptionType { kNone, kEnforceNotMet, kEOF };
|
|
|
|
|
ExceptionType type_{kNone};
|
|
|
|
|
|
|
|
|
|