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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [uClibc/] [libc/] [stdlib/] [atexit.c] - Diff between revs 1325 and 1765

Only display areas with differences | Details | Blame | View Log

Rev 1325 Rev 1765
/* Copyright (C) 1995,1996 Robert de Bath <rdebath@cix.compulink.co.uk>
/* Copyright (C) 1995,1996 Robert de Bath <rdebath@cix.compulink.co.uk>
 * This file is part of the Linux-8086 C library and is distributed
 * This file is part of the Linux-8086 C library and is distributed
 * under the GNU Library General Public License.
 * under the GNU Library General Public License.
 */
 */
 
 
/*
/*
 * Dec 2000          Manuel Novoa III
 * Dec 2000          Manuel Novoa III
 *
 *
 *   Made atexit handling conform to standards... i.e. no args.
 *   Made atexit handling conform to standards... i.e. no args.
 *   Removed on_exit since it did not match gnu libc definition.
 *   Removed on_exit since it did not match gnu libc definition.
 *   Combined atexit and __do_exit into one object file.
 *   Combined atexit and __do_exit into one object file.
 *
 *
 * Feb 2001          Manuel Novoa III
 * Feb 2001          Manuel Novoa III
 *
 *
 *   Reworked file after addition of __uClibc_main.
 *   Reworked file after addition of __uClibc_main.
 *   Changed name of __do_exit to atexit_handler.
 *   Changed name of __do_exit to atexit_handler.
 *   Changed name of __cleanup to __uClibc_cleanup.
 *   Changed name of __cleanup to __uClibc_cleanup.
 *   Moved declaration of __uClibc_cleanup to __uClibc_main
 *   Moved declaration of __uClibc_cleanup to __uClibc_main
 *      where it is initialized with (possibly weak alias)
 *      where it is initialized with (possibly weak alias)
 *      _stdio_term.
 *      _stdio_term.
 *
 *
 * Jul 2001          Steve Thayer
 * Jul 2001          Steve Thayer
 *
 *
 *   Added an on_exit implementation (that now matches gnu libc definition.)
 *   Added an on_exit implementation (that now matches gnu libc definition.)
 *   Pulled atexit_handler out of the atexit object since it is now required by
 *   Pulled atexit_handler out of the atexit object since it is now required by
 *   on_exit as well.  Renamed it to __exit_handler.
 *   on_exit as well.  Renamed it to __exit_handler.
 *   Fixed a problem where exit functions stop getting called if one of
 *   Fixed a problem where exit functions stop getting called if one of
 *   them calls exit().
 *   them calls exit().
 *   As a side effect of these changes, abort() no longer calls the exit
 *   As a side effect of these changes, abort() no longer calls the exit
 *   functions (it now matches the gnu libc definition).
 *   functions (it now matches the gnu libc definition).
 *
 *
 * August 2002    Erik Andersen
 * August 2002    Erik Andersen
 *   Added locking so atexit and friends can be thread safe
 *   Added locking so atexit and friends can be thread safe
 *
 *
 */
 */
 
 
#define _GNU_SOURCE
#define _GNU_SOURCE
#include <features.h>
#include <features.h>
#include <unistd.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdlib.h>
#include <errno.h>
#include <errno.h>
 
 
 
 
#ifdef __UCLIBC_HAS_THREADS__
#ifdef __UCLIBC_HAS_THREADS__
#include <pthread.h>
#include <pthread.h>
extern pthread_mutex_t mylock;
extern pthread_mutex_t mylock;
# define LOCK   __pthread_mutex_lock(&mylock)
# define LOCK   __pthread_mutex_lock(&mylock)
# define UNLOCK __pthread_mutex_unlock(&mylock);
# define UNLOCK __pthread_mutex_unlock(&mylock);
#else
#else
# define LOCK
# define LOCK
# define UNLOCK
# define UNLOCK
#endif
#endif
 
 
 
 
typedef void (*aefuncp) (void);         /* atexit function pointer */
typedef void (*aefuncp) (void);         /* atexit function pointer */
typedef void (*oefuncp) (int, void *);  /* on_exit function pointer */
typedef void (*oefuncp) (int, void *);  /* on_exit function pointer */
typedef enum {
typedef enum {
        ef_atexit,
        ef_atexit,
        ef_on_exit
        ef_on_exit
} ef_type; /* exit function types */
} ef_type; /* exit function types */
 
 
/* this is in the L_exit object */
/* this is in the L_exit object */
extern void (*__exit_cleanup) (int);
extern void (*__exit_cleanup) (int);
 
 
/* these are in the L___do_exit object */
/* these are in the L___do_exit object */
extern int __exit_slots;
extern int __exit_slots;
extern int __exit_count;
extern int __exit_count;
extern void __exit_handler(int);
extern void __exit_handler(int);
struct exit_function {
struct exit_function {
        ef_type type;   /* ef_atexit or ef_on_exit */
        ef_type type;   /* ef_atexit or ef_on_exit */
        union {
        union {
                aefuncp atexit;
                aefuncp atexit;
                struct {
                struct {
                        oefuncp func;
                        oefuncp func;
                        void *arg;
                        void *arg;
                } on_exit;
                } on_exit;
        } funcs;
        } funcs;
};
};
#ifdef __UCLIBC_DYNAMIC_ATEXIT__
#ifdef __UCLIBC_DYNAMIC_ATEXIT__
extern struct exit_function *__exit_function_table;
extern struct exit_function *__exit_function_table;
#else
#else
extern struct exit_function __exit_function_table[__UCLIBC_MAX_ATEXIT];
extern struct exit_function __exit_function_table[__UCLIBC_MAX_ATEXIT];
#endif
#endif
 
 
#ifdef L_atexit
#ifdef L_atexit
        /*
        /*
 * register a function to be called at normal program termination
 * register a function to be called at normal program termination
 * (the registered function takes no arguments)
 * (the registered function takes no arguments)
         */
         */
