+/**
+ * \brief Pops the Lua task on top of the stack and registers it so that a
+ * receiver can retrieve it later knowing the C task.
+ *
+ * After calling this function, you can send the C task to someone and he
+ * will be able to also get the corresponding Lua task.
+ *
+ * \param L a lua state
+ */
+static void task_register(lua_State* L) {
+
+ m_task_t task = sglua_checktask(L, -1);
+ /* ... task */
+ /* put in the C task a ref to the lua task so that the receiver finds it */
+ unsigned long ref = luaL_ref(L, LUA_REGISTRYINDEX);
+ /* ... */
+ MSG_task_set_data(task, (void*) ref);
+}
+
+/**
+ * \brief Pushes onto the stack the Lua task corresponding to a C task.
+ *
+ * The Lua task must have been previously registered with task_register so
+ * that it can be retrieved knowing the C task.
+ *
+ * \param L a lua state
+ * \param task a C task
+ */
+static void task_unregister(lua_State* L, m_task_t task) {
+
+ /* ... */
+ /* the task is in my registry, put it onto my stack */
+ unsigned long ref = (unsigned long) MSG_task_get_data(task);
+ lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
+ /* ... task */
+ luaL_unref(L, LUA_REGISTRYINDEX, ref);
+ MSG_task_set_data(task, NULL);
+}
+
+/**
+ * \brief When a C task has been received, retrieves the corresponding Lua
+ * task from the sender and pushes it onto the receiver's stack.
+ *
+ * This function should be called from the receiver process.
+ *
+ * \param dst the receiver
+ * \param task the task just received
+ */
+static void task_copy(lua_State* dst, m_task_t task) {
+
+ m_process_t src_proc = MSG_task_get_sender(task);
+ lua_State* src = MSG_process_get_data(src_proc);
+
+ /* src: ...
+ dst: ... */
+ task_unregister(src, task);
+ /* src: ... task */
+ sglua_copy_value(src, dst);
+ /* src: ... task
+ dst: ... task */
+
+ /* the receiver is the owner of the task and may destroy it:
+ * make the C task NULL on the sender side so that it doesn't garbage
+ * collect it */
+ lua_getfield(src, -1, "__simgrid_task");
+ /* src: ... task ctask */
+ m_task_t* udata = (m_task_t*) luaL_checkudata(src, -1, TASK_MODULE_NAME);
+ *udata = NULL;
+ lua_pop(src, 2);
+ /* src: ... */
+}
+
+/**
+ * \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 values (nil or string): nil 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 */
+ task_register(L);
+ /* -- */
+ MSG_error_t res = MSG_task_send(task, mailbox);
+
+ if (res == MSG_OK) {
+ return 0;
+ }
+ else {
+ /* the communication has failed, I'm still the owner of the task */
+ task_unregister(L, task);
+ /* task */
+ lua_pushstring(L, msg_errors[res]);
+ /* task error */
+ return 1;
+ }
+}
+
+/**
+ * \brief Sends a task on a mailbox.
+ * \param L a Lua state
+ * \return number of values returned to Lua
+ *
+ * This is a non-blocking function: use simgrid.comm.wait() or
+ * simgrid.comm.test() to end the communication.
+ *
+ * - 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 (comm): a communication object to be used later with wait or test
+ */
+static int l_task_isend(lua_State* L)