469,336 Members | 5,933 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,336 developers. It's quick & easy.

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

Jul 20 '06 #1
3 2011
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/dllpro...dincrement.asp

--
Best regards,
Kim Gräsman
Jul 20 '06 #2
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
Jul 20 '06 #3
"Vinzenz Feenstra" <vi**************@blog.evilissimo.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...
Jul 20 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Elbert Lev | last post: by
8 posts views Thread by Glenn Kasten | last post: by
1 post views Thread by Tony Johansson | last post: by
4 posts views Thread by aaronfude | last post: by
reply views Thread by Chris Thomasson | last post: by
1 post views Thread by oec.deepak | last post: by
10 posts views Thread by Dmitriy V'jukov | last post: by
11 posts views Thread by Jon Harrop | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.