Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
small improvements suggested by Arnaud
[simgrid.git] / examples / msg / actions / actions.c
index d04d866..d60901c 100644 (file)
 #include "msg/msg.h"            /* Yeah! If you want to use msg, you need to include msg/msg.h */
 #include "xbt.h"                /* calloc, printf */
 
+
 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
                  "Messages specific for this msg example");
 
+
 /* Helper function */
 static double parse_double(const char *string) {
   double value;
@@ -24,7 +26,6 @@ static double parse_double(const char *string) {
   return value;
 }
 
-
 /* My actions */
 static void send(xbt_dynar_t action)
 {
@@ -50,6 +51,49 @@ static void recv(xbt_dynar_t action)
   free(name);
 }
 
+static int spawned_recv(int argc, char *argv[])
+{
+  m_task_t task = NULL;
+  char* name = (char *) MSG_process_get_data(MSG_process_self());
+  INFO1("Receiving on %s", name);
+  MSG_task_receive(&task, name);
+  INFO1("Received %s", MSG_task_get_name(task));
+  MSG_task_send(MSG_task_create("waiter",0,0,NULL),MSG_process_self()->name); 
+  
+  MSG_task_destroy(task);
+  return 0;
+}
+
+static void Irecv(xbt_dynar_t action)
+{
+  char *name = xbt_str_join(action, " ");
+  m_process_t comm_helper;
+
+  INFO1("Irecv on %s: spawn process ", 
+        MSG_process_get_name(MSG_process_self()));
+
+  sprintf(name,"%s_wait",MSG_process_self()->name);
+  comm_helper = MSG_process_create(name,spawned_recv,
+                 (void *) MSG_process_get_name(MSG_process_self()),
+                 MSG_host_self());
+
+
+  free(name);
+}
+
+static void wait(xbt_dynar_t action)
+{
+  char *name = xbt_str_join(action, " ");
+  char task_name[80];
+  m_task_t task = NULL;
+  
+  INFO1("wait: %s", name);
+  sprintf(task_name,"%s_wait",MSG_process_self()->name);
+  MSG_task_receive(&task,task_name);
+  INFO1("waited: %s", name);
+  free(name);
+}
+
 static void sleep(xbt_dynar_t action)
 {
   char *name = xbt_str_join(action, " ");
@@ -95,6 +139,8 @@ int main(int argc, char *argv[])
   /*   Action registration */
   MSG_action_register("send", send);
   MSG_action_register("recv", recv);
+  MSG_action_register("Irecv", Irecv);
+  MSG_action_register("wait", wait);
   MSG_action_register("sleep", sleep);
   MSG_action_register("compute", compute);