]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - include/asm-mips/atomic.h
[ARM] 3397/1: AT91RM9200 Header update
[linux-2.6-omap-h63xx.git] / include / asm-mips / atomic.h
index 6202eb8a14b75ac161db078b6566a15aad325ad0..2c8b853376c995892680d5cf3f9b139b57bc5b5b 100644 (file)
 #define _ASM_ATOMIC_H
 
 #include <asm/cpu-features.h>
+#include <asm/interrupt.h>
 #include <asm/war.h>
 
-extern spinlock_t atomic_lock;
-
 typedef struct { volatile int counter; } atomic_t;
 
 #define ATOMIC_INIT(i)    { (i) }
@@ -85,9 +84,9 @@ static __inline__ void atomic_add(int i, atomic_t * v)
        } else {
                unsigned long flags;
 
-               spin_lock_irqsave(&atomic_lock, flags);
+               local_irq_save(flags);
                v->counter += i;
-               spin_unlock_irqrestore(&atomic_lock, flags);
+               local_irq_restore(flags);
        }
 }
 
@@ -127,9 +126,9 @@ static __inline__ void atomic_sub(int i, atomic_t * v)
        } else {
                unsigned long flags;
 
-               spin_lock_irqsave(&atomic_lock, flags);
+               local_irq_save(flags);
                v->counter -= i;
-               spin_unlock_irqrestore(&atomic_lock, flags);
+               local_irq_restore(flags);
        }
 }
 
@@ -173,11 +172,11 @@ static __inline__ int atomic_add_return(int i, atomic_t * v)
        } else {
                unsigned long flags;
 
-               spin_lock_irqsave(&atomic_lock, flags);
+               local_irq_save(flags);
                result = v->counter;
                result += i;
                v->counter = result;
-               spin_unlock_irqrestore(&atomic_lock, flags);
+               local_irq_restore(flags);
        }
 
        return result;
@@ -220,22 +219,23 @@ static __inline__ int atomic_sub_return(int i, atomic_t * v)
        } else {
                unsigned long flags;
 
-               spin_lock_irqsave(&atomic_lock, flags);
+               local_irq_save(flags);
                result = v->counter;
                result -= i;
                v->counter = result;
-               spin_unlock_irqrestore(&atomic_lock, flags);
+               local_irq_restore(flags);
        }
 
        return result;
 }
 
 /*
- * atomic_sub_if_positive - add integer to atomic variable
+ * atomic_sub_if_positive - conditionally subtract integer from atomic variable
+ * @i: integer value to subtract
  * @v: pointer of type atomic_t
  *
- * Atomically test @v and decrement if it is greater than 0.
- * The function returns the old value of @v minus 1.
+ * Atomically test @v and subtract @i if @v is greater or equal than @i.
+ * The function returns the old value of @v minus @i.
  */
 static __inline__ int atomic_sub_if_positive(int i, atomic_t * v)
 {
@@ -250,7 +250,10 @@ static __inline__ int atomic_sub_if_positive(int i, atomic_t * v)
                "       subu    %0, %1, %3                              \n"
                "       bltz    %0, 1f                                  \n"
                "       sc      %0, %2                                  \n"
+               "       .set    noreorder                               \n"
                "       beqzl   %0, 1b                                  \n"
+               "        subu   %0, %1, %3                              \n"
+               "       .set    reorder                                 \n"
                "       sync                                            \n"
                "1:                                                     \n"
                "       .set    mips0                                   \n"
@@ -266,7 +269,10 @@ static __inline__ int atomic_sub_if_positive(int i, atomic_t * v)
                "       subu    %0, %1, %3                              \n"
                "       bltz    %0, 1f                                  \n"
                "       sc      %0, %2                                  \n"
+               "       .set    noreorder                               \n"
                "       beqz    %0, 1b                                  \n"
+               "        subu   %0, %1, %3                              \n"
+               "       .set    reorder                                 \n"
                "       sync                                            \n"
                "1:                                                     \n"
                "       .set    mips0                                   \n"
@@ -276,17 +282,39 @@ static __inline__ int atomic_sub_if_positive(int i, atomic_t * v)
        } else {
                unsigned long flags;
 
-               spin_lock_irqsave(&atomic_lock, flags);
+               local_irq_save(flags);
                result = v->counter;
                result -= i;
                if (result >= 0)
                        v->counter = result;
-               spin_unlock_irqrestore(&atomic_lock, flags);
+               local_irq_restore(flags);
        }
 
        return result;
 }
 
