Logo AND Algorithmique Numérique Distribuée

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