1 /* Copyright (c) 2012-2021. 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 /***************************** Bugged2 ****************************************/
7 /* This example implements a centralized mutual exclusion algorithm. */
8 /* One client stay always in critical section */
9 /* LTL property checked : !(GFcs) */
10 /******************************************************************************/
12 #include <simgrid/modelchecker.h>
13 #include <simgrid/s4u.hpp>
15 XBT_LOG_NEW_DEFAULT_CATEGORY(bugged2_liveness, "my log messages");
19 enum class Kind { GRANT, NOT_GRANT, REQUEST };
20 Kind kind = Kind::GRANT;
21 simgrid::s4u::Mailbox* return_mailbox = nullptr;
22 explicit Message(Message::Kind kind, simgrid::s4u::Mailbox* mbox) : kind(kind), return_mailbox(mbox) {}
27 static void coordinator()
29 bool CS_used = false; // initially the CS is idle
30 std::queue<simgrid::s4u::Mailbox*> requests;
32 simgrid::s4u::Mailbox* mbox = simgrid::s4u::Mailbox::by_name("coordinator");
35 auto m = mbox->get_unique<Message>();
36 if (m->kind == Message::Kind::REQUEST) {
38 XBT_INFO("CS already used.");
39 m->return_mailbox->put(new Message(Message::Kind::NOT_GRANT, mbox), 1000);
40 } else { // can serve it immediately
41 XBT_INFO("CS idle. Grant immediately");
42 m->return_mailbox->put(new Message(Message::Kind::GRANT, mbox), 1000);
45 } else { // that's a release. Check if someone was waiting for the lock
46 XBT_INFO("CS release. resource now idle");
52 static void client(int id)
54 aid_t my_pid = simgrid::s4u::this_actor::get_pid();
56 simgrid::s4u::Mailbox* my_mailbox = simgrid::s4u::Mailbox::by_name(std::to_string(id));
59 XBT_INFO("Client (%d) asks the request", id);
60 simgrid::s4u::Mailbox::by_name("coordinator")->put(new Message(Message::Kind::REQUEST, my_mailbox), 1000);
62 auto grant = my_mailbox->get_unique<Message>();
64 if (grant->kind == Message::Kind::GRANT) {
65 XBT_INFO("Client (%d) got the answer (grant). Sleep a bit and release it", id);
69 XBT_INFO("Client (%d) got the answer (not grant). Try again", id);
72 simgrid::s4u::this_actor::sleep_for(my_pid);
76 int main(int argc, char* argv[])
78 simgrid::s4u::Engine e(&argc, argv);
80 MC_automaton_new_propositional_symbol_pointer("cs", &cs);
82 e.load_platform(argv[1]);
84 simgrid::s4u::Actor::create("coordinator", e.host_by_name("Tremblay"), coordinator);
85 simgrid::s4u::Actor::create("client", e.host_by_name("Fafard"), client, 1);
86 simgrid::s4u::Actor::create("client", e.host_by_name("Boivin"), client, 2);