Atomic reference counting

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ole Nielsby

    #1

    Atomic reference counting

    I need to implement reference counting in a class hierarch,
    in a thread safe manner. (The classes are umanaged but I
    might want to compile them with the /clr option.)

    Some of the objects - atoms - are registered in a global hash
    table - let's call it an atom table. When an atom refernce
    count reaches zero, the atom is removed from the table.

    The catch is, after an atom has been counted down, another
    thread might find it in the atom table and create a reference
    to it before it has been removed.


    In an assembler version of the program, I did this (hope the
    following mix of mumbojumbo and pseudocode is clear):

    - The atom table is guarded by a critical section.
    - The objects in the table are chained together unidirectionall y.
    It is possible to have more objects with the same key in the
    chains, though only one of can be valid.
    - Atoms in the table are valid when their reference count is
    not zero.

    For this to work, I needed atomic operations for:

    1. increment and tell if now one
    2. decrement and tell if now zero.

    Since the LOCK INC instruction doesn't tell if it reached one,
    I decided to offset all refcounts by -1, so I can test the z (zero)
    flag after the increment, and the s (sign) flag after decrements
    will tell me if I got below 0.

    This works for me in ASM but I'd rather trash the asm code
    and get to C++.

    Any suggestions how???

    The intrinsics don't seem to cover doing a locked inc/dec
    and testing its flags, or have I ovelooked something - or
    is there a better way to accomplish what I want?



  • Kim Gräsman

    #2
    Re: Atomic reference counting

    Hi Ole,
    1. increment and tell if now one
    2. decrement and tell if now zero.
    Since the LOCK INC instruction doesn't tell if it reached one,
    I decided to offset all refcounts by -1, so I can test the z (zero)
    flag after the increment, and the s (sign) flag after decrements
    will tell me if I got below 0.
    This works for me in ASM but I'd rather trash the asm code and get to
    C++.
    >
    Any suggestions how???
    >
    The intrinsics don't seem to cover doing a locked inc/dec and testing
    its flags, or have I ovelooked something - or is there a better way to
    accomplish what I want?
    I'm not sure if I'm missing something, but aren't you looking for InterlockedIncr ement
    and InterlockedDecr ement?
    Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.


    --
    Best regards,
    Kim Gräsman


    Comment

    • Vinzenz Feenstra

      #3
      Re: Atomic reference counting

      Hi Ole,

      Please take a look at the Interlocked* functions from the WinAPI they
      used for atomic operations.
      Or maybe, since you're using the CLR extensions, you might be
      interessted in the .NET Library class Interlocked

      http://msdn.microsoft.com/library/de...classtopic.asp

      BR
      Vinzenz

      Comment

      • Ole Nielsby

        #4
        Re: Atomic reference counting

        "Vinzenz Feenstra" <vinzenz.feenst ra@blog.eviliss imo.netwrote:
        Hi Ole,
        >
        Please take a look at the Interlocked* functions from the WinAPI they used
        for atomic operations.
        Or maybe, since you're using the CLR extensions, you might be interessted
        in the .NET Library class Interlocked
        >
        http://msdn.microsoft.com/library/de...classtopic.asp
        Thanks - this seems to be what I need. I wasn't aware of these
        routines or the lock xadd instructiont they use. Seems I've been
        clinging to i386 assembler docs for too long...


        Comment

        Working...