Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9f373a198b170d90b361891397f5c3d999f3e54b
[simgrid.git] / examples / s4u / exec-dependent / s4u-exec-dependent.cpp
1 /* Copyright (c) 2007-2019. 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_test, "Messages specific for this s4u example");
9
10 simgrid::s4u::ExecPtr child;
11
12 static void worker()
13 {
14   double computation_amount = simgrid::s4u::this_actor::get_host()->get_speed();
15
16   simgrid::s4u::ExecPtr first_parent  = simgrid::s4u::this_actor::exec_init(computation_amount);
17   simgrid::s4u::ExecPtr second_parent = simgrid::s4u::this_actor::exec_init(2 * computation_amount);
18
19   child = simgrid::s4u::this_actor::exec_init(computation_amount);
20
21   first_parent->add_successor(child.get());
22   second_parent->add_successor(child.get());
23   second_parent->start();
24   first_parent->wait();
25   second_parent->wait();
26 }
27
28 static void vetoed_worker()
29 {
30   child->vetoable_start();
31   while (not child->test()) {
32     if (child->get_state() == simgrid::s4u::Exec::State::STARTING)
33       XBT_INFO("child cannot start yet");
34     else
35       XBT_INFO("child is now in state %d", (int)child->get_state());
36     simgrid::s4u::this_actor::sleep_for(0.25);
37   }
38   XBT_INFO("Should be okay now, child is in state %d", (int)child->get_state());
39 }
40
41 int main(int argc, char* argv[])
42 {
43   simgrid::s4u::Engine e(&argc, argv);
44   e.load_platform(argv[1]);
45
46   simgrid::s4u::Actor::create("worker", simgrid::s4u::Host::by_name("Fafard"), worker);
47   simgrid::s4u::Actor::create("vetoed_worker", simgrid::s4u::Host::by_name("Fafard"), vetoed_worker);
48
49   e.run();
50
51   XBT_INFO("Simulation time %g", e.get_clock());
52
53   return 0;
54 }