X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/d2ba533b2ba779dc85b3faf0d297dc6f13c7e7c1..58f0f206de294942ec8fa796556362fcc52e7f23:/src/bindings/lua/simgrid_lua.c diff --git a/src/bindings/lua/simgrid_lua.c b/src/bindings/lua/simgrid_lua.c index b2f57d5109..fa6ab955ef 100644 --- a/src/bindings/lua/simgrid_lua.c +++ b/src/bindings/lua/simgrid_lua.c @@ -62,6 +62,11 @@ static m_task_t sglua_checktask(lua_State* L, int index) * - Argument 2 (number): computation size * - Argument 3 (number): communication size * - Return value (task): the task created + * + * A Lua task is a regular table with a full userdata inside, and both share + * the same metatable. For the regular table, the metatable allows OO-style + * writing such as your_task:send(someone). + * For the userdata, the metatable is used to check its type. */ static int l_task_new(lua_State* L) { @@ -76,6 +81,10 @@ static int l_task_new(lua_State* L) lua_newtable(L); /* task */ + luaL_getmetatable(L, TASK_MODULE_NAME); + /* task mt */ + lua_setmetatable(L, -2); + /* task */ m_task_t* lua_task = (m_task_t*) lua_newuserdata(L, sizeof(m_task_t)); /* task ctask */ *lua_task = msg_task; @@ -111,7 +120,7 @@ static int l_task_get_name(lua_State* L) * - Argument 1 (task): a task * - Return value (number): computation duration of this task */ -static int l_task_computation_duration(lua_State* L) +static int l_task_get_computation_duration(lua_State* L) { m_task_t task = sglua_checktask(L, 1); lua_pushnumber(L, MSG_task_get_compute_duration(task)); @@ -124,14 +133,30 @@ static int l_task_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); + } } /** @@ -140,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) { @@ -157,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 */ - } - 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; } /** @@ -192,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); @@ -215,39 +249,45 @@ 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); + lua_pushliteral(L, "transfer failure"); + return 2; + + case MSG_HOST_FAILURE: + XBT_DEBUG("MSG_task_send failed: host failure"); lua_pushnil(L); - /* nil */ + 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[] = { {"new", l_task_new}, - {"name", l_task_get_name}, - {"computation_duration", l_task_computation_duration}, + {"get_name", l_task_get_name}, + {"get_computation_duration", l_task_get_computation_duration}, {"execute", l_task_execute}, {"send", l_task_send}, {"recv", l_task_recv}, @@ -288,8 +328,11 @@ static int l_task_tostring(lua_State* L) return 1; } +/** + * \brief Metamethods of both a task table and the userdata inside it. + */ static const luaL_reg task_meta[] = { - {"__gc", l_task_gc}, + {"__gc", l_task_gc}, /* will be called only for userdata */ {"__tostring", l_task_tostring}, {NULL, NULL} }; @@ -849,12 +892,9 @@ static void register_task_functions(lua_State* L) lua_pushvalue(L, -2); /* simgrid.task mt simgrid.task */ /* metatable.__index = simgrid.task - * we put the task functions inside the task userdata itself: + * we put the task functions inside the task itself: * this allows to write task:method(args) for * simgrid.task.method(task, args) */ - // FIXME: in the current implementation, a Lua task is a table with a - // __simgrid_task field that contains the userdata, so the OO-style - // writing doesn't work lua_setfield(L, -2, "__index"); /* simgrid.task mt */ lua_pop(L, 2);