xs module, threads and clone() - how does clone work ?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

I have an extension (xsub). The underlying C code calls malloc(),
and returns pointers to the perl interpreter as T_PTROBJ. When
the garbage collector comes along, it correctly calls my
xxx_DESTROY() functions which clean up and free() allocated

When I try to use perls threads, this breaks terribly as is
expected. When a thread finishes, it cleans up, calls all the
destructors. They call free() because they do not know that other
threads still have a reference to the objects. The other threads
fall over and die.

This is all expected and there are hooks, but they do not do what
I want. Using CLONE_SKIP stops the double free()ing, but it means
that a child cannot create an object and pass it back to the
parent. The destructor is called when the child finishes. Using
CLONE is terrible. The only parameter you get is the name of the
package, not an individual object. This is all in the man pages,
so one cannot complain. Next, I wondered if I could write my own
data cloner. Then I could put in a simple reference count scheme
and teach my _DESTROY functions to behave correctly.

If I make a function in my .xs file, xxxx_CLONE(yyyy), then the
interpreter calls it when it does the data cloning to make a
thread. Here, I became very confused. XS_xxxx_CLONE(yyyy) is
called, but I cannot work out what yyyy is. It is not a pointer
to the allocated object. I fear I am not doing the right thing.
xxxx_CLONE does not seem to be documented, so am I allowed to
override it ? Am I going to make a mess of the perl's internal
bookkeeping if I just use this hook to increment a reference
counter ?
Many thanks for any comments.

Site Timeline