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/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 +- 7 files changed, 127 insertions(+), 802 deletions(-) delete mode 100644 src/LuaSL/LuaSL_threads.c delete mode 100644 src/LuaSL/LuaSL_threads.h (limited to 'src/LuaSL') 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'}, '') -- cgit v1.1