dec_and_lock.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /*
  2. * Copyright (C) 2003 Jerome Marchand, Bull S.A.
  3. * Cleaned up by David Mosberger-Tang <davidm@hpl.hp.com>
  4. *
  5. * This file is released under the GPLv2, or at your option any later version.
  6. *
  7. * ia64 version of "atomic_dec_and_lock()" using the atomic "cmpxchg" instruction. This
  8. * code is an adaptation of the x86 version of "atomic_dec_and_lock()".
  9. */
  10. #include <linux/compiler.h>
  11. #include <linux/module.h>
  12. #include <linux/spinlock.h>
  13. #include <asm/atomic.h>
  14. /*
  15. * Decrement REFCOUNT and if the count reaches zero, acquire the spinlock. Both of these
  16. * operations have to be done atomically, so that the count doesn't drop to zero without
  17. * acquiring the spinlock first.
  18. */
  19. int
  20. _atomic_dec_and_lock (atomic_t *refcount, spinlock_t *lock)
  21. {
  22. int old, new;
  23. do {
  24. old = atomic_read(refcount);
  25. new = old - 1;
  26. if (unlikely (old == 1)) {
  27. /* oops, we may be decrementing to zero, do it the slow way... */
  28. spin_lock(lock);
  29. if (atomic_dec_and_test(refcount))
  30. return 1;
  31. spin_unlock(lock);
  32. return 0;
  33. }
  34. } while (cmpxchg(&refcount->counter, old, new) != old);
  35. return 0;
  36. }
  37. EXPORT_SYMBOL(_atomic_dec_and_lock);