Module Name:    src
Committed By:   christos
Date:           Fri Apr 26 18:29:55 UTC 2013

Modified Files:
        src/lib/libc/stdlib: Makefile.inc atexit.3 atexit.c exit.3
Added Files:
        src/lib/libc/stdlib: at_quick_exit.3 quick_exit.3 quick_exit.c

Log Message:
add {at_,}quick_exit(3) from FreeBSD


To generate a diff of this commit:
cvs rdiff -u -r1.78 -r1.79 src/lib/libc/stdlib/Makefile.inc
cvs rdiff -u -r0 -r1.1 src/lib/libc/stdlib/at_quick_exit.3 \
    src/lib/libc/stdlib/quick_exit.3 src/lib/libc/stdlib/quick_exit.c
cvs rdiff -u -r1.9 -r1.10 src/lib/libc/stdlib/atexit.3
cvs rdiff -u -r1.24 -r1.25 src/lib/libc/stdlib/atexit.c
cvs rdiff -u -r1.15 -r1.16 src/lib/libc/stdlib/exit.3

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/lib/libc/stdlib/Makefile.inc
diff -u src/lib/libc/stdlib/Makefile.inc:1.78 src/lib/libc/stdlib/Makefile.inc:1.79
--- src/lib/libc/stdlib/Makefile.inc:1.78	Wed Sep 14 19:33:51 2011
+++ src/lib/libc/stdlib/Makefile.inc	Fri Apr 26 14:29:55 2013
@@ -1,4 +1,4 @@
-#	$NetBSD: Makefile.inc,v 1.78 2011/09/14 23:33:51 christos Exp $
+#	$NetBSD: Makefile.inc,v 1.79 2013/04/26 18:29:55 christos Exp $
 #	from: @(#)Makefile.inc	8.3 (Berkeley) 2/4/95
 
 # stdlib sources
@@ -11,7 +11,7 @@ SRCS+=	_env.c _rand48.c \
 	hcreate.c heapsort.c imaxdiv.c insque.c jrand48.c l64a.c lldiv.c \
 	lcong48.c lrand48.c lsearch.c merge.c mi_vector_hash.c mrand48.c \
 	nrand48.c putenv.c qabs.c qdiv.c qsort.c posix_openpt.c pty.c \
-	radixsort.c rand.c rand_r.c random.c remque.c \
+	quick_exit.c radixsort.c rand.c rand_r.c random.c remque.c \
 	seed48.c setenv.c srand48.c strsuftoll.c \
 	strtoimax.c strtol.c strtoll.c strtoq.c strtoul.c strtoull.c \
 	strtoumax.c strtouq.c system.c tdelete.c tfind.c tsearch.c twalk.c \
@@ -38,6 +38,7 @@ CPPFLAGS.strtouq.c+=	-I${LIBCDIR}/../../
 .include "${ARCHDIR}/stdlib/Makefile.inc"
 
 MAN+=	a64l.3 abort.3 abs.3 alloca.3 atexit.3 atof.3 atoi.3 atol.3 atoll.3 \
+	at_quick_exit.3 \
 	bsearch.3 \
 	div.3 \
 	exit.3 \
@@ -48,7 +49,7 @@ MAN+=	a64l.3 abort.3 abs.3 alloca.3 atex
 	lsearch.3 \
 	malloc.3 memory.3 mi_vector_hash.3 \
 	posix_memalign.3 posix_openpt.3 ptsname.3 \
-	qabs.3 qdiv.3 qsort.3 \
+	qabs.3 qdiv.3 quick_exit.3 qsort.3 \
 	radixsort.3 rand48.3 rand.3 random.3 \
 	strfmon.3 strsuftoll.3 strtod.3 strtol.3 strtoul.3 system.3 \
 	tsearch.3 \

Index: src/lib/libc/stdlib/atexit.3
diff -u src/lib/libc/stdlib/atexit.3:1.9 src/lib/libc/stdlib/atexit.3:1.10
--- src/lib/libc/stdlib/atexit.3:1.9	Thu Aug  7 12:43:38 2003
+++ src/lib/libc/stdlib/atexit.3	Fri Apr 26 14:29:55 2013
@@ -1,4 +1,4 @@
-.\"	$NetBSD: atexit.3,v 1.9 2003/08/07 16:43:38 agc Exp $
+.\"	$NetBSD: atexit.3,v 1.10 2013/04/26 18:29:55 christos Exp $
 .\"
 .\" Copyright (c) 1990, 1991, 1993
 .\"	The Regents of the University of California.  All rights reserved.
