Page 2 of 2

Re: Fixing potential race condition

Posted: Wed Nov 05, 2008 11:21 am
by Combuster
the lock dec; jnz implementation fixes the condition under all circumstances (since the flags get set according to the written result) and works on 386s

for atomic decrement in your style, you'd need to adapt fetch-and-add (xadd), but it requires a 486 and costs more time.

Re: Fixing potential race condition

Posted: Thu Nov 06, 2008 12:14 am
by Colonel Kernel
Combuster wrote:the lock dec; jnz implementation fixes the condition under all circumstances (since the flags get set according to the written result) and works on 386s

for atomic decrement in your style, you'd need to adapt fetch-and-add (xadd), but it requires a 486 and costs more time.
This is what I get for reading the C pseudo-code and ignoring the assembler. :roll:

Re: Fixing potential race condition

Posted: Thu Nov 06, 2008 6:20 am
by cyr1x
Thanks to you all. :D
Now I have something to play for the moment, 'redesigning' included.

Cheers.

Re: Fixing potential race condition

Posted: Fri Nov 07, 2008 1:36 am
by thre3dee
As mentioned before, the owner should pass a new reference to the customer (and have a lock in the retain/release functions) before releasing its instance. This would satisfy all conditions as the customer would be guaranteed its own reference by the time the owner reaches the release(). Any other threads retaining and releasing references would have no impact as they have their own reference to act on via the above method.

Code: Select all

void *retain_object(void *obj)
{
	struct object_base *o = (struct object_base *)obj;
	lock o->refs;
	o->refs++;
	unlock;
	return obj;
}

struct my_object *o = my_object_new();
// regardless of thread
other = retain_object(o);
release_object(o);