dec_and_lock.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * MIPS version of atomic_dec_and_lock() using cmpxchg
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/spinlock.h>
  11. #include <asm/atomic.h>
  12. #include <asm/system.h>
  13. /*
  14. * This is an implementation of the notion of "decrement a
  15. * reference count, and return locked if it decremented to zero".
  16. *
  17. * This implementation can be used on any architecture that
  18. * has a cmpxchg, and where atomic->value is an int holding
  19. * the value of the atomic (i.e. the high bits aren't used
  20. * for a lock or anything like that).
  21. *
  22. * N.B. ATOMIC_DEC_AND_LOCK gets defined in include/linux/spinlock.h
  23. * if spinlocks are empty and thus atomic_dec_and_lock is defined
  24. * to be atomic_dec_and_test - in that case we don't need it
  25. * defined here as well.
  26. */
  27. #ifndef ATOMIC_DEC_AND_LOCK
  28. int _atomic_dec_and_lock(atomic_t *atomic, spinlock_t *lock)
  29. {
  30. int counter;
  31. int newcount;
  32. for (;;) {
  33. counter = atomic_read(atomic);
  34. newcount = counter - 1;
  35. if (!newcount)
  36. break; /* do it the slow way */
  37. newcount = cmpxchg(&atomic->counter, counter, newcount);
  38. if (newcount == counter)
  39. return 0;
  40. }
  41. spin_lock(lock);
  42. if (atomic_dec_and_test(atomic))
  43. return 1;
  44. spin_unlock(lock);
  45. return 0;
  46. }
  47. EXPORT_SYMBOL(_atomic_dec_and_lock);
  48. #endif /* ATOMIC_DEC_AND_LOCK */