- lua_newtable(L);
- /* task */
- m_task_t* lua_task = (m_task_t*) lua_newuserdata(L, sizeof(m_task_t));
- /* task ctask */
- *lua_task = msg_task;
- luaL_getmetatable(L, TASK_MODULE_NAME);
- /* task ctask mt */
- lua_setmetatable(L, -2);
- /* task ctask */
- lua_setfield(L, -2, "__simgrid_task");
- /* task */
- return 1;
-}
-
-static int l_task_get_name(lua_State* L)
-{
- m_task_t task = sglua_checktask(L, 1);
- lua_pushstring(L, MSG_task_get_name(task));
- return 1;
-}
-
-static int l_task_computation_duration(lua_State* L)
-{
- m_task_t task = sglua_checktask(L, 1);
- lua_pushnumber(L, MSG_task_get_compute_duration(task));
- return 1;
-}
-
-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;
-}
-
-static int l_task_send(lua_State* L)
-{
- m_task_t task = sglua_checktask(L, 1);
- const char* mailbox = luaL_checkstring(L, 2);
- /* task mailbox */
- lua_settop(L, 1);
- /* task */
- /* copy my stack into the task, so that the receiver can copy the lua task */
- MSG_task_set_data(task, L);
- MSG_error_t res = MSG_task_send(task, mailbox);
- while (MSG_task_get_data(task) != NULL) {
- /* don't mess up with my stack: the receiver didn't copy the data yet */
- MSG_process_sleep(0);
- }
-
- if (res == 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;
-}
-
-static int l_task_recv_with_timeout(lua_State *L)
-{
- m_task_t task = NULL;
- const char* mailbox = luaL_checkstring(L, 1);
- int timeout = luaL_checknumber(L, 2);
- /* mailbox timeout */
- lua_settop(L, 0);
- /* -- */
- MSG_error_t res = MSG_task_receive_with_timeout(&task, mailbox, timeout);
-
- if (res == 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);
- }
- 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;
- }
- lua_pushnil(L);
- /* nil */
- }
- /* task/nil */
- return 1;
-}
-
-static int l_task_recv(lua_State * L)
-{
- lua_pushnumber(L, -1.0);
- return l_task_recv_with_timeout(L);
-}
-
-static const luaL_reg task_functions[] = {
- {"new", l_task_new},
- {"name", l_task_get_name},
- {"computation_duration", l_task_computation_duration},
- {"execute", l_task_execute},
- {"send", l_task_send},
- {"recv", l_task_recv},
- {"recv_timeout", l_task_recv_with_timeout},
- {NULL, NULL}
-};
-
-static int l_task_gc(lua_State* L)
-{
- /* ctask */
- m_task_t task = *((m_task_t*) luaL_checkudata(L, 1, TASK_MODULE_NAME));
- /* the task is NULL if I sent it to someone else */
- if (task != NULL) {
- MSG_task_destroy(task);
- }