__cxa_thread_atexit_impl.cpp revision df79c330d895af31f39ee301dee62731fa586168
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 18struct thread_local_dtor { 19 void (*func) (void *); 20 void *arg; 21 void *dso_handle; // unused... 22 thread_local_dtor* next; 23}; 24 25__thread thread_local_dtor* thread_local_dtors = nullptr; 26 27extern "C" int __cxa_thread_atexit_impl(void (*func) (void *), void *arg, void *dso_handle) { 28 thread_local_dtor* dtor = new thread_local_dtor(); 29 30 dtor->func = func; 31 dtor->arg = arg; 32 dtor->dso_handle = dso_handle; 33 dtor->next = thread_local_dtors; 34 35 thread_local_dtors = dtor; 36 37 return 0; 38} 39 40extern "C" __LIBC_HIDDEN__ void __cxa_thread_finalize() { 41 while (thread_local_dtors != nullptr) { 42 thread_local_dtor* current = thread_local_dtors; 43 thread_local_dtors = current->next; 44 45 current->func(current->arg); 46 delete current; 47 } 48} 49