lua-users home
lua-l archive

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


On 12 January 2018 at 03:53, Dan Tull <dtull@adobe.com> wrote:
>
> On 10 January 2018 at 04:31, Dan Tull <dtull@adobe.com> wrote:
>
> > > The change to support our sampling profiler was to add a counter to the
> > > global part of the Lua state ...
> > What you've described is what debug.sethook does!
> > L->hookmask is an atomic value, which makes it safe to set from another thread.
> Not quite.
>
> I haven't looked at this in a long time, but IIRC one problem is that lua_sethook is
> per state (coroutine/thread) but I needed one that was global so that it would stop
> regardless of which coroutine is running.
>
> As an aside, the per-state/coroutine nature of the hooking mechanism has been a
> pain on more than one occasion and only rarely useful.
>
> Even without the global vs per coroutine problem, given that except for the main state,
> any coroutine could get garbage collected (leaving the state pointer dangling), so
> lua_sethook is not safe to call from an arbitrary other thread.


The lack of an ability to set a hook for all threads is indeed an issue.

*unless* you're willing to compile lua yourself, in which case you can define
luai_userstatethread and luai_userstatefree. See http://www.lua.org/source/5.3/llimits.h.html#luai_userstatethread
Which should at least be easier and more maintainable than modifying the lua source.

However, I do wish that there was a way to hook new coroutine creation from lua itself.
I've seen a few applications redefine coroutine.wrap and coroutine.create. However that won't catch a C module doing it via lua_newthread.