[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Asynchronous callback - no attachments
- From: Rob Hoelz <rob@...>
- Date: Thu, 15 Sep 2011 13:59:45 -0500
On Thu, 15 Sep 2011 14:34:38 -0400
<AllanPfalzgraf@eaton.com> wrote:
> Below is the content of a small library that simulates the behavior of
> software that produces asynchronous data. This library is intended to
> move that data into the data space of a Lua script.
>
> If the Lua function that is called in fnCallback is called directly
> from Lua it works, if it is called via fnCallback it fails when the
> table is declared for storage of the three parameters. (see the
> console output near the end of this email)
>
> Appologies for the length of this, it could not have been made
> shorter.
>
> -- Essential code for the cb.dll -----------------------------------
> static lua_State *gpLuaState = 0; // Used by callback function to
> send status to the script.
> static int cbNewData( lua_State * );
> void fnCallback( int, int, int );
>
> static int cbNewData( lua_State *L )
> {
> int a, b, c;
>
> a = luaL_checkint( L, 1 );
> b = luaL_checkint( L, 2 );
> c = luaL_checkint( L, 3 );
>
> fnCallback( a, b, c );
>
> return 0;
> }
>
> void fnCallback( int nEvent, int nParam1, int nParam2 )
> {
> int stat;
>
> if (gpLuaState != 0)
> {
> lua_getglobal( gpLuaState, "cbCallback" );
> lua_pushinteger( gpLuaState, nEvent );
> lua_pushinteger( gpLuaState, nParam1 );
> lua_pushinteger( gpLuaState, nParam2 );
>
> // do the call ( 3 arguments, no return, no error function )
> if (stat = lua_pcall( gpLuaState, 3, 0, 0 ) != 0 )
> {
> printf("eipCallback failed, code %d - %s", stat,
> lua_tostring(gpLuaState, -1));
> lua_pop(gpLuaState, 1);
> }
> }
> else
> {
> printf("gpLuaState was not initialized");
> }
> }
>
> static const luaL_Reg cblib[] = {
> {"write", cbNewData},
> {NULL, NULL}
> };
>
> LUALIB_API int luaopen_cb (lua_State *L) {
> gpLuaState = L;
> /* open library */
> luaL_register(L, "cb", cblib);
> return 1;
> }
>
> -- Contents of cbq.lua -----------------------------------
> FAILURE = 1
> SUCCESS = 0
>
> q = {first = 1,
> last = 0,
> list={}
> }
>
> function cbCallback(event, param1, param2)
> local last = q.last + 1
> q.last = last
> print("callback",event, param1, param2)
> print(type(q.list))
> print(last)
> q.list[last] = {}
> print(type(q.list[last]))
> q.list[last][1]=event
> print(q.list[last][1])
> q.list[last][2]=param1
> print(q.list[last][2])
> q.list[last][3]=param2
> print(q.list[last][3])
> end
>
> function pop()
> local first = q.first
> local event, param1, param2
>
> if first > q.last then
> return FAILURE
> end
>
> event = q.list[first][1]
> param1 = q.list[first][2]
> param2 = q.list[first][3]
> print("pop",event, param1, param2)
>
> q.list[first] = nil
> q.first = first + 1
>
> return SUCCESS, event, param1, param2
> end
>
> -- Console output of test ---------------------------------------
> Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
> > require"cb"
> > require"cbq"
> >
> > -- Direct Use
> > cbCallback(12,13,14)
> callback 12 13 14
> table
> 1
> table
> 12
> 13
> 14
> > cbCallback(15,16,17)
> callback 15 16 17
> table
> 2
> table
> 15
> 16
> 17
> > print(pop())
> pop 12 13 14
> 0 12 13 14
> > print(pop())
> pop 15 16 17
> 0 15 16 17
> > print(pop())
> 1
> > --Indirect Use
> > cb.write(18,19,20)
> callback 18 19 20
> table
> 3
>
>
> Thanks in advance for any suggestions.
>
>
> Allan Pfalzgraf
> EATON
>
> 4201 North 27 Street
> Milwaukee, WI 53216
> tel: 414 449-6872
> fax: 414 449-6616
> AllanPfalzgraf@eaton.com
> www.Eaton.com <http://www.eaton.com/>
> <http://www.eaton.com/>
>
>
What happens if you set gpLuaState = L in cbNewData, right before
fnCallback? I realize you probably can't do this when you're running
the real asynchronous code, but I'd like to see if it solves the issue
in your example.
Also, glad to see another Wisconsinite on the list. =)
-Rob
Attachment:
signature.asc
Description: PGP signature