From owner-cvs-src@FreeBSD.ORG Wed Jun 9 17:54:02 2004 Return-Path: Delivered-To: cvs-src@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 4FE5516A4CE; Wed, 9 Jun 2004 17:54:02 +0000 (GMT) Received: from freefall.freebsd.org (freefall.freebsd.org [216.136.204.21]) by mx1.FreeBSD.org (Postfix) with ESMTP id 4663943D31; Wed, 9 Jun 2004 17:54:02 +0000 (GMT) (envelope-from bmilekic@FreeBSD.org) Received: from freefall.freebsd.org (bmilekic@localhost [127.0.0.1]) i59HrvMn033018; Wed, 9 Jun 2004 17:53:57 GMT (envelope-from bmilekic@freefall.freebsd.org) Received: (from bmilekic@localhost) by freefall.freebsd.org (8.12.11/8.12.11/Submit) id i59HrvLh033017; Wed, 9 Jun 2004 17:53:57 GMT (envelope-from bmilekic) Date: Wed, 9 Jun 2004 17:53:57 +0000 From: Bosko Milekic To: Pawel Jakub Dawidek Message-ID: <20040609175357.GA32787@freefall.freebsd.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.4.1i cc: src-committers@FreeBSD.org cc: cvs-src@FreeBSD.org cc: cvs-all@FreeBSD.org cc: phk@phk.freebsd.dk cc: Nate Lawson cc: "M. Warner Losh" Subject: Re: cvs commit: src/sys/kern kern_proc.c X-BeenThere: cvs-src@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: CVS commit messages for the src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 09 Jun 2004 17:54:02 -0000 Pawel Jakub Dawidek wrote: >But isn't you reference counting mechanism limited to only 0 and 1 >values? Nope. The cmpset does exactly this, atomically: if the refcnt is 0, set it to 1 and return non-zero, otherwise leave it as it and return 0. Therefore, if two threads are freeing at the same time, the refcount will get dropped twice [atomically, so we don't have to worry about a missed decrement], and the threads will race on that atomic cmpset. But since the cmpset is atomic, then only one thread will get to set the refcnt to 1 and free, and the other will see that it is not zero, and so its cmpset will merely return 0 and it will be done (it won't have to be the one cleaning up/freeing the object). The reference count, after hitting zero, cannot go back up because the object is being freed and no other references exist. If they do, then the reference counting model is broken. Note that in the cmpset, if the refcnt is NOT zero, all that has happened is that it was decremented by 1 and the object not freed. Again, the code is correct. -Bosko