Commit 8ad5db8a authored by Al Viro's avatar Al Viro

introduce kref_put_mutex()

equivalent of
	mutex_lock(mutex);
	if (!kref_put(kref, release))
		mutex_unlock(mutex);
Signed-off-by: default avatarAl Viro <viro@zeniv.linux.org.uk>
parent 934ad4c2
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#include <linux/bug.h> #include <linux/bug.h>
#include <linux/atomic.h> #include <linux/atomic.h>
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/mutex.h>
struct kref { struct kref {
atomic_t refcount; atomic_t refcount;
...@@ -93,4 +94,21 @@ static inline int kref_put(struct kref *kref, void (*release)(struct kref *kref) ...@@ -93,4 +94,21 @@ static inline int kref_put(struct kref *kref, void (*release)(struct kref *kref)
{ {
return kref_sub(kref, 1, release); return kref_sub(kref, 1, release);
} }
static inline int kref_put_mutex(struct kref *kref,
void (*release)(struct kref *kref),
struct mutex *lock)
{
WARN_ON(release == NULL);
if (unlikely(!atomic_add_unless(&kref->refcount, -1, 1))) {
mutex_lock(lock);
if (unlikely(!atomic_dec_and_test(&kref->refcount))) {
mutex_unlock(lock);
return 0;
}
release(kref);
return 1;
}
return 0;
}
#endif /* _KREF_H_ */ #endif /* _KREF_H_ */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment