From patchwork Thu Aug 30 10:47:50 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thiago Macieira X-Patchwork-Id: 180815 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id 57D8B2C0169 for ; Thu, 30 Aug 2012 20:48:26 +1000 (EST) Comment: DKIM? See http://www.dkim.org DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=gcc.gnu.org; s=default; x=1346928507; h=Comment: DomainKey-Signature:Received:Received:Received:Received:Received: From:To:Subject:Date:Message-ID:User-Agent:MIME-Version: Content-Type:Content-Transfer-Encoding:Mailing-List:Precedence: List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender: Delivered-To; bh=oD2Jmksekecb3wOF3fIDSCI3Guo=; b=vAh8lTEvl/bHZWl 4z/CDUWDR68Omx27S7DR75rEl6eSreMIPqv6l4iFGh01IvQHaGGbcveXnloVn5RR /rXqXRunLD6RF/GxfW3aJB7TEcuhvsGHaLFhf0+WtwO73DDQQi7hoBDmgVhvHqdH oO6SgkWEZFQYRNpCmUvDarquyQf0= Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=gcc.gnu.org; h=Received:Received:X-SWARE-Spam-Status:X-Spam-Check-By:Received:Received:X-ExtLoop1:Received:From:To:Subject:Date:Message-ID:User-Agent:MIME-Version:Content-Type:Content-Transfer-Encoding:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=oU2g4/591LFOYrNoOhs1wr4uVGdsVySl5qIi5a8H6bURIfAX/Sr8Sk52iD+Whl 9DHl+ITbfLdifrfvG46Br1YNzcqA4z0oXeN+VvS/HlZRmynuiRbSUvMwdnPT2rMH 6TgaJxpTZeazpkoIPjHSQYYKYrUVH6afPr0mZmpH/szvU=; Received: (qmail 30539 invoked by alias); 30 Aug 2012 10:48:17 -0000 Received: (qmail 30519 invoked by uid 22791); 30 Aug 2012 10:48:16 -0000 X-SWARE-Spam-Status: No, hits=-6.6 required=5.0 tests=BAYES_00, KHOP_RCVD_UNTRUST, RCVD_IN_DNSWL_HI, RCVD_IN_HOSTKARMA_W, RP_MATCHES_RCVD, TW_CX X-Spam-Check-By: sourceware.org Received: from mga09.intel.com (HELO mga09.intel.com) (134.134.136.24) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 30 Aug 2012 10:48:03 +0000 Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga102.jf.intel.com with ESMTP; 30 Aug 2012 03:47:50 -0700 X-ExtLoop1: 1 Received: from unknown (HELO tjmaciei-mobl2.localnet) ([10.252.121.147]) by orsmga001.jf.intel.com with ESMTP; 30 Aug 2012 03:47:51 -0700 From: Thiago Macieira To: gcc-patches@gcc.gnu.org, libstdc++@gcc.gnu.org Subject: [PATCH, libstdc++] Use acquire semantics in case of CAS failure Date: Thu, 30 Aug 2012 12:47:50 +0200 Message-ID: <1697044.gs2fX9z0Mp@tjmaciei-mobl2> User-Agent: KMail/4.9 beta1 (Linux/3.5.2-3.fc17.x86_64; KDE/4.8.5; x86_64; git-2bb8395; 2012-06-03) MIME-Version: 1.0 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Hello I detected this issue as I was updating the patches to send to the mailing list. I have not created a bug report. When the CAS operation fails and expected == guard_bit, __cxa_guard_acquire will return immediately indicating that the initialisation has already succeeded. However, it's missing the acquire barrier for the changes done on the other thread, to match the release barrier from __cxa_guard_release. That is: thread A thread B load.acq == 0 load.acq == 0 __cxa_guard_acquire __cxa_guard_acquire CAS(0 -> 256) success __cxa_guard_release store.rel(1) CAS(0 ->256) fails At this point, we must synchronise with the store-release from thread A. 2012-08-30 Thiago Macieira * libsupc++/guard.cc (__cxa_guard_acquire): must use acquire semantics in case of failure, to acquire changes done by the other thread --- libstdc++-v3/libsupc++/guard.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libstdc++-v3/libsupc++/guard.cc b/libstdc++-v3/libsupc++/guard.cc index 36352e7..73d7221 100644 --- a/libstdc++-v3/libsupc++/guard.cc +++ b/libstdc++-v3/libsupc++/guard.cc @@ -253,7 +253,7 @@ namespace __cxxabiv1 int expected(0); if (__atomic_compare_exchange_n(gi, &expected, pending_bit, false, __ATOMIC_ACQ_REL, - __ATOMIC_RELAXED)) + __ATOMIC_ACQUIRE)) { // This thread should do the initialization. return 1; -- 1.7.11.4