atomic32.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * atomic32.c: 32-bit atomic_t implementation
  3. *
  4. * Copyright (C) 2004 Keith M Wesolowski
  5. *
  6. * Based on asm-parisc/atomic.h Copyright (C) 2000 Philipp Rumpf
  7. */
  8. #include <asm/atomic.h>
  9. #include <linux/spinlock.h>
  10. #include <linux/module.h>
  11. #ifdef CONFIG_SMP
  12. #define ATOMIC_HASH_SIZE 4
  13. #define ATOMIC_HASH(a) (&__atomic_hash[(((unsigned long)a)>>8) & (ATOMIC_HASH_SIZE-1)])
  14. spinlock_t __atomic_hash[ATOMIC_HASH_SIZE] = {
  15. [0 ... (ATOMIC_HASH_SIZE-1)] = SPIN_LOCK_UNLOCKED
  16. };
  17. #else /* SMP */
  18. static DEFINE_SPINLOCK(dummy);
  19. #define ATOMIC_HASH_SIZE 1
  20. #define ATOMIC_HASH(a) (&dummy)
  21. #endif /* SMP */
  22. int __atomic_add_return(int i, atomic_t *v)
  23. {
  24. int ret;
  25. unsigned long flags;
  26. spin_lock_irqsave(ATOMIC_HASH(v), flags);
  27. ret = (v->counter += i);
  28. spin_unlock_irqrestore(ATOMIC_HASH(v), flags);
  29. return ret;
  30. }
  31. EXPORT_SYMBOL(__atomic_add_return);
  32. int atomic_cmpxchg(atomic_t *v, int old, int new)
  33. {
  34. int ret;
  35. unsigned long flags;
  36. spin_lock_irqsave(ATOMIC_HASH(v), flags);
  37. ret = v->counter;
  38. if (likely(ret == old))
  39. v->counter = new;
  40. spin_unlock_irqrestore(ATOMIC_HASH(v), flags);
  41. return ret;
  42. }
  43. int atomic_add_unless(atomic_t *v, int a, int u)
  44. {
  45. int ret;
  46. unsigned long flags;
  47. spin_lock_irqsave(ATOMIC_HASH(v), flags);
  48. ret = v->counter;
  49. if (ret != u)
  50. v->counter += a;
  51. spin_unlock_irqrestore(ATOMIC_HASH(v), flags);
  52. return ret != u;
  53. }
  54. /* Atomic operations are already serializing */
  55. void atomic_set(atomic_t *v, int i)
  56. {
  57. unsigned long flags;
  58. spin_lock_irqsave(ATOMIC_HASH(v), flags);
  59. v->counter = i;
  60. spin_unlock_irqrestore(ATOMIC_HASH(v), flags);
  61. }
  62. EXPORT_SYMBOL(atomic_set);