1 /* Copyright (c) 2012-2023. 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");
16 namespace sg4 = simgrid::s4u;
20 enum class Kind { GRANT, NOT_GRANT, REQUEST };
21 Kind kind = Kind::GRANT;
22 sg4::Mailbox* return_mailbox = nullptr;
23 explicit Message(Message::Kind kind, sg4::Mailbox* mbox) : kind(kind), return_mailbox(mbox) {}
28 static void coordinator()
30 bool CS_used = false; // initially the CS is idle
31 std::queue<sg4::Mailbox*> requests;
33 sg4::Mailbox* mbox = sg4::Mailbox::by_name("coordinator");
36 auto m = mbox->get_unique<Message>();
37 if (m->kind == Message::Kind::REQUEST) {
39 XBT_INFO("CS already used.");
40 m->return_mailbox->put(new Message(Message::Kind::NOT_GRANT, mbox), 1000);
41 } else { // can serve it immediately
42 XBT_INFO("CS idle. Grant immediately");
43 m->return_mailbox->put(new Message(Message::Kind::GRANT, mbox), 1000);
46 } else { // that's a release. Check if someone was waiting for the lock
47 XBT_INFO("CS release. resource now idle");
53 static void client(int id)
55 aid_t my_pid = sg4::this_actor::get_pid();
57 sg4::Mailbox* my_mailbox = sg4::Mailbox::by_name(std::to_string(id));
60 XBT_INFO("Client (%d) asks the request", id);
61 sg4::Mailbox::by_name("coordinator")->put(new Message(Message::Kind::REQUEST, my_mailbox), 1000);
63 auto grant = my_mailbox->get_unique<Message>();
65 if (grant->kind == Message::Kind::GRANT) {
66 XBT_INFO("Client (%d) got the answer (grant). Sleep a bit and release it", id);
70 XBT_INFO("Client (%d) got the answer (not grant). Try again", id);
73 sg4::this_actor::sleep_for(my_pid);
77 int main(int argc, char* argv[])
79 sg4::Engine e(&argc, argv);
81 MC_automaton_new_propositional_symbol_pointer("cs", &cs);
83 e.load_platform(argv[1]);
85 sg4::Actor::create("coordinator", e.host_by_name("Tremblay"), coordinator);
86 sg4::Actor::create("client", e.host_by_name("Fafard"), client, 1);
87 sg4::Actor::create("client", e.host_by_name("Boivin"), client, 2);