Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge before commiting VM changes - Adrien
[simgrid.git] / examples / msg / mc / centralized_mutex.c
1 /* Copyright (c) 2010-2014. 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 /***************** Centralized Mutual Exclusion Algorithm *********************/
8 /* This example implements a centralized mutual exclusion algorithm.          */
9 /* There is no bug on it, it is just provided to test the state space         */
10 /* reduction of DPOR.                                                         */
11 /******************************************************************************/
12
13 #include "simgrid/msg.h"
14
15 #define AMOUNT_OF_CLIENTS 4
16 #define CS_PER_PROCESS 2
17 XBT_LOG_NEW_DEFAULT_CATEGORY(centralized, "my log messages");
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   int todo = AMOUNT_OF_CLIENTS * CS_PER_PROCESS;        // amount of releases we are expecting
27   while (todo > 0) {
28     msg_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         msg_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_shift(requests, &req);
48         MSG_task_send(MSG_task_create("grant", 0, 1000, NULL), req);
49         todo--;
50       } else {                  // nobody wants it
51         XBT_INFO("CS release. resource now idle");
52         CS_used = 0;
53         todo--;
54       }
55     }
56     MSG_task_destroy(task);
57   }
58   XBT_INFO("Received all releases, quit now");
59   return 0;
60 }
61
62 int client(int argc, char *argv[])
63 {
64   int my_pid = MSG_process_get_PID(MSG_process_self());
65   // use my pid as name of mailbox to contact me
66   char *my_mailbox = bprintf("%d", my_pid);
67   // request the CS 3 times, sleeping a bit in between
68   int i;
69   for (i = 0; i < CS_PER_PROCESS; i++) {
70     XBT_INFO("Ask the request");
71     MSG_task_send(MSG_task_create("request", 0, 1000, my_mailbox),
72                   "coordinator");
73     // wait the answer
74     msg_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     MSG_process_sleep(1);
79     MSG_task_send(MSG_task_create("release", 0, 1000, NULL),
80                   "coordinator");
81     MSG_process_sleep(my_pid);
82   }
83   XBT_INFO("Got all the CS I wanted, quit now");
84   return 0;
85 }
86
87 int main(int argc, char *argv[])
88 {
89   MSG_init(&argc, argv);
90   MSG_create_environment("../msg_platform.xml");
91   MSG_function_register("coordinator", coordinator);
92   MSG_function_register("client", client);
93   MSG_launch_application("deploy_mutex.xml");
94   MSG_main();
95   return 0;
96 }