提交 6ae39da4 编写于 作者: M Megvii Engine Team

fix(core): force delete thread_local data on

android:
at dlopen/dlclose case, android do not call cb(exit)
register from pthread_key_create(&m_key, exit)

GitOrigin-RevId: c78352e7114ad362a9c72d15dc9a84331d3bb00a
上级 2abb03b9
...@@ -257,6 +257,20 @@ void SyncableCounter::wait_zero() { ...@@ -257,6 +257,20 @@ void SyncableCounter::wait_zero() {
} }
} }
/* =============== ThreadLocalForceFree =============== */
#if defined(__ANDROID__) && !USE_STL_THREAD_LOCAL
void ThreadLocalForceFree::push(void* d) {
MGB_LOCK_GUARD(m_mutex);
td.push_back(d);
}
//! make ff init as soon as possible
static ThreadLocalForceFree ff;
ThreadLocalForceFree& get_thread_local_force_free_instance() {
return ff;
}
#endif
#else // MGB_HAVE_THREAD #else // MGB_HAVE_THREAD
#pragma message "threading support is disabled" #pragma message "threading support is disabled"
#if MGB_CUDA #if MGB_CUDA
......
...@@ -22,6 +22,24 @@ ...@@ -22,6 +22,24 @@
#pragma message("force disable USE_STL_THREAD_LOCAL for thread_local mem leak at dlopen/dlclose") #pragma message("force disable USE_STL_THREAD_LOCAL for thread_local mem leak at dlopen/dlclose")
#undef USE_STL_THREAD_LOCAL #undef USE_STL_THREAD_LOCAL
#define USE_STL_THREAD_LOCAL 0 #define USE_STL_THREAD_LOCAL 0
class ThreadData;
class ThreadLocalForceFree {
public:
ThreadLocalForceFree() = default;
~ThreadLocalForceFree() {
MGB_LOCK_GUARD(m_mutex);
for (auto& d : td) {
delete (ThreadData*)d;
}
}
void push(void* d);
private:
std::vector<void*> td;
MGB_MUTEX m_mutex;
};
ThreadLocalForceFree& get_thread_local_force_free_instance();
#endif #endif
#if USE_STL_THREAD_LOCAL #if USE_STL_THREAD_LOCAL
...@@ -57,6 +75,10 @@ class ThreadLocalPtr { ...@@ -57,6 +75,10 @@ class ThreadLocalPtr {
return static_cast<ThreadData*>(d)->data; return static_cast<ThreadData*>(d)->data;
} }
ThreadData* t_data = new ThreadData(); ThreadData* t_data = new ThreadData();
#if defined(__ANDROID__)
get_thread_local_force_free_instance().push((void*)t_data);
#endif
t_data->data = m_constructor(); t_data->data = m_constructor();
t_data->self = this; t_data->self = this;
pthread_setspecific(m_key, t_data); pthread_setspecific(m_key, t_data);
...@@ -67,7 +89,9 @@ class ThreadLocalPtr { ...@@ -67,7 +89,9 @@ class ThreadLocalPtr {
ThreadData* td = static_cast<ThreadData*>(d); ThreadData* td = static_cast<ThreadData*>(d);
if (td && td->self->m_destructor) if (td && td->self->m_destructor)
td->self->m_destructor(td->data); td->self->m_destructor(td->data);
#if !defined(__ANDROID__)
delete td; delete td;
#endif
} }
public: public:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册