1 /** 2 * Copyright 2020-2022 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_CCSRC_INCLUDE_COMMON_UTILS_SCOPED_LONG_RUNNING_H_ 18 #define MINDSPORE_CCSRC_INCLUDE_COMMON_UTILS_SCOPED_LONG_RUNNING_H_ 19 20 #include <memory> 21 #include <utility> 22 #include "include/common/visible.h" 23 24 namespace mindspore { 25 // Base Class for scoped long running code. 26 // Enter() should release some global resource, like Python GIL; 27 // Leave() should acquire the same global resource released. 28 class ScopedLongRunningHook { 29 public: 30 ScopedLongRunningHook() = default; 31 virtual ~ScopedLongRunningHook() = default; 32 virtual void Enter() = 0; 33 virtual void Leave() noexcept = 0; 34 }; 35 using ScopedLongRunningHookPtr = std::unique_ptr<ScopedLongRunningHook>; 36 37 // Before calling into long-running code, construct this RAII class to release global resource 38 // like Python GIL. 39 class COMMON_EXPORT ScopedLongRunning { 40 public: 41 ScopedLongRunning(); 42 ~ScopedLongRunning(); 43 static void SetHook(ScopedLongRunningHookPtr hook); 44 45 private: 46 inline static ScopedLongRunningHookPtr hook_ = nullptr; 47 }; 48 } // namespace mindspore 49 #endif // MINDSPORE_CCSRC_INCLUDE_COMMON_UTILS_SCOPED_LONG_RUNNING_H_ 50