MEDIUM: lua: change the timeout execution
Now, the Lua timeout is relative to the effective run time.
When the Lua is waiting for I/O, this time is not took in
lua run time account.
diff --git a/include/types/hlua.h b/include/types/hlua.h
index ac56c2a..48f7487 100644
--- a/include/types/hlua.h
+++ b/include/types/hlua.h
@@ -47,7 +47,9 @@
int nargs; /* The number of arguments in the stack at the start of execution. */
unsigned int flags; /* The current execution flags. */
int wake_time; /* The lua wants to be waked at this time, or before. */
- int expire; /* Lua execution must be stopped over this time. */
+ unsigned int max_time; /* The max amount of execution time for an Lua process, in ms. */
+ unsigned int start_time; /* The ms time when the Lua starts the last execution. */
+ unsigned int run_time; /* Lua total execution time in ms. */
struct task *task; /* The task associated with the lua stack execution.
We must wake this task to continue the task execution */
struct list com; /* The list head of the signals attached to this task. */
diff --git a/src/hlua.c b/src/hlua.c
index a7cbdc7..2c35960 100644
--- a/src/hlua.c
+++ b/src/hlua.c
@@ -889,7 +889,7 @@
/* Set the wake timeout. If timeout is required, we set
* the expiration time.
*/
- hlua->wake_time = tick_first(timeout, hlua->expire);
+ hlua->wake_time = timeout;
hlua->flags |= flags;
@@ -1035,11 +1035,15 @@
/* If we cannot yield, update the clock and check the timeout. */
tv_update_date(0, 1);
- if (tick_is_expired(hlua->expire, now_ms)) {
+ hlua->run_time += now_ms - hlua->start_time;
+ if (hlua->max_time && hlua->run_time >= hlua->max_time) {
lua_pushfstring(L, "execution timeout");
WILL_LJMP(lua_error(L));
}
+ /* Update the start time. */
+ hlua->start_time = now_ms;
+
/* Try to interrupt the process at the end of the current
* unyieldable function.
*/
@@ -1066,11 +1070,9 @@
int ret;
const char *msg;
- /* If we want to resume the task, then check first the execution timeout.
- * if it is reached, we can interrupt the Lua processing.
- */
- if (tick_is_expired(lua->expire, now_ms))
- goto timeout_reached;
+ /* Initialise run time counter. */
+ if (!HLUA_IS_RUNNING(lua))
+ lua->run_time = 0;
resume_execution:
@@ -1085,6 +1087,9 @@
HLUA_CLR_WAKERESWR(lua);
HLUA_CLR_WAKEREQWR(lua);
+ /* Update the start time. */
+ lua->start_time = now_ms;
+
/* Call the function. */
ret = lua_resume(lua->T, gL.T, lua->nargs);
switch (ret) {
@@ -1097,10 +1102,9 @@
/* Check if the execution timeout is expired. It it is the case, we
* break the Lua execution.
*/
- if (tick_is_expired(lua->expire, now_ms)) {
-
-timeout_reached:
-
+ tv_update_date(0, 1);
+ lua->run_time += now_ms - lua->start_time;
+ if (lua->max_time && lua->run_time > lua->max_time) {
lua_settop(lua->T, 0); /* Empty the stack. */
if (!lua_checkstack(lua->T, 1)) {
ret = HLUA_E_ERR;
@@ -4997,7 +5001,7 @@
* execution timeouts.
*/
if (!HLUA_IS_RUNNING(hlua))
- hlua->expire = tick_add_ifset(now_ms, hlua_timeout_task);
+ hlua->max_time = hlua_timeout_task;
/* Execute the Lua code. */
status = hlua_ctx_resume(hlua, 1);
@@ -5159,7 +5163,7 @@
}
/* We must initialize the execution timeouts. */
- stream->hlua.expire = tick_add_ifset(now_ms, hlua_timeout_session);
+ stream->hlua.max_time = hlua_timeout_session;
/* At this point the execution is safe. */
RESET_SAFE_LJMP(stream->hlua.T);
@@ -5261,7 +5265,7 @@
}
/* We must initialize the execution timeouts. */
- stream->hlua.expire = tick_add_ifset(now_ms, hlua_timeout_session);
+ stream->hlua.max_time = hlua_timeout_session;
/* At this point the execution is safe. */
RESET_SAFE_LJMP(stream->hlua.T);
@@ -5501,7 +5505,7 @@
RESET_SAFE_LJMP(s->hlua.T);
/* We must initialize the execution timeouts. */
- s->hlua.expire = tick_add_ifset(now_ms, hlua_timeout_session);
+ s->hlua.max_time = hlua_timeout_session;
}
/* Execute the function. */
@@ -5606,7 +5610,7 @@
}
/* Set timeout according with the applet configuration. */
- hlua->expire = tick_add_ifset(now_ms, ctx->applet->timeout);
+ hlua->max_time = ctx->applet->timeout;
/* The following Lua calls can fail. */
if (!SET_SAFE_LJMP(hlua->T)) {
@@ -5779,7 +5783,7 @@
}
/* Set timeout according with the applet configuration. */
- hlua->expire = tick_add_ifset(now_ms, ctx->applet->timeout);
+ hlua->max_time = ctx->applet->timeout;
/* The following Lua calls can fail. */
if (!SET_SAFE_LJMP(hlua->T)) {