Index: sys/sys/refcount.h =================================================================== --- sys/sys/refcount.h +++ sys/sys/refcount.h @@ -50,7 +50,7 @@ { KASSERT(*count < UINT_MAX, ("refcount %p overflowed", count)); - atomic_add_acq_int(count, 1); + atomic_add_int(count, 1); } static __inline int @@ -58,10 +58,22 @@ { u_int old; - /* XXX: Should this have a rel membar? */ old = atomic_fetchadd_int(count, -1); KASSERT(old > 0, ("negative refcount %p", count)); - return (old == 1); + if (old > 1) + return (0); + + /* + * Last reference. Signal the user to call the desctructor. + * + * Ensure that the destructing code sees all updates. The + * constructor and updating consumers must have executed the + * release operations, which are synchronized with this fence. + * XXXKIB. The recommendation there is to use + * fetchadd_acq_rel(), but we do not have this atomic variant. + */ + atomic_thread_fence_acq(); + return (1); } #endif /* ! __SYS_REFCOUNT_H__ */