-static int Task_new(lua_State* L)
-{
- XBT_DEBUG("Task new");
- const char* name = luaL_checkstring(L, 1);
- int comp_size = luaL_checkint(L, 2);
- int msg_size = luaL_checkint(L, 3);
- /* name comp comm */
- lua_settop(L, 0);
- /* -- */
- m_task_t msg_task = MSG_task_create(name, comp_size, msg_size, NULL);
-
- 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 Task_get_name(lua_State* L)
-{
- m_task_t task = checkTask(L, 1);
- lua_pushstring(L, MSG_task_get_name(task));
- return 1;
-}
-
-static int Task_computation_duration(lua_State* L)
-{
- m_task_t task = checkTask(L, 1);
- lua_pushnumber(L, MSG_task_get_compute_duration(task));
- return 1;
-}
-
-static int Task_execute(lua_State* L)
-{
- m_task_t task = checkTask(L, 1);
- int res = MSG_task_execute(task);
- lua_pushnumber(L, res);
- return 1;
-}
-
-static int Task_send(lua_State* L)
-{
- m_task_t task = 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 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 Task_recv(lua_State * L)
-{
- lua_pushnumber(L, -1.0);
- return Task_recv_with_timeout(L);
-}