cvs commit: src/sys/kern kern_proc.c
Pawel Jakub Dawidek
pjd at FreeBSD.org
Wed Jun 9 18:47:32 GMT 2004
On Wed, Jun 09, 2004 at 05:53:57PM +0000, Bosko Milekic wrote:
+> Pawel Jakub Dawidek wrote:
+> >But isn't you reference counting mechanism limited to only 0 and 1
+> 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.
Ok, I get it now. Even if there will be a race between two threads, i.e.:
[ref_cnt is 2]
[ref_cnt is 1]
[ref_cnt is 0]
atomic_cmpset(obj, 0, 1) -> true
[do clean up]
atomic_cmpset(obj, 0, 1) -> false
(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.
Pawel Jakub Dawidek http://www.FreeBSD.org
pjd at FreeBSD.org http://garage.freebsd.pl
FreeBSD committer Am I Evil? Yes, I Am!
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Size: 187 bytes
Desc: not available
Url : http://lists.freebsd.org/pipermail/cvs-src/attachments/20040609/e3834f43/attachment.bin
More information about the cvs-src