Commit 39ea11ce authored by Matthias Clasen's avatar Matthias Clasen Committed by Matthias Clasen
Browse files

Make refcounting threadsafe by using atomic operations. (#166020, Wim

2005-07-15  Matthias Clasen  <mclasen@redhat.com>

	Make refcounting threadsafe by using atomic
	operations.  (#166020, Wim Taymans)

	* gobject.c: Use a recursive lock to protect the
	notify queue.
	(g_object_unref): Get rid of g_object_last_unref and
	do the last unref handling in g_object_unref.
	(g_object_ref, g_object_unref): Use atomic operations.

	* gsignal.c (struct _HandlerMatch): Use a full integer
	for the ref_count field.
	(handler_ref, handler_unref_R): Use atomic operations.

	* gparam.c (g_param_spec_ref, g_param_spec_unref):
	Use atomic operations instead of a lock to make the
	refcounting threadsafe.

	* gclosure.c (g_closure_ref, g_closure_unref): Use atomic
	operations. This is more complicated here, since the
	refcount is stored in a bitfield, so we also have
	to access all other bitfield members atomically.

	* gsignal.c (handlers_find): Read the meta_marshal flag
	of the closure atomically.

	* tests/Makefile.am (SUBDIRS): Add tests/refcount

	* configure.in: Add tests/refcount

	* tests/refcount/properties.c: Test property changes
	from multiple threads.

	* tests/refcount/signals.c: Test signal emission from
	multiple threads.

	* tests/refcount/objects.c: Test refcounting from
	multiple threads.

	* tests/refcount/objects2.c:
	* tests/refcount/properties2.c: Tests to measure the
	overhead of threadsafe refcounting.

	* glib/giochannel.c (g_io_channel_ref, g_io_channel_unref):
	Use atomic operations to make refcounting
	threadsafe.  (#166020, Wim Taymans)
parent 58729b46
2005-07-15 Matthias Clasen <mclasen@redhat.com>
* tests/Makefile.am (SUBDIRS): Add tests/refcount
* configure.in: Add tests/refcount
* tests/refcount/properties.c: Test property changes
from multiple threads.
* tests/refcount/signals.c: Test signal emission from
multiple threads.
* tests/refcount/objects.c: Test refcounting from
multiple threads.
* tests/refcount/objects2.c:
* tests/refcount/properties2.c: Tests to measure the
overhead of threadsafe refcounting.
* glib/giochannel.c (g_io_channel_ref, g_io_channel_unref):
Use atomic operations to make refcounting
threadsafe. (#166020, Wim Taymans)
* NEWS: Updates
2005-07-14 Matthias Clasen <mclasen@redhat.com>
......
2005-07-15 Matthias Clasen <mclasen@redhat.com>
* tests/Makefile.am (SUBDIRS): Add tests/refcount
* configure.in: Add tests/refcount
* tests/refcount/properties.c: Test property changes
from multiple threads.
* tests/refcount/signals.c: Test signal emission from
multiple threads.
* tests/refcount/objects.c: Test refcounting from
multiple threads.
* tests/refcount/objects2.c:
* tests/refcount/properties2.c: Tests to measure the
overhead of threadsafe refcounting.
* glib/giochannel.c (g_io_channel_ref, g_io_channel_unref):
Use atomic operations to make refcounting
threadsafe. (#166020, Wim Taymans)
* NEWS: Updates
2005-07-14 Matthias Clasen <mclasen@redhat.com>
......
2005-07-15 Matthias Clasen <mclasen@redhat.com>
* tests/Makefile.am (SUBDIRS): Add tests/refcount
* configure.in: Add tests/refcount
* tests/refcount/properties.c: Test property changes
from multiple threads.
* tests/refcount/signals.c: Test signal emission from
multiple threads.
* tests/refcount/objects.c: Test refcounting from
multiple threads.
* tests/refcount/objects2.c:
* tests/refcount/properties2.c: Tests to measure the
overhead of threadsafe refcounting.
* glib/giochannel.c (g_io_channel_ref, g_io_channel_unref):
Use atomic operations to make refcounting
threadsafe. (#166020, Wim Taymans)
* NEWS: Updates
2005-07-14 Matthias Clasen <mclasen@redhat.com>
......
2005-07-15 Matthias Clasen <mclasen@redhat.com>
* tests/Makefile.am (SUBDIRS): Add tests/refcount
* configure.in: Add tests/refcount
* tests/refcount/properties.c: Test property changes
from multiple threads.
* tests/refcount/signals.c: Test signal emission from
multiple threads.
* tests/refcount/objects.c: Test refcounting from
multiple threads.
* tests/refcount/objects2.c:
* tests/refcount/properties2.c: Tests to measure the
overhead of threadsafe refcounting.
* glib/giochannel.c (g_io_channel_ref, g_io_channel_unref):
Use atomic operations to make refcounting
threadsafe. (#166020, Wim Taymans)
* NEWS: Updates
2005-07-14 Matthias Clasen <mclasen@redhat.com>
......
......@@ -2723,6 +2723,7 @@ docs/reference/gobject/Makefile
docs/reference/gobject/version.xml
tests/Makefile
tests/gobject/Makefile
tests/refcount/Makefile
m4macros/Makefile
])
......
......@@ -97,7 +97,7 @@ g_io_channel_ref (GIOChannel *channel)
{
g_return_val_if_fail (channel != NULL, NULL);
channel->ref_count++;
g_atomic_int_inc (&channel->ref_count);
return channel;
}
......@@ -105,10 +105,13 @@ g_io_channel_ref (GIOChannel *channel)
void
g_io_channel_unref (GIOChannel *channel)
{
gboolean is_zero;
g_return_if_fail (channel != NULL);
channel->ref_count--;
if (channel->ref_count == 0)
is_zero = g_atomic_int_dec_and_test (&channel->ref_count);
if (G_UNLIKELY (is_zero))
{
if (channel->close_on_unref)
g_io_channel_shutdown (channel, TRUE, NULL);
......
2005-07-15 Matthias Clasen <mclasen@redhat.com>
Make refcounting threadsafe by using atomic
operations. (#166020, Wim Taymans)
* gobject.c: Use a recursive lock to protect the
notify queue.
(g_object_unref): Get rid of g_object_last_unref and
do the last unref handling in g_object_unref.
(g_object_ref, g_object_unref): Use atomic operations.
* gsignal.c (struct _HandlerMatch): Use a full integer
for the ref_count field.
(handler_ref, handler_unref_R): Use atomic operations.
* gparam.c (g_param_spec_ref, g_param_spec_unref):
Use atomic operations instead of a lock to make the
refcounting threadsafe.
* gclosure.c (g_closure_ref, g_closure_unref): Use atomic
operations. This is more complicated here, since the
refcount is stored in a bitfield, so we also have
to access all other bitfield members atomically.
* gsignal.c (handlers_find): Read the meta_marshal flag
of the closure atomically.
2005-07-11 Matthias Clasen <mclasen@redhat.com>
* gobject.h:
......
......@@ -36,6 +36,44 @@
#define CLOSURE_N_NOTIFIERS(cl) (CLOSURE_N_MFUNCS (cl) + \
(cl)->n_fnotifiers + \
(cl)->n_inotifiers)
/* union of first int we need to make atomic */
typedef union {
GClosure bits;
gint atomic;
} GAtomicClosureBits;
#define BITS_AS_INT(b) (((GAtomicClosureBits*)(b))->atomic)
#define CLOSURE_READ_BITS(cl,bits) (BITS_AS_INT(bits) = g_atomic_int_get ((gint*)(cl)))
#define CLOSURE_READ_BITS2(cl,old,new) (BITS_AS_INT(old) = CLOSURE_READ_BITS (cl, new))
#define CLOSURE_SWAP_BITS(cl,old,new) (g_atomic_int_compare_and_exchange ((gint*)(cl), \
BITS_AS_INT(old),BITS_AS_INT(new)))
#define CLOSURE_REF(closure) \
G_STMT_START { \
GClosure old, new; \
do { \
CLOSURE_READ_BITS2 (closure, &old, &new); \
new.ref_count++; \
} \
while (!CLOSURE_SWAP_BITS (closure, &old, &new)); \
} G_STMT_END
#define CLOSURE_UNREF(closure, is_zero) \
G_STMT_START { \
GClosure old, new; \
do { \
CLOSURE_READ_BITS2 (closure, &old, &new); \
if (old.ref_count == 1) /* last unref, invalidate first */ \
g_closure_invalidate ((closure)); \
new.ref_count--; \
is_zero = (new.ref_count == 0); \
} \
while (!CLOSURE_SWAP_BITS (closure, &old, &new)); \
} G_STMT_END
enum {
FNOTIFY,
INOTIFY,
......@@ -76,6 +114,8 @@ static inline void
closure_invoke_notifiers (GClosure *closure,
guint notify_type)
{
GClosure bits, new;
/* notifier layout:
* meta_marshal n_guards n_guards n_fnotif. n_inotifiers
* ->[[meta_marshal][pre_guards][post_guards][fnotifiers][inotifiers]]
......@@ -99,11 +139,12 @@ closure_invoke_notifiers (GClosure *closure,
GClosureNotifyData *ndata;
guint i, offs;
case FNOTIFY:
while (closure->n_fnotifiers)
CLOSURE_READ_BITS (closure, &bits);
while (bits.n_fnotifiers)
{
register guint n = --closure->n_fnotifiers;
register guint n = --bits.n_fnotifiers;
ndata = closure->notifiers + CLOSURE_N_MFUNCS (closure) + n;
ndata = closure->notifiers + CLOSURE_N_MFUNCS (&bits) + n;
closure->marshal = (GClosureMarshal) ndata->notify;
closure->data = ndata->data;
ndata->notify (ndata->data, closure);
......@@ -112,23 +153,34 @@ closure_invoke_notifiers (GClosure *closure,
closure->data = NULL;
break;
case INOTIFY:
closure->in_inotify = TRUE;
while (closure->n_inotifiers)
do {
CLOSURE_READ_BITS2 (closure, &bits, &new);
new.in_inotify = TRUE;
}
while (!CLOSURE_SWAP_BITS (closure, &bits, &new));
while (bits.n_inotifiers)
{
register guint n = --closure->n_inotifiers;
register guint n = --bits.n_inotifiers;
ndata = closure->notifiers + CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers + n;
ndata = closure->notifiers + CLOSURE_N_MFUNCS (&bits) + bits.n_fnotifiers + n;
closure->marshal = (GClosureMarshal) ndata->notify;
closure->data = ndata->data;
ndata->notify (ndata->data, closure);
}
closure->marshal = NULL;
closure->data = NULL;
closure->in_inotify = FALSE;
do {
CLOSURE_READ_BITS2 (closure, &bits, &new);
new.n_inotifiers = 0;
new.in_inotify = FALSE;
}
while (!CLOSURE_SWAP_BITS (closure, &bits, &new));
break;
case PRE_NOTIFY:
i = closure->n_guards;
offs = closure->meta_marshal;
CLOSURE_READ_BITS (closure, &bits);
i = bits.n_guards;
offs = bits.meta_marshal;
while (i--)
{
ndata = closure->notifiers + offs + i;
......@@ -136,8 +188,9 @@ closure_invoke_notifiers (GClosure *closure,
}
break;
case POST_NOTIFY:
i = closure->n_guards;
offs = closure->meta_marshal + i;
CLOSURE_READ_BITS (closure, &bits);
i = bits.n_guards;
offs = bits.meta_marshal + i;
while (i--)
{
ndata = closure->notifiers + offs + i;
......@@ -152,29 +205,48 @@ g_closure_set_meta_marshal (GClosure *closure,
gpointer marshal_data,
GClosureMarshal meta_marshal)
{
GClosureNotifyData *notifiers;
GClosureNotifyData *old_notifiers, *new_notifiers;
guint n;
GClosure old, new;
g_return_if_fail (closure != NULL);
g_return_if_fail (meta_marshal != NULL);
g_return_if_fail (closure->is_invalid == FALSE);
g_return_if_fail (closure->in_marshal == FALSE);
g_return_if_fail (closure->meta_marshal == 0);
n = CLOSURE_N_NOTIFIERS (closure);
notifiers = closure->notifiers;
closure->notifiers = g_renew (GClosureNotifyData, NULL, CLOSURE_N_NOTIFIERS (closure) + 1);
if (notifiers)
retry:
CLOSURE_READ_BITS2 (closure, &old, &new);
g_return_if_fail (old.is_invalid == FALSE);
g_return_if_fail (old.in_marshal == FALSE);
g_return_if_fail (old.meta_marshal == 0);
n = CLOSURE_N_NOTIFIERS (&old);
old_notifiers = closure->notifiers;
new_notifiers = g_renew (GClosureNotifyData, NULL, n + 1);
if (old_notifiers)
{
/* usually the meta marshal will be setup right after creation, so the
* g_memmove() should be rare-case scenario
*/
g_memmove (closure->notifiers + 1, notifiers, CLOSURE_N_NOTIFIERS (closure) * sizeof (notifiers[0]));
g_free (notifiers);
g_memmove (new_notifiers + 1, old_notifiers, n * sizeof (old_notifiers[0]));
}
closure->notifiers[0].data = marshal_data;
closure->notifiers[0].notify = (GClosureNotify) meta_marshal;
closure->meta_marshal = 1;
new_notifiers[0].data = marshal_data;
new_notifiers[0].notify = (GClosureNotify) meta_marshal;
new.meta_marshal = 1;
/* this cannot be made atomic, as soon as we switch on the meta_marshal
* bit, another thread could use the notifier while we have not yet
* copied it. the safest is to install the new_notifiers first and then
* switch on the meta_marshal flag. */
closure->notifiers = new_notifiers;
if (!CLOSURE_SWAP_BITS (closure, &old, &new)) {
g_free (new_notifiers);
goto retry;
}
g_free (old_notifiers);
}
void
......@@ -185,40 +257,56 @@ g_closure_add_marshal_guards (GClosure *closure,
GClosureNotify post_marshal_notify)
{
guint i;
GClosure old, new;
GClosureNotifyData *old_notifiers, *new_notifiers;
g_return_if_fail (closure != NULL);
g_return_if_fail (pre_marshal_notify != NULL);
g_return_if_fail (post_marshal_notify != NULL);
g_return_if_fail (closure->is_invalid == FALSE);
g_return_if_fail (closure->in_marshal == FALSE);
g_return_if_fail (closure->n_guards < CLOSURE_MAX_N_GUARDS);
closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 2);
if (closure->n_inotifiers)
closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
closure->n_fnotifiers +
closure->n_inotifiers + 1)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
closure->n_fnotifiers + 0)];
if (closure->n_inotifiers > 1)
closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
closure->n_fnotifiers +
closure->n_inotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
closure->n_fnotifiers + 1)];
if (closure->n_fnotifiers)
closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
closure->n_fnotifiers + 1)] = closure->notifiers[CLOSURE_N_MFUNCS (closure) + 0];
if (closure->n_fnotifiers > 1)
closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
closure->n_fnotifiers)] = closure->notifiers[CLOSURE_N_MFUNCS (closure) + 1];
if (closure->n_guards)
closure->notifiers[(closure->meta_marshal +
closure->n_guards +
closure->n_guards + 1)] = closure->notifiers[closure->meta_marshal + closure->n_guards];
i = closure->n_guards++;
closure->notifiers[closure->meta_marshal + i].data = pre_marshal_data;
closure->notifiers[closure->meta_marshal + i].notify = pre_marshal_notify;
closure->notifiers[closure->meta_marshal + i + 1].data = post_marshal_data;
closure->notifiers[closure->meta_marshal + i + 1].notify = post_marshal_notify;
retry:
CLOSURE_READ_BITS2 (closure, &old, &new);
g_return_if_fail (old.is_invalid == FALSE);
g_return_if_fail (old.in_marshal == FALSE);
g_return_if_fail (old.n_guards < CLOSURE_MAX_N_GUARDS);
old_notifiers = closure->notifiers;
new_notifiers = g_renew (GClosureNotifyData, old_notifiers, CLOSURE_N_NOTIFIERS (&old) + 2);
if (old.n_inotifiers)
new_notifiers[(CLOSURE_N_MFUNCS (&old) +
old.n_fnotifiers +
old.n_inotifiers + 1)] = new_notifiers[(CLOSURE_N_MFUNCS (&old) +
old.n_fnotifiers + 0)];
if (old.n_inotifiers > 1)
new_notifiers[(CLOSURE_N_MFUNCS (&old) +
old.n_fnotifiers +
old.n_inotifiers)] = new_notifiers[(CLOSURE_N_MFUNCS (&old) +
old.n_fnotifiers + 1)];
if (old.n_fnotifiers)
new_notifiers[(CLOSURE_N_MFUNCS (&old) +
old.n_fnotifiers + 1)] = new_notifiers[CLOSURE_N_MFUNCS (&old) + 0];
if (old.n_fnotifiers > 1)
new_notifiers[(CLOSURE_N_MFUNCS (&old) +
old.n_fnotifiers)] = new_notifiers[CLOSURE_N_MFUNCS (&old) + 1];
if (old.n_guards)
new_notifiers[(old.meta_marshal +
old.n_guards +
old.n_guards + 1)] = new_notifiers[old.meta_marshal + old.n_guards];
i = old.n_guards;
new.n_guards = i+1;
new_notifiers[old.meta_marshal + i].data = pre_marshal_data;
new_notifiers[old.meta_marshal + i].notify = pre_marshal_notify;
new_notifiers[old.meta_marshal + i + 1].data = post_marshal_data;
new_notifiers[old.meta_marshal + i + 1].notify = post_marshal_notify;
/* not really atomic */
closure->notifiers = new_notifiers;
if (!CLOSURE_SWAP_BITS (closure, &old, &new))
goto retry;
}
void
......@@ -227,20 +315,35 @@ g_closure_add_finalize_notifier (GClosure *closure,
GClosureNotify notify_func)
{
guint i;
GClosure old, new;
GClosureNotifyData *old_notifiers, *new_notifiers;
g_return_if_fail (closure != NULL);
g_return_if_fail (notify_func != NULL);
g_return_if_fail (closure->n_fnotifiers < CLOSURE_MAX_N_FNOTIFIERS);
closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 1);
if (closure->n_inotifiers)
closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
closure->n_fnotifiers +
closure->n_inotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
closure->n_fnotifiers + 0)];
i = CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers++;
closure->notifiers[i].data = notify_data;
closure->notifiers[i].notify = notify_func;
retry:
CLOSURE_READ_BITS2 (closure, &old, &new);
g_return_if_fail (old.n_fnotifiers < CLOSURE_MAX_N_FNOTIFIERS);
old_notifiers = closure->notifiers;
new_notifiers = g_renew (GClosureNotifyData, old_notifiers, CLOSURE_N_NOTIFIERS (&old) + 1);
if (old.n_inotifiers)
new_notifiers[(CLOSURE_N_MFUNCS (&old) +
old.n_fnotifiers +
old.n_inotifiers)] = new_notifiers[(CLOSURE_N_MFUNCS (&old) +
old.n_fnotifiers + 0)];
i = CLOSURE_N_MFUNCS (&old) + old.n_fnotifiers;
new.n_fnotifiers++;
new_notifiers[i].data = notify_data;
new_notifiers[i].notify = notify_func;
/* not really atomic */
closure->notifiers = new_notifiers;
while (!CLOSURE_SWAP_BITS (closure, &old, &new))
goto retry;
}
void
......@@ -249,16 +352,31 @@ g_closure_add_invalidate_notifier (GClosure *closure,
GClosureNotify notify_func)
{
guint i;
GClosure old, new;
GClosureNotifyData *old_notifiers, *new_notifiers;
g_return_if_fail (closure != NULL);
g_return_if_fail (notify_func != NULL);
g_return_if_fail (closure->is_invalid == FALSE);
g_return_if_fail (closure->n_inotifiers < CLOSURE_MAX_N_INOTIFIERS);
closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 1);
i = CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers + closure->n_inotifiers++;
closure->notifiers[i].data = notify_data;
closure->notifiers[i].notify = notify_func;
retry:
CLOSURE_READ_BITS2 (closure, &old, &new);
g_return_if_fail (old.is_invalid == FALSE);
g_return_if_fail (old.n_inotifiers < CLOSURE_MAX_N_INOTIFIERS);
old_notifiers = closure->notifiers;
new_notifiers = g_renew (GClosureNotifyData, old_notifiers, CLOSURE_N_NOTIFIERS (&old) + 1);
i = CLOSURE_N_MFUNCS (&old) + old.n_fnotifiers + old.n_inotifiers;
new.n_inotifiers++;
new_notifiers[i].data = notify_data;
new_notifiers[i].notify = notify_func;
/* not really atomic */
closure->notifiers = new_notifiers;
while (!CLOSURE_SWAP_BITS (closure, &old, &new))
goto retry;
}
static inline gboolean
......@@ -267,12 +385,19 @@ closure_try_remove_inotify (GClosure *closure,
GClosureNotify notify_func)
{
GClosureNotifyData *ndata, *nlast;
GClosure old, new;
retry:
CLOSURE_READ_BITS2 (closure, &old, &new);
nlast = closure->notifiers + CLOSURE_N_NOTIFIERS (closure) - 1;
for (ndata = nlast + 1 - closure->n_inotifiers; ndata <= nlast; ndata++)
nlast = closure->notifiers + CLOSURE_N_NOTIFIERS (&old) - 1;
for (ndata = nlast + 1 - old.n_inotifiers; ndata <= nlast; ndata++)
if (ndata->notify == notify_func && ndata->data == notify_data)
{
closure->n_inotifiers -= 1;
new.n_inotifiers -= 1;
if (!CLOSURE_SWAP_BITS (closure, &old, &new))
goto retry;
if (ndata < nlast)
*ndata = *nlast;
......@@ -287,19 +412,27 @@ closure_try_remove_fnotify (GClosure *closure,
GClosureNotify notify_func)
{
GClosureNotifyData *ndata, *nlast;
GClosure old, new;
nlast = closure->notifiers + CLOSURE_N_NOTIFIERS (closure) - closure->n_inotifiers - 1;
for (ndata = nlast + 1 - closure->n_fnotifiers; ndata <= nlast; ndata++)
retry:
CLOSURE_READ_BITS2 (closure, &old, &new);
nlast = closure->notifiers + CLOSURE_N_NOTIFIERS (&old) - old.n_inotifiers - 1;
for (ndata = nlast + 1 - old.n_fnotifiers; ndata <= nlast; ndata++)
if (ndata->notify == notify_func && ndata->data == notify_data)
{
closure->n_fnotifiers -= 1;
new.n_fnotifiers -= 1;
if (!CLOSURE_SWAP_BITS (closure, &old, &new))
goto retry;
if (ndata < nlast)
*ndata = *nlast;
if (closure->n_inotifiers)
closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
closure->n_fnotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
closure->n_fnotifiers +
closure->n_inotifiers)];
if (new.n_inotifiers)
closure->notifiers[(CLOSURE_N_MFUNCS (&new) +
new.n_fnotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (&new) +
new.n_fnotifiers +
new.n_inotifiers)];
return TRUE;
}
return FALSE;
......@@ -312,7 +445,7 @@ g_closure_ref (GClosure *closure)
g_return_val_if_fail (closure->ref_count > 0, NULL);
g_return_val_if_fail (closure->ref_count < CLOSURE_MAX_REF_COUNT, NULL);
closure->ref_count += 1;
CLOSURE_REF (closure);
return closure;
}
......@@ -320,12 +453,21 @@ g_closure_ref (GClosure *closure)
void
g_closure_invalidate (GClosure *closure)
{
GClosure old, new;
g_return_if_fail (closure != NULL);
if (!closure->is_invalid)
retry:
CLOSURE_READ_BITS2 (closure, &old, &new);
if (!old.is_invalid)
{
closure->ref_count += 1; /* preserve floating flag */
closure->is_invalid = TRUE;