lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi,

> I just discovered that coroutines have their own independent set of
> hooks, and don't inherit any from their creator, but I can't find where
> it says so in the documentation.
> 
> Could the documentation perhaps make this obvious somehow, or am I just
> blind?  And is there an easy way to make coroutines inherit hooks upon
> creation?

I think it's undocumented. And a bit awkward, too. The culprit is
preinit_state() in src/lstate.c. It resets all hooks and is called
from lua_open() (aka lua_newstate() in 5.1) and luaE_newthread().
Changing it to inherit the hooks would be trivial.

However I'm not sure if this is the best solution. Debuggers and Profilers
need to be aware of thread creation anyway. So I suggest to add
LUA_HOOKTHREAD and call it from luaE_newthread() and luaE_freethread()
(with the just-created or the to-be-deleted lua_State). You can then
add the hooks to the new thread or clean up your own state.

I'm offering to write the patch and the docs for this, if Roberto wants
it for 5.1. ;-)

> I'm working on an allocation profiler (without calling lua from inside
> realloc() this time... :]) and I noticed that coroutine.resume() was
> making lots of garbage, but of course it obviously wasn't, because none
> of the coroutine functions showed up at all.
> 
> If others are interested I will clean it up and add it to the wiki; it
> displays a flat profile showing the line numbers that allocated the most
> live memory and the line numbers that generated the most garbage.

Yes, please! This would be greatly appreciated. I always had to refer
to guessing and stopgap solutions to find out about these things.

Note that your job is going to get easier with Lua 5.1 since the allocation
callback is part of global_State (but the member is unfortunately called
'realloc' -- this is a potential name clash and asking for trouble.
Umm ... Roberto?). But we would need lua_setallocf() in addition to
lua_getallocf().

BTW: Is there a package that collects all known profiling, tracing and
     coverage tools?

Bye,
     Mike