Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / teshsuite / mc / mutex-handling / mutex-handling.cpp
1 /* Copyright (c) 2015-2023. 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 /* In this test, we have two senders sending one message to a common receiver.
7  * The receiver should be able to see any ordering between the two messages.
8  * If we model-check the application with assertions on a specific order of
9  * the messages (see the assertions in the receiver code), it should fail
10  * because both ordering are possible.
11  *
12  * If the senders sends the message directly, the current version of the MC
13  * finds that the ordering may differ and the MC find a counter-example.
14  *
15  * However, if the senders send the message in a mutex, the MC always let
16  * the first process take the mutex because it thinks that the effect of
17  * a mutex is purely local: the ordering of the messages is always the same
18  * and the MC does not find the counter-example.
19  */
20
21 #include "simgrid/modelchecker.h"
22 #include "simgrid/s4u/Engine.hpp"
23 #include "simgrid/s4u/Host.hpp"
24 #include "simgrid/s4u/Mailbox.hpp"
25 #include "simgrid/s4u/Mutex.hpp"
26
27 #include <mutex> // std::unique_lock
28
29 XBT_LOG_NEW_DEFAULT_CATEGORY(mutex_handling, "Messages specific for this test");
30
31 static int receiver(const char* box_name)
32 {
33   auto* mb = simgrid::s4u::Mailbox::by_name(box_name);
34   std::unique_ptr<int> payload;
35
36   payload = mb->get_unique<int>();
37   MC_assert(*payload == 1);
38
39   payload = mb->get_unique<int>();
40   MC_assert(*payload == 2);
41
42   return 0;
43 }
44
45 static int sender(const char* box_name, simgrid::s4u::MutexPtr mutex, int value)
46 {
47   auto* payload = new int(value);
48   auto* mb      = simgrid::s4u::Mailbox::by_name(box_name);
49
50   std::unique_lock<simgrid::s4u::Mutex> lock;
51   if (mutex)
52     lock = std::unique_lock(*mutex);
53
54   mb->put(payload, 8);
55   return 0;
56 }
57
58 int main(int argc, char* argv[])
59 {
60   simgrid::s4u::Engine e(&argc, argv);
61   xbt_assert(argc > 1,
62              "Usage: %s platform_file\n"
63              "\tExample: %s platform.xml\n",
64              argv[0], argv[0]);
65
66   simgrid::s4u::MutexPtr mutex;
67 #ifndef DISABLE_THE_MUTEX
68   mutex = simgrid::s4u::Mutex::create();
69 #endif
70
71   e.load_platform(argv[1]);
72   simgrid::s4u::Actor::create("receiver", e.host_by_name("Jupiter"), receiver, "box");
73   simgrid::s4u::Actor::create("sender", e.host_by_name("Boivin"), sender, "box", mutex, 1);
74   simgrid::s4u::Actor::create("sender", e.host_by_name("Fafard"), sender, "box", mutex, 2);
75
76   e.run();
77   XBT_INFO("Simulation time %g", simgrid::s4u::Engine::get_clock());
78
79   return 0;
80 }