diff options
author | cinap_lenrek <cinap_lenrek@felloff.net> | 2017-10-20 20:58:38 +0200 |
---|---|---|
committer | cinap_lenrek <cinap_lenrek@felloff.net> | 2017-10-20 20:58:38 +0200 |
commit | e3d8fe9d4a1085cbf7237411bcd1996613334994 (patch) | |
tree | 0e04341f5ab79efed8865810d3fb4954b1a026e3 /sys/src/libc | |
parent | 67d9c6b2f98888dc81154b0499bbd26171f908a6 (diff) |
libc: cleanup atexit and put exits() in its own compilation unit
this avoids having to pull in atexit() and its dependencies
(lock(), unlock()) into every program. (as exits() is called
by _main() from main9.s).
Diffstat (limited to 'sys/src/libc')
-rw-r--r-- | sys/src/libc/port/atexit.c | 46 | ||||
-rw-r--r-- | sys/src/libc/port/exits.c | 13 | ||||
-rw-r--r-- | sys/src/libc/port/mkfile | 1 |
3 files changed, 35 insertions, 25 deletions
diff --git a/sys/src/libc/port/atexit.c b/sys/src/libc/port/atexit.c index e9cb6c297..b2339f0f7 100644 --- a/sys/src/libc/port/atexit.c +++ b/sys/src/libc/port/atexit.c @@ -1,7 +1,7 @@ #include <u.h> #include <libc.h> -#define NEXIT 33 +extern void (*_onexit)(void); typedef struct Onex Onex; struct Onex{ @@ -10,16 +10,31 @@ struct Onex{ }; static Lock onexlock; -Onex onex[NEXIT]; +static Onex onex[33]; + +static void +onexit(void) +{ + int i, pid; + void (*f)(void); + + pid = getpid(); + for(i = nelem(onex)-1; i >= 0; i--) + if((f = onex[i].f) != nil && onex[i].pid == pid) { + onex[i].f = nil; + (*f)(); + } +} int atexit(void (*f)(void)) { int i; + _onexit = onexit; lock(&onexlock); - for(i=0; i<NEXIT; i++) - if(onex[i].f == 0) { + for(i=0; i<nelem(onex); i++) + if(onex[i].f == nil) { onex[i].pid = getpid(); onex[i].f = f; unlock(&onexlock); @@ -35,26 +50,7 @@ atexitdont(void (*f)(void)) int i, pid; pid = getpid(); - for(i=0; i<NEXIT; i++) + for(i=0; i<nelem(onex); i++) if(onex[i].f == f && onex[i].pid == pid) - onex[i].f = 0; + onex[i].f = nil; } - -#pragma profile off - -void -exits(char *s) -{ - int i, pid; - void (*f)(void); - - pid = getpid(); - for(i = NEXIT-1; i >= 0; i--) - if((f = onex[i].f) && pid == onex[i].pid) { - onex[i].f = 0; - (*f)(); - } - _exits(s); -} - -#pragma profile on diff --git a/sys/src/libc/port/exits.c b/sys/src/libc/port/exits.c new file mode 100644 index 000000000..f4d76a6c6 --- /dev/null +++ b/sys/src/libc/port/exits.c @@ -0,0 +1,13 @@ +#include <u.h> +#include <libc.h> + +void (*_onexit)(void); + +#pragma profile off + +void +exits(char *s) +{ + if(_onexit != nil) (*_onexit)(); + _exits(s); +} diff --git a/sys/src/libc/port/mkfile b/sys/src/libc/port/mkfile index f3d32aaca..402990422 100644 --- a/sys/src/libc/port/mkfile +++ b/sys/src/libc/port/mkfile @@ -22,6 +22,7 @@ CFILES=\ ctype.c\ encodefmt.c\ execl.c\ + exits.c\ exp.c\ fabs.c\ floor.c\ |