Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / examples / c / actor-exiting / actor-exiting.c
index c556c15..2e7a536 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2017-2020. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2017-2022. The SimGrid Team. All rights reserved.          */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
 #include <simgrid/actor.h>
 #include <simgrid/engine.h>
 #include <simgrid/host.h>
+#include <simgrid/mailbox.h>
 
 #include <xbt/asserts.h>
 #include <xbt/log.h>
 
 XBT_LOG_NEW_DEFAULT_CATEGORY(actor_exiting, "Messages specific for this example");
 
-static int my_on_exit(XBT_ATTRIB_UNUSED int ignored1, XBT_ATTRIB_UNUSED void* ignored2)
+static void A_on_exit(int ignored1, void* ignored2)
 {
   XBT_INFO("I stop now");
-  return 0;
 }
 
-static void actor_fun(XBT_ATTRIB_UNUSED int argc, XBT_ATTRIB_UNUSED char* argv[])
+static void actorA_fun(int argc, char* argv[])
+{
+  // Register a lambda function to be executed once it stops
+  sg_actor_on_exit(&A_on_exit, NULL);
+
+  sg_actor_sleep_for(1);
+}
+static void actorB_fun(int argc, char* argv[])
+{
+  sg_actor_sleep_for(2);
+}
+static void C_on_exit(int failed, void* ignored2)
+{
+  if (failed) {
+    XBT_INFO("I was killed!");
+    if (xbt_log_no_loc)
+      XBT_INFO("The backtrace would be displayed here if --log=no_loc would not have been passed");
+    else
+      xbt_backtrace_display_current();
+  } else
+    XBT_INFO("Exiting gracefully.");
+}
+static void actorC_fun(int argc, char* argv[])
 {
   // Register a lambda function to be executed once it stops
-  sg_actor_on_exit(&my_on_exit, NULL);
+  sg_actor_on_exit(&C_on_exit, NULL);
 
-  sg_actor_self_execute(1e9);
+  sg_actor_sleep_for(3);
+  XBT_INFO("And now, induce a deadlock by waiting for a message that will never come\n\n");
+  sg_mailbox_get(sg_mailbox_by_name("nobody"));
+  xbt_die("Receiving is not supposed to succeed when nobody is sending");
 }
 
 int main(int argc, char* argv[])
@@ -46,8 +71,9 @@ int main(int argc, char* argv[])
 
   simgrid_load_platform(argv[1]); /* - Load the platform description */
 
-  sg_actor_t actor = sg_actor_init("A", sg_host_by_name("Tremblay"));
-  sg_actor_start(actor, actor_fun, 0, NULL);
+  sg_actor_create("A", sg_host_by_name("Tremblay"), actorA_fun, 0, NULL);
+  sg_actor_create("B", sg_host_by_name("Fafard"), actorB_fun, 0, NULL);
+  sg_actor_create("C", sg_host_by_name("Ginette"), actorC_fun, 0, NULL);
 
   simgrid_run();