cvs commit: src/sys/kern kern_proc.c

Bosko Milekic bmilekic at FreeBSD.org
Wed Jun 9 17:54:02 GMT 2004


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



More information about the cvs-src mailing list