dec_and_lock.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. int _atomic_dec_and_lock(atomic_t *atomic, spinlock_t *lock)
  23. {
  24. int counter;
  25. int newcount;
  26. for (;;) {
  27. counter = atomic_read(atomic);
  28. newcount = counter - 1;
  29. if (!newcount)
  30. break; /* do it the slow way */
  31. newcount = cmpxchg(&atomic->counter, counter, newcount);
  32. if (newcount == counter)
  33. return 0;
  34. }
  35. spin_lock(lock);
  36. if (atomic_dec_and_test(atomic))
  37. return 1;
  38. spin_unlock(lock);
  39. return 0;
  40. }
  41. EXPORT_SYMBOL(_atomic_dec_and_lock);