Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : comment erroneous ignore (not completely sure ...)
[simgrid.git] / examples / msg / mc / bugged1_liveness.c
1 /* Copyright (c) 2012-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 /***************** Centralized Mutual Exclusion Algorithm *********************/
8 /* This example implements a centralized mutual exclusion algorithm.          */
9 /* Bug : CS requests of client 1 not satisfied                                      */
10 /* LTL property checked : G(r->F(cs)); (r=request of CS, cs=CS ok)            */
11 /******************************************************************************/
12
13 #include "msg/msg.h"
14 #include "mc/mc.h"
15 #include "xbt/automaton.h"
16 #include "bugged1_liveness.h"
17
18 XBT_LOG_NEW_DEFAULT_CATEGORY(bugged1_liveness, "my log messages");
19
20 int r=0; 
21 int cs=0;
22
23 int predR(){
24   return r;
25 }
26
27 int predCS(){
28   return cs;
29 }
30
31
32 int coordinator(int argc, char *argv[])
33 {
34
35   int CS_used = 0;   
36   msg_task_t task = NULL, answer = NULL; 
37   xbt_dynar_t requests = xbt_dynar_new(sizeof(char *), NULL);
38   char *req;
39
40   while(1){  
41     MSG_task_receive(&task, "coordinator");
42     const char *kind = MSG_task_get_name(task); 
43     if (!strcmp(kind, "request")) {    
44       req = MSG_task_get_data(task);
45       if (CS_used) {           
46         XBT_INFO("CS already used. Queue the request.");
47         xbt_dynar_push(requests, &req);
48       } else {               
49         if(strcmp(req, "1") != 0){
50           XBT_INFO("CS idle. Grant immediatly");
51           answer = MSG_task_create("grant", 0, 1000, NULL);
52           MSG_task_send(answer, req);
53           CS_used = 1;
54           answer = NULL;
55         }
56       }
57     } else {      
58       if (!xbt_dynar_is_empty(requests)) {
59         XBT_INFO("CS release. Grant to queued requests (queue size: %lu)", xbt_dynar_length(requests));
60         xbt_dynar_shift(requests, &req);
61         if(strcmp(req, "1") != 0){
62           MSG_task_send(MSG_task_create("grant", 0, 1000, NULL), req);
63         }else{
64           xbt_dynar_push(requests, &req);
65           CS_used = 0;
66         }
67       }else{
68         XBT_INFO("CS release. resource now idle");
69         CS_used = 0;
70       }
71     }
72     MSG_task_destroy(task);
73     task = NULL;
74     kind = NULL;
75     req = NULL;
76   }
77  
78   return 0;
79 }
80
81 int client(int argc, char *argv[])
82 {
83   int my_pid = MSG_process_get_PID(MSG_process_self());
84
85   char *my_mailbox = xbt_strdup(argv[1]);
86   msg_task_t grant = NULL, release = NULL;
87     
88   while(1){
89     XBT_INFO("Ask the request");
90     MSG_task_send(MSG_task_create("request", 0, 1000, my_mailbox), "coordinator");
91
92     if(strcmp(my_mailbox, "1") == 0){
93       r = 1;
94       cs = 0;
95       XBT_INFO("Propositions changed : r=1, cs=0");
96     }
97
98     MSG_task_receive(&grant, my_mailbox);
99     const char *kind = MSG_task_get_name(grant);
100
101     if((strcmp(my_mailbox, "1") == 0) && (strcmp("grant", kind) == 0)){
102       cs = 1;
103       r = 0;
104       XBT_INFO("Propositions changed : r=0, cs=1");
105     }
106
107     MSG_task_destroy(grant);
108     grant = NULL;
109     kind = NULL;
110
111     XBT_INFO("%s got the answer. Sleep a bit and release it", argv[1]);
112
113     MSG_process_sleep(1);
114
115     release = MSG_task_create("release", 0, 1000, NULL);
116     MSG_task_send(release, "coordinator");
117
118     release = NULL;
119
120     MSG_process_sleep(my_pid);
121     
122     if(strcmp(my_mailbox, "1") == 0){
123       cs=0;
124       r=0;
125       XBT_INFO("Propositions changed : r=0, cs=0");
126     }
127     
128   }
129
130   return 0;
131 }
132
133 int main(int argc, char *argv[])
134 {
135
136   MSG_init(&argc, argv);
137
138   char **options = &argv[1];
139
140   MSG_config("model-check/property","promela_bugged1_liveness");
141   MC_automaton_new_propositional_symbol("r", &predR);
142   MC_automaton_new_propositional_symbol("cs", &predCS);
143
144   const char* platform_file = options[0];
145   const char* application_file = options[1];
146   
147   MSG_create_environment(platform_file);
148   MSG_function_register("coordinator", coordinator);
149   MSG_function_register("client", client);
150   MSG_launch_application(application_file);
151   MSG_main();
152
153   return 0;
154 }