@@ -68,7 +68,9 @@ No memory was available to add the funct
 The existing list of functions is unmodified.
 .El
 .Sh SEE ALSO
+.Xr at_quick_exit 3
 .Xr exit 3
+.Xr quick_exit 3
 .Sh STANDARDS
 The
 .Fn atexit

Index: src/lib/libc/stdlib/atexit.c
diff -u src/lib/libc/stdlib/atexit.c:1.24 src/lib/libc/stdlib/atexit.c:1.25
--- src/lib/libc/stdlib/atexit.c:1.24	Thu Oct  8 12:33:45 2009
+++ src/lib/libc/stdlib/atexit.c	Fri Apr 26 14:29:55 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: atexit.c,v 1.24 2009/10/08 16:33:45 pooka Exp $	*/
+/*	$NetBSD: atexit.c,v 1.25 2013/04/26 18:29:55 christos Exp $	*/
 
 /*-
  * Copyright (c) 2003 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
 
 #include <sys/cdefs.h>
 #if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: atexit.c,v 1.24 2009/10/08 16:33:45 pooka Exp $");
+__RCSID("$NetBSD: atexit.c,v 1.25 2013/04/26 18:29:55 christos Exp $");
 #endif /* LIBC_SCCS and not lint */
 
 #include "reentrant.h"
@@ -74,7 +74,7 @@ static struct atexit_handler *atexit_han
 
 #ifdef _REENTRANT
 /* ..and a mutex to protect it all. */
-static mutex_t atexit_mutex;
+mutex_t __atexit_mutex;
 #endif /* _REENTRANT */
 
 void	__libc_atexit_init(void) __attribute__ ((visibility("hidden")));
@@ -85,7 +85,7 @@ void	__libc_atexit_init(void) __attribut
  * if possible. cxa_atexit handlers are never allocated from the static
  * pool.
  *
- * atexit_mutex must be held.
+ * __atexit_mutex must be held.
  */
 static struct atexit_handler *
 atexit_handler_alloc(void *dso)
@@ -105,7 +105,7 @@ atexit_handler_alloc(void *dso)
 
 	/*
 	 * Either no static slot was free, or this is a cxa_atexit
-	 * handler.  Allocate a new one.  We keep the atexit_mutex
+	 * handler.  Allocate a new one.  We keep the __atexit_mutex
 	 * held to prevent handlers from being run while we (potentially)
 	 * block in malloc().
 	 */
@@ -114,7 +114,7 @@ atexit_handler_alloc(void *dso)
 }
 
 /*
- * Initialize atexit_mutex with the PTHREAD_MUTEX_RECURSIVE attribute.
+ * Initialize __atexit_mutex with the PTHREAD_MUTEX_RECURSIVE attribute.
  * Note that __cxa_finalize may generate calls to __cxa_atexit.
  */
 void
@@ -123,7 +123,7 @@ __libc_atexit_init(void)
 	mutexattr_t atexit_mutex_attr;
 	mutexattr_init(&atexit_mutex_attr);
 	mutexattr_settype(&atexit_mutex_attr, PTHREAD_MUTEX_RECURSIVE);
