From patchwork Mon Feb 4 10:05:16 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 31294 Received: (qmail 44056 invoked by alias); 4 Feb 2019 10:05:20 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 44042 invoked by uid 89); 4 Feb 2019 10:05:20 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=self, Our, our X-HELO: mx1.redhat.com From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH] nptl: Use C11-style atomics for access to __nptl_nthreads Date: Mon, 04 Feb 2019 11:05:16 +0100 Message-ID: <87pns7x40z.fsf@oldenburg2.str.redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.1 (gnu/linux) MIME-Version: 1.0 2019-02-03 Florian Weimer * nptl/allocatestack.c (__reclaim_stacks): Use C-11-style atomic access for __nptl_nthreads. * nptl/pthread_create.c (__nptl_deallocate_tsd): Likewise. (START_THREAD_DEFN): Likewise. (__pthread_create_2_1): Likewise. diff --git a/nptl/allocatestack.c b/nptl/allocatestack.c index 670cb8ffe6..0dbb155f70 100644 --- a/nptl/allocatestack.c +++ b/nptl/allocatestack.c @@ -952,7 +952,7 @@ __reclaim_stacks (void) list_add (&self->list, &stack_used); /* There is one thread running. */ - __nptl_nthreads = 1; + atomic_store_relaxed (&__nptl_nthreads, 1); in_flight_stack = 0; diff --git a/nptl/pthread_create.c b/nptl/pthread_create.c index 04d1c6453e..15fbeceac7 100644 --- a/nptl/pthread_create.c +++ b/nptl/pthread_create.c @@ -341,7 +341,7 @@ __nptl_main_thread_exited (void) { __nptl_deallocate_tsd (); - if (! atomic_decrement_and_test (&__nptl_nthreads)) + if (atomic_fetch_add_relaxed (&__nptl_nthreads, -1) > 1) /* Exit the thread, but not the process. */ __exit_thread (); } @@ -510,7 +510,7 @@ START_THREAD_DEFN /* If this is the last thread we terminate the process now. We do not notify the debugger, it might just irritate it if there is no thread left. */ - if (__glibc_unlikely (atomic_decrement_and_test (&__nptl_nthreads))) + if (atomic_fetch_add_relaxed (&__nptl_nthreads, -1) == 1) /* This was the last thread. */ exit (0); @@ -769,7 +769,7 @@ __pthread_create_2_1 (pthread_t *newthread, const pthread_attr_t *attr, collect_default_sched (pd); } - if (__glibc_unlikely (__nptl_nthreads == 1)) + if (__glibc_unlikely (atomic_load_relaxed (&__nptl_nthreads) == 1)) _IO_enable_locks (); /* Pass the descriptor to the caller. */ @@ -785,7 +785,7 @@ __pthread_create_2_1 (pthread_t *newthread, const pthread_attr_t *attr, we momentarily store a false value; this doesn't matter because there is no kosher thing a signal handler interrupting us right here can do that cares whether the thread count is correct. */ - atomic_increment (&__nptl_nthreads); + atomic_fetch_add_relaxed (&__nptl_nthreads, 1); /* Our local value of stopped_start and thread_ran can be accessed at any time. The PD->stopped_start may only be accessed if we have @@ -850,7 +850,7 @@ __pthread_create_2_1 (pthread_t *newthread, const pthread_attr_t *attr, NOTES above). */ /* Oops, we lied for a second. */ - atomic_decrement (&__nptl_nthreads); + atomic_fetch_add_relaxed (&__nptl_nthreads, -1); /* Perhaps a thread wants to change the IDs and is waiting for this stillborn thread. */