[backport proposed 4.8 improvement to __cxa_guard_acquire ] From: Thiago Macieira Subject: [PATCH, libstdc++] Improve slightly __cxa_guard_acquire Date: Thu, 30 Aug 2012 12:48:34 +0200 List-Id: Hello The attached patch is a simple improvement to make a thread that failed to set the waiting bit to exit the function earlier, if it detects that another thread has successfully finished initialising. It matches the CAS code from a few lines above. The change from RELAXED to ACQUIRE is noted in the previous patch I've just sent. 2012-08-30 Thiago Macieira * libsupc++/guard.cc (__cxa_guard_acquire): exit the loop earlier if we detect that another thread has had success. --- gcc-4.7.1/libstdc++-v3/libsupc++/guard.cc.~1~ 2012-09-08 22:16:41.000000000 +0200 +++ gcc-4.7.1/libstdc++-v3/libsupc++/guard.cc 2012-09-08 22:19:01.000000000 +0200 @@ -269,8 +269,16 @@ namespace __cxxabiv1 int newv = expected | waiting_bit; if (!__atomic_compare_exchange_n(gi, &expected, newv, false, __ATOMIC_ACQ_REL, - __ATOMIC_RELAXED)) - continue; + __ATOMIC_ACQUIRE)) + { + if (expected == guard_bit) + { + // Already initialized. + return 0; + } + if (expected == 0) + continue; + } expected = newv; }