+#define atomic_cmpxchg(v, o, n) ((int)cmpxchg(&((v)->counter), (o), (n)))
+#define atomic_xchg(v, new) (xchg(&((v)->counter), new))
+
+/**
+ * atomic_add_unless - add unless the number is a given value
+ * @v: pointer of type atomic_t
+ * @a: the amount to add to v...
+ * @u: ...unless v is equal to u.
+ *
+ * Atomically adds @a to @v, so long as it was not @u.
+ * Returns non-zero if @v was not @u, and zero otherwise.
+ */
+#define atomic_add_unless(v, a, u)                             \
+({                                                             \
+       int c, old;                                             \
+       c = atomic_read(v);                                     \
+       while (c != (u) && (old = atomic_cmpxchg((v), c, c + (a))) != c) \
+               c = old;                                        \
+       c != (u);                                               \
+})
+#define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
+
 #define atomic_dec_return(v) atomic_sub_return(1,(v))
 #define atomic_inc_return(v) atomic_add_return(1,(v))
 
@@ -410,9 +438,9 @@ static __inline__ void atomic64_add(long i, atomic64_t * v)
        } else {
                unsigned long flags;
 
-               spin_lock_irqsave(&atomic_lock, flags);
+               local_irq_save(flags);
                v->counter += i;
-               spin_unlock_irqrestore(&atomic_lock, flags);
+               local_irq_restore(flags);
        }
 }
 
@@ -452,9 +480,9 @@ static __inline__ void atomic64_sub(long i, atomic64_t * v)
        } else {
                unsigned long flags;
 
-               spin_lock_irqsave(&atomic_lock, flags);
+               local_irq_save(flags);
                v->counter -= i;
-               spin_unlock_irqrestore(&atomic_lock, flags);
+               local_irq_restore(flags);
        }
 }
 
@@ -498,11 +526,11 @@ static __inline__ long atomic64_add_return(long i, atomic64_t * v)
        } else {
                unsigned long flags;
 
-               spin_lock_irqsave(&atomic_lock, flags);
+               local_irq_save(flags);
                result = v->counter;
                result += i;
                v->counter = result;
-               spin_unlock_irqrestore(&atomic_lock, flags);
+               local_irq_restore(flags);
        }
 
        return result;
@@ -545,22 +573,23 @@ static __inline__ long atomic64_sub_return(long i, atomic64_t * v)
        } else {
                unsigned long flags;
 
-               spin_lock_irqsave(&atomic_lock, flags);
+               local_irq_save(flags);
                result = v->counter;
                result -= i;
                v->counter = result;
-               spin_unlock_irqrestore(&atomic_lock, flags);
+               local_irq_restore(flags);
        }
 
        return result;
 }
 
 /*
- * atomic64_sub_if_positive - add integer to atomic variable
+ * atomic64_sub_if_positive - conditionally subtract integer from atomic variable
+ * @i: integer value to subtract
  * @v: pointer of type atomic64_t
  *
- * Atomically test @v and decrement if it is greater than 0.
- * The function returns the old value of @v minus 1.
+ * Atomically test @v and subtract @i if @v is greater or equal than @i.
+ * The function returns the old value of @v minus @i.
  */
 static __inline__ long atomic64_sub_if_positive(long i, atomic64_t * v)
 {
@@ -575,7 +604,10 @@ static __inline__ long atomic64_sub_if_positive(long i, atomic64_t * v)
                "       dsubu   %0, %1, %3                              \n"
                "       bltz    %0, 1f                                  \n"
                "       scd     %0, %2                                  \n"
+               "       .set    noreorder                               \n"
                "       beqzl   %0, 1b                                  \n"
+               "        dsubu  %0, %1, %3                              \n"
+               "       .set    reorder                                 \n"
                "       sync                                            \n"
                "1:                                                     \n"
                "       .set    mips0                                   \n"
@@ -591,7 +623,10 @@ static __inline__ long atomic64_sub_if_positive(long i, atomic64_t * v)
                "       dsubu   %0, %1, %3                              \n"
                "       bltz    %0, 1f                                  \n"
                "       scd     %0, %2                                  \n"
+               "       .set    noreorder                               \n"
                "       beqz    %0, 1b                                  \n"
+               "        dsubu  %0, %1, %3                              \n"
+               "       .set    reorder                                 \n"
                "       sync                                            \n"
                "1:                                                     \n"
                "       .set    mips0                                   \n"
@@ -601,12 +636,12 @@ static __inline__ long atomic64_sub_if_positive(long i, atomic64_t * v)
        } else {
                unsigned long flags;
 
-               spin_lock_irqsave(&atomic_lock, flags);
+               local_irq_save(flags);
                result = v->counter;
                result -= i;
                if (result >= 0)
                        v->counter = result;
-               spin_unlock_irqrestore(&atomic_lock, flags);
+               local_irq_restore(flags);
        }
 
        return result;
@@ -690,4 +725,5 @@ static __inline__ long atomic64_sub_if_positive(long i, atomic64_t * v)
 #define smp_mb__before_atomic_inc()    smp_mb()
 #define smp_mb__after_atomic_inc()     smp_mb()
 
+#include <asm-generic/atomic.h>
 #endif /* _ASM_ATOMIC_H */