]> git.karo-electronics.de Git - karo-tx-linux.git/commitdiff
kref: add kref_test_and_get
authorJerome Marchand <jmarchan@redhat.com>
Wed, 5 Jan 2011 15:57:37 +0000 (16:57 +0100)
committerGreg Kroah-Hartman <gregkh@suse.de>
Thu, 17 Feb 2011 23:14:17 +0000 (15:14 -0800)
commit e4a683c899cd5a49f8d684a054c95bd115a0c005 upstream.

Add kref_test_and_get() function, which atomically add a reference only if
refcount is not zero. This prevent to add a reference to an object that is
already being removed.

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
include/linux/kref.h
lib/kref.c

index 6cc38fc07ab76c5a052d530eec2fd31ec4d25392..90b9e44abf54aa5c346524e4306b6695b9fca980 100644 (file)
@@ -23,6 +23,7 @@ struct kref {
 
 void kref_init(struct kref *kref);
 void kref_get(struct kref *kref);
+int kref_test_and_get(struct kref *kref);
 int kref_put(struct kref *kref, void (*release) (struct kref *kref));
 
 #endif /* _KREF_H_ */
index d3d227a08a4bdb79cae0b2f21be905451c607719..e7a6e106712231ba43b0aa426bcfd003ce8409a7 100644 (file)
@@ -36,6 +36,18 @@ void kref_get(struct kref *kref)
        smp_mb__after_atomic_inc();
 }
 
+/**
+ * kref_test_and_get - increment refcount for object only if refcount is not
+ * zero.
+ * @kref: object.
+ *
+ * Return non-zero if the refcount was incremented, 0 otherwise
+ */
+int kref_test_and_get(struct kref *kref)
+{
+       return atomic_inc_not_zero(&kref->refcount);
+}
+
 /**
  * kref_put - decrement refcount for object.
  * @kref: object.