// // ExecutorScope.cpp // MNN // // Created by MNN on 2020/10/26. // Copyright © 2018, Alibaba Group Holding Limited // #include #include #include #include #include namespace MNN { namespace Express { typedef std::shared_ptr ExecutorRef; #if TARGET_OS_IPHONE #include static pthread_key_t gKey; static std::once_flag gInitFlag; #else thread_local static std::once_flag gInitFlag; thread_local static Scope* g_executor_scope = nullptr; #endif static Scope* _getGlobalScope() { std::call_once(gInitFlag, [&]() { #if TARGET_OS_IPHONE pthread_key_create(&gKey, NULL); #else thread_local static Scope initValue; g_executor_scope = &initValue; #endif }); #if TARGET_OS_IPHONE Scope* scope = static_cast*>(pthread_getspecific(gKey)); if (!scope) { scope = new Scope; pthread_setspecific(gKey, static_cast(scope)); } return scope; #else return g_executor_scope; #endif } ExecutorScope::ExecutorScope(const std::shared_ptr& current) { _getGlobalScope()->EnterScope(current); } ExecutorScope::ExecutorScope(const std::string& scope_name, const std::shared_ptr& current) { _getGlobalScope()->EnterScope(scope_name, current); } ExecutorScope::~ExecutorScope() { _getGlobalScope()->ExitScope(); } const std::shared_ptr ExecutorScope::Current() { auto exe = _getGlobalScope()->Content(); if (exe) { return exe; } return Executor::getGlobalExecutor(); } } // namespace Express } // namespace MNN