Re: A race-condition in a SUN paper by Detlefs on reference counting...
- From: "Chris Thomasson" <cristom@xxxxxxxxxxx>
- Date: Fri, 4 Apr 2008 11:02:18 -0700
"Greg Herlihy" <greghe@xxxxxxx> wrote in message news:c1ca49f2-5aed-426e-8506-e60da294d9e2@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
On Apr 4, 6:55 am, "Chris Thomasson" <cris...@xxxxxxxxxxx> wrote:
Here is the paper in question:
http://citeseer.ist.psu.edu/cache/papers/cs/25408/http:zSzzSzwww.sun....
on page 8 they present pseudo-code. Here is the function:
___________________________________________________________
void LFRCLoad(SNode **A, SNode **dest) {
1 SNode *a, *olddest = *dest;
2 long r;
3 while (true) {
4 a = *A;
5 if (a == Null) {
6 *dest = Null;
7 break;
}
8 r = a->rc;
9 if (DCAS(A, &a->rc, a, r, a, r+1)) {
10 *dest = a;
11 break;
}
}
12 LFRCDestroy(olddest);}
___________________________________________________________
AFAICT, this will not work at all. The reference count can be dropped to
zero right before 'line 8'. Then it goes BOOM!
There is a note in Section 5 of the paper that explains why "a"'s
reference count will not fall to zero while LFRCLoad() is executing:
"(Note that there is no risk that the object containing the pointer
being read by LFRCLoad is freed during the execution of LFRCLoad
because the calling thread has a pointer to this object that is not
destroyed during the execution of LFRCLoad, so its reference count
cannot fall to zero.)"
In other words, "a" will not free until "A" is freed - and "A" will
not be freed as long as LFRCLoad() is executing - because LFRCLoad()'s
caller will still be holding a reference to "A" (namely, the "A"
argument that was passed to LFRCLoad () when the function was called).
AFAICT, LFRCLoad allows a thread to load a reference from a shared location. In other words:
static SNode* g_shared_location = new SNode;
The reference count of the object in the shared location is 1. Now, lets say a thread wants to grab a reference:
void Thread() {
SNode* local_location = NULL;
LFRCLoad(&g_shared_location, &local_location);
[...];
}
The calling thread does not own a reference to the object in g_shared_location before it calls LFRCLoad. I thought that this was legal because their description of LFRCLoad goes like this:
"LFRCLoad(p,A) A is a pointer to a shared memory location that contains a pointer, and p is a pointer to a local pointer variable. The effect is to load the value from the location pointed to by A into the variable
pointed to by p."
This implies that the algorithm is strongly thread-safe such that a thread can acquire a reference to an object in a shared location that it previously did not own a reference to. Just like in the example I gave. According to the note you cited, well, that means that their algorithm is only provides basic thread-safety such that a thread can only acquire a reference to an object that it already owns a reference to. Is my example breaking the rules? I was always under the impression that this algorithm was strongly thread-safe due to the wording in the description of the LFRCLoad function.
If I am wrong, and this algorithm is only has basic thread-safety, then I know why they put a patent application out on a counting algorithm with strong-thread safety. That algorithm was invented by Joe Seigh:
http://groups.google.com/group/comp.programming.threads/browse_frm/thread/f2c94118046142e8
SUN is trying to patent Joe's work. I was wondering why they would try and patent (20060037026) a new reference count algorithm when that already had one. Its because the one they had was not atomically thread-safe.
Here is some more information on strong-thread safety:
http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2007/n2297.html#atomic
It looks like they are going to add this feature to shared_ptr. Won't that be nice?
:^)
.
- Follow-Ups:
- Re: A race-condition in a SUN paper by Detlefs on reference counting...
- From: Chris Thomasson
- Re: A race-condition in a SUN paper by Detlefs on reference counting...
- References:
- Re: A race-condition in a SUN paper by Detlefs on reference counting...
- From: Greg Herlihy
- Re: A race-condition in a SUN paper by Detlefs on reference counting...
- Prev by Date: Re: A race-condition in a SUN paper by Detlefs on reference counting...
- Next by Date: Re: A race-condition in a SUN paper by Detlefs on reference counting...
- Previous by thread: Re: A race-condition in a SUN paper by Detlefs on reference counting...
- Next by thread: Re: A race-condition in a SUN paper by Detlefs on reference counting...
- Index(es):
Relevant Pages
|