Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : print statistics in all cases (property satisfied or not)
[simgrid.git] / examples / msg / mc / centralized_liveness_deadlock.c
1 /***************** Centralized Mutual Exclusion Algorithm *********************/
2 /* This example implements a centralized mutual exclusion algorithm.          */
3 /* LTL property checked : !(GFcs)                                                                                                                                                       */
4 /******************************************************************************/
5
6 #include "msg/msg.h"
7 #include "mc/mc.h"
8 #include "xbt/automaton.h"
9 #include "xbt/automatonparse_promela.h"
10 #include "centralized_liveness.h"
11 #include "y.tab.c"
12
13 XBT_LOG_NEW_DEFAULT_CATEGORY(centralized, "my log messages");
14  
15 int cs = 0;
16
17 int predCS(){
18   return cs;
19 }
20
21
22 int coordinator(int argc, char **argv);
23 int client(int argc, char **argv);
24
25 int coordinator(int argc, char *argv[])
26 {
27   xbt_dynar_t requests = xbt_dynar_new(sizeof(char *), NULL);   // dynamic vector storing requests (which are char*)
28   int CS_used = 0;              // initially the CS is idle
29   
30   while (1) {
31     m_task_t task = NULL;
32     MSG_task_receive(&task, "coordinator");
33     const char *kind = MSG_task_get_name(task); //is it a request or a release?
34     if (!strcmp(kind, "request")) {     // that's a request
35       char *req = MSG_task_get_data(task);
36       if (CS_used) {            // need to push the request in the vector
37         XBT_INFO("CS already used. Queue the request");
38         xbt_dynar_push(requests, &req);
39       } else {                  // can serve it immediatly
40         XBT_INFO("CS idle. Grant immediatly");
41         m_task_t answer = MSG_task_create("grant", 0, 1000, NULL);
42         MSG_task_send(answer, req);
43         CS_used = 1;
44       }
45     } else {                    // that's a release. Check if someone was waiting for the lock
46       if (!xbt_dynar_is_empty(requests)) {
47         XBT_INFO("CS release. Grant to queued requests (queue size: %lu)",
48               xbt_dynar_length(requests));
49         char *req;
50         xbt_dynar_pop(requests, &req);
51         MSG_task_send(MSG_task_create("grant", 0, 1000, NULL), req);
52       } else {                  // nobody wants it
53         XBT_INFO("CS release. resource now idle");
54         CS_used = 0;
55       }
56     }
57     MSG_task_destroy(task);
58   }
59   
60   return 0;
61 }
62
63 int client(int argc, char *argv[])
64 {
65   int my_pid = MSG_process_get_PID(MSG_process_self());
66   char *my_mailbox = bprintf("%s", argv[1]);
67  
68   while(1){
69
70     XBT_INFO("Client (%s) asks the request", my_mailbox);
71     MSG_task_send(MSG_task_create("request", 0, 1000, my_mailbox),
72                   "coordinator");
73     // wait the answer
74     m_task_t grant = NULL;
75     MSG_task_receive(&grant, my_mailbox);
76
77     MSG_task_destroy(grant);
78     XBT_INFO("Client (%s) got the answer. Sleep a bit and release it", my_mailbox);
79
80     if(!strcmp(my_mailbox, "1"))
81       cs = 1;
82
83     /*MSG_process_sleep(my_pid);
84     MSG_task_send(MSG_task_create("release", 0, 1000, NULL),
85                   "coordinator");
86     XBT_INFO("Client (%s) releases the CS", my_mailbox);
87
88     if(!strcmp(my_mailbox, "1"))
89     cs = 0;*/
90     
91     MSG_process_sleep(my_pid);
92
93   }
94
95   return 0;
96 }
97
98 int main(int argc, char *argv[])
99 {
100   init();
101   yyparse();
102   automaton = get_automaton();
103   xbt_new_propositional_symbol(automaton,"cs", &predCS); 
104   
105   MSG_global_init(&argc, argv);
106   MSG_create_environment("../msg_platform.xml");
107   MSG_function_register("coordinator", coordinator);
108   MSG_function_register("client", client);
109   MSG_launch_application("deploy_centralized_liveness.xml");
110   MSG_main_liveness(automaton, argv[0]);
111
112   return 0;
113
114 }