int atexit(aefuncp func)
int atexit(aefuncp func)
{
{
    struct exit_function *efp;
    struct exit_function *efp;
 
 
    LOCK;
    LOCK;
    if (func) {
    if (func) {
#ifdef __UCLIBC_DYNAMIC_ATEXIT__
#ifdef __UCLIBC_DYNAMIC_ATEXIT__
        /* If we are out of function table slots, make some more */
        /* If we are out of function table slots, make some more */
        if (__exit_slots < __exit_count+1) {
        if (__exit_slots < __exit_count+1) {
            efp=realloc(__exit_function_table,
            efp=realloc(__exit_function_table,
                                        (__exit_slots+20)*sizeof(struct exit_function));
                                        (__exit_slots+20)*sizeof(struct exit_function));
            if (efp==NULL) {
            if (efp==NULL) {
                UNLOCK;
                UNLOCK;
                __set_errno(ENOMEM);
                __set_errno(ENOMEM);
                return -1;
                return -1;
            }
            }
                __exit_function_table = efp;
                __exit_function_table = efp;
            __exit_slots+=20;
            __exit_slots+=20;
        }
        }
#else
#else
        if (__exit_count >= __UCLIBC_MAX_ATEXIT) {
        if (__exit_count >= __UCLIBC_MAX_ATEXIT) {
            UNLOCK;
            UNLOCK;
            __set_errno(ENOMEM);
            __set_errno(ENOMEM);
            return -1;
            return -1;
        }
        }
#endif
#endif
        __exit_cleanup = __exit_handler; /* enable cleanup */
        __exit_cleanup = __exit_handler; /* enable cleanup */
        efp = &__exit_function_table[__exit_count++];
        efp = &__exit_function_table[__exit_count++];
        efp->type = ef_atexit;
        efp->type = ef_atexit;
        efp->funcs.atexit = func;
        efp->funcs.atexit = func;
    }
    }
    UNLOCK;
    UNLOCK;
    return 0;
    return 0;
}
}
#endif
#endif
 
 
#ifdef L_on_exit
#ifdef L_on_exit
/*
/*
 * register a function to be called at normal program termination
 * register a function to be called at normal program termination
 * the registered function takes two arguments:
 * the registered function takes two arguments:
 *     status - the exit status that was passed to the exit() function
 *     status - the exit status that was passed to the exit() function
 *     arg - generic argument
 *     arg - generic argument
 */
 */
int on_exit(oefuncp func, void *arg)
int on_exit(oefuncp func, void *arg)
{
{
    struct exit_function *efp;
    struct exit_function *efp;
 
 
    LOCK;
    LOCK;
    if (func) {
    if (func) {
#ifdef __UCLIBC_DYNAMIC_ATEXIT__
#ifdef __UCLIBC_DYNAMIC_ATEXIT__
        /* If we are out of function table slots, make some more */
        /* If we are out of function table slots, make some more */
        if (__exit_slots < __exit_count+1) {
        if (__exit_slots < __exit_count+1) {
            efp=realloc(__exit_function_table,
            efp=realloc(__exit_function_table,
                                        (__exit_slots+20)*sizeof(struct exit_function));
                                        (__exit_slots+20)*sizeof(struct exit_function));
            if (efp==NULL) {
            if (efp==NULL) {
                UNLOCK;
                UNLOCK;
                __set_errno(ENOMEM);
                __set_errno(ENOMEM);
                return -1;
                return -1;
            }
            }
                __exit_function_table=efp;
                __exit_function_table=efp;
            __exit_slots+=20;
            __exit_slots+=20;
        }
        }
#else
#else
        if (__exit_count >= __UCLIBC_MAX_ATEXIT) {
        if (__exit_count >= __UCLIBC_MAX_ATEXIT) {
            UNLOCK;
            UNLOCK;
            __set_errno(ENOMEM);
            __set_errno(ENOMEM);
            return -1;
            return -1;
        }
        }
#endif
#endif
 
 
        __exit_cleanup = __exit_handler; /* enable cleanup */
        __exit_cleanup = __exit_handler; /* enable cleanup */
        efp = &__exit_function_table[__exit_count++];
        efp = &__exit_function_table[__exit_count++];
        efp->type = ef_on_exit;
        efp->type = ef_on_exit;
        efp->funcs.on_exit.func = func;
        efp->funcs.on_exit.func = func;
        efp->funcs.on_exit.arg = arg;
        efp->funcs.on_exit.arg = arg;
    }
    }
    UNLOCK;
    UNLOCK;
    return 0;
    return 0;
}
}
#endif
#endif
 
 
#ifdef L___exit_handler
#ifdef L___exit_handler
int __exit_count = 0; /* Number of registered exit functions */
int __exit_count = 0; /* Number of registered exit functions */
#ifdef __UCLIBC_DYNAMIC_ATEXIT__
#ifdef __UCLIBC_DYNAMIC_ATEXIT__
struct exit_function *__exit_function_table = NULL;
struct exit_function *__exit_function_table = NULL;
int __exit_slots = 0; /* Size of __exit_function_table */
int __exit_slots = 0; /* Size of __exit_function_table */
#else
#else
struct exit_function __exit_function_table[__UCLIBC_MAX_ATEXIT];
struct exit_function __exit_function_table[__UCLIBC_MAX_ATEXIT];
#endif
#endif
 
 
 
 
/*
/*
 * Handle the work of executing the registered exit functions
 * Handle the work of executing the registered exit functions
 * This is called while we are locked, so no additional locking
 * This is called while we are locked, so no additional locking
 * is needed...
 * is needed...
 */
 */
