lua-users home
lua-l archive

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


> Can you explain how that works? Does the yield call inside a registered
> C function not return until some other state yields back to the first
> state? 

No. You must call lua_yield as "return lua_yield(...)". So your
registered C function ends when it calls yield. We "yield" Lua code, not
C code.

> If so, I don't understand how that's possible (without blowing the stack
> out, for example). 
> If not, it's not exactly the same thing.

It is now exactly the same thing from C, but it is from Lua. So, you
must write your "blocking C functions" with care. For instance, one
solution for a blocking read could be something like this:

  int aux_read (lua_State *L) {
    if (cannot_read()) return lua_yield();
    else {
      ...
    }
  }

and wrap that in Lua:

  function read ()
    while 1 do
      local res = aux_read()
      if res then return res end
    end
  end

A more sofisticated solution could avoid the loop, by making sure that the
scheduler does not return to that thread until its stream is ready...

-- Roberto