From owner-cvs-src@FreeBSD.ORG Wed Jun 9 18:47:32 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 65EB116A4CE; Wed, 9 Jun 2004 18:47:32 +0000 (GMT) Received: from darkness.comp.waw.pl (darkness.comp.waw.pl [195.117.238.236]) by mx1.FreeBSD.org (Postfix) with ESMTP id 1063143D1F; Wed, 9 Jun 2004 18:47:32 +0000 (GMT) (envelope-from pjd@darkness.comp.waw.pl) Received: by darkness.comp.waw.pl (Postfix, from userid 1009) id E75E4ACBF9; Wed, 9 Jun 2004 20:20:41 +0200 (CEST) Date: Wed, 9 Jun 2004 20:20:41 +0200 From: Pawel Jakub Dawidek To: Bosko Milekic Message-ID: <20040609182041.GV12007@darkness.comp.waw.pl> References: <20040609175357.GA32787@freefall.freebsd.org> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="EDT6MSV0B3GxyNyZ" Content-Disposition: inline In-Reply-To: <20040609175357.GA32787@freefall.freebsd.org> User-Agent: Mutt/1.4.2i X-PGP-Key-URL: http://people.freebsd.org/~pjd/pjd.asc X-OS: FreeBSD 5.2.1-RC2 i386 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 18:47:32 -0000 --EDT6MSV0B3GxyNyZ Content-Type: text/plain; charset=iso-8859-2 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, Jun 09, 2004 at 05:53:57PM +0000, Bosko Milekic wrote: +>=20 +> Pawel Jakub Dawidek wrote: +> >But isn't you reference counting mechanism limited to only 0 and 1 +> >values? +>=20 +> Nope. +>=20 +> 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. +>=20 +> 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). +>=20 +> 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. +>=20 +> Note that in the cmpset, if the refcnt is NOT zero, all that has happe= ned +> is that it was decremented by 1 and the object not freed. +>=20 +> Again, the code is correct. Ok, I get it now. Even if there will be a race between two threads, i.e.: [ref_cnt is 2] thread1 thread2 atomic_substract(obj, 1) [ref_cnt is 1] atomic_substract(obj, 1) [ref_cnt is 0] atomic_cmpset(obj, 0, 1) -> true [do clean up] atomic_cmpset(obj, 0, 1) -> false [skip cleanups] (thread2 sets ref_cnt to 0, but thread1 destroys objects) we free object only once. Cool, I like it:) BTW. Do we assume that atomic operations are atomic on MP machines? I read atomic(9), but I still don't know if I should use it or not for things like those. --=20 Pawel Jakub Dawidek http://www.FreeBSD.org pjd@FreeBSD.org http://garage.freebsd.pl FreeBSD committer Am I Evil? Yes, I Am! --EDT6MSV0B3GxyNyZ Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.4 (FreeBSD) iD8DBQFAx1T5ForvXbEpPzQRAmHiAKCg1a3t7wW9MW3tkqgXDE4w7oIsNQCgkGdL hMQNxECXIHT3uMfK30IlT/M= =nxVM -----END PGP SIGNATURE----- --EDT6MSV0B3GxyNyZ--