Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
5f50a5968e37f94304a8654c5a368a7c6ccd6da5
[simgrid.git] / examples / msg / mc / bugged1_while_liveness.c
1 /***************** Centralized Mutual Exclusion Algorithm *********************/
2 /* This example implements a centralized mutual exclusion algorithm.          */
3 /* CS requests of client 1 not satisfied                                      */
4 /* LTL property checked : G(r->F(cs)); (r=request of CS, cs=CS ok)            */
5 /******************************************************************************/
6
7 #include "msg/msg.h"
8 #include "mc/mc.h"
9 #include "xbt/automaton.h"
10 #include "bugged1_liveness.h"
11
12 XBT_LOG_NEW_DEFAULT_CATEGORY(bugged1_liveness, "my log messages");
13
14 int r=0; 
15 int cs=0;
16
17 int predR(){
18   return r;
19 }
20
21 int predCS(){
22   return cs;
23 }
24
25
26 int coordinator(int argc, char *argv[])
27 {
28   xbt_dynar_t requests = xbt_dynar_new(sizeof(char *), NULL);   // dynamic vector storing requests (which are char*)
29   int CS_used = 0;              // initially the CS is idle
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 of client %d", atoi(req) +1);
38         xbt_dynar_push(requests, &req);
39       } else {                  // can serve it immediatly
40   if(strcmp(req, "2") == 0){
41     m_task_t answer = MSG_task_create("grant", 0, 1000, NULL);
42     MSG_task_send(answer, req);
43     CS_used = 1;
44     XBT_INFO("CS idle. Grant immediatly");
45   }
46       }
47     } else {                    // that's a release. Check if someone was waiting for the lock
48       if (xbt_dynar_length(requests) > 0) {
49         XBT_INFO("CS release. Grant to queued requests (queue size: %lu)", xbt_dynar_length(requests));
50         char *req ;
51   xbt_dynar_get_cpy(requests, (xbt_dynar_length(requests) - 1), &req);
52   if(strcmp(req, "2") == 0){
53     xbt_dynar_pop(requests, &req);
54     MSG_task_send(MSG_task_create("grant", 0, 1000, NULL), req);
55   }else{
56     xbt_dynar_pop(requests, &req);
57     MSG_task_send(MSG_task_create("notgrant", 0, 1000, NULL), req);
58     CS_used = 0;
59   }
60       } else {                  // nobody wants it
61         XBT_INFO("CS release. resource now idle");
62         CS_used = 0;
63       }
64     }
65     MSG_task_destroy(task);
66   }
67  
68   return 0;
69 }
70
71 int client(int argc, char *argv[])
72 {
73   int my_pid = MSG_process_get_PID(MSG_process_self());
74
75   char *my_mailbox = bprintf("%s", argv[1]);
76
77   // request the CS, sleeping a bit in between
78   while(1) {
79       
80     XBT_INFO("Ask the request");
81     MSG_task_send(MSG_task_create("request", 0, 1000, my_mailbox), "coordinator");
82
83     if(strcmp(my_mailbox, "1") == 0){
84       r = 1;
85       cs = 0;
86       XBT_INFO("Propositions changed : r=1, cs=0");
87     }
88
89     // wait the answer
90     m_task_t grant = NULL;
91     MSG_task_receive(&grant, my_mailbox);
92     const char *kind = MSG_task_get_name(grant);
93
94     if((strcmp(my_mailbox, "1") == 0) && (strcmp("grant", kind) == 0)){
95       cs = 1;
96       r = 0;
97       XBT_INFO("Propositions changed : r=0, cs=1");
98     }
99
100
101     MSG_task_destroy(grant);
102     XBT_INFO("%s got the answer. Sleep a bit and release it", argv[1]);
103     MSG_process_sleep(1);
104     MSG_task_send(MSG_task_create("release", 0, 1000, NULL), "coordinator");
105
106     MSG_process_sleep(my_pid);
107     
108     if(strcmp(my_mailbox, "1") == 0){
109       cs=0;
110       r=0;
111       XBT_INFO("Propositions changed : r=0, cs=0");
112     }
113     
114   }
115
116   return 0;
117 }
118
119 int main(int argc, char *argv[])
120 {
121
122   MSG_init(&argc, argv);
123
124   MSG_config("model-check/property","promela1_bugged1_liveness");
125   MC_automaton_new_propositional_symbol("r", &predR);
126   MC_automaton_new_propositional_symbol("cs", &predCS);
127   
128   MSG_create_environment("../msg_platform.xml");
129   MSG_function_register("coordinator", coordinator);
130   MSG_function_register("client", client);
131   MSG_launch_application("deploy_bugged1_liveness.xml");
132   MSG_main();
133
134   return 0;
135 }