11
11
*/
12
12
#include " bq_common/platform/no_lib_cpp_impl.h"
13
13
#if defined(BQ_NO_LIBCPP)
14
+ #include < pthread.h>
14
15
#include " bq_common/bq_common.h"
15
16
16
17
void * operator new (size_t size)
@@ -102,6 +103,55 @@ extern "C" void __cxa_guard_abort(_guard_t* gv)
102
103
// Release fence is used to make all stores performed by the construction function
103
104
// visible in other threads.
104
105
gv->state .store (CONSTRUCTION_NOT_YET_STARTED, bq::platform::memory_order::release);
105
- // ////////////////////////////////////////////cxa_guard end/////////////////////////////////////
106
106
}
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
+
107
157
#endif
0 commit comments