1 /* Copyright (c) 2010-2020. The SimGrid Team.
2 * All rights reserved. */
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. */
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 /******************************************************************************/
13 #include "simgrid/msg.h"
15 #define AMOUNT_OF_CLIENTS 4
16 #define CS_PER_PROCESS 2
17 XBT_LOG_NEW_DEFAULT_CATEGORY(centralized, "my log messages");
19 static int coordinator(XBT_ATTRIB_UNUSED int argc, XBT_ATTRIB_UNUSED char* argv[])
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
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);
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));
43 xbt_dynar_shift(requests, &req);
44 MSG_task_send(MSG_task_create("grant", 0, 1000, NULL), req);
46 } else { // nobody wants it
47 XBT_INFO("CS release. resource now idle");
52 MSG_task_destroy(task);
54 XBT_INFO("Received all releases, quit now");
58 static int client(XBT_ATTRIB_UNUSED int argc, XBT_ATTRIB_UNUSED char* argv[])
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
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");
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");
74 MSG_task_send(MSG_task_create("release", 0, 1000, NULL), "coordinator");
75 MSG_process_sleep(my_pid);
77 XBT_INFO("Got all the CS I wanted, quit now");
81 int main(int argc, char *argv[])
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");