1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <unistd.h>
18 
19 extern "C" void __loader_add_thread_local_dtor(void* dso_handle) __attribute__((weak));
20 extern "C" void __loader_remove_thread_local_dtor(void* dso_handle) __attribute__((weak));
21 
22 extern "C" int native_bridge___cxa_thread_atexit_impl(void (*func)(void*),
23                                                       void* arg,
24                                                       void* dso_handle);
25 
26 struct WrappedArg {
27   typedef void (*thread_atexit_fn_t)(void*);
28   thread_atexit_fn_t fn;
29   void* arg;
30   void* dso_handle;
31 };
32 
WrappedFn(void * arg)33 static void WrappedFn(void* arg) {
34   WrappedArg* wrapped_arg = static_cast<WrappedArg*>(arg);
35   WrappedArg::thread_atexit_fn_t origin_fn = wrapped_arg->fn;
36   void* origin_arg = wrapped_arg->arg;
37   void* dso_handle = wrapped_arg->dso_handle;
38 
39   delete wrapped_arg;
40 
41   origin_fn(origin_arg);
42 
43   if (__loader_remove_thread_local_dtor != nullptr) {
44     __loader_remove_thread_local_dtor(dso_handle);
45   }
46 }
47 
__cxa_thread_atexit_impl(void (* func)(void *),void * arg,void * dso_handle)48 extern "C" int __cxa_thread_atexit_impl(void (*func)(void*), void* arg, void* dso_handle) {
49   WrappedArg* wrapped_arg = new WrappedArg();
50   wrapped_arg->fn = func;
51   wrapped_arg->arg = arg;
52   wrapped_arg->dso_handle = dso_handle;
53 
54   if (__loader_add_thread_local_dtor != nullptr) {
55     __loader_add_thread_local_dtor(dso_handle);
56   }
57 
58   return native_bridge___cxa_thread_atexit_impl(WrappedFn, wrapped_arg, dso_handle);
59 }
60