2009-05-13 22:56:35 +00:00
|
|
|
/*
|
2011-07-26 23:09:08 +00:00
|
|
|
* Generic C implementation of atomic counter operations. Usable on
|
|
|
|
* UP systems only. Do not include in machine independent code.
|
|
|
|
*
|
2009-05-13 22:56:35 +00:00
|
|
|
* Originally implemented for MN10300.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
|
|
|
|
* Written by David Howells (dhowells@redhat.com)
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public Licence
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
* 2 of the Licence, or (at your option) any later version.
|
|
|
|
*/
|
|
|
|
#ifndef __ASM_GENERIC_ATOMIC_H
|
|
|
|
#define __ASM_GENERIC_ATOMIC_H
|
|
|
|
|
2012-03-28 17:30:03 +00:00
|
|
|
#include <asm/cmpxchg.h>
|
arch: Prepare for smp_mb__{before,after}_atomic()
Since the smp_mb__{before,after}*() ops are fundamentally dependent on
how an arch can implement atomics it doesn't make sense to have 3
variants of them. They must all be the same.
Furthermore, the 3 variants suggest they're only valid for those 3
atomic ops, while we have many more where they could be applied.
So move away from
smp_mb__{before,after}_{atomic,clear}_{dec,inc,bit}() and reduce the
interface to just the two: smp_mb__{before,after}_atomic().
This patch prepares the way by introducing default implementations in
asm-generic/barrier.h that default to a full barrier and providing
__deprecated inlines for the previous 6 barriers if they're not
provided by the arch.
This should allow for a mostly painless transition (lots of deprecated
warns in the interim).
Signed-off-by: Peter Zijlstra <peterz@infradead.org>
Acked-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Link: http://lkml.kernel.org/n/tip-wr59327qdyi9mbzn6x937s4e@git.kernel.org
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: "Chen, Gong" <gong.chen@linux.intel.com>
Cc: John Sullivan <jsrhbz@kanargh.force9.co.uk>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Mauro Carvalho Chehab <m.chehab@samsung.com>
Cc: Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>
Cc: "Theodore Ts'o" <tytso@mit.edu>
Cc: linux-arch@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2014-02-06 17:16:07 +00:00
|
|
|
#include <asm/barrier.h>
|
2012-03-28 17:30:03 +00:00
|
|
|
|
2014-04-23 14:12:30 +00:00
|
|
|
/*
|
|
|
|
* atomic_$op() - $op integer to atomic variable
|
|
|
|
* @i: integer value to $op
|
|
|
|
* @v: pointer to the atomic variable
|
|
|
|
*
|
|
|
|
* Atomically $ops @i to @v. Does not strictly guarantee a memory-barrier, use
|
|
|
|
* smp_mb__{before,after}_atomic().
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* atomic_$op_return() - $op interer to atomic variable and returns the result
|
|
|
|
* @i: integer value to $op
|
|
|
|
* @v: pointer to the atomic variable
|
|
|
|
*
|
|
|
|
* Atomically $ops @i to @v. Does imply a full memory barrier.
|
|
|
|
*/
|
|
|
|
|
2009-05-13 22:56:35 +00:00
|
|
|
#ifdef CONFIG_SMP
|
2014-04-23 14:12:30 +00:00
|
|
|
|
|
|
|
/* we can build all atomic primitives from cmpxchg */
|
|
|
|
|
|
|
|
#define ATOMIC_OP(op, c_op) \
|
|
|
|
static inline void atomic_##op(int i, atomic_t *v) \
|
|
|
|
{ \
|
|
|
|
int c, old; \
|
|
|
|
\
|
|
|
|
c = v->counter; \
|
|
|
|
while ((old = cmpxchg(&v->counter, c, c c_op i)) != c) \
|
|
|
|
c = old; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define ATOMIC_OP_RETURN(op, c_op) \
|
|
|
|
static inline int atomic_##op##_return(int i, atomic_t *v) \
|
|
|
|
{ \
|
|
|
|
int c, old; \
|
|
|
|
\
|
|
|
|
c = v->counter; \
|
|
|
|
while ((old = cmpxchg(&v->counter, c, c c_op i)) != c) \
|
|
|
|
c = old; \
|
|
|
|
\
|
|
|
|
return c c_op i; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#include <linux/irqflags.h>
|
|
|
|
|
|
|
|
#define ATOMIC_OP(op, c_op) \
|
|
|
|
static inline void atomic_##op(int i, atomic_t *v) \
|
|
|
|
{ \
|
|
|
|
unsigned long flags; \
|
|
|
|
\
|
|
|
|
raw_local_irq_save(flags); \
|
|
|
|
v->counter = v->counter c_op i; \
|
|
|
|
raw_local_irq_restore(flags); \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define ATOMIC_OP_RETURN(op, c_op) \
|
|
|
|
static inline int atomic_##op##_return(int i, atomic_t *v) \
|
|
|
|
{ \
|
|
|
|
unsigned long flags; \
|
|
|
|
int ret; \
|
|
|
|
\
|
|
|
|
raw_local_irq_save(flags); \
|
|
|
|
ret = (v->counter = v->counter c_op i); \
|
|
|
|
raw_local_irq_restore(flags); \
|
|
|
|
\
|
|
|
|
return ret; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_SMP */
|
|
|
|
|
|
|
|
#ifndef atomic_add_return
|
|
|
|
ATOMIC_OP_RETURN(add, +)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef atomic_sub_return
|
|
|
|
ATOMIC_OP_RETURN(sub, -)
|
|
|
|
#endif
|
|
|
|
|
2015-07-15 13:47:25 +00:00
|
|
|
#ifndef atomic_and
|
2014-04-23 14:12:30 +00:00
|
|
|
ATOMIC_OP(and, &)
|
2015-07-15 13:47:25 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef atomic_or
|
2014-04-23 14:12:30 +00:00
|
|
|
ATOMIC_OP(or, |)
|
2015-07-15 13:47:25 +00:00
|
|
|
#endif
|
|
|
|
|
2014-04-23 17:32:50 +00:00
|
|
|
#ifndef atomic_xor
|
|
|
|
ATOMIC_OP(xor, ^)
|
2014-04-23 14:12:30 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#undef ATOMIC_OP_RETURN
|
|
|
|
#undef ATOMIC_OP
|
|
|
|
|
2009-05-13 22:56:35 +00:00
|
|
|
/*
|
|
|
|
* Atomic operations that C can't guarantee us. Useful for
|
|
|
|
* resource counting etc..
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define ATOMIC_INIT(i) { (i) }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* atomic_read - read atomic variable
|
|
|
|
* @v: pointer of type atomic_t
|
|
|
|
*
|
2010-05-24 21:33:09 +00:00
|
|
|
* Atomically reads the value of @v.
|
2009-05-13 22:56:35 +00:00
|
|
|
*/
|
2011-07-26 23:09:11 +00:00
|
|
|
#ifndef atomic_read
|
2014-09-23 14:29:50 +00:00
|
|
|
#define atomic_read(v) ACCESS_ONCE((v)->counter)
|
2011-07-26 23:09:11 +00:00
|
|
|
#endif
|
2009-05-13 22:56:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* atomic_set - set atomic variable
|
|
|
|
* @v: pointer of type atomic_t
|
|
|
|
* @i: required value
|
|
|
|
*
|
2010-05-24 21:33:09 +00:00
|
|
|
* Atomically sets the value of @v to @i.
|
2009-05-13 22:56:35 +00:00
|
|
|
*/
|
|
|
|
#define atomic_set(v, i) (((v)->counter) = (i))
|
|
|
|
|
2010-10-07 13:08:55 +00:00
|
|
|
#include <linux/irqflags.h>
|
2009-05-13 22:56:35 +00:00
|
|
|
|
|
|
|
static inline int atomic_add_negative(int i, atomic_t *v)
|
|
|
|
{
|
|
|
|
return atomic_add_return(i, v) < 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void atomic_add(int i, atomic_t *v)
|
|
|
|
{
|
|
|
|
atomic_add_return(i, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void atomic_sub(int i, atomic_t *v)
|
|
|
|
{
|
|
|
|
atomic_sub_return(i, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void atomic_inc(atomic_t *v)
|
|
|
|
{
|
|
|
|
atomic_add_return(1, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void atomic_dec(atomic_t *v)
|
|
|
|
{
|
|
|
|
atomic_sub_return(1, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define atomic_dec_return(v) atomic_sub_return(1, (v))
|
|
|
|
#define atomic_inc_return(v) atomic_add_return(1, (v))
|
|
|
|
|
|
|
|
#define atomic_sub_and_test(i, v) (atomic_sub_return((i), (v)) == 0)
|
2011-07-26 23:09:09 +00:00
|
|
|
#define atomic_dec_and_test(v) (atomic_dec_return(v) == 0)
|
|
|
|
#define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
|
2009-05-13 22:56:35 +00:00
|
|
|
|
2010-06-27 10:26:06 +00:00
|
|
|
#define atomic_xchg(ptr, v) (xchg(&(ptr)->counter, (v)))
|
|
|
|
#define atomic_cmpxchg(v, old, new) (cmpxchg(&((v)->counter), (old), (new)))
|
|
|
|
|
2011-07-26 23:09:07 +00:00
|
|
|
static inline int __atomic_add_unless(atomic_t *v, int a, int u)
|
2010-06-27 10:26:06 +00:00
|
|
|
{
|
2014-04-23 14:12:30 +00:00
|
|
|
int c, old;
|
|
|
|
c = atomic_read(v);
|
|
|
|
while (c != u && (old = atomic_cmpxchg(v, c, c + a)) != c)
|
|
|
|
c = old;
|
|
|
|
return c;
|
2009-05-13 22:56:35 +00:00
|
|
|
}
|
2011-07-26 23:09:10 +00:00
|
|
|
|
2009-05-13 22:56:35 +00:00
|
|
|
#endif /* __ASM_GENERIC_ATOMIC_H */
|