blob: f687fd1797e1cb9404ad81a72a5a7fc4a3806edc [file] [log] [blame]
Dmitriy Ivanovdf79c332015-03-25 17:38:10 -07001/*
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
dimitryfa432522017-10-25 13:07:45 +020018#include <private/bionic_defs.h>
19
Yabin Cui952e9eb2015-11-24 17:24:06 -080020#include "pthread_internal.h"
21
Elliott Hughes42d949f2016-01-06 19:51:43 -080022class thread_local_dtor {
23 public:
Dmitriy Ivanovdf79c332015-03-25 17:38:10 -070024 void (*func) (void *);
25 void *arg;
26 void *dso_handle; // unused...
27 thread_local_dtor* next;
28};
29
dimitryfa432522017-10-25 13:07:45 +020030extern "C" int __cxa_thread_atexit_impl(void (*func) (void *), void *arg, void *dso_handle);
31
32__BIONIC_WEAK_FOR_NATIVE_BRIDGE
33int __cxa_thread_atexit_impl(void (*func) (void *), void *arg, void *dso_handle) {
Dmitriy Ivanovdf79c332015-03-25 17:38:10 -070034 thread_local_dtor* dtor = new thread_local_dtor();
35
36 dtor->func = func;
37 dtor->arg = arg;
38 dtor->dso_handle = dso_handle;
Dmitriy Ivanovdf79c332015-03-25 17:38:10 -070039
Yabin Cui952e9eb2015-11-24 17:24:06 -080040 pthread_internal_t* thread = __get_thread();
41 dtor->next = thread->thread_local_dtors;
42 thread->thread_local_dtors = dtor;
Dmitriy Ivanovdf79c332015-03-25 17:38:10 -070043 return 0;
44}
45
46extern "C" __LIBC_HIDDEN__ void __cxa_thread_finalize() {
Yabin Cui952e9eb2015-11-24 17:24:06 -080047 pthread_internal_t* thread = __get_thread();
48 while (thread->thread_local_dtors != nullptr) {
49 thread_local_dtor* current = thread->thread_local_dtors;
50 thread->thread_local_dtors = current->next;
Dmitriy Ivanovdf79c332015-03-25 17:38:10 -070051
52 current->func(current->arg);
53 delete current;
54 }
55}