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 /******************************************************************************/
9 #define AMOUNT_OF_CLIENTS 4
10 #define CS_PER_PROCESS 2
11 XBT_LOG_NEW_DEFAULT_CATEGORY(centralized, "my log messages");
13 int coordinator(int argc, char **argv);
14 int client(int argc, char **argv);
16 int coordinator(int argc, char *argv[])
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
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 INFO0("CS already used. Queue the request");
29 xbt_dynar_push(requests, &req);
30 } else { // can serve it immediatly
31 INFO0("CS idle. Grant immediatly");
32 m_task_t answer = MSG_task_create("grant", 0, 1000, NULL);
33 MSG_task_send(answer, req);
36 } else { // that's a release. Check if someone was waiting for the lock
37 if (xbt_dynar_length(requests) > 0) {
38 INFO1("CS release. Grant to queued requests (queue size: %lu)",
39 xbt_dynar_length(requests));
41 xbt_dynar_pop(requests, &req);
42 MSG_task_send(MSG_task_create("grant", 0, 1000, NULL), req);
44 } else { // nobody wants it
45 INFO0("CS release. resource now idle");
50 MSG_task_destroy(task);
52 INFO0("Received all releases, quit now");
56 int client(int argc, char *argv[])
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
63 for (i = 0; i < CS_PER_PROCESS; i++) {
64 INFO0("Ask the request");
65 MSG_task_send(MSG_task_create("request", 0, 1000, my_mailbox),
68 m_task_t grant = NULL;
69 MSG_task_receive(&grant, my_mailbox);
70 MSG_task_destroy(grant);
71 INFO0("got the answer. Sleep a bit and release it");
73 MSG_task_send(MSG_task_create("release", 0, 1000, NULL),
75 MSG_process_sleep(my_pid);
77 INFO0("Got all the CS I wanted, quit now");
81 int main(int argc, char *argv[])
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");