-	mutex_init(&atexit_mutex, &atexit_mutex_attr);
+	mutex_init(&__atexit_mutex, &atexit_mutex_attr);
 }
 
 /*
@@ -140,11 +140,11 @@ __cxa_atexit(void (*func)(void *), void 
 
 	_DIAGASSERT(func != NULL);
 
-	mutex_lock(&atexit_mutex);
+	mutex_lock(&__atexit_mutex);
 
 	ah = atexit_handler_alloc(dso);
 	if (ah == NULL) {
-		mutex_unlock(&atexit_mutex);
+		mutex_unlock(&__atexit_mutex);
 		return (-1);
 	}
 
@@ -155,7 +155,7 @@ __cxa_atexit(void (*func)(void *), void 
 	ah->ah_next = atexit_handler_stack;
 	atexit_handler_stack = ah;
 
-	mutex_unlock(&atexit_mutex);
+	mutex_unlock(&__atexit_mutex);
 	return (0);
 }
 
@@ -175,7 +175,7 @@ __cxa_finalize(void *dso)
 	void (*cxa_func)(void *);
 	void (*atexit_func)(void);
 
-	mutex_lock(&atexit_mutex);
+	mutex_lock(&__atexit_mutex);
 	call_depth++;
 
 	/*
@@ -220,7 +220,7 @@ again:
 			prevp = &ah->ah_next;
 	}
 	call_depth--;
-	mutex_unlock(&atexit_mutex);
+	mutex_unlock(&__atexit_mutex);
 
 	if (call_depth > 0)
 		return;

Index: src/lib/libc/stdlib/exit.3
diff -u src/lib/libc/stdlib/exit.3:1.15 src/lib/libc/stdlib/exit.3:1.16
--- src/lib/libc/stdlib/exit.3:1.15	Mon Jan  2 10:55:29 2012
+++ src/lib/libc/stdlib/exit.3	Fri Apr 26 14:29:55 2013
@@ -1,4 +1,4 @@
-.\" $NetBSD: exit.3,v 1.15 2012/01/02 15:55:29 dholland Exp $
+.\" $NetBSD: exit.3,v 1.16 2013/04/26 18:29:55 christos Exp $
 .\"
 .\" Copyright (c) 1990, 1991, 1993
 .\"	The Regents of the University of California.  All rights reserved.
@@ -98,8 +98,10 @@ function
 never returns.
 .Sh SEE ALSO
 .Xr _exit 2 ,
+.Xr at_quick_exit 3 ,
 .Xr atexit 3 ,
 .Xr intro 3 ,
+.Xr quick_exit 3 ,
 .Xr tmpfile 3
 .Sh STANDARDS
 The

Added files:

Index: src/lib/libc/stdlib/at_quick_exit.3
diff -u /dev/null src/lib/libc/stdlib/at_quick_exit.3:1.1
--- /dev/null	Fri Apr 26 14:29:55 2013
+++ src/lib/libc/stdlib/at_quick_exit.3	Fri Apr 26 14:29:55 2013
@@ -0,0 +1,63 @@
+.\"	$NetBSD: at_quick_exit.3,v 1.1 2013/04/26 18:29:55 christos Exp $
+.\"  Copyright (c) 2011 David Chisnall
+.\"  All rights reserved.
+.\"
+.\"  Redistribution and use in source and binary forms, with or without
+.\"  modification, are permitted provided that the following conditions
+.\"  are met:
+.\"  1. Redistributions of source code must retain the above copyright
+.\"     notice, this list of conditions and the following disclaimer.
+.\"  2. Redistributions in binary form must reproduce the above copyright
+.\"     notice, this list of conditions and the following disclaimer in the
+.\"     documentation and/or other materials provided with the distribution.
+.\"
+.\"  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+.\"  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+.\"  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+.\"  ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+.\"  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+.\"  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+.\"  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\"  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+.\"  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+.\"  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+.\"  SUCH DAMAGE.
+.\"
+.\"  $FreeBSD: src/lib/libc/stdlib/at_quick_exit.3,v 1.5 2012/11/17 01:49:41 svnexp Exp $
+.\"
+.Dd December 7, 2011
+.Dt AT_QUICK_EXIT 3
+.Os
+.Sh NAME
+.Nm at_quick_exit
+.Nd registers a cleanup function to run on quick exit
+.Sh LIBRARY
+.Lb libc
+.Sh SYNOPSIS
+.In stdlib.h
+.Ft int
+.Fn at_quick_exit "void (*func)(void)"
+.Sh DESCRIPTION
+The
+.Fn at_quick_exit
+function registers a cleanup function to be called when the program exits as a
+result of calling
+.Xr quick_exit 3 .
+The cleanup functions are called in the reverse order and will not be called if
+the program exits by calling
+.Xr exit 3 ,
+.Xr _Exit 3 ,
+or
+.Xr abort 3 .
+.Sh RETURN VALUES
+The
+.Fn at_quick_exit
+function returns the value 0 if successful and a non-zero value on failure.
+.Sh SEE ALSO
+.Xr exit 3 ,
+.Xr quick_exit 3
+.Sh STANDARDS
+The
+.Fn at_quick_exit
+function conforms to
+.St -isoC-2011 .
Index: src/lib/libc/stdlib/quick_exit.3
diff -u /dev/null src/lib/libc/stdlib/quick_exit.3:1.1
--- /dev/null	Fri Apr 26 14:29:55 2013
+++ src/lib/libc/stdlib/quick_exit.3	Fri Apr 26 14:29:55 2013
@@ -0,0 +1,59 @@
+.\"	$NetBSD: quick_exit.3,v 1.1 2013/04/26 18:29:55 christos Exp $
+.\"  Copyright (c) 2011 David Chisnall
+.\"  All rights reserved.
+.\" 
+.\"  Redistribution and use in source and binary forms, with or without
+.\"  modification, are permitted provided that the following conditions
+.\"  are met:
+.\"  1. Redistributions of source code must retain the above copyright
+.\"     notice, this list of conditions and the following disclaimer.
+.\"  2. Redistributions in binary form must reproduce the above copyright
+.\"     notice, this list of conditions and the following disclaimer in the
+.\"     documentation and/or other materials provided with the distribution.
+.\" 
+.\"  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+.\"  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+.\"  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+.\"  ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+.\"  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+.\"  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+.\"  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\"  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+.\"  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+.\"  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+.\"  SUCH DAMAGE.
+.\" 
+.\"  $FreeBSD: src/lib/libc/stdlib/quick_exit.3,v 1.4 2012/11/17 01:49:41 svnexp Exp $
+.\"
+.Dd December 7, 2011
+.Dt QUICK_EXIT 3
+.Os
+.Sh NAME
+.Nm quick_exit
+.Nd exits a program quickly, running minimal cleanup
+.Sh LIBRARY
+.Lb libc
+.Sh SYNOPSIS
+.In stdlib.h
+.Ft _Noreturn void
+.Fn quick_exit "void"
+.Sh DESCRIPTION
+The
+.Fn quick_exit
+function exits the program quickly calling any cleanup functions registered
+with
+.Xr at_quick_exit 3
+but not any C++ destructors or cleanup code registered with
+.Xr atexit 3 .
+.Sh RETURN VALUES
+The
+.Fn quick_exit
+function does not return.
+.Sh SEE ALSO
+.Xr at_quick_exit 3 ,
+.Xr exit 3
+.Sh STANDARDS
+The
+.Fn quick_exit
+function conforms to
+.St -isoC-2011 .
Index: src/lib/libc/stdlib/quick_exit.c
diff -u /dev/null src/lib/libc/stdlib/quick_exit.c:1.1
--- /dev/null	Fri Apr 26 14:29:55 2013
+++ src/lib/libc/stdlib/quick_exit.c	Fri Apr 26 14:29:55 2013
@@ -0,0 +1,93 @@
+/*	$NetBSD: quick_exit.c,v 1.1 2013/04/26 18:29:55 christos Exp $	*/
+
+/*-
+ * Copyright (c) 2011 David Chisnall
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD: src/lib/libc/stdlib/quick_exit.c,v 1.4 2012/11/17 01:49:41 svnexp Exp $
+ */
+#include <sys/cdefs.h>
+__RCSID("$NetBSD: quick_exit.c,v 1.1 2013/04/26 18:29:55 christos Exp $");
+
+#include "namespace.h"
+#include "reentrant.h"
+
+#include <stdlib.h>
+
+/**
+ * Linked list of quick exit handlers.  This is simpler than the atexit()
+ * version, because it is not required to support C++ destructors or
+ * DSO-specific cleanups.
+ */
+struct quick_exit_handler {
+	struct quick_exit_handler *next;
+	void (*cleanup)(void);
+};
+
+/**
+ * Lock protecting the handlers list.
+ */
+#ifdef _REENTRANT
+extern mutex_t __atexit_mutex;
+#endif
+
+/**
+ * Stack of cleanup handlers.  These will be invoked in reverse order when 
+ */
+static struct quick_exit_handler *handlers;
+
+int
+at_quick_exit(void (*func)(void))
+{
+	struct quick_exit_handler *h;
+	
+	h = malloc(sizeof(*h));
+
+	if (NULL == h)
+		return 1;
+	h->cleanup = func;
+#ifdef _REENTRANT
+	mutex_lock(&__atexit_mutex);
+#endif
+	h->next = handlers;
+	handlers = h;
+#ifdef _REENTRANT
+	mutex_unlock(&__atexit_mutex);
+#endif
+	return 0;
+}
+
+void
+quick_exit(int status)
+{
+	struct quick_exit_handler *h;
+
+	/*
+	 * XXX: The C++ spec requires us to call std::terminate if there is an
+	 * exception here.
+	 */
+	for (h = handlers; NULL != h; h = h->next)
+		(*h->cleanup)();
+	_Exit(status);
+}

Reply via email to