Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
48c39ffe1805ad5923427a4ea3c6774afeaec72f
[simgrid.git] / examples / cpp / mc-bugged2-liveness / s4u-mc-bugged2-liveness.cpp
1 /* Copyright (c) 2012-2021. 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 /***************************** Bugged2 ****************************************/
7 /* This example implements a centralized mutual exclusion algorithm.          */
8 /* One client stay always in critical section                                 */
9 /* LTL property checked : !(GFcs)                                             */
10 /******************************************************************************/
11
12 #include <simgrid/modelchecker.h>
13 #include <simgrid/s4u.hpp>
14
15 XBT_LOG_NEW_DEFAULT_CATEGORY(bugged2_liveness, "my log messages");
16
17 class Message {
18 public:
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) {}
23 };
24
25 int cs = 0;
26
27 static void coordinator()
28 {
29   bool CS_used = false; // initially the CS is idle
30   std::queue<simgrid::s4u::Mailbox*> requests;
31
32   simgrid::s4u::Mailbox* mbox = simgrid::s4u::Mailbox::by_name("coordinator");
33
34   while (true) {
35     auto m = mbox->get_unique<Message>();
36     if (m->kind == Message::Kind::REQUEST) {
37       if (CS_used) {
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);
43         CS_used = true;
44       }
45     } else { // that's a release. Check if someone was waiting for the lock
46       XBT_INFO("CS release. resource now idle");
47       CS_used = false;
48     }
49   }
50 }
51
52 static void client(int id)
53 {
54   aid_t my_pid = simgrid::s4u::this_actor::get_pid();
55
56   simgrid::s4u::Mailbox* my_mailbox = simgrid::s4u::Mailbox::by_name(std::to_string(id));
57
58   while (true) {
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);
61
62     auto grant = my_mailbox->get_unique<Message>();
63
64     if (grant->kind == Message::Kind::GRANT) {
65       XBT_INFO("Client (%d) got the answer (grant). Sleep a bit and release it", id);
66       if (id == 1)
67         cs = 1;
68     } else {
69       XBT_INFO("Client (%d) got the answer (not grant). Try again", id);
70     }
71
72     simgrid::s4u::this_actor::sleep_for(my_pid);
73   }
74 }
75
76 int main(int argc, char* argv[])
77 {
78   simgrid::s4u::Engine e(&argc, argv);
79
80   MC_automaton_new_propositional_symbol_pointer("cs", &cs);
81
82   e.load_platform(argv[1]);
83
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);
87
88   e.run();
89
90   return 0;
91 }