[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Lua, threading & sockets
- From: Rena <hyperhacker@...>
- Date: Fri, 19 Sep 2014 17:18:08 -0400
On Fri, Sep 19, 2014 at 5:12 PM, chris 0 <viscousplacebo@gmail.com> wrote:
> Hi,
>
> I'm looking for a way in which to spawn a pre-emptive thread (e.g. pthread)
> for each TCP connection, in both lua and luajit.
>
> The problem being I can't seem to find any libraries that are able to do
> this.
>
> I've tried combining lua lanes with lua sockets, which fails because of the
> 'deep' data
> from lua sockets. I was working on trying to handle deep data using
> luaG_newdeepuserdata(L, idfunc) in lua sockets meth_accept function, but i'm
> currently
> getting nothing really but segfaults which I can't seem to diagnose why:
>
> Program received signal SIGSEGV, Segmentation fault.
> [Switching to Thread 0x7ffff6820700 (LWP 19896)]
> 0x000000000041057c in luaH_get ()
> (gdb) bt
> #0 0x000000000041057c in luaH_get ()
> #1 0x0000000000410ca7 in luaH_set ()
> #2 0x0000000000406961 in lua_rawset ()
> #3 0x00007ffff6c69915 in populate_func_lookup_table (L=0x653d40, _i=-1,
> name_=0x7ffff6c70cbc "set_finalizer") at tools.c:530
> #4 0x00007ffff6c648ed in lane_main (vs=0x667b40) at lanes.c:2055
>
>
> I've looked at luv - based on libuv, however this doesn't appear to support
> threading.
>
> If anyones got any suggestions, I'd be most appreciative!
>
> Thanks
>
> Chris
The way I dealt with threading+sockets in one case was to modify
LuaSocket to expose the `fd` field of the socket objects and to add a
`socket.fromfd()` function. Then you can pass the fd (an integer) from
one thread to another, and the receiving thread can create its own
socket object.
This approach does have a thorn though: when a socket object is
collected, it will want to close the fd, even if another thread is
still using it. The simplest way I found to solve that is to add a
flag to the socket object, that tells it not to close the fd, and set
that flag on one thread before giving the fd to the other thread.
Reference counting would be an even better solution, but then you'd
have to add mutexes and pthread to the socket library, so a bit more
work.
--
Sent from my Game Boy.