Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright headers.
[simgrid.git] / examples / msg / mc / centralized_mutex.c
1 /* Copyright (c) 2010-2018. 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 static int coordinator(int argc, char *argv[])
20 {
21   xbt_dynar_t requests = xbt_dynar_new(sizeof(char *), NULL);   // dynamic vector storing requests (which are char*)
22   int CS_used = 0;              // initially the CS is idle
23   int todo = AMOUNT_OF_CLIENTS * CS_PER_PROCESS;        // amount of releases we are expecting
24   while (todo > 0) {
25     msg_task_t task = NULL;
26     MSG_task_receive(&task, "coordinator");
27     const char *kind = MSG_task_get_name(task); //is it a request or a release?
28     if (!strcmp(kind, "request")) {     // that's a request
29       char *req = MSG_task_get_data(task);
30       if (CS_used) {            // need to push the request in the vector
31         XBT_INFO("CS already used. Queue the request");
32         xbt_dynar_push(requests, &req);
33       } else {                  // can serve it immediately
34         XBT_INFO("CS idle. Grant immediately");
35         msg_task_t answer = MSG_task_create("grant", 0, 1000, NULL);
36         MSG_task_send(answer, req);
37         CS_used = 1;
38       }
39     } else {                    // that's a release. Check if someone was waiting for the lock
40       if (!xbt_dynar_is_empty(requests)) {
41         XBT_INFO("CS release. Grant to queued requests (queue size: %lu)", xbt_dynar_length(requests));
42         char *req;
43         xbt_dynar_shift(requests, &req);
44         MSG_task_send(MSG_task_create("grant", 0, 1000, NULL), req);
45         todo--;
46       } else {                  // nobody wants it
47         XBT_INFO("CS release. resource now idle");
48         CS_used = 0;
49         todo--;
50       }
51     }
52     MSG_task_destroy(task);
53   }
54   XBT_INFO("Received all releases, quit now");
55   return 0;
56 }
57
58 static int client(int argc, char *argv[])
59 {
60   int my_pid = MSG_process_get_PID(MSG_process_self());
61   // use my pid as name of mailbox to contact me
62   char *my_mailbox = bprintf("%d", my_pid);
63   // request the CS 3 times, sleeping a bit in between
64   int i;
65   for (i = 0; i < CS_PER_PROCESS; i++) {
66     XBT_INFO("Ask the request");
67     MSG_task_send(MSG_task_create("request", 0, 1000, my_mailbox), "coordinator");
68     // wait the answer
69     msg_task_t grant = NULL;
70     MSG_task_receive(&grant, my_mailbox);
71     MSG_task_destroy(grant);
72     XBT_INFO("got the answer. Sleep a bit and release it");
73     MSG_process_sleep(1);
74     MSG_task_send(MSG_task_create("release", 0, 1000, NULL), "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_init(&argc, argv);
84   MSG_create_environment("../../platforms/small_platform.xml");
85   MSG_function_register("coordinator", coordinator);
86   MSG_function_register("client", client);
87   MSG_launch_application("deploy_centralized_mutex.xml");
88   MSG_main();
89   return 0;
90 }