Commit cdc30635 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

Lua: remove the timers API

The Lua interpreter is not thread-safe. So this API made absolutely no
sense: the timer callback is called asynchronously from another thread.
Fortunately, none of our Lua scripts used this except for testing.
parent f0393c52
......@@ -177,107 +177,6 @@ static int vlclua_action_id( lua_State *L )
return 1;
}
/*****************************************************************************
* Timer functions
*****************************************************************************/
static int vlclua_timer_schedule( lua_State *L );
static int vlclua_timer_getoverrun( lua_State *L);
static const luaL_Reg vlclua_timer_reg[] = {
{ "schedule", vlclua_timer_schedule },
{ "getoverrun", vlclua_timer_getoverrun },
{ NULL, NULL }
};
typedef struct
{
lua_State *L;
vlc_timer_t timer;
char *psz_callback;
} vlclua_timer_t;
static int vlclua_timer_schedule( lua_State *L )
{
vlclua_timer_t **pp_timer = (vlclua_timer_t**)luaL_checkudata( L, 1, "timer" );
if( !pp_timer || !*pp_timer )
luaL_error( L, "Can't get pointer to timer" );
bool b_relative = luaL_checkboolean( L, 2 );
mtime_t i_value = luaL_checkinteger( L, 3 );
mtime_t i_interval = luaL_checkinteger( L, 4 );
vlc_timer_schedule( (*pp_timer)->timer, b_relative, i_value, i_interval );
return 0;
}
static int vlclua_timer_getoverrun( lua_State *L )
{
vlclua_timer_t **pp_timer = (vlclua_timer_t**)luaL_checkudata(L, 1, "timer" );
if( !pp_timer || !*pp_timer )
luaL_error( L, "Can't get pointer to timer" );
lua_pushinteger( L, vlc_timer_getoverrun( (*pp_timer)->timer ) );
return 1;
}
static void vlclua_timer_callback( void *data )
{
vlclua_timer_t *p_timer = (vlclua_timer_t*)data;
lua_State *L = p_timer->L;
lua_getglobal( L, p_timer->psz_callback );
if( lua_pcall( L, 0, 0, 0 ) )
{
const char *psz_err = lua_tostring( L, -1 );
msg_Err( vlclua_get_this( L ), "Error while running the timer callback: '%s'", psz_err );
lua_settop( L, 0 );
}
}
static int vlclua_timer_delete( lua_State *L )
{
vlclua_timer_t **pp_timer = (vlclua_timer_t**)luaL_checkudata( L, 1, "timer" );
if( !pp_timer || !*pp_timer )
luaL_error( L, "Can't get pointer to timer" );
vlc_timer_destroy( (*pp_timer)->timer );
free( (*pp_timer)->psz_callback );
free( (*pp_timer) );
return 0;
}
static int vlclua_timer_create( lua_State *L )
{
if( !lua_isstring( L, 1 ) )
return luaL_error( L, "timer(function_name)" );
vlclua_timer_t *p_timer = malloc( sizeof( vlclua_timer_t ) );
if( vlc_timer_create( &p_timer->timer, vlclua_timer_callback, p_timer ) )
{
free( p_timer );
return luaL_error( L, "Cannot initialize the timer" );
}
p_timer->L = L;
p_timer->psz_callback = strdup( luaL_checkstring( L, 1 ) );
vlclua_timer_t **pp_timer = lua_newuserdata( L, sizeof( vlclua_timer_t* ) );
*pp_timer = p_timer;
/* Create the object */
if( luaL_newmetatable( L, "timer" ) )
{
lua_newtable( L );
luaL_register( L, NULL, vlclua_timer_reg );
lua_setfield( L, -2, "__index" );
lua_pushcfunction( L, vlclua_timer_delete );
lua_setfield( L, -2, "__gc" );
}
lua_setmetatable( L, -2 );
return 1;
}
/*****************************************************************************
*
*****************************************************************************/
......@@ -296,8 +195,6 @@ static const luaL_Reg vlclua_misc_reg[] = {
{ "should_die", vlclua_intf_should_die },
{ "quit", vlclua_quit },
{ "timer", vlclua_timer_create },
{ NULL, NULL }
};
......
......@@ -170,10 +170,6 @@ misc.lock_and_wait(): Lock our object thread and wait for a wake up signal.
misc.should_die(): Returns true if the interface should quit.
misc.quit(): Quit VLC.
misc.timer(callback): Create a timer which call the callback function
:schedule(relative, value, interval): schedule the timer
:getoverrun(): number of time the timer got overrun (normally 0)
Net
---
net.url_parse( url, [option delimiter] ): Parse URL. Returns a table with
......
......@@ -7,24 +7,12 @@ function assert_url(result, protocol, username, password, host, port, path)
assert(result.path == path)
end
local timer_count = 0
function timer_callback()
timer_count = timer_count + 1
end
vlc.msg.info('---- Testing misc functions ----')
vlc.msg.info('version: ' .. vlc.misc.version())
vlc.msg.info('copyright: ' .. vlc.misc.copyright())
vlc.msg.info('license: ' .. vlc.misc.license())
vlc.msg.info('mdate: ' .. vlc.misc.mdate())
vlc.msg.info(' * Testing the timer')
local timer = vlc.misc.timer('timer_callback')
timer:schedule(true, 100, 0)
vlc.misc.mwait(vlc.misc.mdate()+1000000)
assert(timer_count == 1)
vlc.msg.info(' [done]')
vlc.msg.info('---- Testing config functions ----')
vlc.msg.info('datadir: ' .. vlc.config.datadir())
vlc.msg.info('userdatadir: ' .. vlc.config.userdatadir())
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment