Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 9 Jun 2004 17:53:57 +0000
From:      Bosko Milekic <bmilekic@FreeBSD.org>
To:        Pawel Jakub Dawidek <pjd@FreeBSD.org>
Cc:        "M. Warner Losh" <imp@bsdimp.com>
Subject:   Re: cvs commit: src/sys/kern kern_proc.c
Message-ID:  <20040609175357.GA32787@freefall.freebsd.org>

next in thread | raw e-mail | index | archive | help

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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20040609175357.GA32787>