Merge tag 'stable/for-linus-3.6-rc3-tag' of git://git.kernel.org/pub/scm/linux/kernel...
[firefly-linux-kernel-4.4.55.git] / include / linux / kref.h
index abc0120b09b772ff90fe539016295a00dfb08107..65af6887872f1e62d51afa7381ac3b790a302182 100644 (file)
@@ -17,6 +17,8 @@
 
 #include <linux/bug.h>
 #include <linux/atomic.h>
+#include <linux/kernel.h>
+#include <linux/mutex.h>
 
 struct kref {
        atomic_t refcount;
@@ -92,4 +94,21 @@ static inline int kref_put(struct kref *kref, void (*release)(struct kref *kref)
 {
        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_ */