atomic32.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. EXPORT_SYMBOL(atomic_cmpxchg);
  44. int atomic_add_unless(atomic_t *v, int a, int u)
  45. {
  46. int ret;
  47. unsigned long flags;
  48. spin_lock_irqsave(ATOMIC_HASH(v), flags);
  49. ret = v->counter;
  50. if (ret != u)
  51. v->counter += a;
  52. spin_unlock_irqrestore(ATOMIC_HASH(v), flags);
  53. return ret != u;
  54. }
  55. EXPORT_SYMBOL(atomic_add_unless);
  56. /* Atomic operations are already serializing */
  57. void atomic_set(atomic_t *v, int i)
  58. {
  59. unsigned long flags;
  60. spin_lock_irqsave(ATOMIC_HASH(v), flags);
  61. v->counter = i;
  62. spin_unlock_irqrestore(ATOMIC_HASH(v), flags);
  63. }
  64. EXPORT_SYMBOL(atomic_set);
  65. unsigned long ___set_bit(unsigned long *addr, unsigned long mask)
  66. {
  67. unsigned long old, flags;
  68. spin_lock_irqsave(ATOMIC_HASH(addr), flags);
  69. old = *addr;
  70. *addr = old | mask;
  71. spin_unlock_irqrestore(ATOMIC_HASH(addr), flags);
  72. return old & mask;
  73. }
  74. EXPORT_SYMBOL(___set_bit);
  75. unsigned long ___clear_bit(unsigned long *addr, unsigned long mask)
  76. {
  77. unsigned long old, flags;
  78. spin_lock_irqsave(ATOMIC_HASH(addr), flags);
  79. old = *addr;
  80. *addr = old & ~mask;
  81. spin_unlock_irqrestore(ATOMIC_HASH(addr), flags);
  82. return old & mask;
  83. }
  84. EXPORT_SYMBOL(___clear_bit);
  85. unsigned long ___change_bit(unsigned long *addr, unsigned long mask)
  86. {
  87. unsigned long old, flags;
  88. spin_lock_irqsave(ATOMIC_HASH(addr), flags);
  89. old = *addr;
  90. *addr = old ^ mask;
  91. spin_unlock_irqrestore(ATOMIC_HASH(addr), flags);
  92. return old & mask;
  93. }
  94. EXPORT_SYMBOL(___change_bit);