Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove useless wrapper around pthread_atfork().
[simgrid.git] / teshsuite / mc / mutex-handling / mutex-handling.c
1 /* Copyright (c) 2015-2019. 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/msg.h"
23 #include <xbt/synchro.h>
24
25 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
26
27 #define BOX_NAME "box"
28
29 #ifndef DISABLE_THE_MUTEX
30 static xbt_mutex_t mutex = NULL;
31 #endif
32
33 static int receiver(int argc, char *argv[])
34 {
35   msg_task_t task = NULL;
36
37   MSG_task_receive(&task, BOX_NAME);
38   MC_assert(strcmp(MSG_task_get_name(task), "X") == 0);
39   MSG_task_destroy(task);
40
41   MSG_task_receive(&task, BOX_NAME);
42   MC_assert(strcmp(MSG_task_get_name(task), "Y") == 0);
43   MSG_task_destroy(task);
44
45   return 0;
46 }
47
48 static int sender(int argc, char *argv[])
49 {
50   char* message_name = argv[1];
51 #ifndef DISABLE_THE_MUTEX
52   xbt_mutex_acquire(mutex);
53 #endif
54   MSG_task_send(MSG_task_create(message_name, 0.0, 0.0, NULL), BOX_NAME);
55 #ifndef DISABLE_THE_MUTEX
56   xbt_mutex_release(mutex);
57 #endif
58   return 0;
59 }
60
61 int main(int argc, char *argv[])
62 {
63   MSG_init(&argc, argv);
64   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
65        "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
66
67   MSG_create_environment(argv[1]);
68   MSG_function_register("receiver", receiver);
69   MSG_function_register("sender", sender);
70
71   MSG_launch_application(argv[2]);
72 #ifndef DISABLE_THE_MUTEX
73   mutex = xbt_mutex_init();
74 #endif
75   msg_error_t res = MSG_main();
76 #ifndef DISABLE_THE_MUTEX
77   xbt_mutex_destroy(mutex); mutex = NULL;
78 #endif
79   XBT_INFO("Simulation time %g", MSG_get_clock());
80
81   return res != MSG_OK;
82 }