lua-users home
lua-l archive

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


Hi,

Diego Nehab wrote:
> How do we prevent hell from being unleashed?

You can't. None of the pre-emptive threading libraries solve this
problem. Because there is no general solution given the degrees
of freedom for the programmer these libraries provide.

LuaThreads exposes enough of the underlying functionality to make
it suffer from the same problem.

So turn the argument around: exiting the main thread without
cleaning up all other threads is a programmer error. Document it
as such and be done.

[
  Optionally detect this case and call a thread.panic function.
  The default action would be an error message and _exit(1).

  Well, this would need two macros then: 1. luai_userstateclose()
  where it currently is (before all other threads are freed) and
  2. a new luai_userstatecleanup() [name?] after luaC_freeall().

  Pseudo-code:

  luai_userstateclose()
  {
    if (#threads > 0) {  // The main thread is not counted.
      do full GC
      do full GC
      if (#threads > 0) {
	call panic function
      }
    }
  }

  luai_userstatecleanup()  // Not to be called for lua_newstate failure.
  {
    unlock mutex
    destroy mutex
  }
]

[Now you know why I'm so fond about non-pre-emptive approaches.]

> BTW, it looks like I can set the __gc metamethod for
> threads. If this is really the case, I could use it instead
> of keeping a list of threads myself. But still, this looks
> like a complicated solution.

You can set __gc for every object. But it will only be called for
userdata objects. The code above only needs a thread counter.

Bye,
     Mike