1*4684ddb6SLionel Sambuc /**
2*4684ddb6SLionel Sambuc * Copyright (c) 2012 David Chisnall.
3*4684ddb6SLionel Sambuc *
4*4684ddb6SLionel Sambuc * Permission is hereby granted, free of charge, to any person obtaining a copy
5*4684ddb6SLionel Sambuc * of this software and associated documentation files (the "Software"), to
6*4684ddb6SLionel Sambuc * deal in the Software without restriction, including without limitation the
7*4684ddb6SLionel Sambuc * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8*4684ddb6SLionel Sambuc * sell copies of the Software, and to permit persons to whom the Software is
9*4684ddb6SLionel Sambuc * furnished to do so, subject to the following conditions:
10*4684ddb6SLionel Sambuc *
11*4684ddb6SLionel Sambuc * The above copyright notice and this permission notice shall be included in
12*4684ddb6SLionel Sambuc * all copies or substantial portions of the Software.
13*4684ddb6SLionel Sambuc *
14*4684ddb6SLionel Sambuc * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15*4684ddb6SLionel Sambuc * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16*4684ddb6SLionel Sambuc * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17*4684ddb6SLionel Sambuc * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18*4684ddb6SLionel Sambuc * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19*4684ddb6SLionel Sambuc * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20*4684ddb6SLionel Sambuc * IN THE SOFTWARE.
21*4684ddb6SLionel Sambuc *
22*4684ddb6SLionel Sambuc */
23*4684ddb6SLionel Sambuc
24*4684ddb6SLionel Sambuc /* Special thanks to TBricks for partially funding this work */
25*4684ddb6SLionel Sambuc
26*4684ddb6SLionel Sambuc #ifdef __sun__
27*4684ddb6SLionel Sambuc #include <pthread.h>
28*4684ddb6SLionel Sambuc #include <stdlib.h>
29*4684ddb6SLionel Sambuc
30*4684ddb6SLionel Sambuc static struct atexit_handler {
31*4684ddb6SLionel Sambuc void (*f)(void *);
32*4684ddb6SLionel Sambuc void *p;
33*4684ddb6SLionel Sambuc void *d;
34*4684ddb6SLionel Sambuc struct atexit_handler *next;
35*4684ddb6SLionel Sambuc } *head;
36*4684ddb6SLionel Sambuc
37*4684ddb6SLionel Sambuc static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
38*4684ddb6SLionel Sambuc
__cxa_atexit(void (* f)(void *),void * p,void * d)39*4684ddb6SLionel Sambuc int __cxa_atexit( void (*f)(void *), void *p, void *d) {
40*4684ddb6SLionel Sambuc pthread_mutex_lock(&lock);
41*4684ddb6SLionel Sambuc struct atexit_handler *h = malloc(sizeof(*h));
42*4684ddb6SLionel Sambuc if (!h) {
43*4684ddb6SLionel Sambuc pthread_mutex_unlock(&lock);
44*4684ddb6SLionel Sambuc return 1;
45*4684ddb6SLionel Sambuc }
46*4684ddb6SLionel Sambuc h->f = f;
47*4684ddb6SLionel Sambuc h->p = p;
48*4684ddb6SLionel Sambuc h->d = d;
49*4684ddb6SLionel Sambuc h->next = head;
50*4684ddb6SLionel Sambuc head = h;
51*4684ddb6SLionel Sambuc pthread_mutex_unlock(&lock);
52*4684ddb6SLionel Sambuc return 0;
53*4684ddb6SLionel Sambuc }
54*4684ddb6SLionel Sambuc
__cxa_finalize(void * d)55*4684ddb6SLionel Sambuc void __cxa_finalize(void *d ) {
56*4684ddb6SLionel Sambuc pthread_mutex_lock(&lock);
57*4684ddb6SLionel Sambuc struct atexit_handler **last = &head;
58*4684ddb6SLionel Sambuc for (struct atexit_handler *h = head ; h ; h = h->next) {
59*4684ddb6SLionel Sambuc if ((h->d == d) || (d == 0)) {
60*4684ddb6SLionel Sambuc *last = h->next;
61*4684ddb6SLionel Sambuc h->f(h->p);
62*4684ddb6SLionel Sambuc free(h);
63*4684ddb6SLionel Sambuc } else {
64*4684ddb6SLionel Sambuc last = &h->next;
65*4684ddb6SLionel Sambuc }
66*4684ddb6SLionel Sambuc }
67*4684ddb6SLionel Sambuc pthread_mutex_unlock(&lock);
68*4684ddb6SLionel Sambuc }
69*4684ddb6SLionel Sambuc #endif
70