Skip to content
Snippets Groups Projects
Commit 751fe073 authored by dg's avatar dg
Browse files

revert lua lane to 2.0.3 as it seems to make stuff go wild :/

git-svn-id: http://svn.net-core.org/repos/t-engine4@2493 51575b47-30f0-44d4-a5cc-537603b46e54
parent 3123d9e9
No related branches found
No related tags found
No related merge requests found
......@@ -11,7 +11,7 @@
--[[
===============================================================================
Copyright (C) 2008-10 Asko Kauppi <akauppi@gmail.com>
Copyright (C) 2008 Asko Kauppi <akauppi@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
......@@ -135,7 +135,7 @@ function send( ud, key, ... )
local m= limits[key]
if m and len+n > m then
return false -- would exceed the limit; try again later
return false -- would exceed the limit; try again later
end
for i=1,n do
......
......@@ -6,8 +6,7 @@
-- Author: Asko Kauppi <akauppi@gmail.com>
--
-- History:
-- 3-Dec-10 BGe: Added support to generate a lane from a string
-- Jun-08 AKa: major revise
-- Jun-08 AKa: major revise
-- 15-May-07 AKa: pthread_join():less version, some speedup & ability to
-- handle more threads (~ 8000-9000, up from ~ 5000)
-- 26-Feb-07 AKa: serialization working (C side)
......@@ -16,7 +15,7 @@
--[[
===============================================================================
Copyright (C) 2007-10 Asko Kauppi <akauppi@gmail.com>
Copyright (C) 2007-08 Asko Kauppi <akauppi@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
......@@ -90,7 +89,7 @@ ABOUT=
author= "Asko Kauppi <akauppi@gmail.com>",
description= "Running multiple Lua states in parallel",
license= "MIT/X11",
copyright= "Copyright (c) 2007-10, Asko Kauppi",
copyright= "Copyright (c) 2007-08, Asko Kauppi",
version= _version,
}
......@@ -124,14 +123,6 @@ end
--
-- lane_h.state: "pending"/"running"/"waiting"/"done"/"error"/"cancelled"
--
-- Note: Would be great to be able to have '__ipairs' metamethod, that gets
-- called by 'ipairs()' function to custom iterate objects. We'd use it
-- for making sure a lane has ended (results are available); not requiring
-- the user to precede a loop by explicit 'h[0]' or 'h:join()'.
--
-- Or, even better, 'ipairs()' should start valuing '__index' instead
-- of using raw reads that bypass it.
--
local lane_mt= {
__index= function( me, k )
if type(k) == "number" then
......@@ -269,9 +260,8 @@ function gen( ... )
end
local func= select(n,...)
local functype = type(func)
if functype ~= "function" and functype ~= "string" then
error( "Last parameter not function or string: "..tostring(func))
if type(func)~="function" then
error( "Last parameter not function: "..tostring(func) )
end
-- Check 'libs' already here, so the error goes in the right place
......@@ -312,10 +302,9 @@ lane_proxy= function( ud )
local proxy= {
_ud= ud,
-- true|false= me:cancel()
-- void= me:cancel()
--
cancel= function(me, time, force) return thread_cancel(me._ud, time, force) end,
cancel= function(me) thread_cancel(me._ud) end,
-- [...] | [nil,err,stack_tbl]= me:join( [wait_secs=-1] )
--
......@@ -497,7 +486,7 @@ if first_time then
return next_wakeup -- may be 'nil'
end
--[[
-----
-- Snore loop (run as a lane on the background)
--
......@@ -506,18 +495,14 @@ if first_time then
-- We let the timer lane be a "free running" thread; no handle to it
-- remains.
--
gen( "io", { priority=max_prio, globals={threadName="LanesTimer"} }, function()
gen( "io", { priority=max_prio }, function()
while true do
local next_wakeup= check_timers()
-- Sleep until next timer to wake up, or a set/clear command
--
local secs
if next_wakeup then
secs = next_wakeup - now_secs()
if secs < 0 then secs = 0 end
end
local secs= next_wakeup and (next_wakeup - now_secs()) or nil
local linda= timer_gateway:receive( secs, TGW_KEY )
if linda then
......@@ -530,7 +515,6 @@ if first_time then
end
end
end )()
]]
end
-----
......
This diff is collapsed.
/*
* THREADING.C Copyright (c) 2007-10, Asko Kauppi
* THREADING.C Copyright (c) 2007-08, Asko Kauppi
*
* Lua Lanes OS threading specific code.
*
......@@ -10,7 +10,7 @@
/*
===============================================================================
Copyright (C) 2007-10 Asko Kauppi <akauppi@gmail.com>
Copyright (C) 2007-08 Asko Kauppi <akauppi@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
......@@ -178,11 +178,6 @@ static void prepare_timeout( struct timespec *ts, time_d abs_secs ) {
ts->tv_sec= floor( abs_secs );
ts->tv_nsec= ((long)((abs_secs - ts->tv_sec) * 1000.0 +0.5)) * 1000000UL; // 1ms = 1000000ns
if (ts->tv_nsec == 1000000000UL)
{
ts->tv_nsec = 0;
ts->tv_sec = ts->tv_sec + 1;
}
}
#endif
......
......@@ -37,7 +37,7 @@ typedef unsigned int uint_t;
/* Do this BEFORE including time.h so that it is declaring _mktime32()
* as it would have declared mktime().
*/
# define mktime _mktime32
//# define mktime _mktime32
#endif
#include <time.h>
......
/*
* TOOLS.C Copyright (c) 2002-10, Asko Kauppi
* TOOLS.C Copyright (c) 2002-08, Asko Kauppi
*
* Lua tools to support Lanes.
*/
......@@ -7,7 +7,7 @@
/*
===============================================================================
Copyright (C) 2002-10 Asko Kauppi <akauppi@gmail.com>
Copyright (C) 2002-08 Asko Kauppi <akauppi@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
......@@ -40,6 +40,8 @@ THE SOFTWARE.
#include <ctype.h>
#include <stdlib.h>
static volatile lua_CFunction hijacked_tostring; // = NULL
MUTEX_T deep_lock;
MUTEX_T mtid_lock;
......@@ -614,7 +616,7 @@ uint_t get_mt_id( lua_State *L, int i ) {
// [-2]: reg[REG_MTID]
// [-1]: nil/uint
id= (uint_t)lua_tointeger(L,-1); // 0 for nil
id= lua_tointeger(L,-1); // 0 for nil
lua_pop(L,1);
STACK_MID(L,1)
......@@ -658,60 +660,73 @@ static int buf_writer( lua_State *L, const void* b, size_t n, void* B ) {
* Returns TRUE if the table was cached (no need to fill it!); FALSE if
* it's a virgin.
*/
static bool_t push_cached_table( lua_State *L2, uint_t L2_cache_i, lua_State *L, uint_t i )
{
bool_t ret;
static
bool_t push_cached_table( lua_State *L2, uint_t L2_cache_i, lua_State *L, uint_t i ) {
bool_t ret;
ASSERT_L( L2_cache_i != 0 );
ASSERT_L( hijacked_tostring );
ASSERT_L( L2_cache_i != 0 );
STACK_GROW(L2,3);
STACK_GROW(L,2);
STACK_GROW(L2,3);
// L2_cache[id_str]= [{...}]
//
STACK_CHECK(L2)
// Create an identity string for table at [i]; it should stay unique at
// least during copying of the data (then we can clear the caches).
//
STACK_CHECK(L)
lua_pushcfunction( L, hijacked_tostring );
lua_pushvalue( L, i );
lua_call( L, 1 /*args*/, 1 /*retvals*/ );
//
// [-1]: "table: 0x...."
// We don't need to use the from state ('L') in ID since the life span
// is only for the duration of a copy (both states are locked).
//
lua_pushlightuserdata( L2, (void*)lua_topointer( L, i )); // push a light userdata uniquely representing the table
STACK_END(L,1)
ASSERT_L( lua_type(L,-1) == LUA_TSTRING );
//fprintf( stderr, "<< ID: %s >>\n", lua_tostring(L2,-1) );
// L2_cache[id_str]= [{...}]
//
STACK_CHECK(L2)
lua_pushvalue( L2, -1 );
lua_rawget( L2, L2_cache_i );
//
// [-2]: identity table pointer lightuserdata
// [-1]: table|nil
// We don't need to use the from state ('L') in ID since the life span
// is only for the duration of a copy (both states are locked).
//
lua_pushstring( L2, lua_tostring(L,-1) );
lua_pop(L,1); // remove the 'tostring(tbl)' value (in L!)
if (lua_isnil(L2,-1))
{
lua_pop(L2,1);
lua_newtable(L2);
lua_pushvalue(L2,-1);
lua_insert(L2,-3);
//
// [-3]: new table (2nd ref)
// [-2]: identity table pointer lightuserdata
// [-1]: new table
//fprintf( stderr, "<< ID: %s >>\n", lua_tostring(L2,-1) );
lua_rawset(L2, L2_cache_i);
//
// [-1]: new table (tied to 'L2_cache' table')
ret= FALSE; // brand new
}
else
{
lua_remove(L2,-2);
ret= TRUE; // from cache
}
STACK_END(L2,1)
//
// L2 [-1]: table to use as destination
ASSERT_L( lua_istable(L2,-1) );
return ret;
lua_pushvalue( L2, -1 );
lua_rawget( L2, L2_cache_i );
//
// [-2]: identity string ("table: 0x...")
// [-1]: table|nil
if (lua_isnil(L2,-1)) {
lua_pop(L2,1);
lua_newtable(L2);
lua_pushvalue(L2,-1);
lua_insert(L2,-3);
//
// [-3]: new table (2nd ref)
// [-2]: identity string
// [-1]: new table
lua_rawset(L2, L2_cache_i);
//
// [-1]: new table (tied to 'L2_cache' table')
ret= FALSE; // brand new
} else {
lua_remove(L2,-2);
ret= TRUE; // from cache
}
STACK_END(L2,1)
//
// L2 [-1]: table to use as destination
ASSERT_L( lua_istable(L2,-1) );
return ret;
}
......@@ -723,76 +738,82 @@ static bool_t push_cached_table( lua_State *L2, uint_t L2_cache_i, lua_State *L,
*/
static void inter_copy_func( lua_State *L2, uint_t L2_cache_i, lua_State *L, uint_t i );
static void push_cached_func( lua_State *L2, uint_t L2_cache_i, lua_State *L, uint_t i )
{
// TBD: Merge this and same code for tables
static
void push_cached_func( lua_State *L2, uint_t L2_cache_i, lua_State *L, uint_t i ) {
// TBD: Merge this and same code for tables
ASSERT_L( L2_cache_i != 0 );
ASSERT_L( hijacked_tostring );
ASSERT_L( L2_cache_i != 0 );
STACK_GROW(L2,3);
STACK_GROW(L,2);
STACK_GROW(L2,3);
// L2_cache[id_str]= function
//
STACK_CHECK(L2)
STACK_CHECK(L)
lua_pushcfunction( L, hijacked_tostring );
lua_pushvalue( L, i );
lua_call( L, 1 /*args*/, 1 /*retvals*/ );
//
// [-1]: "function: 0x...."
// We don't need to use the from state ('L') in ID since the life span
// is only for the duration of a copy (both states are locked).
//
lua_pushlightuserdata( L2, (void*)lua_topointer( L, i )); // push a light userdata uniquely representing the function
STACK_END(L,1)
ASSERT_L( lua_type(L,-1) == LUA_TSTRING );
//fprintf( stderr, "<< ID: %s >>\n", lua_tostring(L2,-1) );
// L2_cache[id_str]= function
//
STACK_CHECK(L2)
lua_pushvalue( L2, -1 );
lua_rawget( L2, L2_cache_i );
//
// [-2]: identity lightuserdata function pointer
// [-1]: function|nil|true (true means: we're working on it; recursive)
// We don't need to use the from state ('L') in ID since the life span
// is only for the duration of a copy (both states are locked).
//
lua_pushstring( L2, lua_tostring(L,-1) );
lua_pop(L,1); // remove the 'tostring(tbl)' value (in L!)
if (lua_isnil(L2,-1))
{
lua_pop(L2,1);
//fprintf( stderr, "<< ID: %s >>\n", lua_tostring(L2,-1) );
// Set to 'true' for the duration of creation; need to find self-references
// via upvalues
//
lua_pushvalue( L2, -1);
lua_pushboolean(L2,TRUE);
lua_rawset( L2, L2_cache_i);
lua_pushvalue( L2, -1 );
lua_rawget( L2, L2_cache_i );
//
// [-2]: identity string ("function: 0x...")
// [-1]: function|nil|true (true means: we're working on it; recursive)
inter_copy_func( L2, L2_cache_i, L, i ); // pushes a copy of the func
if (lua_isnil(L2,-1)) {
lua_pop(L2,1);
lua_pushvalue(L2,-1);
lua_insert(L2,-3);
//
// [-3]: function (2nd ref)
// [-2]: identity lightuserdata function pointer
// [-1]: function
// Set to 'true' for the duration of creation; need to find self-references
// via upvalues
//
lua_pushboolean(L2,TRUE);
lua_setfield( L2, L2_cache_i, lua_tostring(L2,-2) );
lua_rawset(L2,L2_cache_i);
//
// [-1]: function (tied to 'L2_cache' table')
inter_copy_func( L2, L2_cache_i, L, i ); // pushes a copy of the func
}
else if (lua_isboolean(L2,-1))
{
// Loop in preparing upvalues; either direct or via a table
//
// Note: This excludes the case where a function directly addresses
// itself as an upvalue (recursive lane creation).
//
STACK_GROW(L,1);
luaL_error( L, "Recursive use of upvalues; cannot copy the function" );
}
else
{
lua_remove(L2,-2);
}
STACK_END(L2,1)
//
// L2 [-1]: function
ASSERT_L( lua_isfunction(L2,-1) );
lua_pushvalue(L2,-1);
lua_insert(L2,-3);
//
// [-3]: function (2nd ref)
// [-2]: identity string
// [-1]: function
lua_rawset(L2,L2_cache_i);
//
// [-1]: function (tied to 'L2_cache' table')
} else if (lua_isboolean(L2,-1)) {
// Loop in preparing upvalues; either direct or via a table
//
// Note: This excludes the case where a function directly addresses
// itself as an upvalue (recursive lane creation).
//
luaL_error( L, "Recursive use of upvalues; cannot copy the function" );
} else {
lua_remove(L2,-2);
}
STACK_END(L2,1)
//
// L2 [-1]: function
ASSERT_L( lua_isfunction(L2,-1) );
}
......@@ -1132,6 +1153,29 @@ void luaG_inter_copy( lua_State* L, lua_State *L2, uint_t n )
uint_t top_L2= lua_gettop(L2);
uint_t i;
/* steal Lua library's 'luaB_tostring()' from the first call. Other calls
* don't have to have access to it.
*
* Note: multiple threads won't come here at once; this function will
* be called before there can be multiple threads (no locking needed).
*/
if (!hijacked_tostring) {
STACK_GROW( L,1 );
STACK_CHECK(L)
lua_getglobal( L, "tostring" );
//
// [-1]: function|nil
hijacked_tostring= lua_tocfunction( L, -1 );
lua_pop(L,1);
STACK_END(L,0)
if (!hijacked_tostring) {
luaL_error( L, "Need to see 'tostring()' once" );
}
}
if (n > top_L)
luaL_error( L, "Not enough values: %d < %d", top_L, n );
......@@ -1152,7 +1196,7 @@ void luaG_inter_copy( lua_State* L, lua_State *L2, uint_t n )
}
/*
* Remove the cache table. Persistant caching would cause i.e. multiple
* Remove the cache table. persistent caching would cause i.e. multiple
* messages passed in the same table to use the same table also in receiving
* end.
*/
......
......@@ -22,7 +22,6 @@
#define STACK_END(L,c) /*nothing*/
#define STACK_DUMP(L) /*nothing*/
#define DEBUG() /*nothing*/
#define DEBUGEXEC(_code) {} /*nothing*/
#else
#define _ASSERT_L(lua,c) { if (!(c)) luaL_error( lua, "ASSERT failed: %s:%d '%s'", __FILE__, __LINE__, #c ); }
//
......@@ -33,7 +32,6 @@
#define STACK_DUMP(L) luaG_dump(L);
#define DEBUG() fprintf( stderr, "<<%s %d>>\n", __FILE__, __LINE__ );
#define DEBUGEXEC(_code) {_code;} /*nothing*/
#endif
#define ASSERT_L(c) _ASSERT_L(L,c)
......
......@@ -260,7 +260,7 @@ void on_tick()
lua_rawgeti(L, LUA_REGISTRYINDEX, current_game);
docall(L, 1, 1);
tickPaused = lua_toboolean(L, -1);
lua_pop(L, 1);
// lua_pop(L, 1);
}
/* Gather our frames per second */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment