Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1587859

[RFC][PATCH 09/10] locking/refcount: Provide refcount_dec_unless()

From Peter Zijlstra <peterz@infradead.org>
Newsgroups linux.kernel
Subject [RFC][PATCH 09/10] locking/refcount: Provide refcount_dec_unless()
Date 2017-02-24 19:50 +0100
Message-ID <tet6h-4bt-7@gated-at.bofh.it> (permalink)
References <tere9-2Ov-9@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


By allowing a different unless value than 1, we can do dec_and_lock
like things on higher values, like 2, which is useful if the data
structure we lock also owns a reference (because in that case we'd
never hit 1).

Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
---
 include/linux/refcount.h |    8 +++++++-
 lib/refcount.c           |   14 ++++++++------
 2 files changed, 15 insertions(+), 7 deletions(-)

--- a/include/linux/refcount.h
+++ b/include/linux/refcount.h
@@ -35,7 +35,13 @@ extern __must_check bool refcount_dec_an
 extern void refcount_dec(refcount_t *r);
 
 extern __must_check bool refcount_dec_if_one(refcount_t *r);
-extern __must_check bool refcount_dec_not_one(refcount_t *r);
+extern __must_check bool refcount_dec_unless(refcount_t *r, unsigned int unless);
+
+static inline __must_check bool refcount_dec_not_one(refcount_t *r)
+{
+	return refcount_dec_unless(r, 1);
+}
+
 extern __must_check bool refcount_dec_and_mutex_lock(refcount_t *r, struct mutex *lock);
 extern __must_check bool refcount_dec_and_lock(refcount_t *r, spinlock_t *lock);
 
--- a/lib/refcount.c
+++ b/lib/refcount.c
@@ -174,12 +174,14 @@ bool refcount_dec_if_one(refcount_t *r)
 EXPORT_SYMBOL_GPL(refcount_dec_if_one);
 
 /*
- * No atomic_t counterpart, it decrements unless the value is 1, in which case
- * it will return false.
+ * No atomic_t counterpart, it decrements unless the value is as specified, in
+ * which case it will return false.
  *
- * Was often done like: atomic_add_unless(&var, -1, 1)
+ * Was often done like: atomic_add_unless(&var, -1, unless), where the most
+ * common variant has unless==1 is provided as a convenience wrapper, see
+ * refcount_dec_not_one().
  */
-bool refcount_dec_not_one(refcount_t *r)
+bool refcount_dec_unless(refcount_t *r, unsigned int unless)
 {
 	unsigned int new, val = atomic_read(&r->refs);
 
@@ -187,7 +189,7 @@ bool refcount_dec_not_one(refcount_t *r)
 		if (unlikely(val == UINT_MAX))
 			return true;
 
-		if (val == 1)
+		if (val == unless)
 			return false;
 
 		new = val - 1;
@@ -200,7 +202,7 @@ bool refcount_dec_not_one(refcount_t *r)
 
 	return true;
 }
-EXPORT_SYMBOL_GPL(refcount_dec_not_one);
+EXPORT_SYMBOL_GPL(refcount_dec_unless);
 
 /*
  * Similar to atomic_dec_and_mutex_lock(), it will WARN on underflow and fail

Back to linux.kernel | Previous | NextNext in thread | Find similar | Unroll thread


Thread

[RFC][PATCH 09/10] locking/refcount: Provide refcount_dec_unless() Peter Zijlstra <peterz@infradead.org> - 2017-02-24 19:50 +0100
  RE: [RFC][PATCH 09/10] locking/refcount: Provide  refcount_dec_unless() "Reshetova, Elena" <elena.reshetova@intel.com> - 2017-02-27 10:40 +0100

csiph-web