Implement "kref_put_mutex()" for the LinuxKPI.

Obtained from:	kmacy @
MFC after:	1 week
Sponsored by:	Mellanox Technologies
This commit is contained in:
Hans Petter Selasky 2016-05-23 12:06:34 +00:00
parent aad02fb444
commit 08a5e6ec7f

View File

@ -36,6 +36,9 @@
#include <sys/refcount.h>
#include <linux/compiler.h>
#include <linux/kernel.h>
#include <linux/mutex.h>
#include <asm/atomic.h>
struct kref {
@ -88,4 +91,20 @@ kref_get_unless_zero(struct kref *kref)
return atomic_add_unless(&kref->refcount, 1, 0);
}
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 /* _LINUX_KREF_H_ */