Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : example for detection of acceptance cycle with liveness properties...
[simgrid.git] / examples / msg / mc / example_liveness_with_cycle.c
1 /***************** Centralized Mutual Exclusion Algorithm *********************/
2 /* This example implements a centralized mutual exclusion algorithm.          */
3 /* There is no bug on it, it is just provided to test the state space         */
4 /* reduction of DPOR.                                                         */
5 /******************************************************************************/
6
7 #include "msg/msg.h"
8 #include "mc/mc.h"
9 #include "xbt/automaton.h"
10 #include "xbt/automatonparse_promela.h"
11 #include "example_liveness_with_cycle.h"
12 #include "y.tab.c"
13
14 #define AMOUNT_OF_CLIENTS 2
15 #define CS_PER_PROCESS 3
16
17 XBT_LOG_NEW_DEFAULT_CATEGORY(example_liveness_with_cycle, "my log messages");
18
19
20 int p=0; 
21 int q=0;
22
23 int predP(){
24   return p;
25 }
26
27 int predQ(){
28   return q;
29 }
30
31
32 int coordinator(int argc, char *argv[])
33 {
34   xbt_dynar_t requests = xbt_dynar_new(sizeof(char *), NULL);   // dynamic vector storing requests (which are char*)
35   int CS_used = 0;              // initially the CS is idle
36   int todo = AMOUNT_OF_CLIENTS * CS_PER_PROCESS;        // amount of releases we are expecting
37   while (todo > 0) {
38     m_task_t task = NULL;
39     MSG_task_receive(&task, "coordinator");
40     const char *kind = MSG_task_get_name(task); //is it a request or a release?
41     if (!strcmp(kind, "request")) {     // that's a request
42       char *req = MSG_task_get_data(task);
43       if (CS_used) {            // need to push the request in the vector
44         XBT_INFO("CS already used. Queue the request of client %d", atoi(req) +1);
45         xbt_dynar_push(requests, &req);
46       } else {                  // can serve it immediatly
47         if(strcmp(req, "1") == 0){
48           m_task_t answer = MSG_task_create("grant", 0, 1000, NULL);
49           MSG_task_send(answer, req);
50           CS_used = 1;
51           XBT_INFO("CS idle. Grant immediatly");
52         }
53       }
54     } else {                    // that's a release. Check if someone was waiting for the lock
55       if (xbt_dynar_length(requests) > 0) {
56         XBT_INFO("CS release. Grant to queued requests (queue size: %lu)", xbt_dynar_length(requests));
57         char *req ;
58         xbt_dynar_get_cpy(requests, (xbt_dynar_length(requests) - 1), &req);
59         if(strcmp(req, "1") == 0){
60           xbt_dynar_pop(requests, &req);
61           MSG_task_send(MSG_task_create("grant", 0, 1000, NULL), req);
62           todo--;
63         }else{
64            CS_used = 0;
65         }
66       } else {                  // nobody wants it
67         XBT_INFO("CS release. resource now idle");
68         CS_used = 0;
69         todo--;
70       }
71     }
72     MSG_task_destroy(task);
73   }
74   XBT_INFO("Received all releases, quit now"); 
75   return 0;
76 }
77
78 int client(int argc, char *argv[])
79 {
80   int my_pid = MSG_process_get_PID(MSG_process_self());
81
82   char *my_mailbox = bprintf("%s", argv[1]);
83   // request the CS 3 times, sleeping a bit in between
84   int i;
85  
86   for (i = 0; i < CS_PER_PROCESS; i++) {
87       
88     XBT_INFO("Ask the request");
89     MSG_task_send(MSG_task_create("request", 0, 1000, my_mailbox), "coordinator");
90     if(strcmp(my_mailbox, "2") == 0){
91       p = 1;
92       q = 0;
93     }
94     // wait the answer
95     m_task_t grant = NULL;
96     MSG_task_receive(&grant, my_mailbox);
97
98     if(strcmp(my_mailbox, "2") == 0){
99       q = 1;
100       p = 0;
101     }
102
103     MSG_task_destroy(grant);
104     XBT_INFO("%s got the answer. Sleep a bit and release it", argv[1]);
105     MSG_process_sleep(1);
106     MSG_task_send(MSG_task_create("release", 0, 1000, NULL), "coordinator");
107
108     MSG_process_sleep(my_pid);
109     
110     q=0;
111     p=0;
112     
113
114   }
115
116   XBT_INFO("Got all the CS I wanted (%s), quit now", my_mailbox);
117   return 0;
118 }
119
120 int main(int argc, char *argv[])
121 {
122   init();
123   yyparse();
124   automaton = get_automaton();
125   xbt_new_propositional_symbol(automaton,"p", &predP); 
126   xbt_new_propositional_symbol(automaton,"q", &predQ); 
127   
128   MSG_global_init(&argc, argv);
129   MSG_create_environment("../msg_platform.xml");
130   MSG_function_register("coordinator", coordinator);
131   MSG_function_register("client", client);
132   MSG_launch_application("deploy_mutex2.xml");
133   MSG_main_liveness_stateless(automaton);
134   //MSG_main();
135   
136   return 0;
137 }