From f97c73bd1e43a0eb32ad8dc43fc28f6e40b28f38 Mon Sep 17 00:00:00 2001 From: David Walter Seikel Date: Fri, 23 May 2014 13:22:36 +1000 Subject: Rewrite the LuaSL script running stuff (twice lol), plus much related tweakage and cleanups. --- src/GuiLua/build.lua | 1 - src/GuiLua/test_c.c | 1 + src/LuaSL/LuaSL.h | 31 +-- src/LuaSL/LuaSL_LSL_tree.h | 10 +- src/LuaSL/LuaSL_compile.c | 73 ++---- src/LuaSL/LuaSL_main.c | 248 ++++++++----------- src/LuaSL/LuaSL_threads.c | 511 -------------------------------------- src/LuaSL/LuaSL_threads.h | 54 ----- src/LuaSL/build.lua | 2 +- src/extantz/build.lua | 2 +- src/extantz/extantz.c | 2 +- src/libraries/LumbrJack.c | 7 +- src/libraries/Runnr.c | 593 ++++++++++++++++++++++++++++++++++++++++++--- src/libraries/Runnr.h | 70 ++++++ src/libraries/SledjHamr.c | 2 + src/love/build.lua | 1 - src/love/love.c | 20 +- 17 files changed, 773 insertions(+), 855 deletions(-) delete mode 100644 src/LuaSL/LuaSL_threads.c delete mode 100644 src/LuaSL/LuaSL_threads.h (limited to 'src') diff --git a/src/GuiLua/build.lua b/src/GuiLua/build.lua index 8b4a1c8..08446f0 100755 --- a/src/GuiLua/build.lua +++ b/src/GuiLua/build.lua @@ -18,7 +18,6 @@ LDFLAGS = '-L ' .. dir .. ' ' .. LDFLAGS removeFiles(dir, {'test_c.so', 'GuiLua.o', lib_d .. '/libGuiLua.so', '../../skang'}) runCommand('C modules', dir, 'gcc ' .. CFLAGS .. ' -fPIC -shared -o test_c.so test_c.c') - runCommand('C libraries', dir, 'gcc ' .. CFLAGS .. ' -fPIC -c GuiLua.c') runCommand(nil, dir, 'gcc ' .. CFLAGS .. ' -shared -Wl,-soname,libGuiLua.so -o ' .. lib_d .. '/libGuiLua.so GuiLua.o') runCommand('C apps', dir, 'gcc ' .. CFLAGS .. ' -Wl,-export-dynamic -o ../../skang skang.c ' .. LDFLAGS .. ' -lGuiLua -lwinFang ' .. libs) diff --git a/src/GuiLua/test_c.c b/src/GuiLua/test_c.c index ed680b0..f964d65 100644 --- a/src/GuiLua/test_c.c +++ b/src/GuiLua/test_c.c @@ -10,6 +10,7 @@ http://lua-users.org/lists/lua-l/2008-01/msg00671.html */ +#include "LumbrJack.h" #include "Runnr.h" #include "GuiLua.h" diff --git a/src/LuaSL/LuaSL.h b/src/LuaSL/LuaSL.h index 299477b..cfee0aa 100644 --- a/src/LuaSL/LuaSL.h +++ b/src/LuaSL/LuaSL.h @@ -16,46 +16,19 @@ #include #include -typedef struct _script script; // Define this here, so LuaSL_threads.h can use it. typedef struct _gameGlobals gameGlobals; // Define this here, so LuaSL_threads.h can use it. -#include "LuaSL_threads.h" #include "LumbrJack.h" +#include "Runnr.h" struct _gameGlobals { Ecore_Con_Server *server; - Eina_Hash *scripts, *names; + Eina_Hash *names; const char *address; int port; }; -struct _script -{ - Eina_Clist node; - gameGlobals *game; - char SID[PATH_MAX]; - char fileName[PATH_MAX]; - char *name; - lua_State *L; - struct timeval startTime; - float timerTime; - int status; - int args; - Eina_Clist messages; - Ecore_Con_Client *client; - Ecore_Timer *timer; -}; - -typedef struct -{ - Eina_Clist node; - script *script; - const char message[PATH_MAX]; -} scriptMessage; - - -void scriptSendBack(void * data); #include "LuaSL_LSL_tree.h" diff --git a/src/LuaSL/LuaSL_LSL_tree.h b/src/LuaSL/LuaSL_LSL_tree.h index 1021be8..fca31db 100644 --- a/src/LuaSL/LuaSL_LSL_tree.h +++ b/src/LuaSL/LuaSL_LSL_tree.h @@ -55,14 +55,6 @@ typedef enum typedef void (*outputToken) (FILE *file, outputMode mode, LSL_Leaf *content); -//#ifndef FALSE -//typedef enum -//{ -// FALSE = 0, -// TRUE = 1 -//} boolean; -//#endif - typedef enum { LSL_NONE = 0, @@ -409,7 +401,7 @@ typedef struct boolean compilerSetup(gameGlobals *ourGlobals); -boolean compileLSL(gameGlobals *ourGlobals, Ecore_Con_Client *client, char *SID, char *script, boolean doConstants); +boolean compileLSL(LuaCompiler *lCompiler, gameGlobals *ourGlobals, Ecore_Con_Client *client, char *SID, char *script, boolean doConstants); void burnLeaf(void *data); LSL_Leaf *addBlock(LuaSL_compiler *compiler, LSL_Leaf *left, LSL_Leaf *lval, LSL_Leaf *right); diff --git a/src/LuaSL/LuaSL_compile.c b/src/LuaSL/LuaSL_compile.c index afe7ec0..61fce8f 100644 --- a/src/LuaSL/LuaSL_compile.c +++ b/src/LuaSL/LuaSL_compile.c @@ -2170,6 +2170,14 @@ static void outputStringToken(FILE *file, outputMode mode, LSL_Leaf *content) fprintf(file, "%s", content->value.stringValue); // The quotes are part of the string value already. } +static void _compileCb(LuaCompiler *compiler) +{ + free(compiler->luaName); + free(compiler->SID); + free(compiler->file); + free(compiler); +} + boolean compilerSetup(gameGlobals *ourGlobals) { int i; @@ -2184,6 +2192,7 @@ boolean compilerSetup(gameGlobals *ourGlobals) if (tokens) { char buf[PATH_MAX]; + LuaCompiler *compiler = calloc(1, sizeof(LuaCompiler)); // Sort the token table. for (i = 0; LSL_Tokens[i].toKen != NULL; i++) @@ -2197,7 +2206,12 @@ boolean compilerSetup(gameGlobals *ourGlobals) snprintf(buf, sizeof(buf), "lua -e 'require(\"LSL\").gimmeLSL()' > %s/constants.lsl", prefix_lib_get()); system(buf); snprintf(buf, sizeof(buf), "%s/constants.lsl", prefix_lib_get()); - compileLSL(ourGlobals, NULL, "FAKE_SID", buf, TRUE); + compiler->file = strdup(buf); + compiler->SID = strdup("FAKE_SID"); + compiler->data = ourGlobals; + compiler->cb = _compileCb; + if (!compileLSL(compiler, ourGlobals, NULL, "FAKE_SID", buf, TRUE)) + _compileCb(compiler); return TRUE; } @@ -2207,17 +2221,7 @@ boolean compilerSetup(gameGlobals *ourGlobals) return FALSE; } -static int luaWriter(lua_State *L, const void* p, size_t sz, void* ud) -{ - FILE *out = ud; - int result = 0; - - if (sz != fwrite(p, 1, sz, out)) - result = -1; - return result; -} - -boolean compileLSL(gameGlobals *ourGlobals, Ecore_Con_Client *client, char *SID, char *script, boolean doConstants) +boolean compileLSL(LuaCompiler *lCompiler, gameGlobals *ourGlobals, Ecore_Con_Client *client, char *SID, char *script, boolean doConstants) { boolean result = FALSE; LuaSL_compiler compiler; @@ -2348,8 +2352,6 @@ boolean compileLSL(gameGlobals *ourGlobals, Ecore_Con_Client *client, char *SID, // Generate the Lua source code. if (out) { - lua_State *L; - int err; char *ext; fprintf(out, "--// Generated code goes here.\n\n"); @@ -2366,49 +2368,14 @@ boolean compileLSL(gameGlobals *ourGlobals, Ecore_Con_Client *client, char *SID, fclose(out); // Compile the Lua source code. - if ((L = luaL_newstate())) - { - luaL_openlibs(L); - // This ends up pushing a function onto the stack. The function is the compiled code. - err = luaL_loadfile(L, luaName); - if (err) - { - compiler.script.bugCount++; - if (LUA_ERRSYNTAX == err) - PE("Lua syntax error in %s: %s", luaName, lua_tostring(L, -1)); - else if (LUA_ERRFILE == err) - PE("Lua compile file error in %s: %s", luaName, lua_tostring(L, -1)); - else if (LUA_ERRMEM == err) - PE("Lua compile memory allocation error in %s: %s", luaName, lua_tostring(L, -1)); - } - else - { - // Write the compiled code to a file. - strcat(luaName, ".out"); - out = fopen(luaName, "w"); - if (out) - { - err = lua_dump(L, luaWriter, out); - if (err) - { - compiler.script.bugCount++; - PE("Lua compile file error writing to %s", luaName); - } - fclose(out); - } - else - PC("Unable to open file %s for writing!", luaName); - } - } - else - PE("Can't create a new Lua state!"); + lCompiler->luaName = strdup(luaName); + lCompiler->bugCount = compiler.script.bugCount; + compileScript(lCompiler); + result = TRUE; } else PC("Unable to open file %s for writing!", luaName); } - - if (0 == compiler.script.bugCount) - result = TRUE; } if (NULL != compiler.file) diff --git a/src/LuaSL/LuaSL_main.c b/src/LuaSL/LuaSL_main.c index 2c40348..e60278f 100644 --- a/src/LuaSL/LuaSL_main.c +++ b/src/LuaSL/LuaSL_main.c @@ -1,31 +1,27 @@ #include "LuaSL.h" -#include "Runnr.h" #include "SledjHamr.h" int logDom = -1; // Our logging domain. -static int CPUs = 4; static Eina_Strbuf *clientStream; static Eina_Bool _sleep_timer_cb(void *data) { script *script = data; - gameGlobals *ourGlobals = script->game; // PD("Waking up %s", script->name); - sendToChannel(ourGlobals, script->SID, "return 0.0"); + send2script(script->SID, "return 0.0"); return ECORE_CALLBACK_CANCEL; } static Eina_Bool _timer_timer_cb(void *data) { script *script = data; - gameGlobals *ourGlobals = script->game; - PD("Timer for %s", script->name); - sendToChannel(ourGlobals, script->SID, "events.timer()"); +// PD("Timer for %s", script->name); + send2script(script->SID, "events.timer()"); return ECORE_CALLBACK_RENEW; } @@ -44,88 +40,63 @@ static script *findThem(gameGlobals *ourGlobals, const char *base, const char *t return eina_hash_find(ourGlobals->names, name); } -static void resetScript(script *victim) +void send2server(script *me, const char *message) { - gameGlobals *ourGlobals = victim->game; - script *me; - char buf[PATH_MAX]; - -// PD("RESETTING %s", victim->name); - sendToChannel(ourGlobals, victim->SID, "quit()"); - - eina_hash_del(ourGlobals->scripts, victim->SID, NULL); - eina_hash_del(ourGlobals->names, victim->fileName, NULL); - - // The old one will eventually die, create a new one. - me = calloc(1, sizeof(script)); - gettimeofday(&me->startTime, NULL); - strncpy(me->SID, victim->SID, sizeof(me->SID)); - strncpy(me->fileName, victim->fileName, sizeof(me->fileName)); - me->name = &me->fileName[strlen(prefix_data_get())]; - me->game = ourGlobals; - me->client = victim->client; - eina_hash_add(ourGlobals->scripts, me->SID, me); - eina_hash_add(ourGlobals->names, me->fileName, me); - sprintf(buf, "%s.lua.out", me->fileName); - newProc(buf, TRUE, me); -} - -void scriptSendBack(void * data) -{ - scriptMessage *message = data; - gameGlobals *ourGlobals = message->script->game; + gameGlobals *ourGlobals = me->data; - if (!message->script) - { - PE("scriptSendBack script is NULL"); - return; - } +//printf("GOT MESSAGE from script %s - '%s'\n", me->name, message); - if (0 == strncmp(message->message, "llSleep(", 8)) - ecore_timer_add(atof(&(message->message)[8]), _sleep_timer_cb, message->script); - else if (0 == strncmp(message->message, "llResetTime(", 12)) + if (0 == strncmp(message, "llSleep(", 8)) + ecore_timer_add(atof(&(message)[8]), _sleep_timer_cb, me); + else if (0 == strncmp(message, "llResetTime(", 12)) { - gettimeofday(&message->script->startTime, NULL); + takeScript(me); + gettimeofday(&me->startTime, NULL); + releaseScript(me); } - else if (0 == strncmp(message->message, "llGetTime(", 10)) + else if (0 == strncmp(message, "llGetTime(", 10)) { struct timeval now; - float time = timeDiff(&now, &message->script->startTime); + float time = timeDiff(&now, &me->startTime); char result[128]; snprintf(result, sizeof(result), "return %f", time); - sendToChannel(ourGlobals, message->script->SID, result); + send2script(me->SID, result); } - else if (0 == strncmp(message->message, "llGetAndResetTime(", 18)) + else if (0 == strncmp(message, "llGetAndResetTime(", 18)) { struct timeval now; - float time = timeDiff(&now, &message->script->startTime); + float time = timeDiff(&now, &me->startTime); char result[128]; + takeScript(me); // Reset it before doing anything else once the result is known. - gettimeofday(&message->script->startTime, NULL); + gettimeofday(&me->startTime, NULL); + releaseScript(me); snprintf(result, sizeof(result), "return %f", time); - sendToChannel(ourGlobals, message->script->SID, result); + send2script(me->SID, result); } - else if (0 == strncmp(message->message, "llSetTimerEvent(", 16)) + else if (0 == strncmp(message, "llSetTimerEvent(", 16)) { - message->script->timerTime = atof(&(message->message)[16]); - if (0.0 == message->script->timerTime) + takeScript(me); + me->timerTime = atof(&(message)[16]); + if (0.0 == me->timerTime) { - if (message->script->timer) - ecore_timer_del(message->script->timer); - message->script->timer = NULL; + if (me->timer) + ecore_timer_del(me->timer); + me->timer = NULL; } else - message->script->timer = ecore_timer_add(message->script->timerTime, _timer_timer_cb, message->script); + me->timer = ecore_timer_add(me->timerTime, _timer_timer_cb, me); + releaseScript(me); } - else if (0 == strncmp(message->message, "llSetScriptState(", 17)) + else if (0 == strncmp(message, "llSetScriptState(", 17)) { script *them; - if ((them = findThem(ourGlobals, message->script->fileName, &(message->message[18])))) + if ((them = findThem(ourGlobals, me->fileName, &(message[18])))) { - char *temp = rindex(&(message->message[18]), ','); + char *temp = rindex(&(message[18]), ','); if (temp) { @@ -133,9 +104,9 @@ void scriptSendBack(void * data) while (isspace(*temp)) temp++; if ('1' == *temp) - sendToChannel(ourGlobals, them->SID, "start()"); + send2script(them->SID, "start()"); else - sendToChannel(ourGlobals, them->SID, "stop()"); + send2script(them->SID, "stop()"); // PD("Stopped %s", them->name); } else @@ -143,25 +114,34 @@ void scriptSendBack(void * data) } else { - char *temp = rindex(&(message->message[18]), '"'); + char *temp = rindex(&(message[18]), '"'); if (temp) *temp = '\0'; - PE("Can't stop script, can't find %s", &(message->message[18])); + PE("Can't stop script, can't find %s", &(message[18])); } } - else if (0 == strncmp(message->message, "llResetOtherScript(", 19)) + else if (0 == strncmp(message, "llResetOtherScript(", 19)) { script *them; - if ((them = findThem(ourGlobals, message->script->fileName, &(message->message[20])))) + if ((them = findThem(ourGlobals, me->fileName, &(message[20])))) + { + PD("RESETTING OTHER %s", them->name); resetScript(them); + } + } + else if (0 == strncmp(message, "llResetScript(", 14)) + { + PD("RESETTING %s", me->name); + resetScript(me); } - else if (0 == strncmp(message->message, "llResetScript(", 14)) - resetScript(message->script); else - sendBack(message->script->client, message->script->SID, message->message); - free(message); + { + takeScript(me); + sendBack(me->client, me->SID, message); + releaseScript(me); + } } static Eina_Bool _add(void *data, int type __UNUSED__, Ecore_Con_Event_Client_Add *ev) @@ -170,6 +150,25 @@ static Eina_Bool _add(void *data, int type __UNUSED__, Ecore_Con_Event_Client_Ad return ECORE_CALLBACK_RENEW; } +static void _compileCb(LuaCompiler *compiler) +{ + if (0 == compiler->bugCount) + { + gameGlobals *ourGlobals = compiler->data; + script *me = scriptAdd(compiler->file, compiler->SID, send2server, ourGlobals); + + me->client = compiler->client; + eina_hash_add(ourGlobals->names, me->fileName, me); + sendBack(compiler->client, compiler->SID, "compiled(true)"); + } + else + sendBack(compiler->client, compiler->SID, "compiled(false)"); + free(compiler->luaName); + free(compiler->SID); + free(compiler->file); + free(compiler); +} + static Eina_Bool _data(void *data, int type __UNUSED__, Ecore_Con_Event_Client_Data *ev) { gameGlobals *ourGlobals = data; @@ -197,6 +196,7 @@ static Eina_Bool _data(void *data, int type __UNUSED__, Ecore_Con_Event_Client_D char *temp; char *file; char *name; + LuaCompiler *compiler = calloc(1, sizeof(LuaCompiler)); strcpy(buf, &command[8]); temp = buf; @@ -207,34 +207,27 @@ static Eina_Bool _data(void *data, int type __UNUSED__, Ecore_Con_Event_Client_D name = &file[strlen(prefix_data_get())]; PD("Compiling %s, %s.", SID, name); - if (compileLSL(ourGlobals, ev->client, SID, file, FALSE)) + compiler->file = strdup(file); + compiler->SID = strdup(SID); + compiler->client = ev->client; + compiler->data = ourGlobals; + compiler->cb = _compileCb; + if (!compileLSL(compiler, ourGlobals, ev->client, SID, file, FALSE)) { - script *me = calloc(1, sizeof(script)); - - gettimeofday(&me->startTime, NULL); - strncpy(me->SID, SID, sizeof(me->SID)); - strncpy(me->fileName, file, sizeof(me->fileName)); - me->name = &me->fileName[strlen(prefix_data_get())]; - me->game = ourGlobals; - me->client = ev->client; - eina_hash_add(ourGlobals->scripts, me->SID, me); - eina_hash_add(ourGlobals->names, me->fileName, me); - sendBack(ev->client, SID, "compiled(true)"); + compiler->bugCount++; + PE("Compile of %s failed in a mysterious way.", file); + _compileCb(compiler); } - else - sendBack(ev->client, SID, "compiled(false)"); } else if (0 == strcmp(command, "run()")) { script *me; - char buf[PATH_MAX]; - me = eina_hash_find(ourGlobals->scripts, SID); + me = getScript(SID); if (me) { - sprintf(buf, "%s.lua.out", me->fileName); - gettimeofday(&me->startTime, NULL); - newProc(buf, TRUE, me); + runScript(me); + releaseScript(me); } } else if (0 == strcmp(command, "exit()")) @@ -243,13 +236,7 @@ static Eina_Bool _data(void *data, int type __UNUSED__, Ecore_Con_Event_Client_D ecore_main_loop_quit(); } else - { - const char *status = NULL; - - status = sendToChannel(ourGlobals, SID, command); - if (status) - PE("Error sending command %s to script %s : %s", command, SID, status); - } + send2script(SID, command); } // Get the next blob to check it. @@ -272,6 +259,12 @@ static Eina_Bool _del(void *data, int type __UNUSED__, Ecore_Con_Event_Client_De return ECORE_CALLBACK_RENEW; } +//static Eina_Bool _statusTimer(void *data) +//{ +// printScriptsStatus(); +// return ECORE_CALLBACK_RENEW; +//} + int main(int argc, char **argv) { gameGlobals ourGlobals; @@ -284,7 +277,6 @@ int main(int argc, char **argv) if (eina_init()) { logDom = HamrTime(argv[0], main, logDom); - ourGlobals.scripts = eina_hash_string_superfast_new(NULL); ourGlobals.names = eina_hash_string_superfast_new(NULL); if (ecore_init()) { @@ -292,7 +284,7 @@ int main(int argc, char **argv) { if ((ourGlobals.server = ecore_con_server_add(ECORE_CON_REMOTE_TCP, ourGlobals.address, ourGlobals.port, &ourGlobals))) { - int i; +// int i; Eina_Iterator *scripts; script *me; @@ -307,29 +299,20 @@ int main(int argc, char **argv) result = 0; compilerSetup(&ourGlobals); - luaprocInit(); - for (i = 0; i < CPUs; i++) - { - if ( sched_create_worker( ) != LUAPROC_SCHED_OK ) - PE("Error creating luaproc worker thread."); - } + +// ecore_timer_add(3.0, _statusTimer, &ourGlobals); + ecore_main_loop_begin(); PD("Fell out of the main loop."); - scripts = eina_hash_iterator_data_new(ourGlobals.scripts); + scripts = eina_hash_iterator_data_new(ourGlobals.names); while(eina_iterator_next(scripts, (void **) &me)) { - const char *status = NULL; - - status = sendToChannel(&ourGlobals, me->SID, "quit()"); - if (status) - PE("Error sending command quit() to script %s : %s", me->SID, status); + if (me->SID[0]) + send2script(me->SID, "quit()"); } PD("Finished quitting scripts."); - // TODO - This is what hangs the system, should change from raw pthreads to ecore threads. - // Or perhaps just run the main loop for a bit longer so all the scripts can quit? - sched_join_workerthreads(); } else PC("Failed to add server!"); @@ -424,33 +407,6 @@ _elua_custom_panic(lua_State *L) // Stack usage [-0, +0, m] return 0; } -static void -_edje_lua2_error_full(const char *file, const char *fnc, int line, - lua_State *L, int err_code) // Stack usage [-0, +0, m] -{ - const char *err_type; - - switch (err_code) - { - case LUA_ERRRUN: - err_type = "runtime"; - break; - case LUA_ERRSYNTAX: - err_type = "syntax"; - break; - case LUA_ERRMEM: - err_type = "memory allocation"; - break; - case LUA_ERRERR: - err_type = "error handler"; - break; - default: - err_type = "unknown"; - break; - } - printf("Lua %s error: %s\n", err_type, lua_tostring(L, -1)); // Stack usage [-0, +0, m] -} - static int errFunc(lua_State *L) { int i = 0; @@ -510,7 +466,8 @@ void runLuaFile(gameGlobals *ourGlobals, const char *filename) * Watchdog thread. * * It's easy enough to have a watchdog thread wake up every now and then to check if any given Lua state has been hogging it's CPU for too long. - * The hard part is forcing Lua states to yield cleanly, without slowing performance too much. + * The hard part is forcing Lua states to yield cleanly, without slowing performance too much. On the other hand, it might be valid to just + * kill CPU hogs and tell the user. * * Identifying scripts. - OpenSim/Region/ScriptEngine/Interfaces/IScriptInstance.cs * @@ -650,7 +607,8 @@ void runLuaFile(gameGlobals *ourGlobals, const char *filename) * Oops, names can have directory slashes in them. lol * On the other hand, sim objects CAN have the same name. * - * So we got sim directories, with an objects directory inside it, with object directories inside that. The object directories have object files in them. This is all like the test setup that is here. + * So we got sim directories, with an objects directory inside it, with object directories inside that. The object directories have object files in them. + * This is all like the test setup that is here. * We need metadata. Sim metadata, object metadata, and object contents metadata. That can be done with a "foo.omg" file at each level. * sim/index.omg - the list of object name.UUIDs, their X,Y,Z location, size, and rotation. * sim/objects/objectName_UUID/index.omg - the list of contents names, item UUIDs, asset UUIDs, and types. @@ -763,7 +721,7 @@ void runLuaFile(gameGlobals *ourGlobals, const char *filename) * * Serialise the script state, send it somewhere. * - * Lua can generally serialise itself as as a string of code to be executed at the destination. There might be some C side state that needs to be take care of as well. We shall see. + * Lua can generally serialise itself as a string of code to be executed at the destination. There might be some C side state that needs to be take care of as well. We shall see. * * Email, HTTP, XML-RPC? * diff --git a/src/LuaSL/LuaSL_threads.c b/src/LuaSL/LuaSL_threads.c deleted file mode 100644 index 4746808..0000000 --- a/src/LuaSL/LuaSL_threads.c +++ /dev/null @@ -1,511 +0,0 @@ -/* This code is heavily based on luaproc. - * - * The luaproc copyright notice and license is - - - *************************************************** - -Copyright 2008 Alexandre Skyrme, Noemi Rodriguez, Roberto Ierusalimschy - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. - - **************************************************** - * - * Additions and changes Copyright 2012 by David Seikel, using the above license. - */ - - -/* This is a redesign of luaproc. The design goals and notes - - * - * In general use EFL where it is useful. - * Probably one fixed unique message channel per object, which each script in the object shares. - * But might be better to handle that C side anyway. - * Better integration with LuaSL. - * Use ecore threads instead of raw pthreads. - * Ecore threads pretty much wraps pthreads on posix, but has Windows support to. - * Merge in the edje Lua code, and keep an eye on that, coz we might want to actually add this to edje Lua in the future. - * Use my coding standards, or EFL ones. Pffft. - * - */ - -#include "LuaSL.h" - - -/* ready process queue insertion status */ -#define LUAPROC_SCHED_QUEUE_PROC_OK 0 -//#define LUAPROC_SCHED_QUEUE_PROC_ERR -1 - -/* process is idle */ -#define LUAPROC_STAT_IDLE 0 -/* process is ready to run */ -#define LUAPROC_STAT_READY 1 -/* process is blocked on send */ -#define LUAPROC_STAT_BLOCKED_SEND 2 -/* process is blocked on receive */ -#define LUAPROC_STAT_BLOCKED_RECV 3 - - -typedef struct -{ - Eina_Clist node; - lua_State *L; -} recycled; - -/********* -* globals -*********/ - -/* ready process list */ -Eina_Clist lpready; - -/* ready process queue access mutex */ -pthread_mutex_t mutex_queue_access = PTHREAD_MUTEX_INITIALIZER; - -/* wake worker up conditional variable */ -pthread_cond_t cond_wakeup_worker = PTHREAD_COND_INITIALIZER; - -/* active luaproc count access mutex */ -pthread_mutex_t mutex_lp_count = PTHREAD_MUTEX_INITIALIZER; - -/* no active luaproc conditional variable */ -pthread_cond_t cond_no_active_lp = PTHREAD_COND_INITIALIZER; - -/* number of active luaprocs */ -int lpcount = 0; - -/* no more lua processes flag */ -int no_more_processes = FALSE; - -/* channel operations mutex */ -pthread_mutex_t mutex_channel = PTHREAD_MUTEX_INITIALIZER; - -/* recycle list mutex */ -pthread_mutex_t mutex_recycle_list = PTHREAD_MUTEX_INITIALIZER; - -/* recycled lua process list */ -Eina_Clist recyclelp; - - -/****************************** -* library functions prototypes -******************************/ -/* send a message to a lua process */ -static int luaproc_send( lua_State *L ); -/* receive a message from a lua process */ -static int luaproc_receive( lua_State *L ); -/* send a message back to the main loop */ -static int luaproc_send_back( lua_State *L ); - -/* luaproc function registration array - main (parent) functions */ -static const struct luaL_reg luaproc_funcs_parent[] = { - { "sendback", luaproc_send_back }, - { NULL, NULL } -}; - -/* luaproc function registration array - newproc (child) functions */ -static const struct luaL_reg luaproc_funcs_child[] = { - { "send", luaproc_send }, - { "receive", luaproc_receive }, - { "sendback", luaproc_send_back }, - { NULL, NULL } -}; - - -/* increase active lua process count */ -static void sched_lpcount_inc(void) -{ - pthread_mutex_lock(&mutex_lp_count); - lpcount++; - pthread_mutex_unlock(&mutex_lp_count); -} - -/* decrease active lua process count */ -static void sched_lpcount_dec(void) -{ - pthread_mutex_lock(&mutex_lp_count); - lpcount--; - /* if count reaches zero, signal there are no more active processes */ - if (lpcount == 0) - pthread_cond_signal(&cond_no_active_lp); - pthread_mutex_unlock(&mutex_lp_count); -} - -/* worker thread main function */ -static void *workermain( void *args ) { - - script *lp; - int procstat; - - /* detach thread so resources are freed as soon as thread exits (no further joining) */ - pthread_detach( pthread_self( )); - - /* main worker loop */ - while ( 1 ) { - - /* get exclusive access to the ready process queue */ - pthread_mutex_lock( &mutex_queue_access ); - - /* wait until instructed to wake up (because there's work to do or because its time to finish) */ - while (( eina_clist_count( &lpready ) == 0 ) && ( no_more_processes == FALSE )) { - pthread_cond_wait( &cond_wakeup_worker, &mutex_queue_access ); - } - - /* pop the first node from the ready process queue */ - if ((lp = (script *) eina_clist_head(&lpready))) - eina_clist_remove(&(lp->node)); - else { - /* free access to the process ready queue */ - pthread_mutex_unlock( &mutex_queue_access ); - /* finished thread */ - pthread_exit( NULL ); - } - - /* free access to the process ready queue */ - pthread_mutex_unlock( &mutex_queue_access ); - - /* execute the lua code specified in the lua process struct */ - procstat = lua_resume(lp->L, lp->args); - /* reset the process argument count */ - lp->args = 0; - - /* check if process finished its whole execution, then recycle it */ - if (procstat == 0) - { -// recycled *trash = malloc(sizeof(recycled)); - - // TODO - Trash stuff trashes memory, fix it. - // Later, it's an optimization we don't need right now. -/* - if (trash) - { - trash->L = lp->L; - pthread_mutex_lock(&mutex_recycle_list); - eina_clist_add_tail(&recyclelp, &(trash->node)); - pthread_mutex_unlock(&mutex_recycle_list); - } -*/ - sched_lpcount_dec(); - lua_close(lp->L); - if (lp->timer) - ecore_timer_del(lp->timer); -// free(lp); - } - - /* check if process yielded */ - else if ( procstat == LUA_YIELD ) { - - /* if so, further check if yield originated from an unmatched send/recv operation */ - if (lp->status == LUAPROC_STAT_BLOCKED_SEND) - { - } - else if (lp->status == LUAPROC_STAT_BLOCKED_RECV) - { - } - /* or if yield resulted from an explicit call to coroutine.yield in the lua code being executed */ - else - { - /* get exclusive access to the ready process queue */ - pthread_mutex_lock( &mutex_queue_access ); - /* re-insert the job at the end of the ready process queue */ - eina_clist_add_tail(&lpready, &(lp->node)); - /* free access to the process ready queue */ - pthread_mutex_unlock( &mutex_queue_access ); - } - } - /* check if there was any execution error (LUA_ERRRUN, LUA_ERRSYNTAX, LUA_ERRMEM or LUA_ERRERR) */ - else - { - /* print error message */ - fprintf( stderr, "close lua_State (error: %s)\n", luaL_checkstring(lp->L, -1 )); - /* close lua state */ - lua_close(lp->L); - /* decrease active lua process count */ - sched_lpcount_dec(); - } - } -} - -/* move process to ready queue (ie, schedule process) */ -static int sched_queue_proc( script *lp ) { - - /* get exclusive access to the ready process queue */ - pthread_mutex_lock( &mutex_queue_access ); - - /* add process to ready queue */ - eina_clist_add_tail(&lpready, &(lp->node)); - - lp->status = LUAPROC_STAT_READY; - - /* wake worker up */ - pthread_cond_signal( &cond_wakeup_worker ); - /* free access to the process ready queue */ - pthread_mutex_unlock( &mutex_queue_access ); - - return LUAPROC_SCHED_QUEUE_PROC_OK; -} - -/* synchronize worker threads */ -void sched_join_workerthreads( void ) { - - pthread_mutex_lock( &mutex_lp_count ); - - /* wait until there is no more active lua processes */ - while( lpcount != 0 ) { - pthread_cond_wait( &cond_no_active_lp, &mutex_lp_count ); - } - /* get exclusive access to the ready process queue */ - pthread_mutex_lock( &mutex_queue_access ); - /* set the no more active lua processes flag to true */ - no_more_processes = TRUE; - /* wake ALL workers up */ - pthread_cond_broadcast( &cond_wakeup_worker ); - /* free access to the process ready queue */ - pthread_mutex_unlock( &mutex_queue_access ); - -// We don't need this, as we only get here during shutdown. Linking this to EFL results in a hang otherwise anyway. - /* wait for (join) worker threads */ -// pthread_exit( NULL ); - - pthread_mutex_unlock( &mutex_lp_count ); - -} - -/* create a new worker pthread */ -int sched_create_worker(void) -{ - pthread_t worker; - - /* create a new pthread */ - if (pthread_create( &worker, NULL, workermain, NULL ) != 0) - return LUAPROC_SCHED_PTHREAD_ERROR; - return LUAPROC_SCHED_OK; -} - -void newProc(const char *code, int file, script *lp) -{ - int ret; -// recycled *trash; - - // Try to recycle a Lua state, otherwise create one from scratch. -#if 0 // TODO - something about this causes a crash. - pthread_mutex_lock(&mutex_recycle_list); - /* pop list head */ - if ((trash = (recycled *) eina_clist_head(&recyclelp))) - { - printf(" Reusing Lua trash.\n"); - eina_clist_remove(&(trash->node)); - lp->L = trash->L; - free(trash); - } - pthread_mutex_unlock(&mutex_recycle_list); -#endif - - if (NULL == lp->L) - { - lp->L = luaL_newstate(); - - luaL_openlibs(lp->L); - luaL_register(lp->L, "luaproc", luaproc_funcs_child); - } - - /* store the script struct in its own Lua state */ - lua_pushlightuserdata(lp->L, lp); - lua_setfield(lp->L, LUA_REGISTRYINDEX, "_SELF"); - - lp->status = LUAPROC_STAT_IDLE; - lp->args = 0; - eina_clist_element_init(&(lp->node)); - eina_clist_init(&(lp->messages)); - - /* load process' code */ - if (file) - ret = luaL_loadfile(lp->L, code); - else - ret = luaL_loadstring(lp->L, code); - - /* in case of errors, destroy Lua process */ - if (ret != 0) - { - lua_close(lp->L); - lp->L = NULL; - } - - if (lp->L) - { - sched_lpcount_inc(); - - /* schedule luaproc */ - if (sched_queue_proc(lp) != LUAPROC_SCHED_QUEUE_PROC_OK) - { - printf( "[luaproc] error queueing Lua process\n" ); - sched_lpcount_dec(); - lua_close(lp->L); - } - } -} - -/* return the lua process associated with a given lua state */ -static script *luaproc_getself(lua_State *L) -{ - script *lp; - - lua_getfield(L, LUA_REGISTRYINDEX, "_SELF"); - lp = (script *) lua_touserdata(L, -1); - lua_pop(L, 1); - return lp; -} - -/* send a message to the client process */ -static int luaproc_send_back(lua_State *L) -{ - script *self = luaproc_getself(L); - const char *message = luaL_checkstring(L, 1); - - if (self) - { - scriptMessage *sm = calloc(1, sizeof(scriptMessage)); - - if (sm) - { - eina_clist_element_init(&(sm->node)); - sm->script = self; - strcpy((char *) sm->message, message); - ecore_main_loop_thread_safe_call_async(scriptSendBack, sm); - } - } - - return 0; -} - -/* error messages for the sendToChannel function */ -const char *sendToChannelErrors[] = -{ - "non-existent channel", - "error scheduling process" -}; - -/* send a message to a lua process */ -const char *sendToChannel(gameGlobals *ourGlobals, const char *SID, const char *message) -{ - const char *result = NULL; - script *dstlp; - - if (!message) - { - PE("sendToChannel NULL message to %s", SID); - return NULL; - } -// PD("sendToChannel message to %s -> %s", SID, message); - - /* get exclusive access to operate on channels */ - pthread_mutex_lock(&mutex_channel); - - // Add the message to the queue. - if ((dstlp = eina_hash_find(ourGlobals->scripts, SID))) - { - scriptMessage *sm = NULL; - - if ((sm = malloc(sizeof(scriptMessage)))) - { - sm->script = dstlp; - strcpy((char *) sm->message, message); - eina_clist_add_tail(&(dstlp->messages), &(sm->node)); - } - - /* if it's already waiting, send the next message to it and (queue) wake it */ - if (dstlp->status == LUAPROC_STAT_BLOCKED_RECV) - { - scriptMessage *msg = (scriptMessage *) eina_clist_head(&(dstlp->messages)); - - // See if there's a message on the queue. Note, this may not be the same as the incoming message, if there was already a queue. - if (msg) - { - eina_clist_remove(&(msg->node)); - message = msg->message; - } - /* push the message onto the receivers stack */ - lua_pushstring(dstlp->L, message); - dstlp->args = lua_gettop(dstlp->L) - 1; - if (msg) - free(msg); - - if (sched_queue_proc(dstlp) != LUAPROC_SCHED_QUEUE_PROC_OK) - { - sched_lpcount_dec(); - lua_close(dstlp->L); - result = sendToChannelErrors[1]; - } - } - } - - pthread_mutex_unlock(&mutex_channel); - - return result; -} - -/* send a message to a lua process */ -static int luaproc_send(lua_State *L) -{ - script *self = luaproc_getself(L); - const char *result = sendToChannel(self->game, luaL_checkstring(L, 1), luaL_checkstring(L, 2)); - - if (result) - { - lua_pushnil(L); - lua_pushstring(L, result); - return 2; - } - - lua_pushboolean(L, TRUE); - return 1; -} - -/* receive a message from a lua process */ -static int luaproc_receive(lua_State *L) -{ - script *self; - const char *chname = luaL_checkstring(L, 1); - scriptMessage *msg; - - // First check if there are queued messages, and grab one. - self = luaproc_getself(L); - if ((msg = (scriptMessage *) eina_clist_head(&(self->messages)))) - { - eina_clist_remove(&(msg->node)); - lua_pushstring(L, msg->message); - free(msg); - return lua_gettop(L) - 1; - } - - /* if trying an asynchronous receive, return an error */ - if ( lua_toboolean( L, 2 )) - { - lua_pushnil(L); - lua_pushfstring(L, "no senders waiting on channel %s", chname); - return 2; - } - /* otherwise (synchronous receive) simply block process */ - self->status = LUAPROC_STAT_BLOCKED_RECV; - return lua_yield(L, lua_gettop(L)); -} - -void luaprocInit(void) -{ - eina_clist_init(&recyclelp); - eina_clist_init(&lpready); -} diff --git a/src/LuaSL/LuaSL_threads.h b/src/LuaSL/LuaSL_threads.h deleted file mode 100644 index 9a11b5c..0000000 --- a/src/LuaSL/LuaSL_threads.h +++ /dev/null @@ -1,54 +0,0 @@ -/* This code is heavily based on luaproc. - * - * The luaproc copyright notice and license is - - - *************************************************** - -Copyright 2008 Alexandre Skyrme, Noemi Rodriguez, Roberto Ierusalimschy - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. - - **************************************************** - * - * Additions and changes Copyright 2012 by David Seikel, using the above license. - */ - -#ifndef __LUASL_THREADS_H__ -#define __LUASL_THREADS_H__ - -/* scheduler function return constants */ -#define LUAPROC_SCHED_OK 0 -#define LUAPROC_SCHED_SOCKET_ERROR -1 -#define LUAPROC_SCHED_SETSOCKOPT_ERROR -2 -#define LUAPROC_SCHED_BIND_ERROR -3 -#define LUAPROC_SCHED_LISTEN_ERROR -4 -#define LUAPROC_SCHED_FORK_ERROR -5 -#define LUAPROC_SCHED_PTHREAD_ERROR -6 -#define LUAPROC_SCHED_INIT_ERROR -7 - - -void luaprocInit(void); -int sched_create_worker(void); -void newProc(const char *code, int file, script *lp); -const char *sendToChannel(gameGlobals *ourGlobals, const char *SID, const char *message); - -/* join all worker threads and exit */ -void sched_join_workerthreads(void); - -#endif diff --git a/src/LuaSL/build.lua b/src/LuaSL/build.lua index de16c2b..0878e3d 100755 --- a/src/LuaSL/build.lua +++ b/src/LuaSL/build.lua @@ -19,4 +19,4 @@ removeFiles(dir, {'../../LuaSL', '*.o', '*.output', '*.backup', 'LuaSL_lexer.h', -- Run lemon first, flex depends on it to define the symbol values. runCommand('lemon', dir, '../../libraries/lemon/lemon -s -T../../libraries/lemon/lempar.c LuaSL_lemon_yaccer.y') runCommand('flex', dir, 'flex -C --outfile=LuaSL_lexer.c --header-file=LuaSL_lexer.h LuaSL_lexer.l') -compileFiles('../../LuaSL', dir, {'LuaSL_main', 'LuaSL_compile', 'LuaSL_threads', 'LuaSL_lexer', 'LuaSL_lemon_yaccer'}, '') +compileFiles('../../LuaSL', dir, {'LuaSL_main', 'LuaSL_compile', 'LuaSL_lexer', 'LuaSL_lemon_yaccer'}, '') diff --git a/src/extantz/build.lua b/src/extantz/build.lua index 1591e8a..99362d1 100755 --- a/src/extantz/build.lua +++ b/src/extantz/build.lua @@ -15,7 +15,7 @@ end CFLAGS = CFLAGS .. ' -I../../libraries/irrlicht-1.8.1/include -I/usr/X11R6/include -I../GuiLua' LDFLAGS = LDFLAGS .. ' -L../../libraries/irrlicht-1.8.1/lib/Linux' -libs = libs .. ' -lIrrlicht -lGL -lbz2 -lGuiLua -lwinFang -lRunnr' +libs = libs .. ' -lIrrlicht -lephysics -lGL -lbz2 -lGuiLua -lwinFang -lRunnr' removeFiles(dir, {'crappisspuke.o', 'CDemo.o', 'extantzCamera.o', 'gears.o', 'ephysics_demo.o', 'Evas_3D_demo.o', '../../media/extantz.edj'}) removeFiles(dir, {'../../extantz', 'camera.o', 'files.o', 'scenri.o', 'woMan.o'}) diff --git a/src/extantz/extantz.c b/src/extantz/extantz.c index a27118b..2ccb97c 100644 --- a/src/extantz/extantz.c +++ b/src/extantz/extantz.c @@ -669,7 +669,7 @@ EAPI_MAIN int elm_main(int argc, char **argv) ourGlobals.LSLGuiMess = GuiLuaLoad("LSLGuiMess", ourGlobals.mainWindow, ourGlobals.world); ourGlobals.files = filesAdd(&ourGlobals, (char *) prefix_data_get(), EINA_TRUE, EINA_FALSE); - // Try to connect to the love server we started before. + // Try to connect to a local love server. serverStream = eina_strbuf_new(); reachOut("127.0.0.1", 8211 + 1, &ourGlobals, (Ecore_Event_Handler_Cb) _add, (Ecore_Event_Handler_Cb) _data, (Ecore_Event_Handler_Cb) _del); diff --git a/src/libraries/LumbrJack.c b/src/libraries/LumbrJack.c index 56d4c50..27ca524 100644 --- a/src/libraries/LumbrJack.c +++ b/src/libraries/LumbrJack.c @@ -15,7 +15,7 @@ int HamrTime(char *argv0, void *main, int logDom) { Eina_Array *path; char *env, name[PATH_MAX], cwd[PATH_MAX], temp[PATH_MAX * 2]; - int i, len = strlen(argv0); + int i, len; if (!eina_init()) { @@ -30,6 +30,7 @@ int HamrTime(char *argv0, void *main, int logDom) logDom = loggingStartup(name, logDom); eina_array_free(path); + len = strlen(name); temp[len] = 0; cwd[len] = 0; for (i = 0; i < len; i++) @@ -39,9 +40,7 @@ int HamrTime(char *argv0, void *main, int logDom) } if (!(prefix = eina_prefix_new(argv0, main, temp, cwd, "checkme.txt", PACKAGE_BIN_DIR, PACKAGE_LIB_DIR, PACKAGE_DATA_DIR, PACKAGE_LOCALE_DIR))) - { PC("Can't find application prefix!"); - } PD("%s is installed in %s", name, eina_prefix_get(prefix)); PD("The binaries are in %s", eina_prefix_bin_get(prefix)); @@ -67,7 +66,7 @@ const char *prefix_get() {return eina_prefix_get(prefix);} const char *prefix_bin_get() {return eina_prefix_bin_get(prefix);} const char *prefix_data_get() {return eina_prefix_data_get(prefix);} const char *prefix_lib_get() {return eina_prefix_lib_get(prefix);} -const char *prefix_locale_get() {return eina_prefix_locale_get(prefix);} +const char *prefix_locale_get() {return eina_prefix_locale_get(prefix);} void pantsOff(int logDom) { diff --git a/src/libraries/Runnr.c b/src/libraries/Runnr.c index 72bc523..c905ce9 100644 --- a/src/libraries/Runnr.c +++ b/src/libraries/Runnr.c @@ -1,62 +1,139 @@ /* Runnr - a library that deals with running Lua scripts. - */ +#include "LumbrJack.h" #include "Runnr.h" +static Ecore_Idle_Enterer *enterer; +static Eina_Clist scripts; + +static int _send(lua_State *L); +static int _receive(lua_State *L); +static void _cancel(void *data, Ecore_Thread *thread); + + +static const struct luaL_reg runnrFunctions[] = +{ + { "send", _send }, + { "receive", _receive }, + { NULL, NULL } +}; + + void dumpStack(lua_State *L, int i) { int type = lua_type(L, i); + const char *t = lua_typename(L, type); + printf("Stack %d is %s", i, t); switch (type) { - case LUA_TNONE : printf("Stack %d is empty\n", i); break; - case LUA_TNIL : printf("Stack %d is a nil\n", i); break; - case LUA_TBOOLEAN : printf("Stack %d is a boolean - %d\n", i, lua_toboolean(L, i)); break; - case LUA_TNUMBER : printf("Stack %d is a number\n - %f", i, lua_tonumber(L, i)); break; - case LUA_TSTRING : printf("Stack %d is a string - %s\n", i, lua_tostring(L, i)); break; - case LUA_TFUNCTION : printf("Stack %d is a function\n", i); break; - case LUA_TTHREAD : printf("Stack %d is a thread\n", i); break; + case LUA_TNONE : break; + case LUA_TNIL : break; + case LUA_TBOOLEAN : printf(" - %d", lua_toboolean(L, i)); break; + case LUA_TNUMBER : printf(" - %f", lua_tonumber(L, i)); break; + case LUA_TSTRING : printf(" - %s", lua_tostring(L, i)); break; + case LUA_TFUNCTION : break; + case LUA_TTHREAD : break; case LUA_TTABLE : { int j; - printf("Stack %d is a table", i); lua_getfield(L, i, "_NAME"); j = lua_gettop(L); if (lua_isstring(L, j)) printf(" - %s", lua_tostring(L, j)); lua_pop(L, 1); - printf("\n"); break; } - case LUA_TUSERDATA : printf("Stack %d is a userdata\n", i); break; - case LUA_TLIGHTUSERDATA : printf("Stack %d is a light userdata\n", i); break; - default : printf("Stack %d is unknown\n", i); break; + case LUA_TUSERDATA : break; + case LUA_TLIGHTUSERDATA : break; + default : printf("- unknown!"); break; } + printf("\n"); } static int traceBack(lua_State *L) { - const char *msg = ""; - int top = lua_gettop(L); -// int i; + lua_Debug ar; + int i, top = lua_gettop(L), b = 1; // printf("Stack is %d deep\n", top); // for (i = 1; i <= top; i++) // dumpStack(L, i); if (top) - msg = lua_tostring(L, 1); - lua_getglobal(L, "debug"); - push_lua(L, "@ ( )", lua_gettop(L), "traceback", 1); - lua_pushstring(L, "\n"); - lua_pushstring(L, msg); - lua_concat(L, 3); - - return 1; + printf("Lua error - %s\n", lua_tostring(L, 1)); + + i = 0; + while (lua_getstack(L, i++, &ar)) + { + if (lua_getinfo(L, "nSlu", &ar)) + { + if (NULL == ar.name) + ar.name = "DUNNO"; + printf(" Lua backtrace %d - %s %s %s @ %s : %d\n", i, ar.what, ar.namewhat, ar.name, ar.short_src, ar.currentline); + b = 0; + } + else + printf(" Failed to get trace line!\n"); + } + + if (b) + printf(" NO BACKTRACE!\n"); + + return 0; +} + +static void printLuaError(int err, char *string, lua_State *L) +{ + const char *err_type; + + switch (err) + { + case LUA_ERRRUN: err_type = "runtime"; break; + case LUA_ERRSYNTAX: err_type = "syntax"; break; + case LUA_ERRMEM: err_type = "memory allocation"; break; + case LUA_ERRERR: err_type = "error handler"; break; + default: err_type = "unknown"; break; + } + printf("Error running - %s, \n %s - %s\n", string, err_type, lua_tostring(L, -1)); +} + +static int panics = 0; +static int _panic(lua_State *L) // Stack usage [-0, +0, m] +{ + // If we somehow manage to have multiple panics, it's likely due to being out + // of memory in the following lua_tostring() call. + panics++; + if (panics) + printf("Lua PANICS!!!!!"); + else + printf("Lua PANIC!!!!!: %s", lua_tostring(L, -1)); // Stack usage [-0, +0, m] + // The docs say that this will cause an exit(EXIT_FAILURE) if we return, + // and that we we should long jump some where to avoid that. This is only + // called for things not called from a protected environment. We always + // use pcalls though, except for the library load calls. If we can't load + // the standard libraries, then perhaps a crash is the right thing. + // + // The above is not true when we deal with the threaded stuff, that uses lua_resume(). + return 0; +} + +void printScriptsStatus() +{ + int active, pending_total, pending_feedback, pending_short, available; + + active = ecore_thread_active_get(); + pending_total = ecore_thread_pending_total_get(); + pending_feedback = ecore_thread_pending_feedback_get(); + pending_short = ecore_thread_pending_get(); + available = ecore_thread_available_get(); + + printf("Scripts - active %d available %d pending short jobs %d pending feedback jobs %d pending total %d\n", + active, available, pending_short, pending_feedback, pending_total); } void doLuaString(lua_State *L, char *string, char *module) @@ -87,23 +164,452 @@ void doLuaString(lua_State *L, char *string, char *module) } } +//printf("doLuaString(%s)\n", string); if ((err = lua_pcall(L, 0, LUA_MULTRET, _T))) + printLuaError(err, string, L); + } +} + +static void _stopScript(script *s) +{ + scriptMessage *sm0, *sm1; + +//printf("^^^^^^^^^^^^^^^^^^^_stop(, %s)\n", s->name); + if (s->L) lua_close(s->L); s->L = NULL; + if (s->timer) ecore_timer_del(s->timer); s->timer = NULL; + EINA_CLIST_FOR_EACH_ENTRY_SAFE(sm0, sm1, &(s->messages), scriptMessage, node) + { + eina_clist_remove(&(sm0->node)); + free(sm0); + } + s->status = RUNNR_NOT_STARTED; +} + +static void _workerFunction(void *data, Ecore_Thread *thread) +{ + script *s = data; + scriptMessage *msg = NULL; + const char *message = NULL; + + takeScript(s); +// if (RUNNR_FINISHED == s->status) +// { +// releaseScript(s); +// return; +// } + + // The documentation is not clear on which thread is which inside and out, + // but states that at least for some they are different. + // So store the internal one as well. +#if THREADIT + s->me = thread; +#endif + + if (RUNNR_RESET == s->status) + _stopScript(s); + + if (RUNNR_READY == s->status) + { +//printf("_workerFunction() READY %s\n", s->name); + if ((msg = (scriptMessage *) eina_clist_head(&(s->messages)))) { - const char *err_type; + eina_clist_remove(&(msg->node)); + message = msg->message; + if (s->L) + s->status = RUNNR_RUNNING; + else + s->status = RUNNR_NOT_STARTED; + } + } + + if (RUNNR_NOT_STARTED == s->status) + { + int err; - switch (err) +//printf("_workerFunction() STARTING %s\n", s->name); + s->status = RUNNR_RUNNING; + s->L = luaL_newstate(); // Sets a standard allocator and panic function. + + lua_atpanic(s->L, _panic); + // TODO - Set our allocator here. + luaL_openlibs(s->L); + luaL_register(s->L, "Runnr", runnrFunctions); + + // Store the script struct in its own Lua state, + lua_pushlightuserdata(s->L, s); + lua_setfield(s->L, LUA_REGISTRYINDEX, "_SELF"); + + err = luaL_loadfile(s->L, s->binName); + if (err != 0) + s->status = RUNNR_FINISHED; + gettimeofday(&s->startTime, NULL); + } + + releaseScript(s); + + if (RUNNR_RUNNING == s->status) + { + int stat; + +//printf("_workerFunction() RUNNING %s %s\n", s->name, message); + // Resume running the script. + // lua_resume() needs a Lua thread, and the initial Lua state is a thread. + // Other Lua threads have their own state, but share the environment with the initial state. + // In theory lua_resume() is a pcall. + if (message) + lua_pushstring(s->L, message); + stat = lua_resume(s->L, (message) ? 1 : 0); + free(msg); + + takeScript(s); + // If the script finished. + if (stat == 0) + s->status = RUNNR_FINISHED; + else if (stat != LUA_YIELD) + { + printf("lua_resume error at %s\n", s->name); + printLuaError(stat, s->name, s->L); + s->status = RUNNR_FINISHED; + } + else + { + if (eina_clist_count(&s->messages) == 0) + s->status = RUNNR_WAIT; + else + s->status = RUNNR_READY; + } + releaseScript(s); + } + + takeScript(s); + // Start again from the top when Ecore_Thread has a spare thread ready, unless the script finished. + if (RUNNR_FINISHED == s->status) + { +//printf("_workerFunction() FINISHED %s\n", s->name); +#if THREADIT + ecore_thread_cancel(thread); +#else + _cancel(s, NULL); +#endif + } + else if (RUNNR_WAIT == s->status) + { +;//printf("_workerFunction() WAIT %s\n", s->name); + } +#if THREADIT + else if (RUNNR_READY == s->status) + ecore_thread_reschedule(thread); +#endif + releaseScript(s); +} + +static void _notify(void *data, Ecore_Thread *thread, void *message) +{ + script *s = data; + + if (s->send2server) s->send2server(s, message); + free(message); +} + +static void _cancel(void *data, Ecore_Thread *thread) +{ + script *s = data; + + takeScript(s); + _stopScript(s); + s->status = RUNNR_FINISHED; + eina_clist_remove(&(s->node)); + if (s->SID[0]) ecore_thread_global_data_del(s->SID); s->SID[0] = 0; +//printf("^^^^^^^^^^^^^^^^^^^_del(, %s)\n", s->name); + // TODO - Perhaps have our own deletion callback to pass back? + releaseScript(s); +#if THREADIT + eina_lock_free(&s->mutex); +#endif + free(s); +} + +#if THREADIT +static void _end(void *data, Ecore_Thread *thread) +{ +} +#endif + +static Eina_Bool _enterer(void *data) +{ + script *s, *s1; + + EINA_CLIST_FOR_EACH_ENTRY_SAFE(s, s1, &(scripts), script, node) + { + takeScript(s); + if ((RUNNR_WAIT == s->status) && (eina_clist_count(&s->messages))) + { + s->status = RUNNR_READY; +#if THREADIT + ecore_thread_feedback_run(_workerFunction, _notify, _end, _cancel, s, EINA_FALSE); +#else + _workerFunction(s, NULL); +#endif + } + if ((RUNNR_RESET == s->status) || (RUNNR_READY == s->status) || (RUNNR_RUNNING == s->status) || (RUNNR_NOT_STARTED == s->status)) + { +#if THREADIT + ecore_thread_feedback_run(_workerFunction, _notify, _end, _cancel, s, EINA_FALSE); +#else + _workerFunction(s, NULL); +#endif + } + releaseScript(s); + } + + return ECORE_CALLBACK_RENEW; +} + +script *scriptAdd(char *file, char *SID, RunnrServerCb send2server, void *data) +{ + script *result; + + if (!enterer) + { + eina_clist_init(&(scripts)); + enterer = ecore_idler_add(_enterer, NULL); + } + + result = calloc(1, sizeof(script)); + gettimeofday(&result->startTime, NULL); + strcpy(result->SID, SID); + result->status = RUNNR_NOT_STARTED; + result->data = data; + result->send2server = send2server; + + strncpy(result->fileName, file, sizeof(result->fileName)); + result->name = &result->fileName[strlen(prefix_data_get())]; + sprintf(result->binName, "%s.lua.out", result->fileName); + +#if THREADIT + eina_lock_new(&result->mutex); +#endif + eina_clist_init(&(result->messages)); + ecore_thread_global_data_add(result->SID, result, NULL, EINA_FALSE); + + eina_clist_add_tail(&(scripts), &(result->node)); + + return result; +} + +static int luaWriter(lua_State *L, const void* p, size_t sz, void* ud) +{ + FILE *out = ud; + int result = 0; + + if (sz != fwrite(p, 1, sz, out)) + result = -1; + return result; +} + +// TODO - This didn't help the compile time much, perhaps move the rest of the compiling stage into this thread as a callback? +static void _compileNotify(void *data, Ecore_Thread *thread, void *message) +{ + LuaCompiler *compiler = data; + + if (compiler->cb) compiler->cb(compiler); +} + +static void _compileThread(void *data, Ecore_Thread *thread) +{ + LuaCompiler *compiler = data; + char name[PATH_MAX]; + lua_State *L; + FILE *out; + int err; + + strcpy(name, compiler->luaName); + if ((L = luaL_newstate())) + { + luaL_openlibs(L); + // This ends up pushing a function onto the stack. The function is the compiled code. + err = luaL_loadfile(L, name); + if (err) + { + compiler->bugCount++; + if (LUA_ERRSYNTAX == err) + printf("Lua syntax error in %s: %s\n", name, lua_tostring(L, -1)); + else if (LUA_ERRFILE == err) + printf("Lua compile file error in %s: %s\n", name, lua_tostring(L, -1)); + else if (LUA_ERRMEM == err) + printf("Lua compile memory allocation error in %s: %s\n", name, lua_tostring(L, -1)); + } + else + { + // Write the compiled code to a file. + strcat(name, ".out"); + out = fopen(name, "w"); + if (out) + { + err = lua_dump(L, luaWriter, out); + if (err) + { + compiler->bugCount++; + printf("Lua compile file error writing to %s\n", name); + } + fclose(out); + } + else + { + compiler->bugCount++; + printf("CRITICAL! Unable to open file %s for writing!\n", name); + } + } + } + else + { + compiler->bugCount++; + printf("Can't create a new Lua state!\n"); + } + + ecore_thread_feedback(thread, compiler); +} + +void compileScript(LuaCompiler *compiler) +{ + ecore_thread_feedback_run(_compileThread, _compileNotify, NULL, NULL, compiler, EINA_FALSE); +} + +// Assumes the scripts mutex is taken already. +void runScript(script *s) +{ +#if THREADIT + if ((RUNNR_NOT_STARTED == s->status) || (RUNNR_FINISHED == s->status)) +#endif + { +#if THREADIT + ecore_thread_feedback_run(_workerFunction, _notify, _end, _cancel, s, EINA_FALSE); +#elsif + _workerFunction(s, NULL); +#endif + } +} + +void resetScript(script *s) +{ + takeScript(s); + s->status = RUNNR_RESET; + releaseScript(s); +} + +script *getScript(char *SID) +{ + script *result = ecore_thread_global_data_find(SID); + + if (result) + { + takeScript(result); + if (RUNNR_FINISHED == result->status) + { + releaseScript(result); + result = NULL; + } + } + return result; +} + +void takeScript(script *s) +{ +#if THREADIT + Eina_Lock_Result result = eina_lock_take(&s->mutex); + if (EINA_LOCK_DEADLOCK == result) printf("Script %s IS DEADLOCKED!\n", s->name); + if (EINA_LOCK_FAIL == result) printf("Script %s LOCK FAILED!\n", s->name); +#endif +} + +void releaseScript(script *s) +{ +#if THREADIT + eina_lock_release(&s->mutex); +#endif +} + +void send2script(const char *SID, const char *message) +{ + if (SID) + { + script *s = ecore_thread_global_data_find(SID); + + if (s) + { + takeScript(s); + if (RUNNR_FINISHED != s->status) { - case LUA_ERRRUN: err_type = "runtime"; break; - case LUA_ERRSYNTAX: err_type = "syntax"; break; - case LUA_ERRMEM: err_type = "memory allocation"; break; - case LUA_ERRERR: err_type = "error handler"; break; - default: err_type = "unknown"; break; + scriptMessage *sm = NULL; + + if ((sm = malloc(sizeof(scriptMessage)))) + { + runnrStatus stat; + + sm->s = s; + strcpy((char *) sm->message, message); + eina_clist_add_tail(&(s->messages), &(sm->node)); + + stat = s->status; + s->status = RUNNR_READY; + if (RUNNR_WAIT == stat) +#if THREADIT + ecore_thread_feedback_run(_workerFunction, _notify, _end, _cancel, s, EINA_FALSE); +#else + _workerFunction(s, NULL); +#endif + } } - printf("Error running - %s, \n%s - %s", string, err_type, lua_tostring(L, -1)); + releaseScript(s); } } } +static script *_getSelf(lua_State *L) +{ + script *s; + + lua_getfield(L, LUA_REGISTRYINDEX, "_SELF"); + s = (script *) lua_touserdata(L, -1); + lua_pop(L, 1); + + return s; +} + +static int _send(lua_State *L) +{ + script *self = _getSelf(L); + const char *SID = NULL, *message = luaL_checkstring(L, 2); + + if (lua_isstring(L, 1)) + SID = lua_tostring(L, 1); + + if (SID) + send2script(SID, message); + else + { + takeScript(self); +#if THREADIT + ecore_thread_feedback(self->me, strdup(message)); +#else + _notify(self, NULL, strdup(message)); +#endif + releaseScript(self); + } + + return 0; +} + +static int _receive(lua_State *L) +{ + script *self = _getSelf(L); + + takeScript(self); + self->status = RUNNR_WAIT; + releaseScript(self); + return lua_yield(L, 0); +} + // These are what the various symbols are for each type - // int % @@ -282,10 +788,24 @@ int push_lua(lua_State *L, char *params, ...) // Stack usage [-0, +n, em] va_list vl; char *f = strdup(params); char *p = f; - int n = 0, table = 0, i = -1; + int n = 0, table = 0, i = -1, needTrace = 0, _T; if (!f) return -1; + // Scan ahead looking for ), so we know to put the traceBack function on the stack first. + while (*p) + { + p++; + if ('0' == *p) + { + lua_pushcfunction(L, traceBack); + _T = lua_gettop(L); + needTrace = 1; + break; + } + } + p = f; + va_start(vl, params); while (*p) @@ -364,7 +884,10 @@ int push_lua(lua_State *L, char *params, ...) // Stack usage [-0, +n, em] } case ')': { - lua_call(L, n - 1, va_arg(vl, int)); + int err; + + if ((err = lua_pcall(L, n - 1, va_arg(vl, int), _T))) + printLuaError(err, params, L); n = 0; set = EINA_FALSE; break; @@ -401,6 +924,8 @@ int push_lua(lua_State *L, char *params, ...) // Stack usage [-0, +n, em] } va_end(vl); + if (needTrace) + lua_remove(L, _T); free(f); return n; } diff --git a/src/libraries/Runnr.h b/src/libraries/Runnr.h index 0e0fc73..ba5c17e 100644 --- a/src/libraries/Runnr.h +++ b/src/libraries/Runnr.h @@ -4,12 +4,82 @@ #include #include +#include +#include #include #include #include #include +// Stick with Plan C for now. +#define THREADIT 0 + +typedef enum +{ + RUNNR_COMPILING, + RUNNR_NOT_STARTED, + RUNNR_RUNNING, + RUNNR_WAIT, + RUNNR_READY, + RUNNR_RESET, + RUNNR_FINISHED +} runnrStatus; + +typedef struct _LuaCompile LuaCompile; +typedef void (* compileCb)(LuaCompile *compiler); + +typedef struct _LuaCompile +{ + char *file, *SID, *luaName; + int bugCount; + void *data; + Ecore_Con_Client *client; + compileCb cb; +} LuaCompiler; + +typedef struct _script script; +typedef void (* RunnrServerCb)(script *me, const char *message); + +typedef struct _script +{ + Eina_Clist node; +#if THREADIT + Eina_Lock mutex; + Ecore_Thread *me; +#endif + void *data; + char SID[PATH_MAX]; + char *name; + char fileName[PATH_MAX]; + char binName[PATH_MAX]; + lua_State *L; + struct timeval startTime; + float timerTime; + runnrStatus status; + RunnrServerCb send2server; + Eina_Clist messages; + Ecore_Con_Client *client; + Ecore_Timer *timer; +} script; + +typedef struct +{ + Eina_Clist node; + script *s; + const char message[PATH_MAX]; +} scriptMessage; + + +script *scriptAdd(char *file, char *SID, RunnrServerCb send2server, void *data); +void compileScript(LuaCompiler *compiler); +void runScript(script *me); +void resetScript(script *me); +script *getScript(char *SID); +void takeScript(script *me); +void releaseScript(script *me); +void send2script(const char *SID, const char *message); +void printScriptsStatus(); void dumpStack(lua_State *L, int i); void doLuaString(lua_State *L, char *string, char *module); diff --git a/src/libraries/SledjHamr.c b/src/libraries/SledjHamr.c index 5f26fde..5e1d387 100644 --- a/src/libraries/SledjHamr.c +++ b/src/libraries/SledjHamr.c @@ -1,3 +1,5 @@ +// This might become nails, since at the moment it's only got comms stuff in it. + #include #include "SledjHamr.h" diff --git a/src/love/build.lua b/src/love/build.lua index 5ecca31..8fd9a29 100755 --- a/src/love/build.lua +++ b/src/love/build.lua @@ -17,5 +17,4 @@ end removeFiles(dir, {'../../love', '*.o', '../../media/love.edj'}) runCommand('edje_cc', dir, 'edje_cc ' .. EDJE_FLAGS .. ' love.edc ../../media/love.edj') - compileFiles('../../love', dir, {'love'}, '') diff --git a/src/love/love.c b/src/love/love.c index 1f885bc..d194f11 100644 --- a/src/love/love.c +++ b/src/love/love.c @@ -44,7 +44,7 @@ typedef struct _gameGlobals boolean ui; // Wether we actually start up the UI. } gameGlobals; -typedef struct _script +typedef struct _Lscript { char SID[PATH_MAX]; char fileName[PATH_MAX]; @@ -52,11 +52,10 @@ typedef struct _script float compileTime; int bugs, warnings; boolean running; -} script; +} LoveScript; int logDom = -1; // Our logging domain. -//static int CPUs = 4; static Eina_Strbuf *LuaSLStream; static Eina_Strbuf *clientStream; static int scriptCount = 0; @@ -145,7 +144,7 @@ static void dirList_compile(const char *name, const char *path, void *data) // TODO - We are leaking these, coz we don't know when scripts get deleted in the script server. // On the other hand, the main use for this is a temporary hack that sends events to all scripts. // So that part will get a rewrite when we make it real later anyway. - script *me = calloc(1, sizeof(script)); + LoveScript *me = calloc(1, sizeof(LoveScript)); scriptCount++; gettimeofday(&me->startTime, NULL); @@ -213,7 +212,7 @@ static Eina_Bool _dataLuaSL(void *data, int type, Ecore_Con_Event_Server_Data *e ext = index(SID, '.'); if (ext) { - script *me; + LoveScript *me; ext[0] = '\0'; command = ext + 1; @@ -383,7 +382,7 @@ static Eina_Bool _dataLuaSL(void *data, int type, Ecore_Con_Event_Server_Data *e else if (0 == strncmp(command, "llMessageLinked(", 16)) { Eina_Iterator *scripts; - script *me; + LoveScript *me; // TODO - For now, just send it to everyone. scripts = eina_hash_iterator_data_new(ourGlobals->scripts); @@ -419,7 +418,7 @@ static Eina_Bool _dataLuaSL(void *data, int type, Ecore_Con_Event_Server_Data *e if (-1 != fd) { Eina_Iterator *scripts; - script *me; + LoveScript *me; long len; temp = NULL; @@ -542,7 +541,7 @@ static Eina_Bool _dataClient(void *data, int type, Ecore_Con_Event_Client_Data * if (0 == strncmp(command, "events.touch_start(", 19)) { Eina_Iterator *scripts; - script *me; + LoveScript *me; // TODO - For now, just send it to everyone. scripts = eina_hash_iterator_data_new(ourGlobals->scripts); @@ -557,7 +556,7 @@ static Eina_Bool _dataClient(void *data, int type, Ecore_Con_Event_Client_Data * else if (0 == strncmp(command, "events.listen(", 14)) { Eina_Iterator *scripts; - script *me; + LoveScript *me; // TODO - For now, just send it to everyone. scripts = eina_hash_iterator_data_new(ourGlobals->scripts); @@ -586,7 +585,7 @@ static Eina_Bool _delClient(void *data, int type, Ecore_Con_Event_Client_Del *ev { Eina_List const *clients; - // This is only really for testing, normally it just runs 24/7, or until told to. + // This is only really for testing, normally it just runs 24/7, or until told not to. clients = ecore_con_server_clients_get(ourGlobals->server); if (0 == eina_list_count(clients)) { @@ -606,7 +605,6 @@ static Eina_Bool _delClient(void *data, int type, Ecore_Con_Event_Client_Del *ev int main(int argc, char **argv) { - /* put here any init specific to this app like parsing args etc. */ gameGlobals ourGlobals; char *programName = argv[0]; boolean badArgs = FALSE; -- cgit v1.1