Logo AND Algorithmique Numérique Distribuée

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