]> AND Public Git Repository - simgrid.git/blob - examples/msg/mc/centralized_liveness.c
Logo AND Algorithmique Numérique Distribuée

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