1 /* Copyright (c) 2012-2020. 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 /***************** Centralized Mutual Exclusion Algorithm *******************/
7 /* This example implements a centralized mutual exclusion algorithm. */
8 /* Bug : CS requests of client 1 not satisfied */
9 /* LTL property checked : G(r->F(cs)); (r=request of CS, cs=CS ok) */
10 /****************************************************************************/
18 #include <simgrid/modelchecker.h>
19 #include <simgrid/s4u.hpp>
20 #include <xbt/dynar.h>
22 XBT_LOG_NEW_DEFAULT_CATEGORY(bugged1_liveness, "my log messages");
26 enum class Kind { GRANT, REQUEST, RELEASE };
27 Kind kind = Kind::GRANT;
28 simgrid::s4u::Mailbox* return_mailbox = nullptr;
29 explicit Message(Message::Kind kind, simgrid::s4u::Mailbox* mbox) : kind(kind), return_mailbox(mbox) {}
36 /** Do not use a clean stack */
37 static void garbage_stack(void)
39 const size_t size = 256;
40 int fd = open("/dev/urandom", O_RDONLY);
47 static void coordinator()
50 const Message* m = nullptr;
51 std::queue<simgrid::s4u::Mailbox*> requests;
53 simgrid::s4u::Mailbox* mbox = simgrid::s4u::Mailbox::by_name("coordinator");
56 m = mbox->get<Message>();
57 if (m->kind == Message::Kind::REQUEST) {
59 XBT_INFO("CS already used. Queue the request.");
60 requests.push(m->return_mailbox);
62 if (m->return_mailbox->get_name() != "1") {
63 XBT_INFO("CS idle. Grant immediately");
64 m->return_mailbox->put(new Message(Message::Kind::GRANT, mbox), 1000);
69 if (not requests.empty()) {
70 XBT_INFO("CS release. Grant to queued requests (queue size: %zu)", requests.size());
71 simgrid::s4u::Mailbox* req = requests.front();
73 if (req->get_name() != "1") {
74 req->put(new Message(Message::Kind::GRANT, mbox), 1000);
80 XBT_INFO("CS release. resource now idle");
88 static void client(int id)
90 aid_t my_pid = simgrid::s4u::this_actor::get_pid();
92 simgrid::s4u::Mailbox* my_mailbox = simgrid::s4u::Mailbox::by_name(std::to_string(id));
95 XBT_INFO("Ask the request");
96 simgrid::s4u::Mailbox::by_name("coordinator")->put(new Message(Message::Kind::REQUEST, my_mailbox), 1000);
101 XBT_INFO("Propositions changed : r=1, cs=0");
104 const auto* grant = my_mailbox->get<Message>();
106 if ((id == 1) && (grant->kind == Message::Kind::GRANT)) {
109 XBT_INFO("Propositions changed : r=0, cs=1");
114 XBT_INFO("%d got the answer. Sleep a bit and release it", id);
116 simgrid::s4u::this_actor::sleep_for(1);
118 simgrid::s4u::Mailbox::by_name("coordinator")->put(new Message(Message::Kind::RELEASE, my_mailbox), 1000);
120 simgrid::s4u::this_actor::sleep_for(static_cast<double>(my_pid));
125 XBT_INFO("Propositions changed : r=0, cs=0");
130 static void raw_client(int id)
133 // At this point the stack of the callee (client) is probably filled with
134 // zeros and uninitialized variables will contain 0. This call will place
135 // random byes in the stack of the callee:
141 int main(int argc, char* argv[])
143 simgrid::s4u::Engine e(&argc, argv);
145 MC_automaton_new_propositional_symbol_pointer("r", &r);
146 MC_automaton_new_propositional_symbol_pointer("cs", &cs);
148 e.load_platform(argv[1]);
150 simgrid::s4u::Actor::create("coordinator", simgrid::s4u::Host::by_name("Tremblay"), coordinator)
151 ->set_kill_time(argc > 3 ? std::stod(argv[3]) : -1.0);
152 if (std::stod(argv[2]) == 0) {
153 simgrid::s4u::Actor::create("client", simgrid::s4u::Host::by_name("Boivin"), raw_client, 1);
154 simgrid::s4u::Actor::create("client", simgrid::s4u::Host::by_name("Fafard"), raw_client, 2);
155 } else { // "Visited" case
156 simgrid::s4u::Actor::create("client", simgrid::s4u::Host::by_name("Boivin"), raw_client, 2);
157 simgrid::s4u::Actor::create("client", simgrid::s4u::Host::by_name("Fafard"), raw_client, 1);