2 * atomic32.c: 32-bit atomic_t implementation
4 * Copyright (C) 2004 Keith M Wesolowski
5 * Copyright (C) 2007 Kyle McMartin
7 * Based on asm-parisc/atomic.h Copyright (C) 2000 Philipp Rumpf
10 #include <asm/atomic.h>
11 #include <linux/spinlock.h>
12 #include <linux/module.h>
15 #define ATOMIC_HASH_SIZE 4
16 #define ATOMIC_HASH(a) (&__atomic_hash[(((unsigned long)a)>>8) & (ATOMIC_HASH_SIZE-1)])
18 spinlock_t __atomic_hash[ATOMIC_HASH_SIZE] = {
19 [0 ... (ATOMIC_HASH_SIZE-1)] = SPIN_LOCK_UNLOCKED
24 static DEFINE_SPINLOCK(dummy);
25 #define ATOMIC_HASH_SIZE 1
26 #define ATOMIC_HASH(a) (&dummy)
30 int __atomic_add_return(int i, atomic_t *v)
34 spin_lock_irqsave(ATOMIC_HASH(v), flags);
36 ret = (v->counter += i);
38 spin_unlock_irqrestore(ATOMIC_HASH(v), flags);
41 EXPORT_SYMBOL(__atomic_add_return);
43 int atomic_cmpxchg(atomic_t *v, int old, int new)
48 spin_lock_irqsave(ATOMIC_HASH(v), flags);
50 if (likely(ret == old))
53 spin_unlock_irqrestore(ATOMIC_HASH(v), flags);
56 EXPORT_SYMBOL(atomic_cmpxchg);
58 int atomic_add_unless(atomic_t *v, int a, int u)
63 spin_lock_irqsave(ATOMIC_HASH(v), flags);
67 spin_unlock_irqrestore(ATOMIC_HASH(v), flags);
70 EXPORT_SYMBOL(atomic_add_unless);
72 /* Atomic operations are already serializing */
73 void atomic_set(atomic_t *v, int i)
77 spin_lock_irqsave(ATOMIC_HASH(v), flags);
79 spin_unlock_irqrestore(ATOMIC_HASH(v), flags);
81 EXPORT_SYMBOL(atomic_set);
83 unsigned long ___set_bit(unsigned long *addr, unsigned long mask)
85 unsigned long old, flags;
87 spin_lock_irqsave(ATOMIC_HASH(addr), flags);
90 spin_unlock_irqrestore(ATOMIC_HASH(addr), flags);
94 EXPORT_SYMBOL(___set_bit);
96 unsigned long ___clear_bit(unsigned long *addr, unsigned long mask)
98 unsigned long old, flags;
100 spin_lock_irqsave(ATOMIC_HASH(addr), flags);
103 spin_unlock_irqrestore(ATOMIC_HASH(addr), flags);
107 EXPORT_SYMBOL(___clear_bit);
109 unsigned long ___change_bit(unsigned long *addr, unsigned long mask)
111 unsigned long old, flags;
113 spin_lock_irqsave(ATOMIC_HASH(addr), flags);
116 spin_unlock_irqrestore(ATOMIC_HASH(addr), flags);
120 EXPORT_SYMBOL(___change_bit);
122 unsigned long __cmpxchg_u32(volatile u32 *ptr, u32 old, u32 new)
127 spin_lock_irqsave(ATOMIC_HASH(ptr), flags);
128 if ((prev = *ptr) == old)
130 spin_unlock_irqrestore(ATOMIC_HASH(ptr), flags);
132 return (unsigned long)prev;
134 EXPORT_SYMBOL(__cmpxchg_u32);