From e3d8fe9d4a1085cbf7237411bcd1996613334994 Mon Sep 17 00:00:00 2001 From: cinap_lenrek Date: Fri, 20 Oct 2017 20:58:38 +0200 Subject: 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). --- sys/src/libc/port/atexit.c | 46 +++++++++++++++++++++------------------------- sys/src/libc/port/exits.c | 13 +++++++++++++ sys/src/libc/port/mkfile | 1 + 3 files changed, 35 insertions(+), 25 deletions(-) create mode 100644 sys/src/libc/port/exits.c (limited to 'sys/src') 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 #include -#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= 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 +#include + +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\ -- cgit v1.2.3