View Full Version : C library sample code for multithreaded callbacks
Tieske8
May 16th, 2012, 02:53 AM
Does any one have some C library sample code to share?
I'm looking to create a binding to a multithreaded library, so when callbacks occur they may be on different threads. For all I know Girder runs Lua 5.0 using the global locks (correct?). So to have a C callback on a separate thread enter the lua state safely, I need to get a handle to the lua state, and I need to know how to acquire and release the locks (to make it threadsafe)
Any ideas where to look?
Ron
May 16th, 2012, 06:50 AM
From the PIR-1 code:
if ( LockLua(1000) ) {
lua_State * L = g_L;
if ( L != 0 && m_CB != LUA_NOREF ) {
lua_rawgeti(L, LUA_REGISTRYINDEX, m_CB);
lua_pushstring(L, serial.c_str());
lua_pushnumber(L, arriveRemove);
lua_pushstring(L,"_TRACEBACK");
lua_gettable(L, LUA_GLOBALSINDEX);
lua_insert( L, -2-2);
if (lua_pcall (L,2,0,-2-2) != 0)
{
luaP_print(L, lua_tostring(L,-1));
}
lua_settop(L,0);
}
UnlockLua();
}
g_L comes from gir_info. The plugin stores the Lua state when Girder calls that function with GIRINFO_SCRIPT_AFTER_STARTED. Girder holds the Lua lock during that time so it should be thread safe.
int WINAPI gir_info(int message, int wparam, int lparam)
{
switch ( message )
{
case GIRINFO_SCRIPT_AFTER_STARTED:
g_L = ( lua_State * ) lparam;
register_lua(g_L);
break;
case GIRINFO_SCRIPT_BEFORE_STOPPED:
unregister_lua(( lua_State * ) lparam);
g_L = 0;
break;
case GIRINFO_DEVICE_CHANGE:
break;
}
return GIR_TRUE;
}
Tieske8
May 17th, 2012, 05:01 AM
So I guess there is no way to do it without using Girder specifics? just using the Lua header files would be my preferred way.
Ron
May 17th, 2012, 07:05 AM
To do it thread safe using the Lua state from Girder you must get the mutex from Girder, that is the LockLua call. If you just need a lua state that is not hooked into the Girder Lua state you can simply use lua_open() without using Girder's functions.
Powered by vBulletin® Version 4.2.2 Copyright © 2018 vBulletin Solutions, Inc. All rights reserved.