Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : new example for liveness properties
[simgrid.git] / examples / msg / mc / centralized_liveness.c
1 #include "msg/msg.h"
2 #include "mc/mc.h"
3 #include "xbt/automaton.h"
4 #include "xbt/automatonparse_promela.h"
5 #include "centralized_liveness.h"
6 #include "y.tab.c"
7
8 #define AMOUNT_OF_CLIENTS 2
9 #define CS_PER_PROCESS 1
10
11 XBT_LOG_NEW_DEFAULT_CATEGORY(centralized, "my log messages");
12  
13 int cs2 = 0;
14
15 int predCS2(){
16   return cs2;
17 }
18
19
20 int coordinator(int argc, char **argv);
21 int client(int argc, char **argv);
22
23 int coordinator(int argc, char *argv[])
24 {
25   xbt_dynar_t requests = xbt_dynar_new(sizeof(char *), NULL);   // dynamic vector storing requests (which are char*)
26   int CS_used = 0;              // initially the CS is idle
27   
28   while (1) {
29     m_task_t task = NULL;
30     MSG_task_receive(&task, "coordinator");
31     const char *kind = MSG_task_get_name(task); //is it a request or a release?
32     if (!strcmp(kind, "request")) {     // that's a request
33       char *req = MSG_task_get_data(task);
34       if (CS_used) {            // need to push the request in the vector
35         XBT_INFO("CS already used. Queue the request");
36         xbt_dynar_push(requests, &req);
37       } else {                  // can serve it immediatly
38         XBT_INFO("CS idle. Grant immediatly");
39         m_task_t answer = MSG_task_create("grant", 0, 1000, NULL);
40         MSG_task_send(answer, req);
41         CS_used = 1;
42       }
43     } else {                    // that's a release. Check if someone was waiting for the lock
44       if (!xbt_dynar_is_empty(requests)) {
45         XBT_INFO("CS release. Grant to queued requests (queue size: %lu)",
46               xbt_dynar_length(requests));
47         char *req;
48         xbt_dynar_pop(requests, &req);
49         MSG_task_send(MSG_task_create("grant", 0, 1000, NULL), req);
50       } else {                  // nobody wants it
51         XBT_INFO("CS release. resource now idle");
52         CS_used = 0;
53       }
54     }
55     MSG_task_destroy(task);
56   }
57   
58   return 0;
59 }
60
61 int client(int argc, char *argv[])
62 {
63   int my_pid = MSG_process_get_PID(MSG_process_self());
64   char *my_mailbox = bprintf("%s", argv[1]);
65  
66   while(1){
67
68     if(!strcmp(my_mailbox, "2"))
69       cs2 = 0;
70
71     XBT_INFO("Client (%s) ask the request", my_mailbox);
72     MSG_task_send(MSG_task_create("request", 0, 1000, my_mailbox),
73                   "coordinator");
74     // wait the answer
75     m_task_t grant = NULL;
76     MSG_task_receive(&grant, my_mailbox);
77     MSG_task_destroy(grant);
78     XBT_INFO("got the answer. Sleep a bit and release it");
79
80     if(!strcmp(my_mailbox, "2"))
81       cs2 = 1;
82
83     MSG_process_sleep(1);
84     MSG_task_send(MSG_task_create("release", 0, 1000, NULL),
85                   "coordinator");
86     MSG_process_sleep(my_pid);
87   }
88
89   return 0;
90 }
91
92 int main(int argc, char *argv[])
93 {
94   init();
95   yyparse();
96   automaton = get_automaton();
97   xbt_new_propositional_symbol(automaton,"cs2", &predCS2); 
98   
99   MSG_global_init(&argc, argv);
100   MSG_create_environment("../msg_platform.xml");
101   MSG_function_register("coordinator", coordinator);
102   MSG_function_register("client", client);
103   MSG_launch_application("deploy_centralized_liveness.xml");
104   MSG_main_liveness(automaton, argv[0]);
105
106   return 0;
107
108 }