[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Lua and multi-threaded FastCGI servers
- From: Peter Colson <pcolson@...>
- Date: Fri, 4 Mar 2005 11:42:48 +1100
I'm in the process of modifying a single-threaded FastCGI server that uses Lua scripts to accomplish it's work (retrieving spooled file contents and returning them to a client).
Please bear with me as I elaborate some FastCGI aspects as they relate to my question re. Lua.
I'm trying to make the server multi-threaded by using Posix threads and the FastCGI API's as follows:
pcreate_thread(&AcceptThread, 0, (void*)&AcceptThreadFunc, 0);
FCGX_InitRequest(&FcgiReq, 0, 0);
while (FCGX_Accept_r(&FcgiReq) >= 0)
L = lua_open();
luaL_loadFile(L, sFileName); // sFileName extracted from FastCGI request.
lua_pcall(L, 0, 0, 0); // run Lua script to do work.
That is, each thread has it's own "accept loop" and an FCGX_Request instance, plus it's own Lua virtual machine (VM).
Now, processing the Lua script will at some point invoke one of our lua extension C functions, l_fcgiout(lua_State *L). In the single-threaded version this was happy to use FCGI_fwrite(FCGI_stdout, ...) where FCGI_stdout is a global constant.
Since I'm trying to multi-thread the server I need to use FCGX_FPrintF or FCGX_PutS (within an FCGX_Accept_r accept loop). Both these routines require an FCGX_Stream typically supplied by FcgiReq.out (using above code).
So the problem is that l_fcgiout() needs access to the FCGX_Request instance set up in the FastCGI accept loop (prior to calling Lua).
What is the best way to make the FCGX_Request instance in the threaded accept loop available to my Lua function l_fcgiout?
a). Push it onto the stack and pop it off in l_fcgiout (along with other parameters passed to fcgiout)?
b). Use a Lua global variable since global variables are distinct for each Lua VM and each worker thread above creates it's own Lua VM?
c). Use the Lua registry? Is the registry a per Lua VM entity like the global variables in b)?