diff --git a/sys/src/lib/c/build.json b/sys/src/lib/c/build.json index cc5469e..60b4489 100644 --- a/sys/src/lib/c/build.json +++ b/sys/src/lib/c/build.json @@ -139,6 +139,7 @@ "port/ctype.c", "port/encodefmt.c", "port/execl.c", + "port/exits.c", "port/exp.c", "port/fabs.c", "port/floor.c", diff --git a/sys/src/lib/c/port/atexit.c b/sys/src/lib/c/port/atexit.c index 5e478c8..555a09a 100644 --- a/sys/src/lib/c/port/atexit.c +++ b/sys/src/lib/c/port/atexit.c @@ -10,6 +10,8 @@ #include #include +extern void (*_jehanne_onexit)(void); + #define NEXIT 33 typedef struct Onex Onex; @@ -21,11 +23,26 @@ struct Onex{ static Lock onexlock; Onex onex[NEXIT]; +static void +onexit(void) +{ + int i, pid; + void (*f)(void); + + pid = jehanne_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 jehanne_atexit(void (*f)(void)) { int i; + _jehanne_onexit = onexit; jehanne_lock(&onexlock); for(i=0; i= 0; i--) - if((f = onex[i].f) && pid == onex[i].pid) { - onex[i].f = 0; - (*f)(); - } - _fini(); - _exits(s); - __builtin_unreachable(); -} - -#pragma profile on diff --git a/sys/src/lib/c/port/exits.c b/sys/src/lib/c/port/exits.c new file mode 100644 index 0000000..bce25bc --- /dev/null +++ b/sys/src/lib/c/port/exits.c @@ -0,0 +1,25 @@ +/* + * This file is part of the UCB release of Plan 9. It is subject to the license + * terms in the LICENSE file found in the top-level directory of this + * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No + * part of the UCB release of Plan 9, including this file, may be copied, + * modified, propagated, or distributed except according to the terms contained + * in the LICENSE file. + */ + +#include +#include + +void (*_jehanne_onexit)(void); + +void +jehanne_exits(const char *s) +{ + void _fini(void); + + if(_jehanne_onexit != nil) + (*_jehanne_onexit)(); + _fini(); + _exits(s); + __builtin_unreachable(); +}