void __exit_handler(int status)
void __exit_handler(int status)
{
{
        struct exit_function *efp;
        struct exit_function *efp;
 
 
        /* In reverse order */
        /* In reverse order */
        while ( __exit_count ) {
        while ( __exit_count ) {
                efp = &__exit_function_table[--__exit_count];
                efp = &__exit_function_table[--__exit_count];
                switch (efp->type) {
                switch (efp->type) {
                case ef_on_exit:
                case ef_on_exit:
                        if (efp->funcs.on_exit.func) {
                        if (efp->funcs.on_exit.func) {
                                (efp->funcs.on_exit.func) (status, efp->funcs.on_exit.arg);
                                (efp->funcs.on_exit.func) (status, efp->funcs.on_exit.arg);
                        }
                        }
                        break;
                        break;
                case ef_atexit:
                case ef_atexit:
                        if (efp->funcs.atexit) {
                        if (efp->funcs.atexit) {
                                (efp->funcs.atexit) ();
                                (efp->funcs.atexit) ();
                        }
                        }
                        break;
                        break;
                }
                }
        }
        }
#ifdef __UCLIBC_DYNAMIC_ATEXIT__
#ifdef __UCLIBC_DYNAMIC_ATEXIT__
        /* Free up memory used by the __exit_function_table structure */
        /* Free up memory used by the __exit_function_table structure */
        if (__exit_function_table)
        if (__exit_function_table)
            free(__exit_function_table);
            free(__exit_function_table);
#endif
#endif
}
}
#endif
#endif
 
 
#ifdef L_exit
#ifdef L_exit
extern void weak_function _stdio_term(void);
extern void weak_function _stdio_term(void);
void (*__exit_cleanup) (int) = 0;
void (*__exit_cleanup) (int) = 0;
#ifdef __UCLIBC_HAS_THREADS__
#ifdef __UCLIBC_HAS_THREADS__
pthread_mutex_t mylock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
pthread_mutex_t mylock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
#endif
#endif
 
 
#ifdef __UCLIBC_CTOR_DTOR__
#ifdef __UCLIBC_CTOR_DTOR__
extern void (*__app_fini)(void);
extern void (*__app_fini)(void);
#endif
#endif
 
 
/*
/*
 * Normal program termination
 * Normal program termination
 */
 */
void exit(int rv)
void exit(int rv)
{
{
        /* Perform exit-specific cleanup (atexit and on_exit) */
        /* Perform exit-specific cleanup (atexit and on_exit) */
        LOCK;
        LOCK;
        if (__exit_cleanup) {
        if (__exit_cleanup) {
                __exit_cleanup(rv);
                __exit_cleanup(rv);
        }
        }
        UNLOCK;
        UNLOCK;
 
 
#ifdef __UCLIBC_CTOR_DTOR__
#ifdef __UCLIBC_CTOR_DTOR__
        if (__app_fini != NULL)
        if (__app_fini != NULL)
                (__app_fini)();
                (__app_fini)();
#endif
#endif
 
 
    /* If we are using stdio, try to shut it down.  At the very least,
    /* If we are using stdio, try to shut it down.  At the very least,
         * this will attempt to commit all buffered writes.  It may also
         * this will attempt to commit all buffered writes.  It may also
         * unbuffer all writable files, or close them outright.
         * unbuffer all writable files, or close them outright.
         * Check the stdio routines for details. */
         * Check the stdio routines for details. */
        if (_stdio_term)
        if (_stdio_term)
            _stdio_term();
            _stdio_term();
 
 
        _exit(rv);
        _exit(rv);
}
}
#endif
#endif
 
 

powered by: WebSVN 2.1.0

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