1 /** 2 * Copyright 2020 Huawei Technologies Co., Ltd 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef MINDSPORE_CORE_UTILS_MS_EXCEPTION_H_ 18 #define MINDSPORE_CORE_UTILS_MS_EXCEPTION_H_ 19 #include <exception> 20 #include <set> 21 #include "utils/ms_utils.h" 22 namespace mindspore { 23 class ExceptionListener { 24 public: 25 virtual void OnException() = 0; 26 }; 27 28 class MsException { 29 public: Instance()30 static MsException &Instance() { 31 static MsException instance; 32 return instance; 33 } 34 SetException()35 void SetException() { 36 exception_ptr_ = std::current_exception(); 37 if (exception_ptr_ != nullptr && listener_ != nullptr) { 38 auto listener = listener_; 39 listener_ = nullptr; 40 listener->OnException(); 41 } 42 } 43 CheckException()44 void CheckException() { 45 if (exception_ptr_ != nullptr) { 46 auto exception_ptr = exception_ptr_; 47 exception_ptr_ = nullptr; 48 std::rethrow_exception(exception_ptr); 49 } 50 } 51 SetExceptionListener(ExceptionListener * listener)52 void SetExceptionListener(ExceptionListener *listener) { listener_ = listener; } 53 54 private: 55 MsException() = default; 56 ~MsException() = default; DISABLE_COPY_AND_ASSIGN(MsException)57 DISABLE_COPY_AND_ASSIGN(MsException) 58 ExceptionListener *listener_{nullptr}; 59 std::exception_ptr exception_ptr_{nullptr}; 60 }; 61 62 class StaticAnalysisException { 63 public: Instance()64 static StaticAnalysisException &Instance() { 65 static StaticAnalysisException instance; 66 return instance; 67 } 68 ClearException()69 void ClearException() { 70 std::lock_guard<std::mutex> lock(lock_); 71 exception_ptr_ = nullptr; 72 } 73 HasException()74 bool HasException() { 75 std::lock_guard<std::mutex> lock(lock_); 76 return exception_ptr_ != nullptr; 77 } 78 SetException()79 void SetException() { 80 std::lock_guard<std::mutex> lock(lock_); 81 if (exception_ptr_ != nullptr) { 82 return; 83 } 84 exception_ptr_ = std::current_exception(); 85 } 86 SetAndRethrowException()87 void SetAndRethrowException() { 88 std::lock_guard<std::mutex> lock(lock_); 89 SetException(); 90 std::rethrow_exception(std::current_exception()); 91 } 92 CheckException()93 void CheckException() { 94 std::lock_guard<std::mutex> lock(lock_); 95 if (exception_ptr_ != nullptr) { 96 auto tmp_exception_ptr = exception_ptr_; 97 std::rethrow_exception(tmp_exception_ptr); 98 } 99 } 100 101 private: 102 StaticAnalysisException() = default; 103 ~StaticAnalysisException() = default; DISABLE_COPY_AND_ASSIGN(StaticAnalysisException)104 DISABLE_COPY_AND_ASSIGN(StaticAnalysisException) 105 106 std::exception_ptr exception_ptr_{nullptr}; 107 std::mutex lock_; 108 }; 109 } // namespace mindspore 110 111 #endif // MINDSPORE_CORE_UTILS_MS_EXCEPTION_H_ 112