From nobody Tue Sep 26 20:26:33 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4RwB956JsQz4vcf8; Tue, 26 Sep 2023 20:26:33 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4RwB955pRrz3VyN; Tue, 26 Sep 2023 20:26:33 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1695759993; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=4eNFYoeJtMvLicy0Kmv/UW6jPewgzE0eg4FwUY9Ct68=; b=q5flPH8sxhXNTWj4r9a1kUKcc2PKGJ8AaKe+7CFkNI36nYQdM5ZZN5d6ORnfcqML2w9lXG xub12VTqxl1iWRsI6+HuRuFCizr29FUg60991S1NLebao+mcdchZcwFUedwu1rjuTBiQrl bNoTUGmPu2+wzEcKcNw/tOODoHhh9goOmUMv6Xq7q/bSdt0tO5C8xI7lQc/TjDpAzrBnys QxJbhqy+EWQYRKoWRyWGMK6m9HekCjXbBt7E3YiB7iCZ4QtXAkjCO7ucAMeYC3XEDz1f3q 4b7wMztVM810V+ZQpLDfBsaEefJmVdk/6yTia3W+p3H7ExIYxKJuTaK5++IBSA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1695759993; a=rsa-sha256; cv=none; b=r9t2CbapA9rBLbJKwoJesNb6v481mouPFBlVF9z1GL7kB7cYw88H6uX+InsvkzIcwnTPlN 9FgoS39MSyvvgraY+u2zTDKDxtumaTB8B7x/zAvbiKIvPWTIKPD2BSKcRMHYxYjVD32Wuy ICGPWVxxdMlwZBYmKtVcRS9U61iRwYg++Wxhigbl0dBI5GbxdLY1G/22XAqt1Q8WRl0XuT r2XdFsxjVDMBS2M2NoEWgEyURT4c9jPFIPneJrIDD3i/T3OaRvMubtUmiI3r6VWHEKX3Vr /30DulG5HIbh9/6UXzqg55s9xk7WU/G1WFnTo1zbdzwudZr1wInCnFMFFfBgvw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1695759993; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=4eNFYoeJtMvLicy0Kmv/UW6jPewgzE0eg4FwUY9Ct68=; b=P2TKI73jk66TM36UViY00EEx29sJ2Wl2IamByGe5KofJWkbZT3WG7ehximZpQ1d5YFz/rD FQmSte0zs90S4gFHCqI0DU9pdczQLddF+rpteevTDj1G+ICFE9C+ulQFAeEFMs4OnfhcSm dzi4b12SggWI40CivA6ty1I2voklEo/cYeGZ8wmL5mzjbU342LclD80ap58WQgUbLgULoT yRqG8Ghk10SgqLp10XHd6jkUhTfjpibgXeJdNIv64GTjek7gWjx+Gvp9qoqFd9+3atW6I4 cykIzjjdYOW4SFSbrpJL61AkKUaaz/a7oFCW9TlMEaFdKUMf9J3CbtvWYOiczg== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4RwB954lMsz19gJ; Tue, 26 Sep 2023 20:26:33 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 38QKQXnK082952; Tue, 26 Sep 2023 20:26:33 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 38QKQXlm082949; Tue, 26 Sep 2023 20:26:33 GMT (envelope-from git) Date: Tue, 26 Sep 2023 20:26:33 GMT Message-Id: <202309262026.38QKQXlm082949@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Dag-Erling =?utf-8?Q?Sm=C3=B8rgrav?= Subject: git: 1dc3abb05243 - main - libc: Rewrite quick_exit() and at_quick_exit() using C11 atomics. List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: des X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 1dc3abb052430279e47c8922d22b30922adcf0f6 Auto-Submitted: auto-generated The branch main has been updated by des: URL: https://cgit.FreeBSD.org/src/commit/?id=1dc3abb052430279e47c8922d22b30922adcf0f6 commit 1dc3abb052430279e47c8922d22b30922adcf0f6 Author: Dag-Erling Smørgrav AuthorDate: 2023-09-26 20:06:27 +0000 Commit: Dag-Erling Smørgrav 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 -#include + +#include #include -#include /** - * 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);