Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[trace] cosmetics on user variables tracing, simpler interface
[simgrid.git] / examples / msg / mc / centralized_mutex.c
1 /***************** Centralized Mutual Exclusion Algorithm *********************/
2 /* This example implements a centralized mutual exclusion algorithm.          */
3 /* There is no bug on it, it is just provided to test the state space         */
4 /* reduction of DPOR.                                                         */
5 /******************************************************************************/
6
7 #include "msg/msg.h"
8
9 #define AMOUNT_OF_CLIENTS 4
10 #define CS_PER_PROCESS 2
11 XBT_LOG_NEW_DEFAULT_CATEGORY(centralized, "my log messages");
12
13 int coordinator(int argc, char **argv);
14 int client(int argc, char **argv);
15
16 int coordinator(int argc, char *argv[])
17 {
18   xbt_dynar_t requests = xbt_dynar_new(sizeof(char *), NULL);   // dynamic vector storing requests (which are char*)
19   int CS_used = 0;              // initially the CS is idle
20   int todo = AMOUNT_OF_CLIENTS * CS_PER_PROCESS;        // amount of releases we are expecting
21   while (todo > 0) {
22     m_task_t task = NULL;
23     MSG_task_receive(&task, "coordinator");
24     const char *kind = MSG_task_get_name(task); //is it a request or a release?
25     if (!strcmp(kind, "request")) {     // that's a request
26       char *req = MSG_task_get_data(task);
27       if (CS_used) {            // need to push the request in the vector
28         XBT_INFO("CS already used. Queue the request");
29         xbt_dynar_push(requests, &req);
30       } else {                  // can serve it immediatly
31         XBT_INFO("CS idle. Grant immediatly");
32         m_task_t answer = MSG_task_create("grant", 0, 1000, NULL);
33         MSG_task_send(answer, req);
34         CS_used = 1;
35       }
36     } else {                    // that's a release. Check if someone was waiting for the lock
37       if (xbt_dynar_length(requests) > 0) {
38         XBT_INFO("CS release. Grant to queued requests (queue size: %lu)",
39               xbt_dynar_length(requests));
40         char *req;
41         xbt_dynar_pop(requests, &req);
42         MSG_task_send(MSG_task_create("grant", 0, 1000, NULL), req);
43         todo--;
44       } else {                  // nobody wants it
45         XBT_INFO("CS release. resource now idle");
46         CS_used = 0;
47         todo--;
48       }
49     }
50     MSG_task_destroy(task);
51   }
52   XBT_INFO("Received all releases, quit now");
53   return 0;
54 }
55
56 int client(int argc, char *argv[])
57 {
58   int my_pid = MSG_process_get_PID(MSG_process_self());
59   // use my pid as name of mailbox to contact me
60   char *my_mailbox = bprintf("%d", my_pid);
61   // request the CS 3 times, sleeping a bit in between
62   int i;
63   for (i = 0; i < CS_PER_PROCESS; i++) {
64     XBT_INFO("Ask the request");
65     MSG_task_send(MSG_task_create("request", 0, 1000, my_mailbox),
66                   "coordinator");
67     // wait the answer
68     m_task_t grant = NULL;
69     MSG_task_receive(&grant, my_mailbox);
70     MSG_task_destroy(grant);
71     XBT_INFO("got the answer. Sleep a bit and release it");
72     MSG_process_sleep(1);
73     MSG_task_send(MSG_task_create("release", 0, 1000, NULL),
74                   "coordinator");
75     MSG_process_sleep(my_pid);
76   }
77   XBT_INFO("Got all the CS I wanted, quit now");
78   return 0;
79 }
80
81 int main(int argc, char *argv[])
82 {
83   MSG_global_init(&argc, argv);
84   MSG_create_environment("../msg_platform.xml");
85   MSG_function_register("coordinator", coordinator);
86   MSG_function_register("client", client);
87   MSG_launch_application("deploy_mutex.xml");
88   MSG_main();
89   return 0;
90 }