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