]> git.ipfire.org Git - thirdparty/gcc.git/commit
c++: Ensure correct destruction order of local statics [PR99613]
authorJakub Jelinek <jakub@redhat.com>
Tue, 16 Mar 2021 20:17:44 +0000 (21:17 +0100)
committerJakub Jelinek <jakub@redhat.com>
Fri, 19 Mar 2021 23:27:54 +0000 (00:27 +0100)
commit1703937a05b8b95bc29d2de292387dfd9eb7c9a3
tree5fb9023171d12d467d07eecd81c3e80ebd523ce8
parent788da80413551fe1a1411c700864640b590dcfc5
c++: Ensure correct destruction order of local statics [PR99613]

As mentioned in the PR, if end of two constructions of local statics
is strongly ordered, their destructors should be run in the reverse order.
As we run __cxa_guard_release before calling __cxa_atexit, it is possible
that we have two threads that access two local statics in the same order
for the first time, one thread wins the __cxa_guard_acquire on the first
one but is rescheduled in between the __cxa_guard_release and __cxa_atexit
calls, then the other thread is scheduled and wins __cxa_guard_acquire
on the second one and calls __cxa_quard_release and __cxa_atexit and only
afterwards the first thread calls its __cxa_atexit.  This means a variable
whose completion of the constructor strongly happened after the completion
of the other one will be destructed after the other variable is destructed.

The following patch fixes that by swapping the __cxa_guard_release and
__cxa_atexit calls.

2021-03-16  Jakub Jelinek  <jakub@redhat.com>

PR c++/99613
* decl.c (expand_static_init): For thread guards, call __cxa_atexit
before calling __cxa_guard_release rather than after it.  Formatting
fixes.

(cherry picked from commit 0251051db64f13c9a31a05c8133c31dc50b2b235)
gcc/cp/decl.c