• P
    locking/atomic: Introduce atomic_try_cmpxchg() · a9ebf306
    Peter Zijlstra 提交于
    Add a new cmpxchg interface:
    
      bool try_cmpxchg(u{8,16,32,64} *ptr, u{8,16,32,64} *val, u{8,16,32,64} new);
    
    Where the boolean returns the result of the compare; and thus if the
    exchange happened; and in case of failure, the new value of *ptr is
    returned in *val.
    
    This allows simplification/improvement of loops like:
    
    	for (;;) {
    		new = val $op $imm;
    		old = cmpxchg(ptr, val, new);
    		if (old == val)
    			break;
    		val = old;
    	}
    
    into:
    
    	do {
    	} while (!try_cmpxchg(ptr, &val, val $op $imm));
    
    while also generating better code (GCC6 and onwards).
    Signed-off-by: NPeter Zijlstra (Intel) <peterz@infradead.org>
    Cc: Andrew Morton <akpm@linux-foundation.org>
    Cc: Andy Lutomirski <luto@kernel.org>
    Cc: Borislav Petkov <bp@alien8.de>
    Cc: Brian Gerst <brgerst@gmail.com>
    Cc: Denys Vlasenko <dvlasenk@redhat.com>
    Cc: H. Peter Anvin <hpa@zytor.com>
    Cc: Josh Poimboeuf <jpoimboe@redhat.com>
    Cc: Linus Torvalds <torvalds@linux-foundation.org>
    Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
    Cc: Peter Zijlstra <peterz@infradead.org>
    Cc: Thomas Gleixner <tglx@linutronix.de>
    Cc: linux-kernel@vger.kernel.org
    Signed-off-by: NIngo Molnar <mingo@kernel.org>
    a9ebf306
atomic64_64.h 6.2 KB