Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines with new year.
[simgrid.git] / examples / s4u / actor-suspend / s4u-actor-suspend.cpp
1 /* Copyright (c) 2017-2020. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include <simgrid/s4u.hpp>
7
8 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_actor_suspend, "Messages specific for this s4u example");
9
10 /* The Lazy guy only wants to sleep, but can be awaken by the dream_master process. */
11 static void lazy_guy()
12 {
13   XBT_INFO("Nobody's watching me ? Let's go to sleep.");
14   simgrid::s4u::this_actor::suspend(); /* - Start by suspending itself */
15   XBT_INFO("Uuuh ? Did somebody call me ?");
16
17   XBT_INFO("Going to sleep..."); /* - Then repetitively go to sleep, but got awaken */
18   simgrid::s4u::this_actor::sleep_for(10);
19   XBT_INFO("Mmm... waking up.");
20
21   XBT_INFO("Going to sleep one more time (for 10 sec)...");
22   simgrid::s4u::this_actor::sleep_for(10);
23   XBT_INFO("Waking up once for all!");
24
25   XBT_INFO("Ok, let's do some work, then (for 10 sec on Boivin).");
26   simgrid::s4u::this_actor::execute(980.95e6);
27
28   XBT_INFO("Mmmh, I'm done now. Goodbye.");
29 }
30
31 /* The Dream master: */
32 static void dream_master()
33 {
34   XBT_INFO("Let's create a lazy guy."); /* - Create a lazy_guy process */
35   simgrid::s4u::ActorPtr lazy = simgrid::s4u::Actor::create("Lazy", simgrid::s4u::this_actor::get_host(), lazy_guy);
36   XBT_INFO("Let's wait a little bit...");
37   simgrid::s4u::this_actor::sleep_for(10); /* - Wait for 10 seconds */
38   XBT_INFO("Let's wake the lazy guy up! >:) BOOOOOUUUHHH!!!!");
39   if (lazy->is_suspended())
40     lazy->resume(); /* - Then wake up the lazy_guy */
41   else
42     XBT_ERROR("I was thinking that the lazy guy would be suspended now");
43
44   simgrid::s4u::this_actor::sleep_for(5); /* Repeat two times: */
45   XBT_INFO("Suspend the lazy guy while he's sleeping...");
46   lazy->suspend(); /* - Suspend the lazy_guy while he's asleep */
47   XBT_INFO("Let him finish his siesta.");
48   simgrid::s4u::this_actor::sleep_for(10); /* - Wait for 10 seconds */
49   XBT_INFO("Wake up, lazy guy!");
50   lazy->resume(); /* - Then wake up the lazy_guy again */
51
52   simgrid::s4u::this_actor::sleep_for(5);
53   XBT_INFO("Suspend again the lazy guy while he's sleeping...");
54   lazy->suspend();
55   XBT_INFO("This time, don't let him finish his siesta.");
56   simgrid::s4u::this_actor::sleep_for(2);
57   XBT_INFO("Wake up, lazy guy!");
58   lazy->resume();
59
60   simgrid::s4u::this_actor::sleep_for(5);
61   XBT_INFO("Give a 2 seconds break to the lazy guy while he's working...");
62   lazy->suspend();
63   simgrid::s4u::this_actor::sleep_for(2);
64   XBT_INFO("Back to work, lazy guy!");
65   lazy->resume();
66
67   XBT_INFO("OK, I'm done here.");
68 }
69
70 int main(int argc, char* argv[])
71 {
72   simgrid::s4u::Engine e(&argc, argv);
73   xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s ../platforms/small_platform.xml\n", argv[0], argv[0]);
74
75   e.load_platform(argv[1]); /* - Load the platform description */
76   std::vector<simgrid::s4u::Host*> list = e.get_all_hosts();
77   simgrid::s4u::Actor::create("dream_master", list.front(), dream_master);
78
79   e.run(); /* - Run the simulation */
80
81   return 0;
82 }