Benjamin Goldberg <[EMAIL PROTECTED]> writes:

> I would like for Parrot to have some way of creating Weak References; I
> think that this is probably a vital feature.
> 
> The way I envision this is as follows.  The following typedef and new
> function would be added:
> 
> typedef void (*pobject_died_cb)(INTERP, PMC* who_asked,
>    Pobj* weakref, void *callback_info);
> void pobject_weakref(INTERP, pobject_died_cb callback,
>    Pobj* weakref, void *callback_info);

This might be sometimes useful. Keeping a container of active objects
like filehandles or windows for example. In this case it can't mark
the reference because this would make the object active forever.

> Inside of a PMC*'s mark method, it registers callbacks (using the above
> function) so that it will be informed about what to do if the object to
> which it weakly refers to is found to not be alive at the end of the DOD
> sweep.

This does not need to go into the mark function. The weakref_callback
code can be called inside the destroy function. Im not sure if it
should be called befor or after the custom destroy-function is
run. And is the order of the weakref_callbacks defined.

> The pobject_weakref function first checks if the 'weakref' argument has
> been marked as alive -- if so, nothing happens.  Then, it adds the Pobj*
> to a lookup table, pointing from the Pobj*, to a list of registered
> callbacks for that Pobj*.

This is far to complicated. Each object has a list of
destroy-functions, one of them is the costom-destroy function the
others are the weakref-callbacks.

But one other thing, what happens if the object holding the weakref
dies before the refrenced object? Then the callback-function will be
called for a dead object. So pobject_weakref() needs to return a handle
for the weakref and there needs to be a function
weakref_destroy(weakref_handle *handle).

Other issue is who owns the data_structure of the weakref? The
referent, the referee, or will this be garbage-collected (which makes
the weakref_handle a PObj* and weakref_destroy its custom destroy function.

> After DOD finishes, the lookup table is walked; for each entry whose
> Pobj* hasn't been marked as alive, the callbacks are called.
> 
> The effect of this of course is that a WeakRef has no cost except during
> Dead Object Detection.

It only has a cost at object destroy-time. (If the weakrefs are
garbagecollected they have an effect on DOD in the way that there are
more objects to trace)

> The first, perhaps most important use, would be to implement a
> string-interning table.
> 
> You'd have a global (per-interpreter) pmc which contains a hashtable of
> cstrings to perlstrings; if the cstring is present, the corresponding
> perlstring is returned; otherwise a new perlstring would be created and
> added to the table, then returned.  If any of the perlstrings go out of
> scope, then their hash entries should disappear from the table.
> Obivously, if the table contained normal references to the strings, then
> they won't ever go out of scope.  And if the table simply doesn't mark
> them as alive, it wouldn't know when they're dead.  But with weakrefs,
> this is easy.

this is only useful if a hashlookup is fast compared with
string_make.

bye
boe
-- 
Juergen Boemmels                        [EMAIL PROTECTED]
Fachbereich Physik                      Tel: ++49-(0)631-205-2817
Universitaet Kaiserslautern             Fax: ++49-(0)631-205-3906
PGP Key fingerprint = 9F 56 54 3D 45 C1 32 6F  23 F6 C7 2F 85 93 DD 47

Reply via email to