2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* atomic32.c: 32-bit atomic_t implementation
|
|
|
|
*
|
|
|
|
* Copyright (C) 2004 Keith M Wesolowski
|
2007-05-29 17:51:13 +08:00
|
|
|
* Copyright (C) 2007 Kyle McMartin
|
2005-04-17 06:20:36 +08:00
|
|
|
*
|
|
|
|
* Based on asm-parisc/atomic.h Copyright (C) 2000 Philipp Rumpf
|
|
|
|
*/
|
|
|
|
|
2011-07-27 07:09:06 +08:00
|
|
|
#include <linux/atomic.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <linux/spinlock.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
|
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
#define ATOMIC_HASH_SIZE 4
|
|
|
|
#define ATOMIC_HASH(a) (&__atomic_hash[(((unsigned long)a)>>8) & (ATOMIC_HASH_SIZE-1)])
|
|
|
|
|
|
|
|
spinlock_t __atomic_hash[ATOMIC_HASH_SIZE] = {
|
2011-01-23 22:19:12 +08:00
|
|
|
[0 ... (ATOMIC_HASH_SIZE-1)] = __SPIN_LOCK_UNLOCKED(__atomic_hash)
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#else /* SMP */
|
|
|
|
|
2005-09-10 04:10:41 +08:00
|
|
|
static DEFINE_SPINLOCK(dummy);
|
2005-04-17 06:20:36 +08:00
|
|
|
#define ATOMIC_HASH_SIZE 1
|
|
|
|
#define ATOMIC_HASH(a) (&dummy)
|
|
|
|
|
|
|
|
#endif /* SMP */
|
|
|
|
|
2016-04-18 07:16:04 +08:00
|
|
|
#define ATOMIC_FETCH_OP(op, c_op) \
|
|
|
|
int atomic_fetch_##op(int i, atomic_t *v) \
|
2014-03-27 01:29:28 +08:00
|
|
|
{ \
|
|
|
|
int ret; \
|
|
|
|
unsigned long flags; \
|
|
|
|
spin_lock_irqsave(ATOMIC_HASH(v), flags); \
|
|
|
|
\
|
2016-04-18 07:16:04 +08:00
|
|
|
ret = v->counter; \
|
|
|
|
v->counter c_op i; \
|
2014-03-27 01:29:28 +08:00
|
|
|
\
|
|
|
|
spin_unlock_irqrestore(ATOMIC_HASH(v), flags); \
|
|
|
|
return ret; \
|
|
|
|
} \
|
2016-04-18 07:16:04 +08:00
|
|
|
EXPORT_SYMBOL(atomic_fetch_##op);
|
2014-03-27 01:29:28 +08:00
|
|
|
|
2016-04-18 07:16:04 +08:00
|
|
|
#define ATOMIC_OP_RETURN(op, c_op) \
|
|
|
|
int atomic_##op##_return(int i, atomic_t *v) \
|
2014-04-24 01:40:25 +08:00
|
|
|
{ \
|
2016-04-18 07:16:04 +08:00
|
|
|
int ret; \
|
2014-04-24 01:40:25 +08:00
|
|
|
unsigned long flags; \
|
|
|
|
spin_lock_irqsave(ATOMIC_HASH(v), flags); \
|
|
|
|
\
|
2016-04-18 07:16:04 +08:00
|
|
|
ret = (v->counter c_op i); \
|
2014-04-24 01:40:25 +08:00
|
|
|
\
|
|
|
|
spin_unlock_irqrestore(ATOMIC_HASH(v), flags); \
|
2016-04-18 07:16:04 +08:00
|
|
|
return ret; \
|
2014-04-24 01:40:25 +08:00
|
|
|
} \
|
2016-04-18 07:16:04 +08:00
|
|
|
EXPORT_SYMBOL(atomic_##op##_return);
|
2014-04-24 01:40:25 +08:00
|
|
|
|
|
|
|
ATOMIC_OP_RETURN(add, +=)
|
2014-03-27 01:29:28 +08:00
|
|
|
|
2016-04-18 07:16:04 +08:00
|
|
|
ATOMIC_FETCH_OP(add, +=)
|
|
|
|
ATOMIC_FETCH_OP(and, &=)
|
|
|
|
ATOMIC_FETCH_OP(or, |=)
|
|
|
|
ATOMIC_FETCH_OP(xor, ^=)
|
|
|
|
|
|
|
|
#undef ATOMIC_FETCH_OP
|
2014-04-24 01:40:25 +08:00
|
|
|
#undef ATOMIC_OP_RETURN
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2014-11-05 22:52:08 +08:00
|
|
|
int atomic_xchg(atomic_t *v, int new)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(ATOMIC_HASH(v), flags);
|
|
|
|
ret = v->counter;
|
|
|
|
v->counter = new;
|
|
|
|
spin_unlock_irqrestore(ATOMIC_HASH(v), flags);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(atomic_xchg);
|
|
|
|
|
2005-11-14 08:07:24 +08:00
|
|
|
int atomic_cmpxchg(atomic_t *v, int old, int new)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2005-11-14 08:07:24 +08:00
|
|
|
int ret;
|
2005-04-17 06:20:36 +08:00
|
|
|
unsigned long flags;
|
|
|
|
|
2005-11-14 08:07:24 +08:00
|
|
|
spin_lock_irqsave(ATOMIC_HASH(v), flags);
|
|
|
|
ret = v->counter;
|
|
|
|
if (likely(ret == old))
|
|
|
|
v->counter = new;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
spin_unlock_irqrestore(ATOMIC_HASH(v), flags);
|
2005-11-14 08:07:24 +08:00
|
|
|
return ret;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2007-03-27 10:10:43 +08:00
|
|
|
EXPORT_SYMBOL(atomic_cmpxchg);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2011-07-28 03:49:44 +08:00
|
|
|
int __atomic_add_unless(atomic_t *v, int a, int u)
|
2005-11-14 08:07:25 +08:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(ATOMIC_HASH(v), flags);
|
|
|
|
ret = v->counter;
|
|
|
|
if (ret != u)
|
|
|
|
v->counter += a;
|
|
|
|
spin_unlock_irqrestore(ATOMIC_HASH(v), flags);
|
2011-08-04 17:47:40 +08:00
|
|
|
return ret;
|
2005-11-14 08:07:25 +08:00
|
|
|
}
|
2011-07-28 03:49:44 +08:00
|
|
|
EXPORT_SYMBOL(__atomic_add_unless);
|
2005-11-14 08:07:25 +08:00
|
|
|
|
|
|
|
/* Atomic operations are already serializing */
|
2005-11-14 08:07:24 +08:00
|
|
|
void atomic_set(atomic_t *v, int i)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2005-11-14 08:07:24 +08:00
|
|
|
spin_lock_irqsave(ATOMIC_HASH(v), flags);
|
|
|
|
v->counter = i;
|
|
|
|
spin_unlock_irqrestore(ATOMIC_HASH(v), flags);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(atomic_set);
|
2006-12-18 08:18:47 +08:00
|
|
|
|
|
|
|
unsigned long ___set_bit(unsigned long *addr, unsigned long mask)
|
|
|
|
{
|
|
|
|
unsigned long old, flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(ATOMIC_HASH(addr), flags);
|
|
|
|
old = *addr;
|
|
|
|
*addr = old | mask;
|
|
|
|
spin_unlock_irqrestore(ATOMIC_HASH(addr), flags);
|
|
|
|
|
|
|
|
return old & mask;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(___set_bit);
|
|
|
|
|
|
|
|
unsigned long ___clear_bit(unsigned long *addr, unsigned long mask)
|
|
|
|
{
|
|
|
|
unsigned long old, flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(ATOMIC_HASH(addr), flags);
|
|
|
|
old = *addr;
|
|
|
|
*addr = old & ~mask;
|
|
|
|
spin_unlock_irqrestore(ATOMIC_HASH(addr), flags);
|
|
|
|
|
|
|
|
return old & mask;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(___clear_bit);
|
|
|
|
|
|
|
|
unsigned long ___change_bit(unsigned long *addr, unsigned long mask)
|
|
|
|
{
|
|
|
|
unsigned long old, flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(ATOMIC_HASH(addr), flags);
|
|
|
|
old = *addr;
|
|
|
|
*addr = old ^ mask;
|
|
|
|
spin_unlock_irqrestore(ATOMIC_HASH(addr), flags);
|
|
|
|
|
|
|
|
return old & mask;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(___change_bit);
|
2007-05-29 17:51:13 +08:00
|
|
|
|
|
|
|
unsigned long __cmpxchg_u32(volatile u32 *ptr, u32 old, u32 new)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
u32 prev;
|
|
|
|
|
2007-05-31 16:19:24 +08:00
|
|
|
spin_lock_irqsave(ATOMIC_HASH(ptr), flags);
|
2007-05-29 17:51:13 +08:00
|
|
|
if ((prev = *ptr) == old)
|
|
|
|
*ptr = new;
|
2007-05-31 16:19:24 +08:00
|
|
|
spin_unlock_irqrestore(ATOMIC_HASH(ptr), flags);
|
2007-05-29 17:51:13 +08:00
|
|
|
|
|
|
|
return (unsigned long)prev;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(__cmpxchg_u32);
|
2014-11-05 22:52:08 +08:00
|
|
|
|
|
|
|
unsigned long __xchg_u32(volatile u32 *ptr, u32 new)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
u32 prev;
|
|
|
|
|
|
|
|
spin_lock_irqsave(ATOMIC_HASH(ptr), flags);
|
|
|
|
prev = *ptr;
|
|
|
|
*ptr = new;
|
|
|
|
spin_unlock_irqrestore(ATOMIC_HASH(ptr), flags);
|
|
|
|
|
|
|
|
return (unsigned long)prev;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(__xchg_u32);
|