OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [uClibc/] [libc/] [stdlib/] [atexit.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1325 phoenix
/* Copyright (C) 1995,1996 Robert de Bath <rdebath@cix.compulink.co.uk>
2
 * This file is part of the Linux-8086 C library and is distributed
3
 * under the GNU Library General Public License.
4
 */
5
 
6
/*
7
 * Dec 2000          Manuel Novoa III
8
 *
9
 *   Made atexit handling conform to standards... i.e. no args.
10
 *   Removed on_exit since it did not match gnu libc definition.
11
 *   Combined atexit and __do_exit into one object file.
12
 *
13
 * Feb 2001          Manuel Novoa III
14
 *
15
 *   Reworked file after addition of __uClibc_main.
16
 *   Changed name of __do_exit to atexit_handler.
17
 *   Changed name of __cleanup to __uClibc_cleanup.
18
 *   Moved declaration of __uClibc_cleanup to __uClibc_main
19
 *      where it is initialized with (possibly weak alias)
20
 *      _stdio_term.
21
 *
22
 * Jul 2001          Steve Thayer
23
 *
24
 *   Added an on_exit implementation (that now matches gnu libc definition.)
25
 *   Pulled atexit_handler out of the atexit object since it is now required by
26
 *   on_exit as well.  Renamed it to __exit_handler.
27
 *   Fixed a problem where exit functions stop getting called if one of
28
 *   them calls exit().
29
 *   As a side effect of these changes, abort() no longer calls the exit
30
 *   functions (it now matches the gnu libc definition).
31
 *
32
 * August 2002    Erik Andersen
33
 *   Added locking so atexit and friends can be thread safe
34
 *
35
 */
36
 
37
#define _GNU_SOURCE
38
#include <features.h>
39
#include <unistd.h>
40
#include <stdlib.h>
41
#include <errno.h>
42
 
43
 
44
#ifdef __UCLIBC_HAS_THREADS__
45
#include <pthread.h>
46
extern pthread_mutex_t mylock;
47
# define LOCK   __pthread_mutex_lock(&mylock)
48
# define UNLOCK __pthread_mutex_unlock(&mylock);
49
#else
50
# define LOCK
51
# define UNLOCK
52
#endif
53
 
54
 
55
typedef void (*aefuncp) (void);         /* atexit function pointer */
56
typedef void (*oefuncp) (int, void *);  /* on_exit function pointer */
57
typedef enum {
58
        ef_atexit,
59
        ef_on_exit
60
} ef_type; /* exit function types */
61
 
62
/* this is in the L_exit object */
63
extern void (*__exit_cleanup) (int);
64
 
65
/* these are in the L___do_exit object */
66
extern int __exit_slots;
67
extern int __exit_count;
68
extern void __exit_handler(int);
69
struct exit_function {
70
        ef_type type;   /* ef_atexit or ef_on_exit */
71
        union {
72
                aefuncp atexit;
73
                struct {
74
                        oefuncp func;
75
                        void *arg;
76
                } on_exit;
77
        } funcs;
78
};
79
#ifdef __UCLIBC_DYNAMIC_ATEXIT__
80
extern struct exit_function *__exit_function_table;
81
#else
82
extern struct exit_function __exit_function_table[__UCLIBC_MAX_ATEXIT];
83
#endif
84
 
85
#ifdef L_atexit
86
        /*
87
 * register a function to be called at normal program termination
88
 * (the registered function takes no arguments)
89
         */
90
int atexit(aefuncp func)
91
{
92
    struct exit_function *efp;
93
 
94
    LOCK;
95
    if (func) {
96
#ifdef __UCLIBC_DYNAMIC_ATEXIT__
97
        /* If we are out of function table slots, make some more */
98
        if (__exit_slots < __exit_count+1) {
99
            efp=realloc(__exit_function_table,
100
                                        (__exit_slots+20)*sizeof(struct exit_function));
101
            if (efp==NULL) {
102
                UNLOCK;
103
                __set_errno(ENOMEM);
104
                return -1;
105
            }
106
                __exit_function_table = efp;
107
            __exit_slots+=20;
108
        }
109
#else
110
        if (__exit_count >= __UCLIBC_MAX_ATEXIT) {
111
            UNLOCK;
112
            __set_errno(ENOMEM);
113
            return -1;
114
        }
115
#endif
116
        __exit_cleanup = __exit_handler; /* enable cleanup */
117
        efp = &__exit_function_table[__exit_count++];
118
        efp->type = ef_atexit;
119
        efp->funcs.atexit = func;
120
    }
121
    UNLOCK;
122
    return 0;
123
}
124
#endif
125
 
126
#ifdef L_on_exit
127
/*
128
 * register a function to be called at normal program termination
129
 * the registered function takes two arguments:
130
 *     status - the exit status that was passed to the exit() function
131
 *     arg - generic argument
132
 */
133
int on_exit(oefuncp func, void *arg)
134
{
135
    struct exit_function *efp;
136
 
137
    LOCK;
138
    if (func) {
139
#ifdef __UCLIBC_DYNAMIC_ATEXIT__
140
        /* If we are out of function table slots, make some more */
141
        if (__exit_slots < __exit_count+1) {
142
            efp=realloc(__exit_function_table,
143
                                        (__exit_slots+20)*sizeof(struct exit_function));
144
            if (efp==NULL) {
145
                UNLOCK;
146
                __set_errno(ENOMEM);
147
                return -1;
148
            }
149
                __exit_function_table=efp;
150
            __exit_slots+=20;
151
        }
152
#else
153
        if (__exit_count >= __UCLIBC_MAX_ATEXIT) {
154
            UNLOCK;
155
            __set_errno(ENOMEM);
156
            return -1;
157
        }
158
#endif
159
 
160
        __exit_cleanup = __exit_handler; /* enable cleanup */
161
        efp = &__exit_function_table[__exit_count++];
162
        efp->type = ef_on_exit;
163
        efp->funcs.on_exit.func = func;
164
        efp->funcs.on_exit.arg = arg;
165
    }
166
    UNLOCK;
167
    return 0;
168
}
169
#endif
170
 
171
#ifdef L___exit_handler
172
int __exit_count = 0; /* Number of registered exit functions */
173
#ifdef __UCLIBC_DYNAMIC_ATEXIT__
174
struct exit_function *__exit_function_table = NULL;
175
int __exit_slots = 0; /* Size of __exit_function_table */
176
#else
177
struct exit_function __exit_function_table[__UCLIBC_MAX_ATEXIT];
178
#endif
179
 
180
 
181
/*
182
 * Handle the work of executing the registered exit functions
183
 * This is called while we are locked, so no additional locking
184
 * is needed...
185
 */
186
void __exit_handler(int status)
187
{
188
        struct exit_function *efp;
189
 
190
        /* In reverse order */
191
        while ( __exit_count ) {
192
                efp = &__exit_function_table[--__exit_count];
193
                switch (efp->type) {
194
                case ef_on_exit:
195
                        if (efp->funcs.on_exit.func) {
196
                                (efp->funcs.on_exit.func) (status, efp->funcs.on_exit.arg);
197
                        }
198
                        break;
199
                case ef_atexit:
200
                        if (efp->funcs.atexit) {
201
                                (efp->funcs.atexit) ();
202
                        }
203
                        break;
204
                }
205
        }
206
#ifdef __UCLIBC_DYNAMIC_ATEXIT__
207
        /* Free up memory used by the __exit_function_table structure */
208
        if (__exit_function_table)
209
            free(__exit_function_table);
210
#endif
211
}
212
#endif
213
 
214
#ifdef L_exit
215
extern void weak_function _stdio_term(void);
216
void (*__exit_cleanup) (int) = 0;
217
#ifdef __UCLIBC_HAS_THREADS__
218
pthread_mutex_t mylock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
219
#endif
220
 
221
#ifdef __UCLIBC_CTOR_DTOR__
222
extern void (*__app_fini)(void);
223
#endif
224
 
225
/*
226
 * Normal program termination
227
 */
228
void exit(int rv)
229
{
230
        /* Perform exit-specific cleanup (atexit and on_exit) */
231
        LOCK;
232
        if (__exit_cleanup) {
233
                __exit_cleanup(rv);
234
        }
235
        UNLOCK;
236
 
237
#ifdef __UCLIBC_CTOR_DTOR__
238
        if (__app_fini != NULL)
239
                (__app_fini)();
240
#endif
241
 
242
    /* If we are using stdio, try to shut it down.  At the very least,
243
         * this will attempt to commit all buffered writes.  It may also
244
         * unbuffer all writable files, or close them outright.
245
         * Check the stdio routines for details. */
246
        if (_stdio_term)
247
            _stdio_term();
248
 
249
        _exit(rv);
250
}
251
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.