Re: atomic reference counting primatives.

From: John Baldwin (jhb_at_FreeBSD.org)
Date: 05/21/04

  • Next message: John Baldwin: "Re: atomic reference counting primatives."
    To: freebsd-arch@FreeBSD.org
    Date: Fri, 21 May 2004 09:59:24 -0400
    
    

    On Thursday 20 May 2004 10:54 pm, M. Warner Losh wrote:
    > In message:
    > <Pine.BSF.4.21.0405201340590.72391-100000@InterJet.elischer.org>
    >
    > Julian Elischer <julian@elischer.org> writes:
    > : This has been raised before but I've come across uses for it again and
    > : again so I'm raising it again.
    > : JHB once posted some atomic referenc counting primatives. (Do you still
    > : have them John?)
    > : Alfred once said he had soem somewhere too, and other s have commentted
    > : on this before, but we still don't seem to have any.
    > :
    > : every object is reference counted with its own code and
    > : sometimes it's done poorly.
    > :
    > : Some peiople indicated that there are cases where a generic refcounter
    > : can not be used and usd this as a reason to not have one at all.
    > :
    > : So, here are some possibilities..
    > : my first "write it down without too much thinking" effort..
    > :
    > : typedef {mumble} refcnt_t
    > :
    > : refcnt_add(refcnt_t *)
    > : Increments the reference count.. no magic except to be atomic.
    > :
    > :
    > : int refcnt_drop(refcnt *, struct mutex *)
    > : Decrements the refcount. If it goes to 0 it returns 0 and locks the
    > : mutex (if the mutex is supplied)..
    >
    > What prevents refcnt_add() from happening after ref count drops to 0?
    > Wouldn't that be a race? Eg, if we have two threads:
    >
    >
    > Thread A Thread B
    >
    > objp = lookup();
    > [1] refcnt_drop(&objp->ref, &objp->mtx);
    > [2] refcnt_add(&obj->ref);
    > BANG!
    >
    > If [1] happens before [2], then bad things happen at BANG! If [2]
    > happens before [1], then the mutex won't be locked at BANG and things
    > is good. Thread A believes it has a valid reference to objp after the
    > refcnt_add and no way of knowing otherwise.
    >
    > Is there a safe way to use the API into what you are proposing?

    This situation can't happen if you are properly using reference counting. For
    the reference count to be at 1 in thread B, it has to have the only reference
    meaning that the object has already been removed from any lists, etc.

    -- 
    John Baldwin <jhb@FreeBSD.org>  <><  http://www.FreeBSD.org/~jhb/
    "Power Users Use the Power to Serve"  =  http://www.FreeBSD.org
    _______________________________________________
    freebsd-arch@freebsd.org mailing list
    http://lists.freebsd.org/mailman/listinfo/freebsd-arch
    To unsubscribe, send any mail to "freebsd-arch-unsubscribe@freebsd.org"
    

  • Next message: John Baldwin: "Re: atomic reference counting primatives."

    Relevant Pages

    • Re: atomic reference counting primatives.
      ... On Thursday 20 May 2004 10:54 pm, M. Warner Losh wrote: ... can not be used and usd this as a reason to not have one at all. ... > What prevents refcnt_add() from happening after ref count drops to 0? ... Thread A believes it has a valid reference to objp after the ...
      (freebsd-arch)
    • Re: is it possible to use out and ref keyword together???
      ... reassign a reference that was passed in, and you can't (for some reason) ... return the new reference in the return value. ... useful that "ref", largely due to adopting the TryGetValue and TryParse ...
      (microsoft.public.dotnet.languages.csharp)
    • Re: Passing arguements by reference
      ... but doSomething doesnt change p unless i use "ref"? ... reference, hence both p and q pointing to the same object, not to different ... don't use the ref keyword. ... void Execute() ...
      (microsoft.public.dotnet.languages.csharp)
    • Re: Documentation suggestions
      ... > Ian> I think it would be very useful if there was reference (not just ... Lang ref as only for "language lawyers". ...
      (comp.lang.python)
    • Re: pass by reference
      ... focus has moved from insisting to use "by ref" in java-context ... independent of any other use of the word "reference" in a language. ... Just because Java-refs can be re-targetted by assignment? ... If Java had an operator to tell an Object ...
      (comp.lang.java.programmer)