| Dmitriy Ivanov | df79c33 | 2015-03-25 17:38:10 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright (C) 2015 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 | #include <sys/cdefs.h> | 
 | 17 |  | 
| dimitry | fa43252 | 2017-10-25 13:07:45 +0200 | [diff] [blame] | 18 | #include <private/bionic_defs.h> | 
 | 19 |  | 
| Yabin Cui | 952e9eb | 2015-11-24 17:24:06 -0800 | [diff] [blame] | 20 | #include "pthread_internal.h" | 
 | 21 |  | 
| Elliott Hughes | 42d949f | 2016-01-06 19:51:43 -0800 | [diff] [blame] | 22 | class thread_local_dtor { | 
 | 23 |  public: | 
| Dmitriy Ivanov | df79c33 | 2015-03-25 17:38:10 -0700 | [diff] [blame] | 24 |   void (*func) (void *); | 
 | 25 |   void *arg; | 
 | 26 |   void *dso_handle; // unused... | 
 | 27 |   thread_local_dtor* next; | 
 | 28 | }; | 
 | 29 |  | 
| dimitry | fa43252 | 2017-10-25 13:07:45 +0200 | [diff] [blame] | 30 | extern "C" int __cxa_thread_atexit_impl(void (*func) (void *), void *arg, void *dso_handle); | 
| dimitry | 06016f2 | 2018-01-05 11:39:28 +0100 | [diff] [blame] | 31 | extern "C" void __loader_add_thread_local_dtor(void* dso_handle) __attribute__((weak)); | 
 | 32 | extern "C" void __loader_remove_thread_local_dtor(void* dso_handle) __attribute__((weak)); | 
| dimitry | fa43252 | 2017-10-25 13:07:45 +0200 | [diff] [blame] | 33 |  | 
 | 34 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE | 
 | 35 | int __cxa_thread_atexit_impl(void (*func) (void *), void *arg, void *dso_handle) { | 
| Dmitriy Ivanov | df79c33 | 2015-03-25 17:38:10 -0700 | [diff] [blame] | 36 |   thread_local_dtor* dtor = new thread_local_dtor(); | 
 | 37 |  | 
 | 38 |   dtor->func = func; | 
 | 39 |   dtor->arg = arg; | 
 | 40 |   dtor->dso_handle = dso_handle; | 
| Dmitriy Ivanov | df79c33 | 2015-03-25 17:38:10 -0700 | [diff] [blame] | 41 |  | 
| Yabin Cui | 952e9eb | 2015-11-24 17:24:06 -0800 | [diff] [blame] | 42 |   pthread_internal_t* thread = __get_thread(); | 
 | 43 |   dtor->next = thread->thread_local_dtors; | 
 | 44 |   thread->thread_local_dtors = dtor; | 
| dimitry | 06016f2 | 2018-01-05 11:39:28 +0100 | [diff] [blame] | 45 |   if (__loader_add_thread_local_dtor != nullptr) { | 
 | 46 |     __loader_add_thread_local_dtor(dso_handle); | 
 | 47 |   } | 
| Dmitriy Ivanov | df79c33 | 2015-03-25 17:38:10 -0700 | [diff] [blame] | 48 |   return 0; | 
 | 49 | } | 
 | 50 |  | 
 | 51 | extern "C" __LIBC_HIDDEN__ void __cxa_thread_finalize() { | 
| Yabin Cui | 952e9eb | 2015-11-24 17:24:06 -0800 | [diff] [blame] | 52 |   pthread_internal_t* thread = __get_thread(); | 
 | 53 |   while (thread->thread_local_dtors != nullptr) { | 
 | 54 |     thread_local_dtor* current = thread->thread_local_dtors; | 
 | 55 |     thread->thread_local_dtors = current->next; | 
| Dmitriy Ivanov | df79c33 | 2015-03-25 17:38:10 -0700 | [diff] [blame] | 56 |  | 
 | 57 |     current->func(current->arg); | 
| dimitry | 06016f2 | 2018-01-05 11:39:28 +0100 | [diff] [blame] | 58 |     if (__loader_remove_thread_local_dtor != nullptr) { | 
 | 59 |       __loader_remove_thread_local_dtor(current->dso_handle); | 
 | 60 |     } | 
| Dmitriy Ivanov | df79c33 | 2015-03-25 17:38:10 -0700 | [diff] [blame] | 61 |     delete current; | 
 | 62 |   } | 
 | 63 | } |