1 /* Copyright (c) 2006-2022. The SimGrid Team. All rights reserved. */
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. */
6 // This example implements a simple producer/consumer schema, passing a bunch of items from one to the other,
7 // hopefully implemented in a way that resists resource failures.
9 #include <simgrid/s4u.hpp>
10 #include <xbt/config.hpp>
12 namespace sg4 = simgrid::s4u;
14 XBT_LOG_NEW_DEFAULT_CATEGORY(sem_monkey, "Simple test of the semaphore");
16 static simgrid::config::Flag<int> cfg_item_count{"item-count", "Amount of items that must be exchanged to succeed", 2};
17 static simgrid::config::Flag<double> cfg_deadline{"deadline", "When to fail the simulation (infinite loop detection)",
21 int value = 0; /* Where the data is exchanged */
22 sg4::SemaphorePtr sem_empty = sg4::Semaphore::create(1); /* indicates whether the buffer is empty */
23 sg4::SemaphorePtr sem_full = sg4::Semaphore::create(0); /* indicates whether the buffer is full */
26 // A stack to keep track of semaphores. When destroyed, semaphores remaining on stack are automatically released.
28 std::vector<sg4::Semaphore*> to_release;
32 SemStack(const SemStack&) = delete;
33 SemStack& operator=(const SemStack&) = delete;
36 for (auto* sem : to_release) {
37 XBT_INFO("Go release a semaphore");
39 XBT_INFO("Released a semaphore on exit. It's now %d", sem->get_capacity());
42 void push(const sg4::SemaphorePtr& sem) { to_release.push_back(sem.get()); }
43 void pop() { to_release.pop_back(); }
46 static void producer(SharedBuffer& buf)
48 static int todo = cfg_item_count; // remaining amount of items to exchange
50 bool rebooting = sg4::Actor::self()->get_restart_count() > 0;
52 XBT_INFO("Producer %s", rebooting ? "rebooting" : "booting");
53 if (not rebooting) // Starting for the first time
54 sg4::this_actor::on_exit(
55 [](bool forcefully) { XBT_INFO("Producer dying %s.", forcefully ? "forcefully" : "peacefully"); });
58 sg4::this_actor::sleep_for(1); // Give a chance to the monkey to kill this actor at this point
60 while (buf.sem_empty->acquire_timeout(10)) {
61 XBT_INFO("Timeouted");
62 xbt_assert(sg4::Engine::get_clock() < cfg_deadline,
63 "Failed to exchange all tasks in less than %d seconds. Is this an infinite loop?", (int)cfg_deadline);
65 to_release.push(buf.sem_empty);
66 XBT_INFO("sem_empty acquired");
68 sg4::this_actor::sleep_for(1); // Give a chance to the monkey to kill this actor at this point
70 XBT_INFO("Pushing item %d", todo - 1);
72 buf.sem_full->release();
74 XBT_INFO("sem_empty removed from to_release");
79 static void consumer(const SharedBuffer& buf)
82 bool rebooting = sg4::Actor::self()->get_restart_count() > 0;
84 XBT_INFO("Consumer %s", rebooting ? "rebooting" : "booting");
85 if (not rebooting) // Starting for the first time
86 sg4::this_actor::on_exit(
87 [](bool forcefully) { XBT_INFO("Consumer dying %s.", forcefully ? "forcefully" : "peacefully"); });
91 sg4::this_actor::sleep_for(0.75); // Give a chance to the monkey to kill this actor at this point
93 while (buf.sem_full->acquire_timeout(10)) {
94 XBT_INFO("Timeouted");
95 xbt_assert(sg4::Engine::get_clock() < cfg_deadline,
96 "Failed to exchange all tasks in less than %d seconds. Is this an infinite loop?", (int)cfg_deadline);
98 to_release.push(buf.sem_full);
100 sg4::this_actor::sleep_for(0.75); // Give a chance to the monkey to kill this actor at this point
103 XBT_INFO("Receiving item %d", item);
104 buf.sem_empty->release();
111 int main(int argc, char** argv)
113 sg4::Engine e(&argc, argv);
115 auto* rootzone = sg4::create_full_zone("root");
116 auto* paul = rootzone->create_host("Paul", 1e9);
117 auto* carol = rootzone->create_host("Carol", 1e9);
118 sg4::LinkInRoute link(rootzone->create_link("link", "1MBps")->set_latency("24us")->seal());
119 rootzone->add_route(paul->get_netpoint(), carol->get_netpoint(), nullptr, nullptr, {link}, true);
122 sg4::Actor::create("producer", paul, producer, std::ref(buffer))->set_auto_restart();
123 sg4::Actor::create("consumer", carol, consumer, std::cref(buffer))->set_auto_restart();