# HG changeset patch # User jbe # Date 1496741107 -7200 # Node ID 42b85f65327cb9be95adf11026fa20f7a9fa3807 # Parent 928473f7df9603a1bc003675efbcf4be1fbc685c Allow "main threads" to be named; main threads also use MOONBR_PROTO_INTERVAL as listener type now diff -r 928473f7df96 -r 42b85f65327c moonbridge.c --- a/moonbridge.c Mon Jun 05 23:19:08 2017 +0200 +++ b/moonbridge.c Tue Jun 06 11:25:07 2017 +0200 @@ -119,10 +119,9 @@ #define MOONBR_PSTATE_FORKED 2 /* Enum for 'proto' field of struct moonbr_listener */ -#define MOONBR_PROTO_MAIN 1 -#define MOONBR_PROTO_INTERVAL 2 -#define MOONBR_PROTO_LOCAL 3 -#define MOONBR_PROTO_TCP 4 +#define MOONBR_PROTO_INTERVAL 1 +#define MOONBR_PROTO_LOCAL 2 +#define MOONBR_PROTO_TCP 3 /* Data structure for a pool's listener that can accept incoming connections */ struct moonbr_listener { @@ -133,6 +132,7 @@ union { struct { char *name; /* name of interval passed to 'connect' function as 'interval' field in table */ + int main; /* nonzero = termination of 'connect' function causes shutdown */ int strict; /* nonzero = runtime of 'connect' function does not delay interval */ struct timeval delay; /* interval between invocations of 'connect' function */ struct timeval wakeup; /* point in time of next invocation */ @@ -512,16 +512,21 @@ for (i=0; ilistener_count; i++) { struct moonbr_listener *listener = &pool->listener[i]; switch (listener->proto) { - case MOONBR_PROTO_MAIN: - /* nothing to do here: starting main thread is performed in moonbr_run() function */ - moonbr_log(LOG_INFO, "Adding main thread"); - break; case MOONBR_PROTO_INTERVAL: - /* nothing to do here: starting intervals is performed in moonbr_run() function */ - if (!listener->type_specific.interval.name) { - moonbr_log(LOG_INFO, "Adding unnamed interval listener"); + if (listener->type_specific.interval.main) { + /* nothing to do here: starting main thread is performed in moonbr_run() function */ + if (!listener->type_specific.interval.name) { + moonbr_log(LOG_INFO, "Adding unnamed main thread"); + } else { + moonbr_log(LOG_INFO, "Adding main thread \"%s\"", listener->type_specific.interval.name); + } } else { - moonbr_log(LOG_INFO, "Adding interval listener \"%s\"", listener->type_specific.interval.name); + /* nothing to do here: starting intervals is performed in moonbr_run() function */ + if (!listener->type_specific.interval.name) { + moonbr_log(LOG_INFO, "Adding unnamed interval listener"); + } else { + moonbr_log(LOG_INFO, "Adding interval listener \"%s\"", listener->type_specific.interval.name); + } } break; case MOONBR_PROTO_LOCAL: @@ -781,9 +786,7 @@ ) { moonbr_child_log_fatal("Received unexpected file descriptor from parent process"); } else if ( - listener->proto != MOONBR_PROTO_MAIN && - listener->proto != MOONBR_PROTO_INTERVAL && - fd < 0 + listener->proto != MOONBR_PROTO_INTERVAL && fd < 0 ) { moonbr_child_log_fatal("Missing file descriptor from parent process"); } @@ -791,15 +794,16 @@ lua_rawgetp(L, LUA_REGISTRYINDEX, moonbr_luakey_connect_func(pool)); if (fd < 0) { lua_newtable(L); - if (listener->proto == MOONBR_PROTO_MAIN) { - lua_pushboolean(L, 1); - lua_setfield(L, -2, "main"); - } else if (listener->proto == MOONBR_PROTO_INTERVAL) { + if (listener->proto == MOONBR_PROTO_INTERVAL) { lua_pushstring(L, listener->type_specific.interval.name ? listener->type_specific.interval.name : "" ); - lua_setfield(L, -2, "interval"); + if (listener->type_specific.interval.main) { + lua_setfield(L, -2, "main"); + } else { + lua_setfield(L, -2, "interval"); + } } } else { lua_pushvalue(L, -2); @@ -1416,22 +1420,21 @@ static void moonbr_connect(struct moonbr_pool *pool) { struct moonbr_listener *listener = moonbr_pop_connected_listener(pool); struct moonbr_worker *worker; - if (listener->proto == MOONBR_PROTO_MAIN) { + if (listener->proto == MOONBR_PROTO_INTERVAL) { worker = moonbr_pop_idle_worker(pool); - if (moonbr_stat) { - moonbr_log(LOG_INFO, "Dispatching main thread of pool #%i to PID %i", listener->pool->poolnum, (int)worker->pid); + if (listener->type_specific.interval.main) { + if (moonbr_stat) { + moonbr_log(LOG_INFO, "Dispatching main thread \"%s\" of pool #%i to PID %i", listener->type_specific.interval.name, listener->pool->poolnum, (int)worker->pid); + } + worker->main = 1; + } else { + if (moonbr_stat) { + moonbr_log(LOG_INFO, "Dispatching interval timer \"%s\" of pool #%i to PID %i", listener->type_specific.interval.name, listener->pool->poolnum, (int)worker->pid); + } + worker->restart_interval_listener = listener; } - worker->main = 1; moonbr_send_control_message(worker, MOONBR_COMMAND_CONNECT, -1, listener); - /* do not push listener to queue of idle listeners */ - } else if (listener->proto == MOONBR_PROTO_INTERVAL) { - worker = moonbr_pop_idle_worker(pool); - if (moonbr_stat) { - moonbr_log(LOG_INFO, "Dispatching interval timer \"%s\" of pool #%i to PID %i", listener->type_specific.interval.name, listener->pool->poolnum, (int)worker->pid); - } - worker->restart_interval_listener = listener; - moonbr_send_control_message(worker, MOONBR_COMMAND_CONNECT, -1, listener); - /* do not push listener to queue of idle listeners yet */ + /* do not push listener to queue of idle listeners (yet) */ } else { int peerfd; do { @@ -1895,6 +1898,7 @@ static int moonbr_listen_init_pool(lua_State *L) { struct moonbr_pool *pool; const char *proto; + int is_main; int i; int dynamic = 0; /* nonzero = listeners exist which require dynamic worker creation */ pool = lua_touserdata(L, 1); @@ -1909,10 +1913,8 @@ #endif lua_getfield(L, 3, "proto"); proto = lua_tostring(L, -1); - if (proto && !strcmp(proto, "main")) { - listener->proto = MOONBR_PROTO_MAIN; - } else if (proto && !strcmp(proto, "interval")) { - dynamic = 1; + is_main = !strcmp(proto, "main"); + if (proto && (is_main || !strcmp(proto, "interval"))) { listener->proto = MOONBR_PROTO_INTERVAL; lua_getfield(L, 3, "name"); { @@ -1926,19 +1928,25 @@ listener->type_specific.interval.name = name_dup; } } - lua_getfield(L, 3, "delay"); - if ( - !moonbr_lua_totimeval(L, -1, &listener->type_specific.interval.delay) || - !timerisset(&listener->type_specific.interval.delay) - ) { - luaL_error(L, "No valid interval delay specified; use listen{{proto=\"interval\", delay=...}, ...}"); - } - lua_getfield(L, 3, "strict"); - if (!lua_isnil(L, -1)) { - if (lua_isboolean(L, -1)) { - if (lua_toboolean(L, -1)) listener->type_specific.interval.strict = 1; - } else { - luaL_error(L, "Option \"strict\" must be a boolean if set; use listen{{proto=\"interval\", strict=true, ...}, ...}"); + if (is_main) { + listener->type_specific.interval.main = 1; + } else { + listener->type_specific.interval.main = 0; + dynamic = 1; + lua_getfield(L, 3, "delay"); + if ( + !moonbr_lua_totimeval(L, -1, &listener->type_specific.interval.delay) || + !timerisset(&listener->type_specific.interval.delay) + ) { + luaL_error(L, "No valid interval delay specified; use listen{{proto=\"interval\", delay=...}, ...}"); + } + lua_getfield(L, 3, "strict"); + if (!lua_isnil(L, -1)) { + if (lua_isboolean(L, -1)) { + if (lua_toboolean(L, -1)) listener->type_specific.interval.strict = 1; + } else { + luaL_error(L, "Option \"strict\" must be a boolean if set; use listen{{proto=\"interval\", strict=true, ...}, ...}"); + } } } } else if (proto && !strcmp(proto, "local")) {