[gclist] Multi threaded reference counting
James McCartney
james@clyde.as.utexas.edu
Sun, 19 Jan 1997 00:18:13 -0700
In the GC-FAQ on the web there is this quote :
>In a multi-threaded system, if threads are preemptively scheduled,
reference counting requires
>additional care to ensure that the updates to reference counts are atomic.
This is straightforward
>using hardware primitives such as fetch-and-add, compare-and-swap, or
>load-linked/store-conditional, but it is definitely necessary to pay
attention to this detail.
>
If it is straightforward I can't figure it out. I beleive that there
is a race condition in simple reference counting that cannot be
got around with the usual atomic primitives. If someone does
have a solution I'd like to see it.
The race condition from my previous post repeated here :
say array X[0] contains the only reference to object Y and thread A
stores NIL into X[0] while thread B pushes X[0] onto its stack.
It is possible for Y's refcount to become zero due to thread A
as thread B tries to increment it.
Say the code executes in the following sequence:
thread B:
/* load contents of X[0] to a register. */
obj = X[0];
thread A :
/* store NIL in object, get previous contents */
prevobj = fetch_and_store(&X[0], NIL);
/* decrement refcount of previous contents */
decrement prevobj->refcount
/* free it */
if (refcount <= 0)
free(prevobj);
thread B :
obj->refcount++; /* crash! object has been freed by thread A. */
--- james mccartney james@clyde.as.utexas.edu james@lcsaudio.com
If you have a PowerMac check out SuperCollider, a real time synth program:
ftp://mirror.apple.com//mirrors/Info-Mac.Archive/gst/snd/super-collider-demo.hqx