lockref.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #ifndef __LINUX_LOCKREF_H
  2. #define __LINUX_LOCKREF_H
  3. /*
  4. * Locked reference counts.
  5. *
  6. * These are different from just plain atomic refcounts in that they
  7. * are atomic with respect to the spinlock that goes with them. In
  8. * particular, there can be implementations that don't actually get
  9. * the spinlock for the common decrement/increment operations, but they
  10. * still have to check that the operation is done semantically as if
  11. * the spinlock had been taken (using a cmpxchg operation that covers
  12. * both the lock and the count word, or using memory transactions, for
  13. * example).
  14. */
  15. #include <linux/spinlock.h>
  16. struct lockref {
  17. spinlock_t lock;
  18. unsigned int count;
  19. };
  20. /**
  21. * lockref_get - Increments reference count unconditionally
  22. * @lockcnt: pointer to lockref structure
  23. *
  24. * This operation is only valid if you already hold a reference
  25. * to the object, so you know the count cannot be zero.
  26. */
  27. static inline void lockref_get(struct lockref *lockref)
  28. {
  29. spin_lock(&lockref->lock);
  30. lockref->count++;
  31. spin_unlock(&lockref->lock);
  32. }
  33. /**
  34. * lockref_get_not_zero - Increments count unless the count is 0
  35. * @lockcnt: pointer to lockref structure
  36. * Return: 1 if count updated successfully or 0 if count is 0
  37. */
  38. static inline int lockref_get_not_zero(struct lockref *lockref)
  39. {
  40. int retval = 0;
  41. spin_lock(&lockref->lock);
  42. if (lockref->count) {
  43. lockref->count++;
  44. retval = 1;
  45. }
  46. spin_unlock(&lockref->lock);
  47. return retval;
  48. }
  49. /**
  50. * lockref_put_or_lock - decrements count unless count <= 1 before decrement
  51. * @lockcnt: pointer to lockref structure
  52. * Return: 1 if count updated successfully or 0 if count <= 1 and lock taken
  53. */
  54. static inline int lockref_put_or_lock(struct lockref *lockref)
  55. {
  56. spin_lock(&lockref->lock);
  57. if (lockref->count <= 1)
  58. return 0;
  59. lockref->count--;
  60. spin_unlock(&lockref->lock);
  61. return 1;
  62. }
  63. #endif /* __LINUX_LOCKREF_H */