From 5265dc224e69f1d3094f04f86b97bb0d1308d80e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Christophe=20Thi=C3=A9ry?= Date: Mon, 14 Nov 2011 14:38:26 +0100 Subject: [PATCH] Lua: return errors from execute/send/recv, or nil in case of success --- src/bindings/lua/simgrid_lua.c | 121 ++++++++++++++++++++------------- src/msg/msg_gos.c | 4 +- 2 files changed, 77 insertions(+), 48 deletions(-) diff --git a/src/bindings/lua/simgrid_lua.c b/src/bindings/lua/simgrid_lua.c index 6c6faf13fc..0fd82dcc3a 100644 --- a/src/bindings/lua/simgrid_lua.c +++ b/src/bindings/lua/simgrid_lua.c @@ -133,14 +133,30 @@ static int l_task_get_computation_duration(lua_State* L) * \return number of values returned to Lua * * - Argument 1 (task): the task to execute - * - Return value (number): error code + * - Return value (nil / error): none if the task was successfully executed, or an error + * string in case of failure, which may be "task canceled" or "host failure" */ static int l_task_execute(lua_State* L) { m_task_t task = sglua_checktask(L, 1); - int res = MSG_task_execute(task); - lua_pushnumber(L, res); - return 1; + MSG_error_t res = MSG_task_execute(task); + + switch (res) { + + case MSG_OK: + return 0; + + case MSG_TASK_CANCELED: + lua_pushliteral(L, "task canceled"); + return 1; + + case MSG_HOST_FAILURE: + lua_pushliteral(L, "host failure"); + return 1; + + default: + xbt_die("Unexpected result of MSG_task_execute(): %d, please report this bug", res); + } } /** @@ -150,6 +166,9 @@ static int l_task_execute(lua_State* L) * * - Argument 1 (task): the task to send * - Argument 2 (string): mailbox + * - Return value (nil / error): none if the communication was successful, or an error string + * in case of failure, which may be "timeout", "host failure" or + * "transfer failure" */ static int l_task_send(lua_State* L) { @@ -166,34 +185,38 @@ static int l_task_send(lua_State* L) MSG_process_sleep(0); } - if (res == MSG_OK) { + switch (res) { + + case MSG_OK: /* the receiver is the owner of the task and may destroy it: * remove the C task on my side so that I don't garbage collect it */ lua_getfield(L, 1, "__simgrid_task"); /* task ctask */ m_task_t* udata = (m_task_t*) luaL_checkudata(L, -1, TASK_MODULE_NAME); *udata = NULL; - lua_pop(L, 1); - /* task */ - } - else { - switch (res) { - case MSG_TIMEOUT: - XBT_DEBUG("MSG_task_send failed : Timeout"); - break; - case MSG_TRANSFER_FAILURE: - XBT_DEBUG("MSG_task_send failed : Transfer Failure"); - break; - case MSG_HOST_FAILURE: - XBT_DEBUG("MSG_task_send failed : Host Failure "); - break; - default: - XBT_ERROR - ("MSG_task_send failed : Unexpected error , please report this bug"); - break; - } + return 0; + + case MSG_TIMEOUT: + XBT_DEBUG("MSG_task_send failed: timeout"); + lua_settop(L, 0); + lua_pushliteral(L, "timeout"); + return 1; + + case MSG_TRANSFER_FAILURE: + XBT_DEBUG("MSG_task_send failed: transfer failure"); + lua_settop(L, 0); + lua_pushliteral(L, "transfer failure"); + return 1; + + case MSG_HOST_FAILURE: + XBT_DEBUG("MSG_task_send failed: host failure"); + lua_settop(L, 0); + lua_pushliteral(L, "host failure"); + return 1; + + default: + xbt_die("Unexpected result of MSG_task_send: %d, please report this bug", res); } - return 0; } /** @@ -203,8 +226,8 @@ static int l_task_send(lua_State* L) * * - Argument 1 (string): mailbox * - Argument 2 (number, optional): timeout (default is no timeout) - * - Return value (task/nil): the task received or nil if the communication - * has failed + * - Return value (task / nil+err): the task received, or nil plus an error message if + * the communication has failed */ static int l_task_recv(lua_State *L) { @@ -224,33 +247,39 @@ static int l_task_recv(lua_State *L) /* -- */ MSG_error_t res = MSG_task_receive_with_timeout(&task, mailbox, timeout); - if (res == MSG_OK) { + switch (res) { + + case MSG_OK: /* copy the data directly from sender's stack */ + { lua_State* sender_stack = MSG_task_get_data(task); sglua_copy_value(sender_stack, L); /* task */ MSG_task_set_data(task, NULL); + return 1; } - else { - switch (res) { - case MSG_TIMEOUT: - XBT_DEBUG("MSG_task_receive failed : Timeout"); - break; - case MSG_TRANSFER_FAILURE: - XBT_DEBUG("MSG_task_receive failed : Transfer Failure"); - break; - case MSG_HOST_FAILURE: - XBT_DEBUG("MSG_task_receive failed : Host Failure "); - break; - default: - XBT_ERROR("MSG_task_receive failed : Unexpected error , please report this bug"); - break; - } + + case MSG_TIMEOUT: + XBT_DEBUG("MSG_task_send failed: timeout"); + lua_pushnil(L); + lua_pushliteral(L, "timeout"); + return 2; + + case MSG_TRANSFER_FAILURE: + XBT_DEBUG("MSG_task_send failed: transfer failure"); lua_pushnil(L); - /* nil */ + lua_pushliteral(L, "transfer failure"); + return 2; + + case MSG_HOST_FAILURE: + XBT_DEBUG("MSG_task_send failed: host failure"); + lua_pushnil(L); + lua_pushliteral(L, "host failure"); + return 2; + + default: + xbt_die("Unexpected result of MSG_task_recv: %d, please report this bug", res); } - /* task/nil */ - return 1; } static const luaL_reg task_functions[] = { diff --git a/src/msg/msg_gos.c b/src/msg/msg_gos.c index 320098830a..aa91522a9a 100644 --- a/src/msg/msg_gos.c +++ b/src/msg/msg_gos.c @@ -31,8 +31,8 @@ MSG_error_t MSG_get_errno(void) * takes only one parameter. * \param task a #m_task_t to execute on the location on which the agent is running. - * \return #MSG_FATAL if \a task is not properly initialized and - * #MSG_OK otherwise. + * \return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED + * or #MSG_HOST_FAILURE otherwise */ MSG_error_t MSG_task_execute(m_task_t task) { -- 2.20.1