dec_and_lock.c 963 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #include <linux/module.h>
  2. #include <linux/spinlock.h>
  3. #include <asm/atomic.h>
  4. /*
  5. * This is an architecture-neutral, but slow,
  6. * implementation of the notion of "decrement
  7. * a reference count, and return locked if it
  8. * decremented to zero".
  9. *
  10. * NOTE NOTE NOTE! This is _not_ equivalent to
  11. *
  12. * if (atomic_dec_and_test(&atomic)) {
  13. * spin_lock(&lock);
  14. * return 1;
  15. * }
  16. * return 0;
  17. *
  18. * because the spin-lock and the decrement must be
  19. * "atomic".
  20. *
  21. * This slow version gets the spinlock unconditionally,
  22. * and releases it if it isn't needed. Architectures
  23. * are encouraged to come up with better approaches,
  24. * this is trivially done efficiently using a load-locked
  25. * store-conditional approach, for example.
  26. */
  27. #ifndef ATOMIC_DEC_AND_LOCK
  28. int _atomic_dec_and_lock(atomic_t *atomic, spinlock_t *lock)
  29. {
  30. spin_lock(lock);
  31. if (atomic_dec_and_test(atomic))
  32. return 1;
  33. spin_unlock(lock);
  34. return 0;
  35. }
  36. EXPORT_SYMBOL(_atomic_dec_and_lock);
  37. #endif