+static msg_comm_t sglua_checkcomm(lua_State* L, int index)
+{
+ msg_comm_t comm = *((msg_comm_t*) luaL_checkudata(L, index, COMM_MODULE_NAME));
+ lua_pop(L, 1);
+ return comm;
+}
+
+/**
+ * \brief Blocks the current process until a communication is finished.
+ * \param L a Lua state
+ * \return number of values returned to Lua
+ *
+ * This function performs a waitany operation: you can specify an
+ * individual communication or a list of communications.
+ * If you provide a list, the function returns whenever one communication of
+ * the list is finished, and this communication will be removed from you list.
+ * This means you can make a waitall operation with successive calls to this
+ * function.
+ *
+ * - Argument 1 (comm or table): a comm or an array of comms
+ * - Argument 2 (number, optional): timeout (supported only when there is only
+ * one comm)
+ * - Return values (comm + string): the first comm of your list that finishes,
+ * plus an error string if this comm was unsuccessful.
+ */
+static int l_comm_wait(lua_State* L) {
+
+ if (lua_istable(L, 1)) {
+ // TODO implement waitany
+ THROW_UNIMPLEMENTED;
+ }
+ else {
+ /* only one comm */
+ msg_comm_t comm = sglua_checkcomm(L, 1);
+ double timeout = -1;
+ if (lua_gettop(L) >= 2) {
+ timeout = luaL_checknumber(L, 2);
+ }
+ /* comm ... */
+ MSG_error_t res = MSG_comm_wait(comm, timeout);
+ lua_settop(L, 1);
+ /* comm */
+ if (res == MSG_OK) {
+ return 1;
+ }
+ else {
+ lua_pushstring(L, msg_errors[res]);
+ /* comm error */
+ return 2;
+ }
+ }
+ return 0;
+}
+
+/**
+ * @brief Returns whether a communication is finished.
+ *
+ * This function always returns immediately.
+ * It performs a testany operation: you can provide an individual
+ * communication or a list of communications.
+ *
+ * - Argument 1 (comm or table): a comm or an array of comms
+ * - Return values (nil or comm + string): if no comm from your list is finished
+ * yet, returns nil. If a comm is finished, removes it from your list and
+ * returns it, plus an error string if it was unsuccessful.
+ */
+static int l_comm_test(lua_State* L) {
+
+ if (lua_istable(L, 1)) {
+ /* TODO implement testany */
+ THROW_UNIMPLEMENTED;
+ }
+ else {
+ /* only one comm */
+ msg_comm_t comm = sglua_checkcomm(L, 1);
+ /* comm ... */
+ if (!MSG_comm_test(comm)) {
+ return 0;
+ }
+ else {
+ lua_settop(L, 1);
+ /* comm */
+ MSG_error_t res = MSG_comm_get_status(comm);
+ if (res == MSG_OK) {
+ return 1;
+ }
+ else {
+ lua_pushstring(L, msg_errors[res]);
+ /* comm error */
+ return 2;
+ }
+ }
+ }
+}
+
+static const luaL_reg comm_functions[] = {
+ {"wait", l_comm_wait},
+ {"test", l_comm_test},
+ {NULL, NULL}
+};
+
+/**
+ * \brief Finalizes a comm userdata.
+ * \param L a Lua state
+ * \return number of values returned to Lua
+ *
+ * - Argument 1 (userdata): a comm
+ */
+static int l_comm_gc(lua_State* L)
+{
+ /* ctask */
+ msg_comm_t comm = *((msg_comm_t*) luaL_checkudata(L, 1, COMM_MODULE_NAME));
+ MSG_comm_destroy(comm);
+ return 0;
+}
+
+/**
+ * \brief Metamethods of the comm userdata.
+ */
+static const luaL_reg comm_meta[] = {
+ {"__gc", l_comm_gc},
+ {NULL, NULL}
+};
+
+/* ********************************************************************************* */
+/* simgrid.host API */
+/* ********************************************************************************* */
+
+/**
+ * \brief Ensures that a value in the stack is a host and returns it.
+ * \param L a Lua state
+ * \param index an index in the Lua stack
+ * \return the C host corresponding to this Lua host
+ */
+static m_host_t sglua_checkhost(lua_State * L, int index)