Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'clean_events' of github.com:Takishipp/simgrid into clean_events
[simgrid.git] / examples / msg / mc / bugged2_liveness.c
1 /* Copyright (c) 2012-2015. 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 /***************************** Bugged2 ****************************************/
8 /* This example implements a centralized mutual exclusion algorithm.          */
9 /* One client stay always in critical section                                 */
10 /* LTL property checked : !(GFcs)                                             */
11 /******************************************************************************/
12
13 #include "simgrid/msg.h"
14 #include "mc/mc.h"
15 #include "bugged2_liveness.h"
16
17 XBT_LOG_NEW_DEFAULT_CATEGORY(bugged3, "my log messages");
18
19 int cs = 0;
20
21 static int coordinator(int argc, char *argv[])
22 {
23   int CS_used = 0;              // initially the CS is idle
24
25   while (1) {
26     msg_task_t task = NULL;
27     MSG_task_receive(&task, "coordinator");
28     const char *kind = MSG_task_get_name(task); //is it a request or a release?
29     if (!strcmp(kind, "request")) {     // that's a request
30       char *req = MSG_task_get_data(task);
31       if (CS_used) {
32         XBT_INFO("CS already used.");
33         msg_task_t answer = MSG_task_create("not grant", 0, 1000, NULL);
34         MSG_task_send(answer, req);
35       } else {                  // can serve it immediately
36         XBT_INFO("CS idle. Grant immediately");
37         msg_task_t answer = MSG_task_create("grant", 0, 1000, NULL);
38         MSG_task_send(answer, req);
39         CS_used = 1;
40       }
41     } else {                    // that's a release. Check if someone was waiting for the lock
42       XBT_INFO("CS release. resource now idle");
43       CS_used = 0;
44     }
45     MSG_task_destroy(task);
46     kind = NULL;
47   }
48
49   return 0;
50 }
51
52 static int client(int argc, char *argv[])
53 {
54   int my_pid = MSG_process_get_PID(MSG_process_self());
55   char *my_mailbox = xbt_strdup(argv[1]);
56   const char* kind;
57
58   while(1){
59     XBT_INFO("Client (%s) asks the request", my_mailbox);
60     MSG_task_send(MSG_task_create("request", 0, 1000, my_mailbox), "coordinator");
61
62     msg_task_t answer = NULL;
63     MSG_task_receive(&answer, my_mailbox);
64
65     kind = MSG_task_get_name(answer);
66
67     if (!strcmp(kind, "grant")) {
68       XBT_INFO("Client (%s) got the answer (grant). Sleep a bit and release it", my_mailbox);
69       if(!strcmp(my_mailbox, "1"))
70         cs = 1;
71     }else{
72       XBT_INFO("Client (%s) got the answer (not grant). Try again", my_mailbox);
73     }
74
75     MSG_task_destroy(answer);
76     kind = NULL;
77
78     MSG_process_sleep(my_pid);
79   }
80   return 0;
81 }
82
83 int main(int argc, char *argv[])
84 {
85   MSG_init(&argc, argv);
86
87   MC_automaton_new_propositional_symbol_pointer("cs", &cs);
88
89   MSG_create_environment("../msg_platform.xml");
90   MSG_function_register("coordinator", coordinator);
91   MSG_function_register("client", client);
92   MSG_launch_application("deploy_bugged2_liveness.xml");
93   MSG_main();
94
95   return 0;
96
97 }