Re: atomic reference counting primatives.

From: Julian Elischer (julian_at_elischer.org)
Date: 05/21/04

  • Next message: Crist J. Clark: "Move /usr/sup to /var/db/sup?"
    Date: Fri, 21 May 2004 12:19:37 -0700 (PDT)
    To: John Baldwin <jhb@FreeBSD.org>
    
    

    On Fri, 21 May 2004, John Baldwin wrote:

    > 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.

    Exactly.. B needs to have got his copy of th reference from somewhere,
    and that reference should have been counted somewhere as should B's copy
    of it.
    So, the reference count should be at least 2 before B drops his
    reference.. and possibly 3..

    I would even go on record as saying that I have seen and liked
    a refcount API which was (from memory something like):

    void * refcnt_add(offsetof(struct obj, refcnt), void ** object_p)

    which takes a pointer to the object pointer you are copyuing, and
    atomically increments it and returns the contents of the pointer.
    If the contents of the pointer are NULL, then it retunrs NULL
    and doesn't increment anything..

    The reference decrement atomically reduced the reference count and
    zapped the pointer, and retunred a copy of the pointer if
    the reference count had gone to 0 (or NULL if not).

    So usage was:
    struct xx *globalpointer; /* has its own owner somewhere */

            mypointer = refcnt_add(offsetof(xx, refcnt), globalptr)
            if (mypointer == NULL) {
                    printf("didn't find an object\n"
                    return (-1);

            }
            manipulate(mypointer)
            if ((tmppointer = refcnt_drop(&mypointer, &globalpointer))) {
                    free(tmppointer);
            }

    someone else who owns the globalpointer reference might might in
    the meanwhile do:

    if ((tmppointer = refcnt_drop(globalpointer->refcnt, &globalpointer))) {
            free(tmppointer);
    }

    and you were guaranteed to get a predictable result.

    _______________________________________________
    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: Crist J. Clark: "Move /usr/sup to /var/db/sup?"

    Relevant Pages

    • Re: Question on LSP
      ... Sure, the developer must keep track of which type has been assigned to each pointer to manage complexity in creating the design, which is why the 'T' is in T*. ... Subtyping is a relation which does not ... Those object types are completely orthogonal to the semantics of being an object reference. ... aggregate references, is the aggregate of referenced objects or target ...
      (comp.object)
    • Re: Question on LSP
      ... Because construction paradigms have different goals. ... But that has nothing to do with what a pointer type ... But that does not mean that the semantics of an object reference is ... aggregate references, is the aggregate of referenced objects or target ...
      (comp.object)
    • Re: Question on LSP
      ... equivalent addresses in the same context. ... Note that the language allows us to use a name like 'T' on the reference as a mnemonic so that the developer can keep track of what is happening with the indirection. ... Modern languages have proper pointer types. ...
      (comp.object)
    • Re: Nothing Keyword Destories Objects rather than just resetting the variable to an empty variable
      ... there is no difference between using ByVal or ByRef. ... as the C1 pointer was the sole reference to the class object. ... Yes, an object variable is a pointer to an object, but that is the only ... the keyword "NOTHING" should only destroy the ...
      (microsoft.public.excel.programming)
    • Re: How java passes object references?
      ... to think of them as being a specific location in a larger block of memory ... Whether a language passes by reference or by value, ... is a pointer pointing at the memory block. ... So when allocating local memory for o, it would simply allocate a ...
      (comp.lang.java.programmer)

  • Quantcast