From owner-svn-src-head@freebsd.org Fri Jun 30 16:16:22 2017 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 7920FD95263; Fri, 30 Jun 2017 16:16:22 +0000 (UTC) (envelope-from kib@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 4712C73E6E; Fri, 30 Jun 2017 16:16:22 +0000 (UTC) (envelope-from kib@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id v5UGGLgd096354; Fri, 30 Jun 2017 16:16:21 GMT (envelope-from kib@FreeBSD.org) Received: (from kib@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id v5UGGLr4096353; Fri, 30 Jun 2017 16:16:21 GMT (envelope-from kib@FreeBSD.org) Message-Id: <201706301616.v5UGGLr4096353@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: kib set sender to kib@FreeBSD.org using -f From: Konstantin Belousov Date: Fri, 30 Jun 2017 16:16:21 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r320501 - head/sys/sys X-SVN-Group: head X-SVN-Commit-Author: kib X-SVN-Commit-Paths: head/sys/sys X-SVN-Commit-Revision: 320501 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 30 Jun 2017 16:16:22 -0000 Author: kib Date: Fri Jun 30 16:16:21 2017 New Revision: 320501 URL: https://svnweb.freebsd.org/changeset/base/320501 Log: Correct fences for sys/refcount.h. The acq barrier in refcount_acquire() has no use, constructor must ensure that the changes are visible before publication by other means. Last release must sync/with the constructor and all updaters. This is based on the refcount/shared_ptr analysis I heard at the Hans Boehm and Herb Sutter talks about C++ atomics. Reviewed by: alc, jhb, markj Sponsored by: The FreeBSD Foundation MFC after: 2 weeks Differential revision: https://reviews.freebsd.org/D11270 Modified: head/sys/sys/refcount.h Modified: head/sys/sys/refcount.h ============================================================================== --- head/sys/sys/refcount.h Fri Jun 30 16:12:57 2017 (r320500) +++ head/sys/sys/refcount.h Fri Jun 30 16:16:21 2017 (r320501) @@ -50,7 +50,7 @@ refcount_acquire(volatile u_int *count) { 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,20 @@ refcount_release(volatile u_int *count) { u_int old; - /* XXX: Should this have a rel membar? */ + atomic_thread_fence_rel(); 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 destructor. + * + * Ensure that the destructor sees all updates. The fence_rel + * at the start of the function synchronized with this fence. + */ + atomic_thread_fence_acq(); + return (1); } #endif /* ! __SYS_REFCOUNT_H__ */