aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJerome Marchand <jmarchan@redhat.com>2011-01-05 16:57:37 +0100
committerGreg Kroah-Hartman <gregkh@suse.de>2011-02-17 15:14:17 -0800
commit07384b42b672c8ea266ea4ba50c5e50c9323ae9f (patch)
tree6dd77da4aa14e5e090c89c75f3625e22c87ee74b
parentc4b03cd47360e5eb751adf2a5f96e7edcfde9821 (diff)
kref: add kref_test_and_get
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>
-rw-r--r--include/linux/kref.h1
-rw-r--r--lib/kref.c12
2 files changed, 13 insertions, 0 deletions
diff --git a/include/linux/kref.h b/include/linux/kref.h
index 6cc38fc07ab..90b9e44abf5 100644
--- a/include/linux/kref.h
+++ b/include/linux/kref.h
@@ -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_ */
diff --git a/lib/kref.c b/lib/kref.c
index d3d227a08a4..e7a6e106712 100644
--- a/lib/kref.c
+++ b/lib/kref.c
@@ -37,6 +37,18 @@ void kref_get(struct kref *kref)
}
/**
+ * 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.
* @release: pointer to the function that will clean up the object when the