Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 26 Sep 2023 20:26:33 GMT
From:      Dag-Erling =?utf-8?Q?Sm=C3=B8rgrav?= <des@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: 1dc3abb05243 - main - libc: Rewrite quick_exit() and at_quick_exit() using C11 atomics.
Message-ID:  <202309262026.38QKQXlm082949@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch main has been updated by des:

URL: https://cgit.FreeBSD.org/src/commit/?id=1dc3abb052430279e47c8922d22b30922adcf0f6

commit 1dc3abb052430279e47c8922d22b30922adcf0f6
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2023-09-26 20:06:27 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2023-09-26 20:07:10 +0000

    libc: Rewrite quick_exit() and at_quick_exit() using C11 atomics.
    
    Compiler memory barriers do not prevent the CPU from executing the code
    out of order.  Switch to C11 atomics.  This also lets us get rid of the
    mutex; instead, loop until the compare_exchange succeeds.
    
    While here, change the return value of at_quick_exit() on failure to
    the more traditional -1, matching atexit().
    
    Sponsored by:   Klara, Inc.
    Reviewed by:    Olivier Certner, kevans, kib
    Differential Revision:  https://reviews.freebsd.org/D41936
---
 lib/libc/stdlib/quick_exit.c | 40 ++++++++++++++++------------------------
 1 file changed, 16 insertions(+), 24 deletions(-)

diff --git a/lib/libc/stdlib/quick_exit.c b/lib/libc/stdlib/quick_exit.c
index 05db690cb6e7..4dee7b20bd2b 100644
--- a/lib/libc/stdlib/quick_exit.c
+++ b/lib/libc/stdlib/quick_exit.c
@@ -2,6 +2,7 @@
  * SPDX-License-Identifier: BSD-2-Clause
  *
  * Copyright (c) 2011 David Chisnall
+ * Copyright (c) 2023 Klara, Inc.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -27,44 +28,35 @@
  */
 
 #include <sys/types.h>
-#include <machine/atomic.h>
+
+#include <stdatomic.h>
 #include <stdlib.h>
-#include <pthread.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.
+ * Linked list of quick exit handlers.  These will be invoked in reverse
+ * order of insertion when quick_exit() is called.  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.
- */
-static pthread_mutex_t atexit_mutex = PTHREAD_MUTEX_INITIALIZER;
-/**
- * Stack of cleanup handlers.  These will be invoked in reverse order when 
- */
-static struct quick_exit_handler *handlers;
+static _Atomic(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);
+	if ((h = calloc(1, sizeof(*h))) == NULL) {
+		return (-1);
+	}
 	h->cleanup = func;
-	pthread_mutex_lock(&atexit_mutex);
-	h->next = handlers;
-	__compiler_membar();
-	handlers = h;
-	pthread_mutex_unlock(&atexit_mutex);
+	while (!atomic_compare_exchange_strong(&handlers, &h->next, h)) {
+		/* nothing */ ;
+	}
 	return (0);
 }
 
@@ -77,8 +69,8 @@ quick_exit(int status)
 	 * XXX: The C++ spec requires us to call std::terminate if there is an
 	 * exception here.
 	 */
-	for (h = handlers; NULL != h; h = h->next) {
-		__compiler_membar();
+	for (h = atomic_load_explicit(&handlers, memory_order_acquire);
+	     h != NULL; h = h->next) {
 		h->cleanup();
 	}
 	_Exit(status);



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202309262026.38QKQXlm082949>