Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MegEngine 天元
MegEngine
提交
721091fa
MegEngine
项目概览
MegEngine 天元
/
MegEngine
1 年多 前同步成功
通知
403
Star
4705
Fork
582
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
MegEngine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
721091fa
编写于
6月 18, 2021
作者:
M
Megvii Engine Team
提交者:
huangxinda
7月 19, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix(core): fix thread local is not supported in ios
GitOrigin-RevId: b7a6928f0bb8b7ac80b7526bbd28304dd6201330
上级
1d865281
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
129 addition
and
9 deletion
+129
-9
src/core/impl/comp_node/cpu/comp_node.cpp
src/core/impl/comp_node/cpu/comp_node.cpp
+5
-9
src/core/include/megbrain/utils/thread.h
src/core/include/megbrain/utils/thread.h
+1
-0
src/core/include/megbrain/utils/thread_local.h
src/core/include/megbrain/utils/thread_local.h
+123
-0
未找到文件。
src/core/impl/comp_node/cpu/comp_node.cpp
浏览文件 @
721091fa
...
...
@@ -604,12 +604,8 @@ class CpuCompNode::CompNodeRecorderImpl final : public CompNodeBaseImpl {
using
EventImpl
::
EventImpl
;
};
//! TODO: because the x-code bug, see
//! https://github.com/tensorflow/tensorflow/issues/18356
//! thread local is no support on IOS,
//! When update x-xode, this code should be deleted
#if !defined(IOS) && MGB_HAVE_THREAD
static
thread_local
SeqRecorderImpl
*
sm_cur_recorder
;
#if MGB_HAVE_THREAD
static
MGB_THREAD_LOCAL_PTR
(
SeqRecorderImpl
)
sm_cur_recorder
;
#else
SeqRecorderImpl
*
sm_cur_recorder
=
nullptr
;
#endif
...
...
@@ -822,9 +818,9 @@ public:
}
};
MGB_DYN_TYPE_OBJ_FINAL_IMPL
(
CompNodeRecorderImpl
);
#if
!defined(IOS) &&
MGB_HAVE_THREAD
thread_local
CpuCompNode
::
SeqRecorderImpl
*
CompNodeRecorderImpl
::
sm_cur_recorder
=
nullptr
;
#if MGB_HAVE_THREAD
MGB_THREAD_LOCAL_PTR
(
CpuCompNode
::
SeqRecorderImpl
)
CompNodeRecorderImpl
::
sm_cur_recorder
=
nullptr
;
#endif
/* ======================== CpuCompNode ======================== */
...
...
src/core/include/megbrain/utils/thread.h
浏览文件 @
721091fa
...
...
@@ -14,6 +14,7 @@
#include "megbrain_build_config.h"
#if MGB_HAVE_THREAD
#include "./thread_impl_1.h"
#include "./thread_local.h"
#else
#include "./thread_impl_0.h"
#endif
...
...
src/core/include/megbrain/utils/thread_local.h
0 → 100644
浏览文件 @
721091fa
/**
* \file src/core/include/megbrain/utils/thread_local.h
* MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
*
* Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*/
#pragma once
#include <cstddef>
#include <functional>
#include <thread>
#if !defined(__APPLE__)
#define USE_STL_THREAD_LOCAL 1
#else
#define USE_STL_THREAD_LOCAL 0
#endif
// clang-format off
#if defined(__APPLE__)
# if (__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ + 0) >= 101000
# define USE_STL_THREAD_LOCAL 1
# else
# define USE_STL_THREAD_LOCAL 0
# endif
#endif
#if USE_STL_THREAD_LOCAL
#define MGB_THREAD_LOCAL_PTR(T) thread_local T*
#else
#define MGB_THREAD_LOCAL_PTR(T) ThreadLocalPtr<T>
#endif
// clang-format on
#if !USE_STL_THREAD_LOCAL
#include <pthread.h>
namespace
mgb
{
template
<
typename
T
>
class
ThreadLocalPtr
{
struct
ThreadData
{
const
ThreadLocalPtr
*
self
=
nullptr
;
T
**
data
=
nullptr
;
};
pthread_key_t
m_key
;
std
::
function
<
T
**
()
>
m_constructor
=
nullptr
;
std
::
function
<
void
(
T
**
)
>
m_destructor
=
nullptr
;
void
move_to
(
T
**
data
)
{
if
(
void
*
d
=
pthread_getspecific
(
m_key
)){
*
data
=
*
static_cast
<
ThreadData
*>
(
d
)
->
data
;
}
}
T
**
get
()
const
{
if
(
auto
d
=
pthread_getspecific
(
m_key
))
{
return
static_cast
<
ThreadData
*>
(
d
)
->
data
;
}
ThreadData
*
t_data
=
new
ThreadData
();
t_data
->
data
=
m_constructor
();
t_data
->
self
=
this
;
pthread_setspecific
(
m_key
,
t_data
);
return
t_data
->
data
;
}
static
void
exit
(
void
*
d
)
{
ThreadData
*
td
=
static_cast
<
ThreadData
*>
(
d
);
if
(
td
&&
td
->
self
->
m_destructor
)
td
->
self
->
m_destructor
(
td
->
data
);
delete
td
;
}
public:
ThreadLocalPtr
(
std
::
function
<
T
**
()
>
constructor
,
std
::
function
<
void
(
T
**
)
>
destructor
=
std
::
default_delete
<
T
*>
())
:
m_constructor
(
constructor
),
m_destructor
(
destructor
)
{
pthread_key_create
(
&
m_key
,
exit
);
}
ThreadLocalPtr
()
:
ThreadLocalPtr
(
std
::
function
<
T
**
()
>
([]
{
return
new
T
*
();
}))
{}
ThreadLocalPtr
(
std
::
nullptr_t
)
:
ThreadLocalPtr
([]
{
return
new
T
*
(
nullptr
);
})
{}
ThreadLocalPtr
(
ThreadLocalPtr
&&
other
)
:
ThreadLocalPtr
()
{
other
.
move_to
(
get
());
}
ThreadLocalPtr
&
operator
=
(
ThreadLocalPtr
&&
other
)
{
other
.
move_to
(
get
());
return
*
this
;
}
ThreadLocalPtr
&
operator
=
(
T
*
v
)
{
*
get
()
=
v
;
return
*
this
;
}
~
ThreadLocalPtr
()
{
pthread_key_delete
(
m_key
);
}
//!& operator like std thread_local
T
**
operator
&
()
const
{
return
get
();
}
//! use in if()
operator
bool
()
const
{
return
*
get
();
}
//! directly access its member
T
*
operator
->
()
const
{
return
*
get
();
}
//! convert to T*
operator
T
*
()
const
{
return
*
get
();
}
};
}
// namespace mgb
#endif
// vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录