X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/e38e9ff52039b2dd8416b120031492f9b7a2844b..f45b6eaa01ced9382f2ec977555b66b55bc72fbf:/src/bindings/lua/simgrid_lua.c diff --git a/src/bindings/lua/simgrid_lua.c b/src/bindings/lua/simgrid_lua.c index 6c6faf13fc..f3367cb597 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 or 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); + } } /** @@ -149,7 +165,11 @@ static int l_task_execute(lua_State* L) * \return number of values returned to Lua * * - Argument 1 (task): the task to send - * - Argument 2 (string): mailbox + * - Argument 2 (string or compatible): mailbox name, as a real string or any + * type convertible to string (numbers always are) + * - Return value (nil or 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 +186,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 */ + 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); } - 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; } /** @@ -201,12 +225,13 @@ static int l_task_send(lua_State* L) * \param L a Lua state * \return number of values returned to Lua * - * - Argument 1 (string): mailbox + * - Argument 1 (string or compatible): mailbox name, as a real string or any + * type convertible to string (numbers always are) * - 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 or nil+err): the task received, or nil plus an error message if + * the communication has failed */ -static int l_task_recv(lua_State *L) +static int l_task_recv(lua_State* L) { m_task_t task = NULL; const char* mailbox = luaL_checkstring(L, 1); @@ -224,33 +249,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); - /* nil */ + lua_pushliteral(L, "timeout"); + return 2; + + case MSG_TRANSFER_FAILURE: + XBT_DEBUG("MSG_task_send failed: transfer failure"); + lua_pushnil(L); + 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[] = { @@ -606,9 +637,9 @@ static const luaL_reg platf_functions[] = { * * - Argument 1 (string): name of the deployment file to load */ -static int launch_application(lua_State * L) -{ - const char *file = luaL_checkstring(L, 1); +static int launch_application(lua_State* L) { + + const char* file = luaL_checkstring(L, 1); MSG_function_register_default(run_lua_code); MSG_launch_application(file); return 0; @@ -621,9 +652,9 @@ static int launch_application(lua_State * L) * * - Argument 1 (string): name of the platform file to load */ -static int create_environment(lua_State * L) -{ - const char *file = luaL_checkstring(L, 1); +static int create_environment(lua_State* L) { + + const char* file = luaL_checkstring(L, 1); XBT_DEBUG("Loading environment file %s", file); MSG_create_environment(file); return 0; @@ -636,9 +667,9 @@ static int create_environment(lua_State * L) * * - Argument 1 (string): the text to print */ -static int debug(lua_State * L) -{ - const char *str = luaL_checkstring(L, 1); +static int debug(lua_State* L) { + + const char* str = luaL_checkstring(L, 1); XBT_DEBUG("%s", str); return 0; } @@ -650,9 +681,9 @@ static int debug(lua_State * L) * * - Argument 1 (string): the text to print */ -static int info(lua_State * L) -{ - const char *str = luaL_checkstring(L, 1); +static int info(lua_State* L) { + + const char* str = luaL_checkstring(L, 1); XBT_INFO("%s", str); return 0; } @@ -662,12 +693,25 @@ static int info(lua_State * L) * \param L a Lua state * \return number of values returned to Lua */ -static int run(lua_State * L) -{ +static int run(lua_State* L) { + MSG_main(); return 0; } +/** + * \brief Returns the current simulated time. + * \param L a Lua state + * \return number of values returned to Lua + * + * - Return value (number): the simulated time + */ +static int get_clock(lua_State* L) { + + lua_pushnumber(L, MSG_get_clock()); + return 1; +} + /** * \brief Cleans the simulation. * \param L a Lua state @@ -742,6 +786,7 @@ static const luaL_Reg simgrid_functions[] = { {"debug", debug}, {"info", info}, {"run", run}, + {"get_clock", get_clock}, /* short names */ {"platform", create_environment}, {"application", launch_application},