atomic32.c 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 spinlock_t dummy = SPIN_LOCK_UNLOCKED;
  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. void atomic_set(atomic_t *v, int i)
  32. {
  33. unsigned long flags;
  34. spin_lock_irqsave(ATOMIC_HASH(v), flags);
  35. v->counter = i;
  36. spin_unlock_irqrestore(ATOMIC_HASH(v), flags);
  37. }
  38. EXPORT_SYMBOL(__atomic_add_return);
  39. EXPORT_SYMBOL(atomic_set);