From mboxrd@z Thu Jan 1 00:00:00 1970 From: Satyam Sharma Subject: Re: [PATCH 0/24] make atomic_read() behave consistently across all architectures Date: Thu, 16 Aug 2007 06:53:45 +0530 (IST) Message-ID: References: <20070809131423.GA9927@shell.boston.redhat.com> <46C2D6F3.3070707@s5r6.in-berlin.de> <9350e9fab505c92af8d5e1f3441d6ad2@kernel.crashing.org> Mime-Version: 1.0 Content-Type: TEXT/PLAIN; charset=us-ascii Cc: Christoph Lameter , heiko.carstens@de.ibm.com, horms@verge.net.au, Stefan Richter , Linux Kernel Mailing List , "Paul E. McKenney" , netdev@vger.kernel.org, ak@suse.de, cfriesen@nortel.com, rpjday@mindspring.com, jesper.juhl@gmail.com, linux-arch@vger.kernel.org, Andrew Morton , zlynx@acm.org, schwidefsky@de.ibm.com, Chris Snook , Herbert Xu , davem@davemloft.net, Linus Torvalds , wensong@linux-vs.org, wjiang@resilience.com To: Segher Boessenkool Return-path: Received: from pentafluge.infradead.org ([213.146.154.40]:50344 "EHLO pentafluge.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1762600AbXHPBLO (ORCPT ); Wed, 15 Aug 2007 21:11:14 -0400 In-Reply-To: Sender: netdev-owner@vger.kernel.org List-Id: netdev.vger.kernel.org On Wed, 15 Aug 2007, Segher Boessenkool wrote: > [...] > > BTW: > > > > #define atomic_read(a) (*(volatile int *)&(a)) > > #define atomic_set(a,i) (*(volatile int *)&(a) = (i)) > > > > int a; > > > > void func(void) > > { > > int b; > > > > b = atomic_read(a); > > atomic_set(a, 20); > > b = atomic_read(a); > > } > > > > gives: > > > > func: > > pushl %ebp > > movl a, %eax > > movl %esp, %ebp > > movl $20, a > > movl a, %eax > > popl %ebp > > ret > > > > so the first atomic_read() wasn't optimized away. > > Of course. It is executed by the abstract machine, so > it will be executed by the actual machine. On the other > hand, try > > b = 0; > if (b) > b = atomic_read(a); > > or similar. Yup, obviously. Volatile accesses (or any access to volatile objects), or even "__volatile__ asms" (which gcc normally promises never to elid) can always be optimized for cases such as these where the compiler can trivially determine that the code in question is not reachable.