]> git.lizzy.rs Git - plan9front.git/blob - sys/src/libc/port/atexit.c
libregexp: improve the transition to next available thread, instruction, and generation
[plan9front.git] / sys / src / libc / port / atexit.c
1 #include <u.h>
2 #include <libc.h>
3
4 #define NEXIT   33
5
6 typedef struct Onex Onex;
7 struct Onex{
8         void    (*f)(void);
9         int     pid;
10 };
11
12 static Lock onexlock;
13 Onex onex[NEXIT];
14
15 int
16 atexit(void (*f)(void))
17 {
18         int i;
19
20         lock(&onexlock);
21         for(i=0; i<NEXIT; i++)
22                 if(onex[i].f == 0) {
23                         onex[i].pid = getpid();
24                         onex[i].f = f;
25                         unlock(&onexlock);
26                         return 1;
27                 }
28         unlock(&onexlock);
29         return 0;
30 }
31
32 void
33 atexitdont(void (*f)(void))
34 {
35         int i, pid;
36
37         pid = getpid();
38         for(i=0; i<NEXIT; i++)
39                 if(onex[i].f == f && onex[i].pid == pid)
40                         onex[i].f = 0;
41 }
42
43 #pragma profile off
44
45 void
46 exits(char *s)
47 {
48         int i, pid;
49         void (*f)(void);
50
51         pid = getpid();
52         for(i = NEXIT-1; i >= 0; i--)
53                 if((f = onex[i].f) && pid == onex[i].pid) {
54                         onex[i].f = 0;
55                         (*f)();
56                 }
57         _exits(s);
58 }
59
60 #pragma profile on