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
210}210}
211 211
212/**212/**
213 * percpu_ref_tryget - try to increment a percpu refcount213 * percpu_ref_tryget_many - try to increment a percpu refcount
214 * @ref: percpu_ref to try-get214 * @ref: percpu_ref to try-get
215 * @nr: number of references to get
215 *216 *
216 * Increment a percpu refcount unless its count already reached zero.217 * Increment a percpu refcount by @nr unless its count already reached zero.
217 * Returns %true on success; %false on failure.218 * Returns %true on success; %false on failure.
218 *219 *
219 * This function is safe to call as long as @ref is between init and exit.220 * This function is safe to call as long as @ref is between init and exit.
220 */221 */
221static inline bool percpu_ref_tryget(struct percpu_ref *ref)222static inline bool percpu_ref_tryget_many(struct percpu_ref *ref,
223 unsigned long nr)
222{224{
223 unsigned long __percpu *percpu_count;225 unsigned long __percpu *percpu_count;
224 bool ret;226 bool ret;
@@ -226,10 +228,10 @@ static inline bool percpu_ref_tryget(struct percpu_ref *ref) · 1 unmodified line
226 rcu_read_lock();228 rcu_read_lock();
227 229
228 if (__ref_is_percpu(ref, &percpu_count)) {230 if (__ref_is_percpu(ref, &percpu_count)) {
229 this_cpu_inc(*percpu_count);231 this_cpu_add(*percpu_count, nr);
230 ret = true;232 ret = true;
231 } else {233 } else {
232 ret = atomic_long_inc_not_zero(&ref->count);234 ret = atomic_long_add_unless(&ref->count, nr, 0);
233 }235 }
234 236
235 rcu_read_unlock();237 rcu_read_unlock();
@@ -237,6 +239,20 @@ static inline bool percpu_ref_tryget(struct percpu_ref *ref) · 1 unmodified line
237 return ret;239 return ret;
238}240}
239 241
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 */
251static inline bool percpu_ref_tryget(struct percpu_ref *ref)
252{
253 return percpu_ref_tryget_many(ref, 1);
254}
255
240/**256/**
241 * percpu_ref_tryget_live - try to increment a live percpu refcount257 * percpu_ref_tryget_live - try to increment a live percpu refcount
242 * @ref: percpu_ref to try-get258 * @ref: percpu_ref to try-get