diff --git a/core/log.c b/core/log.c index 724c518..64a5456 100644 --- a/core/log.c +++ b/core/log.c @@ -333,24 +333,19 @@ int l_log( lua_State * L ) break; case LUA_TBOOLEAN : - if( lua_toboolean( L, i ) ) - lua_pushstring( L, "(true)" ); - else - lua_pushstring( L, "(false)" ); + + lua_pushstring( L, lua_toboolean( L, i ) ? "(true)" : "(false)" ); lua_replace( L, i ); break; case LUA_TUSERDATA: - { - clock_t *c = ( clock_t * ) luaL_checkudata( L, i, "Lsyncd.jiffies" ); - double d = *c; - d /= clocks_per_sec; - lua_pushfstring( L, "(Timestamp: %f)", d ); - lua_replace( L, i ); - } + lua_pushfstring( L, "(Timestamp: %f)", check_jiffies_arg( L, i ) ); + + lua_replace( L, i ); + break; case LUA_TNIL: diff --git a/core/main.c b/core/main.c index 0f6e2e6..646b274 100644 --- a/core/main.c +++ b/core/main.c @@ -176,10 +176,7 @@ masterloop( if( have_alarm ) { - // TODO use trunc instead of long conversions - double d = ( ( double )( alarm_time - cnow ) ) / clocks_per_sec; - tv.tv_sec = d; - tv.tv_nsec = ( ( d - ( long ) d ) ) * 1000000000.0; + double d = time_diff( alarm_time, cnow, &tv ); printlogf( L, "Masterloop", @@ -321,12 +318,6 @@ main1( int argc, char *argv[] ) // registers Lsycnd's core library register_core( L ); - if( check_logcat( "Debug" ) <= settings.log_level ) - { - // printlogf doesnt support %ld :-( - printf( "kernels clocks_per_sec=%ld\n", clocks_per_sec ); - } - signal_init( ); #ifdef WITH_INOTIFY @@ -484,12 +475,11 @@ main1( int argc, char *argv[] ) int main( int argc, char * argv[ ] ) { - // gets a kernel parameter - clocks_per_sec = sysconf( _SC_CLK_TCK ); - setlinebuf( stdout ); setlinebuf( stderr ); + time_first_init( ); + while( true ) main1( argc, argv ); // return -1; diff --git a/core/mci.c b/core/mci.c index 029a862..a75a8f1 100644 --- a/core/mci.c +++ b/core/mci.c @@ -679,7 +679,7 @@ static const luaL_Reg corelib[ ] = /* -| Registers the Lsyncd's core library. +| Registers Lsyncd's core library. */ void register_core( lua_State *L ) diff --git a/core/mci.h b/core/mci.h index a9f8775..2c5d6ce 100644 --- a/core/mci.h +++ b/core/mci.h @@ -14,7 +14,7 @@ extern int l_stackdump( lua_State* L ); -// FIXME +// Registers Lsyncd's core library. extern void register_core( lua_State *L ); diff --git a/core/time.c b/core/time.c index b38dd86..ab43c8f 100644 --- a/core/time.c +++ b/core/time.c @@ -14,6 +14,7 @@ #include #include #include +#include #define LUA_USE_APICHECK 1 #include @@ -26,8 +27,7 @@ /* | The kernel's clock ticks per second. */ -// FIXME make static again -long clocks_per_sec; +static long clocks_per_sec; /* | Non glibc builds need a real tms structure for the times( ) call @@ -40,6 +40,17 @@ long clocks_per_sec; #endif +/* +| Initializes time management. +*/ +void +time_first_init( ) +{ + // gets a kernel parameter + clocks_per_sec = sysconf( _SC_CLK_TCK ); +} + + /* | Returns the current time. */ @@ -53,7 +64,7 @@ clock_t now( ) | Returns (on Lua stack) the current kernels clock state (jiffies). */ int -l_now(lua_State *L) +l_now( lua_State *L ) { clock_t * j = lua_newuserdata( L, sizeof( clock_t ) ); luaL_getmetatable( L, "Lsyncd.jiffies" ); @@ -79,16 +90,15 @@ l_jiffies_add( lua_State *L ) exit( -1 ); } - { - clock_t a1 = p1 ? *p1 : luaL_checknumber( L, 1 ) * clocks_per_sec; - clock_t a2 = p2 ? *p2 : luaL_checknumber( L, 2 ) * clocks_per_sec; - clock_t *r = ( clock_t * ) lua_newuserdata( L, sizeof( clock_t ) ); + clock_t a1 = p1 ? *p1 : luaL_checknumber( L, 1 ) * clocks_per_sec; + clock_t a2 = p2 ? *p2 : luaL_checknumber( L, 2 ) * clocks_per_sec; + clock_t *r = ( clock_t * ) lua_newuserdata( L, sizeof( clock_t ) ); - luaL_getmetatable( L, "Lsyncd.jiffies" ); - lua_setmetatable( L, -2 ); - *r = a1 + a2; - return 1; - } + luaL_getmetatable( L, "Lsyncd.jiffies" ); + lua_setmetatable( L, -2 ); + *r = a1 + a2; + + return 1; } @@ -159,12 +169,13 @@ l_jiffies_lt( lua_State *L ) | True if jiffy1 is before or equals jiffy2. */ static int -l_jiffies_le(lua_State *L) +l_jiffies_le( lua_State *L ) { clock_t a1 = ( *( clock_t * ) luaL_checkudata( L, 1, "Lsyncd.jiffies" ) ); clock_t a2 = ( *( clock_t * ) luaL_checkudata( L, 2, "Lsyncd.jiffies" ) ); lua_pushboolean( L, ( a1 == a2 ) || time_before( a1, a2 ) ); + return 1; } @@ -197,3 +208,44 @@ register_jiffies( lua_State *L ) lua_pop( L, 1 ); // pop( mt ) } + +/* +| Puts the time difference between 't2' (later) and 't1' into 'tv'. +| Returns the time difference as double value. +*/ +double +time_diff( + long t2, + long t1, + struct timespec * tv +) +{ + double d = ( ( double )( t2 - t1 ) ) / clocks_per_sec; + + if( tv ) + { + // TODO use trunc instead of long conversions + tv->tv_sec = d; + tv->tv_nsec = ( ( d - ( long ) d ) ) * 1000000000.0; + } + + return d; +} + + +/* +| Checks if the function argument 'arg' on Lua stack is a jiffie +| and returns the value converted to seconds. +*/ +double +check_jiffies_arg +( + lua_State *L, + int arg +) +{ + clock_t *c = ( clock_t * ) luaL_checkudata( L, arg, "Lsyncd.jiffies" ); + + return ( ( double ) *c ) / clocks_per_sec; +} + diff --git a/core/time.h b/core/time.h index 3ee80ed..5c94c31 100644 --- a/core/time.h +++ b/core/time.h @@ -16,20 +16,29 @@ #define time_before_eq(a,b) time_after_eq(b,a) -// The kernel's clock ticks per second. -extern long clocks_per_sec; +// Initializes time management. +extern void time_first_init( ); // Returns the current time. extern clock_t now( ); +// Puts the time difference between 't2' (later) and 't1' into 'tv' +extern double time_diff( long t2, long t1, struct timespec * tv ); + + // Returns (on Lua stack) the current kernels clock state( jiffies ). -extern int l_now(lua_State *L); +extern int l_now( lua_State *L ); // Registers the jiffies meta table in a Lua state. extern void register_jiffies( lua_State *L ); +// Checks if the function argument 'arg' on Lua stack is a jiffie +// and returns the value converted to seconds. +extern double check_jiffies_arg ( lua_State *L, int arg ); + + #endif diff --git a/mantle/inlet.lua b/mantle/inlet.lua index 9f95cb2..7678d9b 100644 --- a/mantle/inlet.lua +++ b/mantle/inlet.lua @@ -269,8 +269,7 @@ local eventFields = end, -- - -- Returns true if the sync this event belongs - -- to is stopped + -- Returns true if the sync this event belongs to is stopped -- syncStopped = function ( @@ -359,27 +358,36 @@ local eventMeta = -- --- Interface for user scripts to get list fields. +-- Interface for user script to get list fields. +-- +local eventListFields = +{ + -- + -- Returns true if the sync this event list belongs to is stopped + -- + syncStopped = function + ( + dlist + ) + return dlist.sync.stopped + end, +} + + +-- +-- Interface for user scripts to get list functions. -- local eventListFuncs = { -- -- Returns a list of paths of all events in list. -- - -- getPaths = function ( - elist, -- handle returned by getEvents( ) + dlist, -- delayed list mutator -- if not nil called with ( etype, path, path2 ) -- returns one or two strings to add. ) - local dlist = e2d[ elist ] - - if not dlist - then - error( 'cannot find delay list from event list.' ) - end - local result = { } local resultn = 1 @@ -407,8 +415,7 @@ local eventListFuncs = end return result - - end + end, } @@ -420,26 +427,22 @@ local eventListMeta = __index = function ( elist, - func + field ) - if func == 'isList' then return true end + if field == 'isList' then return true end - if func == 'config' then return e2d[ elist ].sync.config end + -- FIXME make an actual field + if field == 'config' then return e2d[ elist ].sync.config end - local f = eventListFuncs[ func ] + local f = eventListFields[ field ] - if not f - then - error( - 'event list does not have function "' .. func .. '"', - 2 - ) - end + if f then return f( e2d[ elist ] ) end - return function - ( ... ) - return f( elist, ... ) - end + f = eventListFuncs[ field ] + + if not f then error( 'event list does not have field "' .. field .. '"', 2 ) end + + return function( ... ) return f( e2d[ elist ], ... ) end end } diff --git a/mantle/syncmaster.lua b/mantle/syncmaster.lua index 3dfbd15..74a570e 100644 --- a/mantle/syncmaster.lua +++ b/mantle/syncmaster.lua @@ -45,6 +45,19 @@ mt.__ipairs = function end +-- +-- Returns sync at listpos key +-- +mt.__index = function +( + self, + key +) + if type( key ) ~= 'number' then error( "key not a number" ) end + + return syncList[ key ] +end + -- -- The round robin counter. In case of global limited maxProcesses -- gives every sync equal chances to spawn the next process. @@ -72,13 +85,6 @@ local function getRound return round end --- --- Returns sync at listpos i --- -local function get -( i ) - return syncList[ i ] -end local inherit @@ -353,7 +359,6 @@ end SyncMaster = { add = add, -- FIXME forward through metatable - get = get, -- FIXME forward through metatable remove = remove, getRound = getRound, concerns = concerns, diff --git a/mantle/user.lua b/mantle/user.lua index 52979e5..f10e05f 100644 --- a/mantle/user.lua +++ b/mantle/user.lua @@ -300,7 +300,7 @@ user.syncs = for k = 0, #SyncMaster - 1 do - list[ k ] = SyncMaster.get( k ):getUserIntf( ) + list[ k ] = SyncMaster[ k ]:getUserIntf( ) end return iter, list, -1