Skip to content

Commit e0daeab

Browse files
author
pippocao
committed
thread_local support without stdlibc++ (NDK ANDROID_STL=none)
1 parent a0059ac commit e0daeab

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

src/bq_common/platform/no_lib_cpp_impl.cpp

+51-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*/
1212
#include "bq_common/platform/no_lib_cpp_impl.h"
1313
#if defined(BQ_NO_LIBCPP)
14+
#include <pthread.h>
1415
#include "bq_common/bq_common.h"
1516

1617
void* operator new(size_t size)
@@ -102,6 +103,55 @@ extern "C" void __cxa_guard_abort(_guard_t* gv)
102103
// Release fence is used to make all stores performed by the construction function
103104
// visible in other threads.
104105
gv->state.store(CONSTRUCTION_NOT_YET_STARTED, bq::platform::memory_order::release);
105-
//////////////////////////////////////////////cxa_guard end/////////////////////////////////////
106106
}
107+
//////////////////////////////////////////////cxa_guard end/////////////////////////////////////
108+
109+
110+
//////////////////////////////////////////////__cxa_thread_atexit begin/////////////////////////////////////
111+
struct tls_entry {
112+
void (*destructor_)(void*);
113+
void* obj_;
114+
tls_entry* next_;
115+
};
116+
117+
static pthread_key_t stl_key;
118+
119+
static BQ_TLS tls_entry* tls_entry_head_ = nullptr;
120+
121+
static void on_thread_exit(void* param)
122+
{
123+
(void)param;
124+
while (tls_entry_head_) {
125+
tls_entry_head_->destructor_(tls_entry_head_->obj_);
126+
auto next = tls_entry_head_->next_;
127+
free(tls_entry_head_);
128+
tls_entry_head_ = next;
129+
}
130+
}
131+
132+
struct st_stl_key_initer {
133+
st_stl_key_initer()
134+
{
135+
pthread_key_create(&stl_key, &on_thread_exit);
136+
}
137+
};
138+
139+
extern "C" int __cxa_thread_atexit(void (*destructor)(void*), void* obj, void* destructor_handle)
140+
{
141+
(void)destructor_handle;
142+
static st_stl_key_initer key_initer;
143+
144+
tls_entry* entry = (tls_entry*)malloc(sizeof(tls_entry));
145+
if (!entry) {
146+
return -1;
147+
}
148+
entry->destructor_ = destructor;
149+
entry->obj_ = obj;
150+
entry->next_ = tls_entry_head_;
151+
tls_entry_head_ = entry;
152+
153+
return 0;
154+
}
155+
//////////////////////////////////////////////__cxa_thread_atexit end/////////////////////////////////////
156+
107157
#endif

0 commit comments

Comments
 (0)