kref.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * kref.h - library routines for handling generic reference counted objects
  3. *
  4. * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
  5. * Copyright (C) 2004 IBM Corp.
  6. *
  7. * based on kobject.h which was:
  8. * Copyright (C) 2002-2003 Patrick Mochel <mochel@osdl.org>
  9. * Copyright (C) 2002-2003 Open Source Development Labs
  10. *
  11. * This file is released under the GPLv2.
  12. *
  13. */
  14. #ifndef _KREF_H_
  15. #define _KREF_H_
  16. #include <linux/spinlock.h>
  17. #include <linux/refcount.h>
  18. struct kref {
  19. refcount_t refcount;
  20. };
  21. #define KREF_INIT(n) { .refcount = REFCOUNT_INIT(n), }
  22. /**
  23. * kref_init - initialize object.
  24. * @kref: object in question.
  25. */
  26. static inline void kref_init(struct kref *kref)
  27. {
  28. refcount_set(&kref->refcount, 1);
  29. }
  30. static inline unsigned int kref_read(const struct kref *kref)
  31. {
  32. return refcount_read(&kref->refcount);
  33. }
  34. /**
  35. * kref_get - increment refcount for object.
  36. * @kref: object.
  37. */
  38. static inline void kref_get(struct kref *kref)
  39. {
  40. refcount_inc(&kref->refcount);
  41. }
  42. /**
  43. * kref_put - decrement refcount for object.
  44. * @kref: object.
  45. * @release: pointer to the function that will clean up the object when the
  46. * last reference to the object is released.
  47. * This pointer is required, and it is not acceptable to pass kfree
  48. * in as this function. If the caller does pass kfree to this
  49. * function, you will be publicly mocked mercilessly by the kref
  50. * maintainer, and anyone else who happens to notice it. You have
  51. * been warned.
  52. *
  53. * Decrement the refcount, and if 0, call release().
  54. * Return 1 if the object was removed, otherwise return 0. Beware, if this
  55. * function returns 0, you still can not count on the kref from remaining in
  56. * memory. Only use the return value if you want to see if the kref is now
  57. * gone, not present.
  58. */
  59. static inline int kref_put(struct kref *kref, void (*release)(struct kref *kref))
  60. {
  61. if (refcount_dec_and_test(&kref->refcount)) {
  62. release(kref);
  63. return 1;
  64. }
  65. return 0;
  66. }
  67. static inline int kref_put_mutex(struct kref *kref,
  68. void (*release)(struct kref *kref),
  69. struct mutex *lock)
  70. {
  71. if (refcount_dec_and_mutex_lock(&kref->refcount, lock)) {
  72. release(kref);
  73. return 1;
  74. }
  75. return 0;
  76. }
  77. static inline int kref_put_lock(struct kref *kref,
  78. void (*release)(struct kref *kref),
  79. spinlock_t *lock)
  80. {
  81. if (refcount_dec_and_lock(&kref->refcount, lock)) {
  82. release(kref);
  83. return 1;
  84. }
  85. return 0;
  86. }
  87. /**
  88. * kref_get_unless_zero - Increment refcount for object unless it is zero.
  89. * @kref: object.
  90. *
  91. * Return non-zero if the increment succeeded. Otherwise return 0.
  92. *
  93. * This function is intended to simplify locking around refcounting for
  94. * objects that can be looked up from a lookup structure, and which are
  95. * removed from that lookup structure in the object destructor.
  96. * Operations on such objects require at least a read lock around
  97. * lookup + kref_get, and a write lock around kref_put + remove from lookup
  98. * structure. Furthermore, RCU implementations become extremely tricky.
  99. * With a lookup followed by a kref_get_unless_zero *with return value check*
  100. * locking in the kref_put path can be deferred to the actual removal from
  101. * the lookup structure and RCU lookups become trivial.
  102. */
  103. static inline int __must_check kref_get_unless_zero(struct kref *kref)
  104. {
  105. return refcount_inc_not_zero(&kref->refcount);
  106. }
  107. #endif /* _KREF_H_ */