Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
second try to fix
[simgrid.git] / teshsuite / mc / mutex-handling / mutex-handling.c
1 /* Copyright (c) 2015. 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 /* In this test, we have two senders sending one message to a common receiver.
8  * The receiver should be able to see any ordering between the two messages.
9  * If we model-check the application with assertions on a specific order of
10  * the messages (see the assertions in the receiver code), it should fail
11  * because both ordering are possible.
12  *
13  * If the senders sends the message directly, the current version of the MC
14  * finds that the ordering may differ and the MC find a counter-example.
15  *
16  * However, if the senders send the message in a mutex, the MC always let
17  * the first process take the mutex because it thinks that the effect of
18  * a mutex is purely local: the ordering of the messages is always the same
19  * and the MC does not find the counter-example.
20  */
21
22 #include "simgrid/msg.h"
23 #include "mc/mc.h"
24 #include <xbt/synchro_core.h>
25
26 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
27
28 #define BOX_NAME "box"
29
30 #ifndef DISABLE_THE_MUTEX
31 static xbt_mutex_t mutex = NULL;
32 #endif
33
34 static int receiver(int argc, char *argv[])
35 {
36   msg_task_t task = NULL;
37
38   MSG_task_receive(&task, BOX_NAME);
39   MC_assert(strcmp(MSG_task_get_name(task), "X") == 0);
40   MSG_task_destroy(task);
41
42   MSG_task_receive(&task, BOX_NAME);
43   MC_assert(strcmp(MSG_task_get_name(task), "Y") == 0);
44   MSG_task_destroy(task);
45
46   return 0;
47 }
48
49 static int sender(int argc, char *argv[])
50 {
51   char* message_name = argv[1];
52 #ifndef DISABLE_THE_MUTEX
53   xbt_mutex_acquire(mutex);
54 #endif
55   MSG_task_send(MSG_task_create(message_name, 0.0, 0.0, NULL), BOX_NAME);
56 #ifndef DISABLE_THE_MUTEX
57   xbt_mutex_release(mutex);
58 #endif
59   return 0;
60 }
61
62 int main(int argc, char *argv[])
63 {
64   MSG_init(&argc, argv);
65   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
66        "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
67
68   MSG_create_environment(argv[1]);
69   MSG_function_register("receiver", receiver);
70   MSG_function_register("sender", sender);
71
72   MSG_launch_application(argv[2]);
73 #ifndef DISABLE_THE_MUTEX
74   mutex = xbt_mutex_init();
75 #endif
76   msg_error_t res = MSG_main();
77 #ifndef DISABLE_THE_MUTEX
78   xbt_mutex_destroy(mutex); mutex = NULL;
79 #endif
80   XBT_INFO("Simulation time %g", MSG_get_clock());
81
82   return res != MSG_OK;
83 }