2 * Copyright (c) 2012 David Chisnall.
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to
6 * deal in the Software without restriction, including without limitation the
7 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 * sell copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 /* Special thanks to TBricks for partially funding this work */
30 static struct atexit_handler
{
34 struct atexit_handler
*next
;
37 static pthread_mutex_t lock
= PTHREAD_MUTEX_INITIALIZER
;
39 int __cxa_atexit( void (*f
)(void *), void *p
, void *d
) {
40 pthread_mutex_lock(&lock
);
41 struct atexit_handler
*h
= malloc(sizeof(*h
));
43 pthread_mutex_unlock(&lock
);
51 pthread_mutex_unlock(&lock
);
55 void __cxa_finalize(void *d
) {
56 pthread_mutex_lock(&lock
);
57 struct atexit_handler
**last
= &head
;
58 for (struct atexit_handler
*h
= head
; h
; h
= h
->next
) {
59 if ((h
->d
== d
) || (d
== 0)) {
67 pthread_mutex_unlock(&lock
);