Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'depencencies' into 'master'
[simgrid.git] / teshsuite / mc / mutex-handling / mutex-handling.cpp
1 /* Copyright (c) 2015-2020. 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 #include <xbt/synchro.h>
27
28 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
29
30 static int receiver(const char* box_name)
31 {
32   int* payload;
33   auto mb = simgrid::s4u::Mailbox::by_name(box_name);
34
35   payload = static_cast<int*>(mb->get());
36   MC_assert(*payload == 0);
37   free(payload);
38
39   payload = static_cast<int*>(mb->get());
40   MC_assert(*payload == 1);
41   free(payload);
42
43   return 0;
44 }
45
46 static int sender(const char* box_name, simgrid::s4u::MutexPtr mutex, int value)
47 {
48   int* payload = new int(value);
49   auto mb      = simgrid::s4u::Mailbox::by_name(box_name);
50
51 #ifndef DISABLE_THE_MUTEX
52   mutex->lock();
53 #endif
54
55   mb->put(static_cast<void*>(payload), 8);
56
57 #ifndef DISABLE_THE_MUTEX
58   mutex->unlock();
59 #endif
60   return 0;
61 }
62
63 int main(int argc, char* argv[])
64 {
65   XBT_ATTRIB_UNUSED simgrid::s4u::MutexPtr mutex;
66 #ifndef DISABLE_THE_MUTEX
67   mutex = simgrid::s4u::Mutex::create();
68 #endif
69
70   simgrid::s4u::Engine e(&argc, argv);
71   xbt_assert(argc > 2,
72              "Usage: %s platform_file deployment_file\n"
73              "\tExample: %s msg_platform.xml msg_deployment.xml\n",
74              argv[0], argv[0]);
75
76   e.load_platform(argv[1]);
77   simgrid::s4u::Actor::create("sender", simgrid::s4u::Host::by_name("Tremblay"), sender, "box", mutex, 1);
78   simgrid::s4u::Actor::create("sender", simgrid::s4u::Host::by_name("Tremblay"), sender, "box", mutex, 2);
79   simgrid::s4u::Actor::create("receiver", simgrid::s4u::Host::by_name("Jupiter"), receiver, "box");
80
81 #ifndef DISABLE_THE_MUTEX
82   mutex = simgrid::s4u::Mutex::create();
83 #endif
84
85   e.run();
86   XBT_INFO("Simulation time %g", e.get_clock());
87
88   return 0;
89 }