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.
Fixing potential race condition
- Colonel Kernel
- Member

- Posts: 1437
- Joined: Tue Oct 17, 2006 6:06 pm
- Location: Vancouver, BC, Canada
- Contact:
Re: Fixing potential race condition
This is what I get for reading the C pseudo-code and ignoring the assembler.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.
Top three reasons why my OS project died:
- Too much overtime at work
- Got married
- My brain got stuck in an infinite loop while trying to design the memory manager
Re: Fixing potential race condition
Thanks to you all.
Now I have something to play for the moment, 'redesigning' included.
Cheers.
Now I have something to play for the moment, 'redesigning' included.
Cheers.
Re: Fixing potential race condition
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);
