MNN/express/ExecutorScope.cpp

57 lines
1.4 KiB
C++
Raw Normal View History

2020-11-05 16:41:56 +08:00
//
// ExecutorScope.cpp
// MNN
//
// Created by MNN on 2020/10/26.
// Copyright © 2018, Alibaba Group Holding Limited
//
#include <thread>
2021-02-07 10:45:07 +08:00
#include <mutex>
2020-11-05 16:41:56 +08:00
#include <MNN/expr/Executor.hpp>
#include <MNN/expr/Scope.hpp>
#include <MNN/expr/ExecutorScope.hpp>
namespace MNN {
namespace Express {
typedef std::shared_ptr<Express::Executor> ExecutorRef;
#if !defined(__APPLE__)
2021-02-07 10:45:07 +08:00
thread_local static std::once_flag gInitFlag;
thread_local static Scope<ExecutorRef>* g_executor_scope = nullptr;
2020-11-05 16:41:56 +08:00
#else
2021-02-07 10:45:07 +08:00
static std::once_flag gInitFlag;
static Scope<ExecutorRef>* g_executor_scope = nullptr;
2020-11-05 16:41:56 +08:00
#endif
2021-02-07 10:45:07 +08:00
static Scope<ExecutorRef>* _getGlobalScope() {
std::call_once(gInitFlag,
[&]() {
g_executor_scope = new Scope<ExecutorRef>;
});
return g_executor_scope;
}
2020-11-05 16:41:56 +08:00
ExecutorScope::ExecutorScope(const std::shared_ptr<Executor>& current) {
2021-02-07 10:45:07 +08:00
_getGlobalScope()->EnterScope(current);
2020-11-05 16:41:56 +08:00
}
ExecutorScope::ExecutorScope(const std::string& scope_name,
const std::shared_ptr<Executor>& current) {
2021-02-07 10:45:07 +08:00
_getGlobalScope()->EnterScope(scope_name, current);
2020-11-05 16:41:56 +08:00
}
ExecutorScope::~ExecutorScope() {
2021-02-07 10:45:07 +08:00
_getGlobalScope()->ExitScope();
2020-11-05 16:41:56 +08:00
}
const std::shared_ptr<Executor> ExecutorScope::Current() {
2021-02-07 10:45:07 +08:00
if (_getGlobalScope()->ScopedLevel() > 0) {
return _getGlobalScope()->Current().content;
2020-11-05 16:41:56 +08:00
}
return Executor::getGlobalExecutor();
}
} // namespace Express
} // namespace MNN