Results 1 to 4 of 4

Thread: C library sample code for multithreaded callbacks

  1. #1
    Join Date
    Jul 2007
    Location
    Netherlands
    Posts
    342

    Default C library sample code for multithreaded callbacks

    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?
    Using: Win7 MCE, Girder, xPL, RFXcom, HomeEasy

    http://www.thijsschreijer.nl

  2. #2
    Join Date
    Jan 2000
    Location
    Jupiter, FL
    Posts
    11,347

    Default

    From the PIR-1 code:

    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.

    Code:
    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;
    }
    Ron
    No support through PM

  3. #3
    Join Date
    Jul 2007
    Location
    Netherlands
    Posts
    342

    Default

    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.
    Using: Win7 MCE, Girder, xPL, RFXcom, HomeEasy

    http://www.thijsschreijer.nl

  4. #4
    Join Date
    Jan 2000
    Location
    Jupiter, FL
    Posts
    11,347

    Default

    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.
    Ron
    No support through PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •