Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : remove binary name as argument of some functions, available with...
[simgrid.git] / examples / msg / mc / centralized_liveness.c
1 /***************** Centralized Mutual Exclusion Algorithm *********************/
2 /* This example implements a centralized mutual exclusion algorithm.          */
3 /* LTL property checked : !(GFcs)                                                                                                                                                       */
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 "centralized_liveness.h"
11 #include "y.tab.c"
12
13 XBT_LOG_NEW_DEFAULT_CATEGORY(centralized, "my log messages");
14  
15 int cs = 0;
16
17 int predCS(){
18   return cs;
19 }
20
21
22 int coordinator(int argc, char **argv);
23 int client(int argc, char **argv);
24
25 int coordinator(int argc, char *argv[])
26 {
27   int CS_used = 0;              // initially the CS is idle
28   
29   while (1) {
30     m_task_t task = NULL;
31     MSG_task_receive(&task, "coordinator");
32     const char *kind = MSG_task_get_name(task); //is it a request or a release?
33     if (!strcmp(kind, "request")) {     // that's a request
34       char *req = MSG_task_get_data(task);
35       if (CS_used) { 
36         XBT_INFO("CS already used.");
37         m_task_t answer = MSG_task_create("not grant", 0, 1000, NULL);
38         MSG_task_send(answer, req);
39       } else {                  // can serve it immediatly
40         XBT_INFO("CS idle. Grant immediatly");
41         m_task_t answer = MSG_task_create("grant", 0, 1000, NULL);
42         MSG_task_send(answer, req);
43         CS_used = 1;
44       }
45     } else {                    // that's a release. Check if someone was waiting for the lock
46       XBT_INFO("CS release. resource now idle");
47       CS_used = 0;
48     }
49     MSG_task_destroy(task);
50   }
51   
52   return 0;
53 }
54
55 int client(int argc, char *argv[])
56 {
57   int my_pid = MSG_process_get_PID(MSG_process_self());
58   char *my_mailbox = bprintf("%s", argv[1]);
59   const char* kind;
60  
61   while(1){
62
63     XBT_INFO("Client (%s) asks the request", my_mailbox);
64     MSG_task_send(MSG_task_create("request", 0, 1000, my_mailbox),
65                   "coordinator");
66     // wait the answer
67     m_task_t answer = NULL;
68     MSG_task_receive(&answer, my_mailbox);
69
70     kind = MSG_task_get_name(answer);
71     
72     if (!strcmp(kind, "grant")) {
73
74       XBT_INFO("Client (%s) got the answer (grant). Sleep a bit and release it", my_mailbox);
75
76       if(!strcmp(my_mailbox, "1"))
77         cs = 1;
78
79       /*MSG_process_sleep(my_pid);
80       MSG_task_send(MSG_task_create("release", 0, 1000, NULL),
81                     "coordinator");
82                     XBT_INFO("Client (%s) releases the CS", my_mailbox);
83       
84       if(!strcmp(my_mailbox, "1"))
85         cs = 0;*/
86       
87     }else{
88       
89       XBT_INFO("Client (%s) got the answer (not grant). Try again", my_mailbox);
90       
91     }
92
93     MSG_task_destroy(answer);
94     
95     MSG_process_sleep(my_pid);
96   }
97
98   return 0;
99 }
100
101 int main(int argc, char *argv[])
102 {
103   init();
104   yyparse();
105   automaton = get_automaton();
106   xbt_new_propositional_symbol(automaton,"cs", &predCS); 
107   
108   MSG_global_init(&argc, argv);
109   MSG_create_environment("../msg_platform.xml");
110   MSG_function_register("coordinator", coordinator);
111   MSG_function_register("client", client);
112   MSG_launch_application("deploy_centralized_liveness.xml");
113   MSG_main_liveness(automaton);
114
115   return 0;
116
117 }