Index: sys/sys/refcount.h =================================================================== --- sys/sys/refcount.h +++ sys/sys/refcount.h @@ -55,6 +55,27 @@ atomic_add_int(count, 1); } +/* + * This functions returns true if the refcount was incremented. Else + * false is returned. + */ +static __inline __result_use_check bool +refcount_acquire_unless_zero(volatile u_int *count) +{ + u_int c = *count; + + KASSERT(c < UINT_MAX, ("refcount %p overflowed", count)); + + /* add one unless zero */ + for (;;) { + if (c == 0) + break; + if (atomic_fcmpset_int(count, &c, c + 1)) + break; + } + return (c != 0); +} + static __inline int refcount_release(volatile u_int *count) {