Atomic reference counting

O

Ole Nielsby

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

=?iso-8859-1?Q?Kim=20Gr=e4sman?=

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 InterlockedIncrement
and InterlockedDecrement?
http://msdn.com/library/en-us/dllproc/base/interlockedincrement.asp
 
O

Ole Nielsby

Vinzenz Feenstra said:
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/d...frlrfsystemthreadinginterlockedclasstopic.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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top