- INFO2("%s%s",msg,buff);
-}
-
-/** @brief ensures that a userdata on the stack is a task and returns the pointer inside the userdata */
-static m_task_t checkTask (lua_State *L,int index) {
- m_task_t *pi,tk;
- luaL_checktype(L,index,LUA_TTABLE);
- lua_getfield(L,index,"__simgrid_task");
- pi = (m_task_t*)luaL_checkudata(L,-1,TASK_MODULE_NAME);
- if(pi == NULL)
- luaL_typerror(L,index,TASK_MODULE_NAME);
- tk = *pi;
- if(!tk)
- luaL_error(L,"null Task");
- lua_pop(L,1);
- return tk;
-}
-
-/** @brief leaves a new userdata on top of the stack, sets its metatable, and sets the Task pointer inside the userdata */
-// NOT USED
-/*static m_task_t *pushTask (lua_State *L,m_task_t tk) {
- m_task_t *pi = NULL;
- pi = (m_task_t*)lua_newuserdata(L,sizeof(m_task_t));
- *pi=tk;
- DEBUG1("push lua task with Name : %s \n",MSG_task_get_name(*pi));
- luaL_getmetatable(L,TASK_MODULE_NAME);
- lua_setmetatable(L,-2);
- return pi;
-}*/
+}
+
+/**
+ * \brief Sends a task to a mailbox and waits for its completion.
+ * \param L a Lua state
+ * \return number of values returned to Lua
+ *
+ * - Argument 1 (task): the task to send
+ * - 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)
+{
+ 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);
+ }
+
+ 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;
+ 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);
+ }
+}
+
+/**
+ * \brief Receives a task.
+ * \param L a Lua state
+ * \return number of values returned to Lua
+ *
+ * - 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 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)
+{
+ m_task_t task = NULL;
+ const char* mailbox = luaL_checkstring(L, 1);
+ int timeout;
+ if (lua_gettop(L) >= 2) {
+ /* mailbox timeout */
+ timeout = luaL_checknumber(L, 2);
+ }
+ else {
+ /* mailbox */
+ timeout = -1;
+ /* no timeout by default */
+ }
+ lua_settop(L, 0);
+ /* -- */
+ MSG_error_t res = MSG_task_receive_with_timeout(&task, mailbox, timeout);
+
+ 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;
+ }
+
+ 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);
+ lua_pushliteral(L, "host failure");
+ return 2;
+
+ default:
+ xbt_die("Unexpected result of MSG_task_recv: %d, please report this bug", res);
+ }
+}
+
+static const luaL_reg task_functions[] = {
+ {"new", l_task_new},
+ {"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},
+ {NULL, NULL}
+};
+
+/**
+ * \brief Finalizes the userdata of a task.
+ * \param L a Lua state
+ * \return number of values returned to Lua
+ *
+ * - Argument 1 (userdata): a C task, possibly NULL if it was sent to another
+ * Lua state
+ */
+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);
+ }
+ return 0;
+}
+
+/**
+ * \brief Returns a string representation of a C task.
+ * \param L a Lua state
+ * \return number of values returned to Lua
+ *
+ * - Argument 1 (userdata): a task
+ * - Return value (string): a string describing this task
+ */
+static int l_task_tostring(lua_State* L)
+{
+ m_task_t task = *((m_task_t*) luaL_checkudata(L, 1, TASK_MODULE_NAME));
+ lua_pushfstring(L, "Task: %p", task);
+ return 1;
+}
+
+/**
+ * \brief Metamethods of both a task table and the userdata inside it.
+ */
+static const luaL_reg task_meta[] = {
+ {"__gc", l_task_gc}, /* will be called only for userdata */
+ {"__tostring", l_task_tostring},
+ {NULL, NULL}
+};