aboutsummaryrefslogtreecommitdiff
path: root/sys/rt/atexit.c
blob: 208cc6ef51346b7b3253a8b2ae63053546edf951 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <u.h>
#include <rt.h>

#define COUNT 32

static struct cleaner
{
    struct cleaner *next;
    void (*clean[COUNT])(void *);
    void *arg[COUNT];
} arena, *head;
static int slot;

void weaklink exit(int code){};

void
rt·clean(void)
{
    void (*clean)(void *), *arg;
    /*LOCK*/
    for(; head; head=head->next, slot=COUNT) {
        while(slot-- > 0){
            arg   = head->arg[slot];
            clean = head->clean[slot];
            /* UNLOCK */
            clean(arg);
            /* LOCK */
        }
    }
}

int
rt·atexit(void (*clean)(void *), void *arg)
{
    /* LOCK */
    if(!head)
        head = &arena;

    /* we need malloc... */
    if(slot==COUNT)
        return -1;

    head->arg[slot]   = arg;
    head->clean[slot] = clean;
    slot++;

    /* UNLOCK */
    return 0;
}