aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorDavid Walter Seikel2012-01-25 02:29:35 +1000
committerDavid Walter Seikel2012-01-25 02:29:35 +1000
commit3236320311f15640083c5bf420bcdeab725ff242 (patch)
tree7f8abdafad10884fbdedf919e49c8b79c1f2287b
parentQuick and dirty testing of luaJIT+luaproc from LuaSL. (diff)
downloadSledjHamr-3236320311f15640083c5bf420bcdeab725ff242.zip
SledjHamr-3236320311f15640083c5bf420bcdeab725ff242.tar.gz
SledjHamr-3236320311f15640083c5bf420bcdeab725ff242.tar.bz2
SledjHamr-3236320311f15640083c5bf420bcdeab725ff242.tar.xz
Hack together LuaSL, LuaJIT, and luaproc. It's frankenstein right now. lol
-rwxr-xr-xLuaSL/build.sh13
-rw-r--r--LuaSL/src/LuaSL_LSL_tree.h7
-rw-r--r--LuaSL/src/LuaSL_main.c185
-rw-r--r--LuaSL/testLua/luaprocTest0.lua7
-rw-r--r--LuaSL/testLua/luaprocTest0_C.lua37
-rw-r--r--LuaSL/testLua/luaprocTest1_C.lua28
-rw-r--r--LuaSL/testLua/luaprocTest2_C.lua65
-rwxr-xr-xbuild.sh4
-rw-r--r--libraries/README3
l---------libraries/luajit-2.0/src/libluajit-5.1.so.21
-rw-r--r--libraries/luaproc/luaproc.c72
-rw-r--r--libraries/luaproc/luaproc.h5
-rw-r--r--libraries/luaproc/sched.c44
-rwxr-xr-xtest.sh6
14 files changed, 439 insertions, 38 deletions
diff --git a/LuaSL/build.sh b/LuaSL/build.sh
index 15e1d52..4433120 100755
--- a/LuaSL/build.sh
+++ b/LuaSL/build.sh
@@ -18,6 +18,9 @@ fi
18# No need for a make file, or dependencies, the entire thing takes only a few seconds to build. 18# No need for a make file, or dependencies, the entire thing takes only a few seconds to build.
19 19
20CFLAGS="-g -Wall -I include -I $LOCALDIR/src" 20CFLAGS="-g -Wall -I include -I $LOCALDIR/src"
21CFLAGS="$CFLAGS -I ../../libraries"
22CFLAGS="$CFLAGS -I ../../libraries/luajit-2.0/src"
23#CFLAGS="$CFLAGS -I /usr/include/lua5.1"
21CFLAGS="$CFLAGS -I $E17DIR/include/eina-1" 24CFLAGS="$CFLAGS -I $E17DIR/include/eina-1"
22CFLAGS="$CFLAGS -I $E17DIR/include/eina-1/eina" 25CFLAGS="$CFLAGS -I $E17DIR/include/eina-1/eina"
23CFLAGS="$CFLAGS -I $E17DIR/include/eet-1" 26CFLAGS="$CFLAGS -I $E17DIR/include/eet-1"
@@ -28,12 +31,13 @@ CFLAGS="$CFLAGS -I $E17DIR/include/ecore-1"
28CFLAGS="$CFLAGS -I $E17DIR/include" 31CFLAGS="$CFLAGS -I $E17DIR/include"
29CFLAGS="$CFLAGS -DPACKAGE_DATA_DIR=\"$LOCALDIR\" $CFLAGOPTS" 32CFLAGS="$CFLAGS -DPACKAGE_DATA_DIR=\"$LOCALDIR\" $CFLAGOPTS"
30 33
31LDFLAGS="-L lib -L /usr/lib -L /lib -L $E17DIR/lib -L ../../libraries/luajit-2.0/src" 34LDFLAGS="-L ../../libraries/luajit-2.0/src -L lib -L /usr/lib -L /lib -L $E17DIR/lib"
32libs="-lecore -levas -ledje -lembryo -leet -leina -lluajit -lpthread" 35libs="-lecore -levas -ledje -lembryo -leet -leina -lluajit -lpthread -lm"
36#LDFLAGS="-L /usr/lib/lua/5.1 -L lib -L /usr/lib -L /lib -L $E17DIR/lib"
37#libs="-lecore -levas -ledje -lembryo -leet -leina -llua5.1 -lpthread -lm"
33# These need to be added to libs if linking staticaly, though some parts of EFL don't like that. 38# These need to be added to libs if linking staticaly, though some parts of EFL don't like that.
34#-lecore_evas \ 39#-lecore_evas \
35#-lecore_file \ 40#-lecore_file \
36#-lm \
37#-ldl \ 41#-ldl \
38#-lfontconfig \ 42#-lfontconfig \
39#-lfreetype \ 43#-lfreetype \
@@ -43,6 +47,7 @@ libs="-lecore -levas -ledje -lembryo -leet -leina -lluajit -lpthread"
43 47
44LFLAGS="-d" 48LFLAGS="-d"
45EDJE_FLAGS="-id images -fd fonts" 49EDJE_FLAGS="-id images -fd fonts"
50LD_RUN_PATH="../../libraries/luajit-2.0/src:"
46 51
47 52
48# Run lemon first, flex depends on it to define the symbol values. 53# Run lemon first, flex depends on it to define the symbol values.
@@ -59,7 +64,7 @@ echo $command
59$command 64$command
60 65
61names="LuaSL_main LuaSL_compile LuaSL_utilities LuaSL_lexer LuaSL_lemon_yaccer" 66names="LuaSL_main LuaSL_compile LuaSL_utilities LuaSL_lexer LuaSL_lemon_yaccer"
62objects="" 67objects="../../libraries/luaproc/channel.o ../../libraries/luaproc/list.o ../../libraries/luaproc/luaproc.o ../../libraries/luaproc/sched.o "
63for i in $names 68for i in $names
64do 69do
65 command="gcc $CFLAGS -c -o $i.o $i.c" 70 command="gcc $CFLAGS -c -o $i.o $i.c"
diff --git a/LuaSL/src/LuaSL_LSL_tree.h b/LuaSL/src/LuaSL_LSL_tree.h
index 687c56c..20277b9 100644
--- a/LuaSL/src/LuaSL_LSL_tree.h
+++ b/LuaSL/src/LuaSL_LSL_tree.h
@@ -20,6 +20,13 @@
20#include <stdio.h> 20#include <stdio.h>
21#include <limits.h> // For PATH_MAX. 21#include <limits.h> // For PATH_MAX.
22 22
23#include <lua.h>
24#include <luajit.h>
25#include <lualib.h>
26#include <lauxlib.h>
27
28#include <luaproc/sched.h>
29
23#include "LuaSL_lemon_yaccer.h" 30#include "LuaSL_lemon_yaccer.h"
24 31
25#define YYERRCODE 256 32#define YYERRCODE 256
diff --git a/LuaSL/src/LuaSL_main.c b/LuaSL/src/LuaSL_main.c
index 3f20460..f2f4f17 100644
--- a/LuaSL/src/LuaSL_main.c
+++ b/LuaSL/src/LuaSL_main.c
@@ -106,6 +106,163 @@ static void dirListLua_cb(const char *name, const char *path, void *data)
106 } 106 }
107 } 107 }
108} 108}
109
110#ifdef _WIN32
111# define FMT_SIZE_T "%Iu"
112#else
113# define FMT_SIZE_T "%zu"
114#endif
115
116#define MAX_LUA_MEM (4 * (1024 * 1024))
117
118#define _edje_lua2_error(L, err_code) _edje_lua2_error_full(__FILE__, __FUNCTION__, __LINE__, L, err_code)
119
120/*
121typedef struct _Edje_Lua_Alloc Edje_Lua_Alloc;
122
123struct _Edje_Lua_Alloc
124{
125 size_t max, cur;
126};
127
128static void *
129_elua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
130{
131 Edje_Lua_Alloc *ela = ud;
132 void *ptr2 = NULL;
133
134 if (ela)
135 {
136 ela->cur += nsize - osize;
137 if (ela->cur > ela->max)
138 {
139 printf("Lua memory limit of " FMT_SIZE_T " bytes reached (" FMT_SIZE_T " allocated)", ela->max, ela->cur);
140 }
141 else if (nsize == 0)
142 {
143 free(ptr);
144 }
145 else
146 {
147 ptr2 = realloc(ptr, nsize);
148 if (NULL == ptr2)
149 printf("Lua cannot re-allocate " FMT_SIZE_T " bytes", nsize);
150 }
151 }
152 else
153 printf("Lua cannoct allocate memory, no Edje_Lua_Alloc");
154
155 return ptr2;
156}
157
158static int panics = 0;
159static int
160_elua_custom_panic(lua_State *L) // Stack usage [-0, +0, m]
161{
162 // If we somehow manage to have multiple panics, it's likely due to being out
163 // of memory in the following lua_tostring() call.
164 panics++;
165 if (panics)
166 {
167 printf("Lua PANICS!!!!!");
168 }
169 else
170 {
171 printf("Lua PANIC!!!!!: %s", lua_tostring(L, -1)); // Stack usage [-0, +0, m]
172 }
173 // The docs say that this will cause an exit(EXIT_FAILURE) if we return,
174 // and that we we should long jump some where to avoid that. This is only
175 // called for things not called from a protected environment. We always
176 // use pcalls though, except for the library load calls. If we can't load
177 // the standard libraries, then perhaps a crash is the right thing.
178 return 0;
179}
180
181static void
182_edje_lua2_error_full(const char *file, const char *fnc, int line,
183 lua_State *L, int err_code) // Stack usage [-0, +0, m]
184{
185 const char *err_type;
186
187 switch (err_code)
188 {
189 case LUA_ERRRUN:
190 err_type = "runtime";
191 break;
192 case LUA_ERRSYNTAX:
193 err_type = "syntax";
194 break;
195 case LUA_ERRMEM:
196 err_type = "memory allocation";
197 break;
198 case LUA_ERRERR:
199 err_type = "error handler";
200 break;
201 default:
202 err_type = "unknown";
203 break;
204 }
205 printf("Lua %s error: %s\n", err_type, lua_tostring(L, -1)); // Stack usage [-0, +0, m]
206}
207
208static int errFunc(lua_State *L)
209{
210 int i = 0;
211 lua_Debug ar;
212
213 while (lua_getstack(L, i++, &ar))
214 {
215 if (lua_getinfo(L, "nSlu", &ar))
216 {
217 if (NULL == ar.name)
218 ar.name = "DUNNO";
219 printf("Lua error in the %s %s %s @ line %d in %s\n%s!", ar.what, ar.namewhat, ar.name, ar.currentline, ar.short_src, ar.source);
220 }
221 }
222 return 0;
223}
224*/
225
226static void runnerSetup(gameGlobals *game)
227{
228 luaprocInit();
229
230 if ( sched_create_worker( ) != LUAPROC_SCHED_OK )
231 PE("Error creating luaproc worker thread.");
232 if ( sched_create_worker( ) != LUAPROC_SCHED_OK )
233 PE("Error creating luaproc worker thread.");
234 if ( sched_create_worker( ) != LUAPROC_SCHED_OK )
235 PE("Error creating luaproc worker thread.");
236 if ( sched_create_worker( ) != LUAPROC_SCHED_OK )
237 PE("Error creating luaproc worker thread.");
238}
239
240static void runnerTearDown(gameGlobals *game)
241{
242// TODO - this is what hangs the system.
243 sched_join_workerthreads();
244}
245
246static void runLuaFile(gameGlobals *game, const char *filename)
247{
248 newProc(filename, TRUE);
249
250// TODO, should set up our panic and errfunc as below. Plus the other TODO stuff.
251
252/*
253// TODO - hack up LuaJIT so that we can limit memory per state.
254// lua_setallocf(L, _elua_alloc, &ela); // LuaJIT uses a heavily hacked up dlmalloc. Seems that standard realloc is not so thread safe?
255 lua_atpanic(L, _elua_custom_panic);
256// TODO - Sandbox out what this opens. See lib_init.c from LuaJIT.
257 luaL_openlibs(L);
258
259 lua_pushcfunction(L, errFunc);
260 ...
261 if ((err = lua_pcall(L, 0, 0, -2)))
262 _edje_lua2_error(L, err);
263*/
264}
265
109#endif 266#endif
110 267
111int 268int
@@ -248,6 +405,7 @@ main(int argc, char **argv)
248 scriptCount = 0; 405 scriptCount = 0;
249 gettimeofday(&lastTime2, 0); 406 gettimeofday(&lastTime2, 0);
250 compilerSetup(&game); 407 compilerSetup(&game);
408 runnerSetup(&game);
251 snprintf(buf, sizeof(buf), "%s/Test sim/objects", PACKAGE_DATA_DIR); 409 snprintf(buf, sizeof(buf), "%s/Test sim/objects", PACKAGE_DATA_DIR);
252 eina_file_dir_list(buf, EINA_TRUE, dirList_cb, &game); 410 eina_file_dir_list(buf, EINA_TRUE, dirList_cb, &game);
253 diff = timeDiff(&thisTime2, &lastTime2); 411 diff = timeDiff(&thisTime2, &lastTime2);
@@ -266,35 +424,46 @@ main(int argc, char **argv)
266 printf("Combined estimate of compiling speed is %f scripts per second.\n", 1 / ((diff0 / lslCount) + (diff / scriptCount))); 424 printf("Combined estimate of compiling speed is %f scripts per second.\n", 1 / ((diff0 / lslCount) + (diff / scriptCount)));
267 425
268 gettimeofday(&lastTime2, 0); 426 gettimeofday(&lastTime2, 0);
269 snprintf(buf, sizeof(buf), "cd %s/testLua; LUA_SOPATH='../../libraries/luaproc/' lua luaprocTest0.lua", PACKAGE_DATA_DIR); 427 snprintf(buf, sizeof(buf), "lua luaprocTest0.lua");
270 system(buf); 428 system(buf);
271 diff = timeDiff(&thisTime2, &lastTime2); 429 diff = timeDiff(&thisTime2, &lastTime2);
272 printf("%s TOOK %f seconds.\n", buf, diff); 430 printf("%s TOOK %f seconds......................................................................................................\n", buf, diff);
273 431
274 gettimeofday(&lastTime2, 0); 432 gettimeofday(&lastTime2, 0);
275 snprintf(buf, sizeof(buf), "cd %s/testLua; LUA_SOPATH='../../libraries/luaproc/' lua luaprocTest1.lua", PACKAGE_DATA_DIR); 433 snprintf(buf, sizeof(buf), "%s/testLua/luaprocTest0_C.lua", PACKAGE_DATA_DIR);
434 runLuaFile(&game, buf);
435 diff = timeDiff(&thisTime2, &lastTime2);
436 printf("Running that last one locally TOOK %f seconds.\n",diff);
437
438 gettimeofday(&lastTime2, 0);
439 snprintf(buf, sizeof(buf), "lua luaprocTest1.lua");
276 system(buf); 440 system(buf);
277 diff = timeDiff(&thisTime2, &lastTime2); 441 diff = timeDiff(&thisTime2, &lastTime2);
278 printf("%s TOOK %f seconds.\n", buf, diff); 442 printf("%s TOOK %f seconds.\n", buf, diff);
279 443
280 gettimeofday(&lastTime2, 0); 444 gettimeofday(&lastTime2, 0);
281 snprintf(buf, sizeof(buf), "cd %s/testLua; LUA_SOPATH='../../libraries/luaproc/' luajit luaprocTest1.lua", PACKAGE_DATA_DIR); 445 snprintf(buf, sizeof(buf), "../../libraries/luajit-2.0/src/luajit luaprocTest1.lua");
282 system(buf); 446 system(buf);
283 diff = timeDiff(&thisTime2, &lastTime2); 447 diff = timeDiff(&thisTime2, &lastTime2);
284 printf("%s TOOK %f seconds.\n", buf, diff); 448 printf("%s TOOK %f seconds.\n", buf, diff);
285 449
286 gettimeofday(&lastTime2, 0); 450 gettimeofday(&lastTime2, 0);
287 snprintf(buf, sizeof(buf), "cd %s/testLua; LUA_SOPATH='../../libraries/luaproc/' lua luaprocTest2.lua", PACKAGE_DATA_DIR); 451 snprintf(buf, sizeof(buf), "lua luaprocTest2.lua");
288 system(buf); 452 system(buf);
289 diff = timeDiff(&thisTime2, &lastTime2); 453 diff = timeDiff(&thisTime2, &lastTime2);
290 printf("%s TOOK %f seconds.\n", buf, diff); 454 printf("%s TOOK %f seconds.\n", buf, diff);
291 455
292 gettimeofday(&lastTime2, 0); 456 gettimeofday(&lastTime2, 0);
293 snprintf(buf, sizeof(buf), "cd %s/testLua; LUA_SOPATH='../../libraries/luaproc/' luajit luaprocTest2.lua", PACKAGE_DATA_DIR); 457 snprintf(buf, sizeof(buf), "../../libraries/luajit-2.0/src/luajit luaprocTest2.lua");
294 system(buf); 458 system(buf);
295 diff = timeDiff(&thisTime2, &lastTime2); 459 diff = timeDiff(&thisTime2, &lastTime2);
296 printf("%s TOOK %f seconds.\n", buf, diff); 460 printf("%s TOOK %f seconds.\n", buf, diff);
297 461
462 gettimeofday(&lastTime2, 0);
463 snprintf(buf, sizeof(buf), "%s/testLua/luaprocTest2_C.lua", PACKAGE_DATA_DIR);
464 runLuaFile(&game, buf);
465 diff = timeDiff(&thisTime2, &lastTime2);
466 printf("Running that last one locally TOOK %f seconds.\n",diff);
298#endif 467#endif
299 468
300 if (game.ui) 469 if (game.ui)
@@ -303,6 +472,10 @@ main(int argc, char **argv)
303 ecore_animator_del(ani); 472 ecore_animator_del(ani);
304 ecore_evas_free(game.ee); 473 ecore_evas_free(game.ee);
305 } 474 }
475
476 runnerTearDown(&game);
477 diff = timeDiff(&thisTime2, &lastTime2);
478 printf("Running that last one locally TOOK %f seconds.\n",diff);
306 edje_shutdown(); 479 edje_shutdown();
307 ecore_evas_shutdown(); 480 ecore_evas_shutdown();
308 } 481 }
diff --git a/LuaSL/testLua/luaprocTest0.lua b/LuaSL/testLua/luaprocTest0.lua
index 5868d6f..94a1d20 100644
--- a/LuaSL/testLua/luaprocTest0.lua
+++ b/LuaSL/testLua/luaprocTest0.lua
@@ -33,7 +33,12 @@ luaproc.createworker()
33luaproc.newproc( [=[ 33luaproc.newproc( [=[
34 luaproc.newchannel( "testchannel" ) 34 luaproc.newchannel( "testchannel" )
35 luaproc.newproc( "luaproc.send( 'testchannel', 'luaproc is working fine!' )" ) 35 luaproc.newproc( "luaproc.send( 'testchannel', 'luaproc is working fine!' )" )
36 luaproc.newproc( "print( luaproc.receive( 'testchannel' ))" ) 36 luaproc.newproc( "print(luaproc.receive( 'testchannel'))" )
37 print("End of parent proc")
37]=] ) 38]=] )
38 39
40print("About to luaproc.exit()!");
39luaproc.exit() 41luaproc.exit()
42print("Should have exited after the luaproc.exit()!");
43
44
diff --git a/LuaSL/testLua/luaprocTest0_C.lua b/LuaSL/testLua/luaprocTest0_C.lua
new file mode 100644
index 0000000..0dd6078
--- /dev/null
+++ b/LuaSL/testLua/luaprocTest0_C.lua
@@ -0,0 +1,37 @@
1----------------------------------------------------
2--
3-- Copyright 2008 Alexandre Skyrme, Noemi Rodriguez, Roberto Ierusalimschy
4--
5-- Permission is hereby granted, free of charge, to any person obtaining a copy
6-- of this software and associated documentation files (the "Software"), to deal
7-- in the Software without restriction, including without limitation the rights
8-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9-- copies of the Software, and to permit persons to whom the Software is
10-- furnished to do so, subject to the following conditions:
11--
12-- The above copyright notice and this permission notice shall be included in
13-- all copies or substantial portions of the Software.
14--
15-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21-- THE SOFTWARE.
22--
23-----------------------------------------------------
24--
25-- test.lua
26--
27-----------------------------------------------------
28
29luaproc.newproc( [=[
30 luaproc.newchannel( "testchannel" )
31 luaproc.newproc( "luaproc.send( 'testchannel', 'luaproc is working fine!' )" )
32 luaproc.newproc( "print(luaproc.receive( 'testchannel'))" )
33 print("End of parent proc")
34]=] )
35
36print("About to exit.");
37
diff --git a/LuaSL/testLua/luaprocTest1_C.lua b/LuaSL/testLua/luaprocTest1_C.lua
new file mode 100644
index 0000000..61cb461
--- /dev/null
+++ b/LuaSL/testLua/luaprocTest1_C.lua
@@ -0,0 +1,28 @@
1result, error_msg = luaproc.newproc( [=[
2
3 local count = 0
4
5-- Hmm, luajit 2 beta 9 crashes at about 8140, lua can go up to 50000 at least.
6 for i = 1, 8000 do
7 local channel = "channel" .. i
8-- local proc = 'print(luaproc.receive("channel' .. i .. '") .. " " .. ' .. i ..')'
9 local proc = 'luaproc.receive("' .. channel .. '")'
10
11-- print(channel .. "\n" .. proc)
12 result, error_msg = luaproc.newchannel(channel)
13 if not result then print(error_msg) end
14 result, error_msg = luaproc.newproc(proc)
15 if not result then print(error_msg) else count = count + 1 end
16 end
17
18 print("Started " .. count .. " Lua threads")
19
20 if 0 ~= count then
21 for i = 1, count do
22 result, error_msg = luaproc.send("channel" .. i, 'luaproc is working fine! ' .. i)
23 if not result then print(error_msg .. " " .. i) end
24 end
25 end
26]=] )
27if not result then print(error_msg .. " for main proc") end
28
diff --git a/LuaSL/testLua/luaprocTest2_C.lua b/LuaSL/testLua/luaprocTest2_C.lua
new file mode 100644
index 0000000..e1a5a83
--- /dev/null
+++ b/LuaSL/testLua/luaprocTest2_C.lua
@@ -0,0 +1,65 @@
1count = 0
2
3proc = [=[
4 require "string"
5 local channel = "channel%d"
6 local message
7
8 result, error_msg = luaproc.newchannel(channel)
9 if not result then print(error_msg) end
10 repeat
11 message = luaproc.receive(channel)
12 local x, y = string.find(message, "@")
13 if not x then
14 x, y = string.find(message, "@")
15 x, y = string.find(message, "@")
16 x, y = string.find(message, "@")
17 x, y = string.find(message, "@")
18 x, y = string.find(message, "@")
19 x, y = string.find(message, "@")
20 x, y = string.find(message, "@")
21 x, y = string.find(message, "@")
22 end
23 until "quit" == message
24]=]
25
26mainProc = [=[
27 local count = %d
28 local messages = 0
29 local length = 0
30 local message = "This is a test message. 56789 12"
31
32 if 0 ~= count then
33 for k = 0, 16 do
34 for j = 0, 199 do
35 for i = 1, count do
36 result, error_msg = luaproc.send("channel" .. i, message)
37 if not result then print(error_msg .. " " .. i) else
38 messages = messages + 1
39 length = length + #message
40 end
41 end
42 end
43 message = message .. message
44 end
45
46 for i = 1, count do
47 result, error_msg = luaproc.send("channel" .. i, "quit")
48 if not result then print(error_msg .. " " .. i) end
49 end
50 end
51
52 print("Sent " .. messages .. " messages totalling " .. length .. " bytes. The largest message was " .. #message .. " bytes.")
53]=]
54
55for i = 1, 10 do
56 result, error_msg = luaproc.newproc(string.format(proc, i))
57 if not result then print(error_msg) else count = count + 1 end
58end
59
60print("Started " .. count .. " Lua threads.")
61
62result, error_msg = luaproc.newproc(string.format(mainProc, count))
63if not result then print(error_msg) end
64
65
diff --git a/build.sh b/build.sh
index 19c7ff9..41a7470 100755
--- a/build.sh
+++ b/build.sh
@@ -3,8 +3,8 @@
3wd=$(pwd) 3wd=$(pwd)
4 4
5echo "_______________ BUILDING LuaJIT _______________" 5echo "_______________ BUILDING LuaJIT _______________"
6cd libraries/luajit-2.0 6cd $wd/libraries/luajit-2.0
7make 7make amalg PREFIX=$()/src
8 8
9 9
10echo "_______________ BUILDING luaproc _______________" 10echo "_______________ BUILDING luaproc _______________"
diff --git a/libraries/README b/libraries/README
index 3f6e7c7..d24f7d2 100644
--- a/libraries/README
+++ b/libraries/README
@@ -6,3 +6,6 @@ should go here to. Makes it easy to compile it all.
6Note that in some cases, there might be changes. Such changes should be 6Note that in some cases, there might be changes. Such changes should be
7documented. 7documented.
8 8
9luaproc has been hacked up a bit, and will continue to be hacked up.
10Merging it into LuaSL, and converting it to EFL.
11
diff --git a/libraries/luajit-2.0/src/libluajit-5.1.so.2 b/libraries/luajit-2.0/src/libluajit-5.1.so.2
new file mode 120000
index 0000000..deaddec
--- /dev/null
+++ b/libraries/luajit-2.0/src/libluajit-5.1.so.2
@@ -0,0 +1 @@
libluajit.so \ No newline at end of file
diff --git a/libraries/luaproc/luaproc.c b/libraries/luaproc/luaproc.c
index 2ec5b31..ff18723 100644
--- a/libraries/luaproc/luaproc.c
+++ b/libraries/luaproc/luaproc.c
@@ -114,6 +114,7 @@ static const struct luaL_reg luaproc_funcs_child[] = {
114 { NULL, NULL } 114 { NULL, NULL }
115}; 115};
116 116
117/*
117static void registerlib( lua_State *L, const char *name, lua_CFunction f ) { 118static void registerlib( lua_State *L, const char *name, lua_CFunction f ) {
118 lua_getglobal( L, "package" ); 119 lua_getglobal( L, "package" );
119 lua_getfield( L, -1, "preload" ); 120 lua_getfield( L, -1, "preload" );
@@ -121,8 +122,9 @@ static void registerlib( lua_State *L, const char *name, lua_CFunction f ) {
121 lua_setfield( L, -2, name ); 122 lua_setfield( L, -2, name );
122 lua_pop( L, 2 ); 123 lua_pop( L, 2 );
123} 124}
124 125*/
125static void openlibs( lua_State *L ) { 126static void openlibs( lua_State *L ) {
127/*
126 lua_cpcall( L, luaopen_base, NULL ); 128 lua_cpcall( L, luaopen_base, NULL );
127 lua_cpcall( L, luaopen_package, NULL ); 129 lua_cpcall( L, luaopen_package, NULL );
128 registerlib( L, "io", luaopen_io ); 130 registerlib( L, "io", luaopen_io );
@@ -131,6 +133,8 @@ static void openlibs( lua_State *L ) {
131 registerlib( L, "string", luaopen_string ); 133 registerlib( L, "string", luaopen_string );
132 registerlib( L, "math", luaopen_math ); 134 registerlib( L, "math", luaopen_math );
133 registerlib( L, "debug", luaopen_debug ); 135 registerlib( L, "debug", luaopen_debug );
136*/
137 luaL_openlibs(L);
134} 138}
135 139
136/* return status (boolean) indicating if lua process should be recycled */ 140/* return status (boolean) indicating if lua process should be recycled */
@@ -195,7 +199,7 @@ int luaproc_recycle_push( luaproc lp ) {
195} 199}
196 200
197/* create new luaproc */ 201/* create new luaproc */
198luaproc luaproc_new( const char *code, int destroyflag ) { 202static luaproc luaproc_new( const char *code, int destroyflag, int file) {
199 203
200 luaproc lp; 204 luaproc lp;
201 int ret; 205 int ret;
@@ -218,7 +222,10 @@ luaproc luaproc_new( const char *code, int destroyflag ) {
218 luaL_register( lpst, "luaproc", luaproc_funcs_child ); 222 luaL_register( lpst, "luaproc", luaproc_funcs_child );
219 223
220 /* load process' code */ 224 /* load process' code */
221 ret = luaL_loadstring( lpst, code ); 225 if (file)
226 ret = luaL_loadfile( lpst, code );
227 else
228 ret = luaL_loadstring( lpst, code );
222 /* in case of errors, destroy recently created lua process */ 229 /* in case of errors, destroy recently created lua process */
223 if ( ret != 0 ) { 230 if ( ret != 0 ) {
224 lua_close( lpst ); 231 lua_close( lpst );
@@ -231,7 +238,7 @@ luaproc luaproc_new( const char *code, int destroyflag ) {
231 238
232/* synchronize worker threads and exit */ 239/* synchronize worker threads and exit */
233static int luaproc_exit( lua_State *L ) { 240static int luaproc_exit( lua_State *L ) {
234 sched_join_workerthreads( ); 241 sched_join_workerthreads();
235 return 0; 242 return 0;
236} 243}
237 244
@@ -297,7 +304,7 @@ static int luaproc_destroy_worker( lua_State *L ) {
297 304
298 /* create new lua process with empty code and destroy worker flag set to true 305 /* create new lua process with empty code and destroy worker flag set to true
299 (ie, conclusion of lua process WILL result in worker thread destruction */ 306 (ie, conclusion of lua process WILL result in worker thread destruction */
300 lp = luaproc_new( "", TRUE ); 307 lp = luaproc_new( "", TRUE, FALSE );
301 308
302 /* ensure process creation was successfull */ 309 /* ensure process creation was successfull */
303 if ( lp == NULL ) { 310 if ( lp == NULL ) {
@@ -328,7 +335,7 @@ static int luaproc_destroy_worker( lua_State *L ) {
328} 335}
329 336
330/* recycle a lua process */ 337/* recycle a lua process */
331luaproc luaproc_recycle( luaproc lp, const char *code ) { 338static luaproc luaproc_recycle( luaproc lp, const char *code, int file ) {
332 339
333 int ret; 340 int ret;
334 341
@@ -351,12 +358,9 @@ luaproc luaproc_recycle( luaproc lp, const char *code ) {
351 return lp; 358 return lp;
352} 359}
353 360
354/* create and schedule a new lua process (luaproc.newproc) */
355static int luaproc_create_newproc( lua_State *L ) {
356
357 /* check if first argument is a string (lua code) */
358 const char *code = luaL_checkstring( L, 1 );
359 361
362int newProc(const char *code, int file)
363{
360 /* new lua process pointer */ 364 /* new lua process pointer */
361 luaproc lp; 365 luaproc lp;
362 366
@@ -365,21 +369,18 @@ static int luaproc_create_newproc( lua_State *L ) {
365 369
366 /* if there is a lua process available on the recycle queue, recycle it */ 370 /* if there is a lua process available on the recycle queue, recycle it */
367 if ( lp != NULL ) { 371 if ( lp != NULL ) {
368 lp = luaproc_recycle( lp, code ); 372 lp = luaproc_recycle( lp, code, file );
369 } 373 }
370 /* otherwise create a new one from scratch */ 374 /* otherwise create a new one from scratch */
371 else { 375 else {
372 /* create new lua process with destroy worker flag set to false 376 /* create new lua process with destroy worker flag set to false
373 (ie, conclusion of lua process will NOT result in worker thread destruction */ 377 (ie, conclusion of lua process will NOT result in worker thread destruction */
374 lp = luaproc_new( code, FALSE ); 378 lp = luaproc_new( code, FALSE, file );
375 } 379 }
376 380
377 /* ensure process creation was successfull */ 381 /* ensure process creation was successfull */
378 if ( lp == NULL ) { 382 if ( lp == NULL ) {
379 /* in case of errors return nil + error msg */ 383 return 1;
380 lua_pushnil( L );
381 lua_pushstring( L, "error loading code string" );
382 return 2;
383 } 384 }
384 385
385 /* increase active luaproc count */ 386 /* increase active luaproc count */
@@ -392,6 +393,26 @@ static int luaproc_create_newproc( lua_State *L ) {
392 sched_lpcount_dec(); 393 sched_lpcount_dec();
393 /* close lua_State */ 394 /* close lua_State */
394 lua_close( lp->lstate ); 395 lua_close( lp->lstate );
396 return 2;
397 }
398
399 return 0;
400}
401
402/* create and schedule a new lua process (luaproc.newproc) */
403static int luaproc_create_newproc( lua_State *L ) {
404
405 /* check if first argument is a string (lua code) */
406 const char *code = luaL_checkstring( L, 1 );
407
408 switch (newProc(code, FALSE))
409 {
410 case 1 :
411 /* in case of errors return nil + error msg */
412 lua_pushnil( L );
413 lua_pushstring( L, "error loading code string" );
414 return 2;
415 case 2 :
395 /* return nil + error msg */ 416 /* return nil + error msg */
396 lua_pushnil( L ); 417 lua_pushnil( L );
397 lua_pushstring( L, "error queuing process" ); 418 lua_pushstring( L, "error queuing process" );
@@ -635,17 +656,24 @@ static int luaproc_receive( lua_State *L ) {
635 } 656 }
636} 657}
637 658
638LUALIB_API int luaopen_luaproc( lua_State *L ) { 659void luaprocInit(void)
639 660{
640 /* register luaproc functions */
641 luaL_register( L, "luaproc", luaproc_funcs_parent );
642
643 /* initialize recycle list */ 661 /* initialize recycle list */
644 recyclelp = list_new(); 662 recyclelp = list_new();
645 663
646 /* initialize local scheduler */ 664 /* initialize local scheduler */
647 sched_init_local( LUAPROC_SCHED_DEFAULT_WORKER_THREADS ); 665 sched_init_local( LUAPROC_SCHED_DEFAULT_WORKER_THREADS );
666}
648 667
668void luaprocRegister(lua_State *L)
669{
670 /* register luaproc functions */
671 luaL_register( L, "luaproc", luaproc_funcs_parent );
672}
673
674LUALIB_API int luaopen_luaproc( lua_State *L ) {
675 luaprocRegister(L);
676 luaprocInit();
649 return 0; 677 return 0;
650} 678}
651 679
diff --git a/libraries/luaproc/luaproc.h b/libraries/luaproc/luaproc.h
index 86617a9..4c71685 100644
--- a/libraries/luaproc/luaproc.h
+++ b/libraries/luaproc/luaproc.h
@@ -44,6 +44,11 @@ THE SOFTWARE.
44/* lua process pointer type */ 44/* lua process pointer type */
45typedef struct stluaproc *luaproc; 45typedef struct stluaproc *luaproc;
46 46
47void luaprocInit(void);
48void luaprocRegister(lua_State *L);
49int newProc(const char *code, int file);
50
51
47/* return a process' status */ 52/* return a process' status */
48int luaproc_get_status( luaproc lp ); 53int luaproc_get_status( luaproc lp );
49 54
diff --git a/libraries/luaproc/sched.c b/libraries/luaproc/sched.c
index f19beeb..474c82b 100644
--- a/libraries/luaproc/sched.c
+++ b/libraries/luaproc/sched.c
@@ -83,9 +83,11 @@ void *workermain( void *args ) {
83 /* detach thread so resources are freed as soon as thread exits (no further joining) */ 83 /* detach thread so resources are freed as soon as thread exits (no further joining) */
84 pthread_detach( pthread_self( )); 84 pthread_detach( pthread_self( ));
85 85
86//printf("NEW WORKER\n");
86 /* main worker loop */ 87 /* main worker loop */
87 while ( 1 ) { 88 while ( 1 ) {
88 89
90//printf("a\n");
89 /* get exclusive access to the ready process queue */ 91 /* get exclusive access to the ready process queue */
90 pthread_mutex_lock( &mutex_queue_access ); 92 pthread_mutex_lock( &mutex_queue_access );
91 93
@@ -94,62 +96,83 @@ void *workermain( void *args ) {
94 pthread_cond_wait( &cond_wakeup_worker, &mutex_queue_access ); 96 pthread_cond_wait( &cond_wakeup_worker, &mutex_queue_access );
95 } 97 }
96 98
99////printf("b\n");
97 /* pop the first node from the ready process queue */ 100 /* pop the first node from the ready process queue */
98 n = list_pop_head( lpready ); 101 n = list_pop_head( lpready );
99 102
103////printf("c\n");
100 /* ensure list pop succeeded before proceeding */ 104 /* ensure list pop succeeded before proceeding */
101 if ( n != NULL ) { 105 if ( n != NULL ) {
106//printf("c.0\n");
102 /* get the popped node's data content (ie, the lua process struct) */ 107 /* get the popped node's data content (ie, the lua process struct) */
103 lp = (luaproc )list_data( n ); 108 lp = (luaproc )list_data( n );
104 } 109 }
105 else { 110 else {
111////printf("c.1\n");
106 /* free access to the process ready queue */ 112 /* free access to the process ready queue */
107 pthread_mutex_unlock( &mutex_queue_access ); 113 pthread_mutex_unlock( &mutex_queue_access );
108 /* finished thread */ 114 /* finished thread */
115//printf("c.2 pthread_exit\n");
109 pthread_exit( NULL ); 116 pthread_exit( NULL );
117//printf("c.3\n");
110 } 118 }
111 119
120////printf("d\n");
112 /* free access to the process ready queue */ 121 /* free access to the process ready queue */
113 pthread_mutex_unlock( &mutex_queue_access ); 122 pthread_mutex_unlock( &mutex_queue_access );
114 123
124//printf("e lua_resum\n");
115 /* execute the lua code specified in the lua process struct */ 125 /* execute the lua code specified in the lua process struct */
116 procstat = lua_resume( luaproc_get_state( lp ), luaproc_get_args( lp )); 126 procstat = lua_resume( luaproc_get_state( lp ), luaproc_get_args( lp ));
117 127
128//printf("f\n");
118 /* reset the process argument count */ 129 /* reset the process argument count */
119 luaproc_set_args( lp, 0 ); 130 luaproc_set_args( lp, 0 );
120 131
132////printf("g\n");
121 /* check if process finished its whole execution */ 133 /* check if process finished its whole execution */
122 if ( procstat == 0 ) { 134 if ( procstat == 0 ) {
123 135
136//printf("g.0\n");
124 /* destroy the corresponding list node */ 137 /* destroy the corresponding list node */
125 list_destroy_node( n ); 138 list_destroy_node( n );
126 139
140////printf("g.1\n");
127 /* check if worker thread should be destroyed */ 141 /* check if worker thread should be destroyed */
128 destroyworker = luaproc_get_destroyworker( lp ); 142 destroyworker = luaproc_get_destroyworker( lp );
129 143
144////printf("g.2 proc finished\n");
130 /* set process status to finished */ 145 /* set process status to finished */
131 luaproc_set_status( lp, LUAPROC_STAT_FINISHED ); 146 luaproc_set_status( lp, LUAPROC_STAT_FINISHED );
132 147
148////printf("g.3\n");
133 /* check if lua process should be recycled and, if not, destroy it */ 149 /* check if lua process should be recycled and, if not, destroy it */
134 if ( luaproc_recycle_push( lp ) == FALSE ) { 150 if ( luaproc_recycle_push( lp ) == FALSE ) {
151//printf("g.3.0 lua_close\n");
135 lua_close( luaproc_get_state( lp )); 152 lua_close( luaproc_get_state( lp ));
136 } 153 }
137 154
155////printf("g.4\n");
138 /* decrease active lua process count */ 156 /* decrease active lua process count */
139 sched_lpcount_dec(); 157 sched_lpcount_dec();
140 158
159////printf("g.5\n");
141 /* check if thread should be finished after lua process conclusion */ 160 /* check if thread should be finished after lua process conclusion */
142 if ( destroyworker ) { 161 if ( destroyworker ) {
162//printf("g.5.0 pthread_exit\n");
143 /* if so, finish thread */ 163 /* if so, finish thread */
144 pthread_exit( NULL ); 164 pthread_exit( NULL );
145 } 165 }
166//printf("g.6\n");
146 } 167 }
147 168
148 /* check if process yielded */ 169 /* check if process yielded */
149 else if ( procstat == LUA_YIELD ) { 170 else if ( procstat == LUA_YIELD ) {
150 171
172//printf("??????????????h.0\n");
151 /* if so, further check if yield originated from an unmatched send/recv operation */ 173 /* if so, further check if yield originated from an unmatched send/recv operation */
152 if ( luaproc_get_status( lp ) == LUAPROC_STAT_BLOCKED_SEND ) { 174 if ( luaproc_get_status( lp ) == LUAPROC_STAT_BLOCKED_SEND ) {
175//printf("??????????????h.1\n");
153 /* queue blocked lua process on corresponding channel */ 176 /* queue blocked lua process on corresponding channel */
154 luaproc_queue_sender( lp ); 177 luaproc_queue_sender( lp );
155 /* unlock channel access */ 178 /* unlock channel access */
@@ -159,6 +182,7 @@ void *workermain( void *args ) {
159 } 182 }
160 183
161 else if ( luaproc_get_status( lp ) == LUAPROC_STAT_BLOCKED_RECV ) { 184 else if ( luaproc_get_status( lp ) == LUAPROC_STAT_BLOCKED_RECV ) {
185//printf("??????????????h.2\n");
162 /* queue blocked lua process on corresponding channel */ 186 /* queue blocked lua process on corresponding channel */
163 luaproc_queue_receiver( lp ); 187 luaproc_queue_receiver( lp );
164 /* unlock channel access */ 188 /* unlock channel access */
@@ -169,6 +193,7 @@ void *workermain( void *args ) {
169 193
170 /* or if yield resulted from an explicit call to coroutine.yield in the lua code being executed */ 194 /* or if yield resulted from an explicit call to coroutine.yield in the lua code being executed */
171 else { 195 else {
196//printf("??????????????h.3\n");
172 /* get exclusive access to the ready process queue */ 197 /* get exclusive access to the ready process queue */
173 pthread_mutex_lock( &mutex_queue_access ); 198 pthread_mutex_lock( &mutex_queue_access );
174 /* re-insert the job at the end of the ready process queue */ 199 /* re-insert the job at the end of the ready process queue */
@@ -180,6 +205,7 @@ void *workermain( void *args ) {
180 205
181 /* check if there was any execution error (LUA_ERRRUN, LUA_ERRSYNTAX, LUA_ERRMEM or LUA_ERRERR) */ 206 /* check if there was any execution error (LUA_ERRRUN, LUA_ERRSYNTAX, LUA_ERRMEM or LUA_ERRERR) */
182 else { 207 else {
208//printf("??????????????i.0\n");
183 /* destroy the corresponding node */ 209 /* destroy the corresponding node */
184 list_destroy_node( n ); 210 list_destroy_node( n );
185 /* print error message */ 211 /* print error message */
@@ -189,6 +215,7 @@ void *workermain( void *args ) {
189 /* decrease active lua process count */ 215 /* decrease active lua process count */
190 sched_lpcount_dec(); 216 sched_lpcount_dec();
191 } 217 }
218//printf("END\n");
192 } 219 }
193} 220}
194 221
@@ -260,29 +287,42 @@ int sched_queue_proc( luaproc lp ) {
260/* synchronize worker threads */ 287/* synchronize worker threads */
261void sched_join_workerthreads( void ) { 288void sched_join_workerthreads( void ) {
262 289
290////printf(" 0\n");
263 pthread_mutex_lock( &mutex_lp_count ); 291 pthread_mutex_lock( &mutex_lp_count );
264 292
293//printf(" 1 wait for procs to end\n");
265 /* wait until there is no more active lua processes */ 294 /* wait until there is no more active lua processes */
266 while( lpcount != 0 ) { 295 while( lpcount != 0 ) {
296//printf(" 1.0\n");
267 pthread_cond_wait( &cond_no_active_lp, &mutex_lp_count ); 297 pthread_cond_wait( &cond_no_active_lp, &mutex_lp_count );
268 } 298 }
269 /* get exclusive access to the ready process queue */ 299 /* get exclusive access to the ready process queue */
300////printf(" 2\n");
270 pthread_mutex_lock( &mutex_queue_access ); 301 pthread_mutex_lock( &mutex_queue_access );
271 /* set the no more active lua processes flag to true */ 302 /* set the no more active lua processes flag to true */
303////printf(" 3\n");
272 no_more_processes = TRUE; 304 no_more_processes = TRUE;
273 /* wake ALL workers up */ 305 /* wake ALL workers up */
306//printf(" 4 wake up all workers.\n");
274 pthread_cond_broadcast( &cond_wakeup_worker ); 307 pthread_cond_broadcast( &cond_wakeup_worker );
275 /* free access to the process ready queue */ 308 /* free access to the process ready queue */
309////printf(" 5\n");
276 pthread_mutex_unlock( &mutex_queue_access ); 310 pthread_mutex_unlock( &mutex_queue_access );
311
312// We don't need this, as we only get here during shutdown. Linking this to EFL results in a hang otherwise anyway.
277 /* wait for (join) worker threads */ 313 /* wait for (join) worker threads */
278 pthread_exit( NULL ); 314//printf(" 6 pthread_exit, waiting for workers to end\n");
315// pthread_exit( NULL );
279 316
317//printf("7\n");
280 pthread_mutex_unlock( &mutex_lp_count ); 318 pthread_mutex_unlock( &mutex_lp_count );
281 319
320//printf("8\n");
282} 321}
283 322
284/* increase active lua process count */ 323/* increase active lua process count */
285void sched_lpcount_inc( void ) { 324void sched_lpcount_inc( void ) {
325//printf("inc procs++++++++++++++++++++++++++++++++++++++++\n");
286 pthread_mutex_lock( &mutex_lp_count ); 326 pthread_mutex_lock( &mutex_lp_count );
287 lpcount++; 327 lpcount++;
288 pthread_mutex_unlock( &mutex_lp_count ); 328 pthread_mutex_unlock( &mutex_lp_count );
@@ -290,10 +330,12 @@ void sched_lpcount_inc( void ) {
290 330
291/* decrease active lua process count */ 331/* decrease active lua process count */
292void sched_lpcount_dec( void ) { 332void sched_lpcount_dec( void ) {
333//printf("dec procs----------------------------------------\n");
293 pthread_mutex_lock( &mutex_lp_count ); 334 pthread_mutex_lock( &mutex_lp_count );
294 lpcount--; 335 lpcount--;
295 /* if count reaches zero, signal there are no more active processes */ 336 /* if count reaches zero, signal there are no more active processes */
296 if ( lpcount == 0 ) { 337 if ( lpcount == 0 ) {
338//printf("dec procs AND NONE LEFT000000000000000000000000000\n");
297 pthread_cond_signal( &cond_no_active_lp ); 339 pthread_cond_signal( &cond_no_active_lp );
298 } 340 }
299 pthread_mutex_unlock( &mutex_lp_count ); 341 pthread_mutex_unlock( &mutex_lp_count );
diff --git a/test.sh b/test.sh
index f0da24e..9c6f58a 100755
--- a/test.sh
+++ b/test.sh
@@ -7,6 +7,8 @@ wd=$(pwd)
7./build.sh || exit 7./build.sh || exit
8 8
9echo "_______________ TESTING LuaSL _______________" 9echo "_______________ TESTING LuaSL _______________"
10cd $wd/LuaSL 10cd $wd/LuaSL/testLua
11./LuaSL 11export LUA_SOPATH='../../libraries/luaproc/'
12export LD_LIBRARY_PATH="../../libraries/luajit-2.0/src:$LD_LIBRARY_PATH"
13../LuaSL
12 14