Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[simgrid.git] / teshsuite / mc / 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 <stdio.h>
23 #include "simgrid/msg.h"
24 #include "xbt/log.h"
25 #include "mc/mc.h"
26 #include <xbt/synchro_core.h>
27
28 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
29                              "Messages specific for this msg example");
30
31 #define BOX_NAME "box"
32
33 #ifndef DISABLE_THE_MUTEX
34 static xbt_mutex_t mutex = NULL;
35 #endif
36
37 static int receiver(int argc, char *argv[])
38 {
39   msg_task_t task = NULL;
40
41   MSG_task_receive(&task, BOX_NAME);
42   MC_assert(strcmp(MSG_task_get_name(task), "X") == 0);
43   MSG_task_destroy(task);
44
45   MSG_task_receive(&task, BOX_NAME);
46   MC_assert(strcmp(MSG_task_get_name(task), "Y") == 0);
47   MSG_task_destroy(task);
48
49   return 0;
50 }
51
52 static int sender(int argc, char *argv[])
53 {
54   char* message_name = argv[1];
55 #ifndef DISABLE_THE_MUTEX
56   xbt_mutex_acquire(mutex);
57 #endif
58   MSG_task_send(MSG_task_create(message_name, 0.0, 0.0, NULL), BOX_NAME);
59 #ifndef DISABLE_THE_MUTEX
60   xbt_mutex_release(mutex);
61 #endif
62   return 0;
63 }
64
65 int main(int argc, char *argv[])
66 {
67   MSG_init(&argc, argv);
68   if (argc != 3) {
69     printf("Usage: %s platform_file deployment_file\n", argv[0]);
70     printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
71     exit(1);
72   }
73   const char *platform_file = argv[1];
74   const char *application_file = argv[2];
75   MSG_create_environment(platform_file);
76   MSG_function_register("receiver", receiver);
77   MSG_function_register("sender", sender);
78
79   MSG_launch_application(application_file);
80 #ifndef DISABLE_THE_MUTEX
81   mutex = xbt_mutex_init();
82 #endif
83   msg_error_t res = MSG_main();
84 #ifndef DISABLE_THE_MUTEX
85   xbt_mutex_destroy(mutex); mutex = NULL;
86 #endif
87   XBT_INFO("Simulation time %g", MSG_get_clock());
88
89   if (res == MSG_OK)
90     return 0;
91   else
92     return 1;
93 }