aboutsummaryrefslogtreecommitdiff
path: root/sys/rt/atexit.c
diff options
context:
space:
mode:
Diffstat (limited to 'sys/rt/atexit.c')
-rw-r--r--sys/rt/atexit.c49
1 files changed, 49 insertions, 0 deletions
diff --git a/sys/rt/atexit.c b/sys/rt/atexit.c
new file mode 100644
index 0000000..208cc6e
--- /dev/null
+++ b/sys/rt/atexit.c
@@ -0,0 +1,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;
+}