lockref.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include <linux/export.h>
  2. #include <linux/lockref.h>
  3. /**
  4. * lockref_get - Increments reference count unconditionally
  5. * @lockcnt: pointer to lockref structure
  6. *
  7. * This operation is only valid if you already hold a reference
  8. * to the object, so you know the count cannot be zero.
  9. */
  10. void lockref_get(struct lockref *lockref)
  11. {
  12. spin_lock(&lockref->lock);
  13. lockref->count++;
  14. spin_unlock(&lockref->lock);
  15. }
  16. EXPORT_SYMBOL(lockref_get);
  17. /**
  18. * lockref_get_not_zero - Increments count unless the count is 0
  19. * @lockcnt: pointer to lockref structure
  20. * Return: 1 if count updated successfully or 0 if count was zero
  21. */
  22. int lockref_get_not_zero(struct lockref *lockref)
  23. {
  24. int retval = 0;
  25. spin_lock(&lockref->lock);
  26. if (lockref->count) {
  27. lockref->count++;
  28. retval = 1;
  29. }
  30. spin_unlock(&lockref->lock);
  31. return retval;
  32. }
  33. EXPORT_SYMBOL(lockref_get_not_zero);
  34. /**
  35. * lockref_get_or_lock - Increments count unless the count is 0
  36. * @lockcnt: pointer to lockref structure
  37. * Return: 1 if count updated successfully or 0 if count was zero
  38. * and we got the lock instead.
  39. */
  40. int lockref_get_or_lock(struct lockref *lockref)
  41. {
  42. spin_lock(&lockref->lock);
  43. if (!lockref->count)
  44. return 0;
  45. lockref->count++;
  46. spin_unlock(&lockref->lock);
  47. return 1;
  48. }
  49. EXPORT_SYMBOL(lockref_get_or_lock);
  50. /**
  51. * lockref_put_or_lock - decrements count unless count <= 1 before decrement
  52. * @lockcnt: pointer to lockref structure
  53. * Return: 1 if count updated successfully or 0 if count <= 1 and lock taken
  54. */
  55. int lockref_put_or_lock(struct lockref *lockref)
  56. {
  57. spin_lock(&lockref->lock);
  58. if (lockref->count <= 1)
  59. return 0;
  60. lockref->count--;
  61. spin_unlock(&lockref->lock);
  62. return 1;
  63. }
  64. EXPORT_SYMBOL(lockref_put_or_lock);