History

pcpu_ref: add percpu_ref_tryget_many()

Add percpu_ref_tryget_many(), which works the same way as percpu_ref_tryget(), but grabs specified number of refs.

Pavel Begunkov committed 6 years ago

Commit 4e5ef021 parent c1ca7571 file · +21 −5Browse files

Unified · Split

include/linux/percpu-refcount.h+21−5
@@ -210,15 +210,17 @@ static inline void percpu_ref_get(struct percpu_ref *ref) · 209 unmodified lines
210210 }
211211
212212 /**
213− * percpu_ref_tryget - try to increment a percpu refcount
213+ * percpu_ref_tryget_many - try to increment a percpu refcount
214214 * @ref: percpu_ref to try-get
215+ * @nr: number of references to get
215216 *
216− * Increment a percpu refcount unless its count already reached zero.
217+ * Increment a percpu refcount by @nr unless its count already reached zero.
217218 * Returns %true on success; %false on failure.
218219 *
219220 * This function is safe to call as long as @ref is between init and exit.
220221 */
221−static inline bool percpu_ref_tryget(struct percpu_ref *ref)
222+static inline bool percpu_ref_tryget_many(struct percpu_ref *ref,
223+ unsigned long nr)
222224 {
223225 unsigned long __percpu *percpu_count;
224226 bool ret;
@@ -226,10 +228,10 @@ static inline bool percpu_ref_tryget(struct percpu_ref *ref) · 1 unmodified line
226228 rcu_read_lock();
227229
228230 if (__ref_is_percpu(ref, &percpu_count)) {
229− this_cpu_inc(*percpu_count);
231+ this_cpu_add(*percpu_count, nr);
230232 ret = true;
231233 } else {
232− ret = atomic_long_inc_not_zero(&ref->count);
234+ ret = atomic_long_add_unless(&ref->count, nr, 0);
233235 }
234236
235237 rcu_read_unlock();
@@ -237,6 +239,20 @@ static inline bool percpu_ref_tryget(struct percpu_ref *ref) · 1 unmodified line
237239 return ret;
238240 }
239241
242+/**
243+ * percpu_ref_tryget - try to increment a percpu refcount
244+ * @ref: percpu_ref to try-get
245+ *
246+ * Increment a percpu refcount unless its count already reached zero.
247+ * Returns %true on success; %false on failure.
248+ *
249+ * This function is safe to call as long as @ref is between init and exit.
250+ */
251+static inline bool percpu_ref_tryget(struct percpu_ref *ref)
252+{
253+ return percpu_ref_tryget_many(ref, 1);
254+}
255+
240256 /**
241257 * percpu_ref_tryget_live - try to increment a live percpu refcount
242258 * @ref: percpu_ref to try-get