Changeset 1433 for trunk


Ignore:
Timestamp:
11/27/11 01:16:15 (18 months ago)
Author:
reyalp
Message:

add script control of lua scheduling. Adds one new function:
old_max_count,old_max_ms=set_yield(max_count,max_ms)
max_count is the maximum number of yield_hook calls (100x vm instructions) to run before yielding, default 25
max_ms is the maximum number milliseconds to run, checked every 100 vm instructions, precision 10ms, default 10
passing nil for either value resets to default. Values are treated as unsigned.

default number of vm instructions per kbd_task iteration is changed from 1000 to 2500, but total execution time is now limited to 10ms (previously unlimited), and lua->c->lua yield_hook are counted so a yield will occure more quickly after the c call ends.

Location:
trunk/core
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/core/luascript.c

    r1423 r1433  
    1818#include "core.h" 
    1919#include "gui_fselect.h" 
     20#include "lang.h" 
    2021#include "gui_lang.h" 
    2122 
     
    2627 
    2728static int lua_script_is_ptp; 
    28  
     29static int run_first_resume; // 1 first 'resume', 0 = resuming from yield 
     30static int run_start_tick; // tick count at start of this kbd_task iteration 
     31static int run_hook_count; // number of calls to the count hook this kbd_task iteration 
     32#define YIELD_CHECK_COUNT 100 // check for yield every N vm instructions 
     33#define YIELD_MAX_COUNT_DEFAULT 25 // 25 checks = 2500 vm instructions 
     34#define YIELD_MAX_MS_DEFAULT 10 
     35static unsigned yield_max_count; 
     36static unsigned yield_max_ms; 
    2937static int yield_hook_enabled; 
    3038 
     
    124132static void lua_count_hook(lua_State *L, lua_Debug *ar) 
    125133{ 
    126   if( L->nCcalls <= L->baseCcalls && yield_hook_enabled ) 
     134  run_hook_count++; 
     135  if( L->nCcalls > L->baseCcalls || !yield_hook_enabled ) 
     136    return; 
     137  if(run_hook_count >= yield_max_count || get_tick_count() - run_start_tick >= yield_max_ms) 
    127138    lua_yield( L, 0 ); 
    128139} 
     
    191202    return 0; 
    192203  } 
    193   lua_sethook(Lt, lua_count_hook, LUA_MASKCOUNT, 1000 ); 
     204  lua_sethook(Lt, lua_count_hook, LUA_MASKCOUNT, YIELD_CHECK_COUNT ); 
    194205  lua_script_enable_yield_hook(); 
    195   return 1; 
     206  run_first_resume = 1; 
     207  yield_max_count = YIELD_MAX_COUNT_DEFAULT; 
     208  yield_max_ms = YIELD_MAX_MS_DEFAULT; 
     209  return 1; 
     210} 
     211 
     212// run a timeslice of lua script 
     213void lua_script_run(void) 
     214{ 
     215    int Lres; 
     216    int top; 
     217    if (run_first_resume) { 
     218        run_first_resume = 0; 
     219        top = 0; 
     220    } else { 
     221        top = lua_gettop(Lt); 
     222    } 
     223    run_start_tick = get_tick_count(); 
     224    run_hook_count = 0; 
     225    Lres = lua_resume( Lt, top ); 
     226 
     227    if (Lres == LUA_YIELD) { 
     228        // yielded 
     229        return; 
     230    } else if(Lres != 0) { 
     231        lua_script_error(Lt,1); 
     232        return; 
     233    } else { 
     234        // finished normally, add ptp result 
     235        lua_script_finish(Lt); 
     236        script_console_add_line(lang_str(LANG_CONSOLE_TEXT_FINISHED)); 
     237        action_pop(); 
     238        script_end(); 
     239    } 
    196240} 
    197241 
     
    19622006} 
    19632007 
     2008/* 
     2009set scheduling parameters 
     2010old_max_count,old_max_ms=set_yield(max_count,max_ms) 
     2011*/ 
     2012static int luaCB_set_yield( lua_State* L ) 
     2013{ 
     2014  lua_pushnumber(L,yield_max_count); 
     2015  lua_pushnumber(L,yield_max_ms); 
     2016  yield_max_count = luaL_optnumber(L,1,YIELD_MAX_COUNT_DEFAULT); 
     2017  yield_max_ms = luaL_optnumber(L,2,YIELD_MAX_MS_DEFAULT); 
     2018  return 2; 
     2019} 
     2020 
     2021 
    19642022static void register_func( lua_State* L, const char *name, void *func) { 
    19652023  lua_pushcfunction( L, func ); 
     
    21302188   FUNC(file_browser) 
    21312189 
     2190   FUNC(set_yield) 
     2191 
    21322192  {NULL, NULL}, 
    21332193}; 
  • trunk/core/luascript.h

    r1101 r1433  
    55 
    66void lua_script_reset(); 
    7 int lua_script_start( char const* script,int is_ptp ); 
     7int lua_script_start( char const* script,int is_ptp ); // initialize and load script 
     8void lua_script_run( void ); // run script timeslice 
    89void lua_script_error( lua_State* L,int runtime ); 
    910void lua_script_finish( lua_State* L ); 
  • trunk/core/script.c

    r1396 r1433  
    8282static int script_loaded_params[SCRIPT_NUM_PARAMS]; 
    8383static long running_script_stack_name = -1; 
    84  
    85 #ifdef OPT_LUA 
    86 static int state_lua_kbd_first_call_to_resume;  // AUJ 
    87 #endif 
    8884 
    8985//------------------------------------------------------------------- 
     
    476472static void process_script() 
    477473{   // Note: This function is called from an action stack for AS_SCRIPT_RUN. 
    478      
    479     long t; 
    480     int Lres; 
    481474 
    482475    if (state_kbd_script_run != 3) { 
    483476#ifdef OPT_LUA 
    484477        if( L ) { 
    485             int top; 
    486             if (state_lua_kbd_first_call_to_resume) { 
    487                 state_lua_kbd_first_call_to_resume = 0; 
    488                 top = 0; 
    489             } else { 
    490                 top = lua_gettop(Lt); 
    491             } 
    492             Lres = lua_resume( Lt, top ); 
    493  
    494             if (Lres != LUA_YIELD && Lres != 0) { 
    495                 lua_script_error(Lt,1); 
    496                 return; 
    497             } 
    498  
    499             if (Lres != LUA_YIELD) { 
    500                 // add ptp result 
    501                 lua_script_finish(Lt); 
    502                 script_console_add_line(lang_str(LANG_CONSOLE_TEXT_FINISHED)); 
    503                 action_pop(); 
    504                 script_end(); 
    505             }     
     478            lua_script_run(); 
    506479        } else 
    507480#endif 
     
    682655            } 
    683656        } 
    684         state_lua_kbd_first_call_to_resume = 1; 
    685657#else 
    686658        char msg[64]; 
     
    716688{ 
    717689  if (!lua_script_start(script,1)) return -1; 
    718   state_lua_kbd_first_call_to_resume = 1; 
    719690  state_kbd_script_run = 1; 
    720691  kbd_set_block(1); 
Note: See TracChangeset for help on using the changeset viewer.