lua-users home
lua-l archive

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


Hey!
 
I work on Luvit and Luv and we store the ref then in the callback call the unref. Perhaps this snippet will help [1]. Or use Luv directly in your project :) We could use the feedback and help.
 
Cheers,
Ryan
 
[1] https://github.com/luvit/luv 
[2] https://github.com/luvit/luv/blob/master/src/handle.c#L61-80
 
 
On Mon, Dec 29, 2014, at 06:21 AM, alexeymelnichuck@gmail.com wrote:
Hello.
 
I writing libuv binding and have problem with close C handle.
I have weak table that map C handles(lightuserdata) to Lua objects.
So when libuv gives me a handle I can find corresponding Lua object.
But because close handle is two-phase operation (call `uv_close` and
wait  callback)  I  need  to  store my pair (handle=>object) valid until
end of callback. In my `__gc` method I create reference in registry
because Lua already remove pair from weak table and call uv_close.
But  problem  is  that Lua can remove pair from weak table and did not
call `__gc` method. So there no way find out Lua object by C Handle.
 
```Lua
-- create garbage handle that Lua have to collect
uv.timer()
 
-- here Lua may remove timer from weak table
-- but did not call `__gc`
 
--  libuv  has timer handle even if we call uv_close (because callback
-- is not called. So we still need map C handle to Lua object.
uv.walk(function(handle) ... end)
```
 
So   only   one   way  I  see is to create another one weak table with
weak  keys  (Lua  object => `true`) and use full scan on it when I can
not find Lua object in first one.
 
May be some one know better solution?
 
 
 
---
Это сообщение проверено на вирусы антивирусом Avast.
http://www.avast.com