Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
19f8d613a9723efb1f64c44aa9173c85399b6e9f
[simgrid.git] / examples / msg / mc / bugged1_liveness.c
1 /***************** Centralized Mutual Exclusion Algorithm *********************/
2 /* This example implements a centralized mutual exclusion algorithm.          */
3 /* Bug : 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
29   int CS_used = 0;   
30   msg_task_t task = NULL, answer = NULL; 
31   xbt_dynar_t requests = xbt_dynar_new(sizeof(char *), NULL);
32
33   while(1){  
34     MSG_task_receive(&task, "coordinator");
35     const char *kind = MSG_task_get_name(task); 
36     if (!strcmp(kind, "request")) {    
37       char *req = MSG_task_get_data(task);
38       if (CS_used) {           
39         XBT_INFO("CS already used. Queue the request.");
40         xbt_dynar_push(requests, &req);
41       } else {               
42         if(strcmp(req, "1") != 0){
43           XBT_INFO("CS idle. Grant immediatly");
44           answer = MSG_task_create("grant", 0, 1000, NULL);
45           MSG_task_send(answer, req);
46           CS_used = 1;
47           answer = NULL;
48         }
49       }
50     } else {      
51       if (!xbt_dynar_is_empty(requests)) {
52         XBT_INFO("CS release. Grant to queued requests (queue size: %lu)", xbt_dynar_length(requests));
53         char *req;
54         xbt_dynar_pop(requests, &req);
55         if(strcmp(req, "1") != 0){
56           MSG_task_send(MSG_task_create("grant", 0, 1000, NULL), req);
57         }else{
58           xbt_dynar_push(requests, &req);
59         }
60       }else{
61         XBT_INFO("CS release. resource now idle");
62         CS_used = 0;
63       }
64     }
65     MSG_task_destroy(task);
66     task = NULL;
67     kind = NULL;
68   }
69  
70   return 0;
71 }
72
73 int client(int argc, char *argv[])
74 {
75   int my_pid = MSG_process_get_PID(MSG_process_self());
76
77   char *my_mailbox = xbt_strdup(argv[1]);
78   msg_task_t grant = NULL, release = NULL;
79     
80   while(1){
81     XBT_INFO("Ask the request");
82     MSG_task_send(MSG_task_create("request", 0, 1000, my_mailbox), "coordinator");
83
84     if(strcmp(my_mailbox, "1") == 0){
85       r = 1;
86       cs = 0;
87       XBT_INFO("Propositions changed : r=1, cs=0");
88     }
89
90     MSG_task_receive(&grant, my_mailbox);
91     const char *kind = MSG_task_get_name(grant);
92
93     if((strcmp(my_mailbox, "1") == 0) && (strcmp("grant", kind) == 0)){
94       cs = 1;
95       r = 0;
96       XBT_INFO("Propositions changed : r=0, cs=1");
97     }
98
99     MSG_task_destroy(grant);
100     grant = NULL;
101     kind = NULL;
102
103     XBT_INFO("%s got the answer. Sleep a bit and release it", argv[1]);
104
105     MSG_process_sleep(1);
106
107     release = MSG_task_create("release", 0, 1000, NULL);
108     MSG_task_send(release, "coordinator");
109
110     release = NULL;
111
112     MSG_process_sleep(my_pid);
113     
114     if(strcmp(my_mailbox, "1") == 0){
115       cs=0;
116       r=0;
117       XBT_INFO("Propositions changed : r=0, cs=0");
118     }
119     
120   }
121
122   return 0;
123 }
124
125 int main(int argc, char *argv[])
126 {
127
128   MSG_init(&argc, argv);
129
130   MSG_config("model-check/property","promela_bugged1_liveness");
131   MC_automaton_new_propositional_symbol("r", &predR);
132   MC_automaton_new_propositional_symbol("cs", &predCS);
133   
134   MSG_create_environment("../msg_platform.xml");
135   MSG_function_register("coordinator", coordinator);
136   MSG_function_register("client", client);
137   MSG_launch_application("deploy_bugged1_liveness.xml");
138   MSG_main();
139
140   return 0;
141 }