Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / examples / cpp / mc-centralized-mutex / s4u-mc-centralized-mutex.cpp
1 /* Copyright (c) 2010-2022. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 /***************** Centralized Mutual Exclusion Algorithm *********************/
8 /* This example implements a centralized mutual exclusion algorithm.          */
9 /* There is no bug on it, it is just provided to test the state space         */
10 /* reduction of DPOR.                                                         */
11 /******************************************************************************/
12
13 #include "simgrid/s4u.hpp"
14
15 #define AMOUNT_OF_CLIENTS 4
16 #define CS_PER_PROCESS 2
17
18 XBT_LOG_NEW_DEFAULT_CATEGORY(centralized, "my log messages");
19
20 class Message {
21 public:
22   enum class Kind { GRANT, REQUEST, RELEASE };
23   Kind kind                             = Kind::GRANT;
24   simgrid::s4u::Mailbox* return_mailbox = nullptr;
25   explicit Message(Message::Kind kind, simgrid::s4u::Mailbox* mbox) : kind(kind), return_mailbox(mbox) {}
26 };
27
28 static void coordinator()
29 {
30   std::queue<simgrid::s4u::Mailbox*> requests;
31   simgrid::s4u::Mailbox* mbox = simgrid::s4u::Mailbox::by_name("coordinator");
32
33   bool CS_used = false;                              // initially the CS is idle
34   int todo     = AMOUNT_OF_CLIENTS * CS_PER_PROCESS; // amount of releases we are expecting
35
36   while (todo > 0) {
37     auto m = mbox->get_unique<Message>();
38     if (m->kind == Message::Kind::REQUEST) {
39       if (CS_used) { // need to push the request in the vector
40         XBT_INFO("CS already used. Queue the request");
41         requests.push(m->return_mailbox);
42       } else { // can serve it immediately
43         XBT_INFO("CS idle. Grant immediately");
44         m->return_mailbox->put(new Message(Message::Kind::GRANT, mbox), 1000);
45         CS_used = true;
46       }
47     } else { // that's a release. Check if someone was waiting for the lock
48       if (not requests.empty()) {
49         XBT_INFO("CS release. Grant to queued requests (queue size: %zu)", requests.size());
50         simgrid::s4u::Mailbox* req = requests.front();
51         requests.pop();
52         req->put(new Message(Message::Kind::GRANT, mbox), 1000);
53         todo--;
54       } else { // nobody wants it
55         XBT_INFO("CS release. resource now idle");
56         CS_used = false;
57         todo--;
58       }
59     }
60   }
61   XBT_INFO("Received all releases, quit now");
62 }
63
64 static void client()
65 {
66   aid_t my_pid = simgrid::s4u::this_actor::get_pid();
67
68   simgrid::s4u::Mailbox* my_mailbox = simgrid::s4u::Mailbox::by_name(std::to_string(my_pid));
69
70   // request the CS 3 times, sleeping a bit in between
71   for (int i = 0; i < CS_PER_PROCESS; i++) {
72     XBT_INFO("Ask the request");
73     simgrid::s4u::Mailbox::by_name("coordinator")->put(new Message(Message::Kind::REQUEST, my_mailbox), 1000);
74     // wait for the answer
75     auto grant = my_mailbox->get_unique<Message>();
76     XBT_INFO("got the answer. Sleep a bit and release it");
77     simgrid::s4u::this_actor::sleep_for(1);
78
79     simgrid::s4u::Mailbox::by_name("coordinator")->put(new Message(Message::Kind::RELEASE, my_mailbox), 1000);
80     simgrid::s4u::this_actor::sleep_for(my_pid);
81   }
82   XBT_INFO("Got all the CS I wanted, quit now");
83 }
84
85 int main(int argc, char* argv[])
86 {
87   simgrid::s4u::Engine e(&argc, argv);
88
89   e.load_platform(argv[1]);
90
91   simgrid::s4u::Actor::create("coordinator", e.host_by_name("Tremblay"), coordinator);
92   simgrid::s4u::Actor::create("client", e.host_by_name("Fafard"), client);
93   simgrid::s4u::Actor::create("client", e.host_by_name("Boivin"), client);
94   simgrid::s4u::Actor::create("client", e.host_by_name("Jacquelin"), client);
95   simgrid::s4u::Actor::create("client", e.host_by_name("Ginette"), client);
96
97   e.run();
98
99   return 0;
100 }