1 /***************** Centralized Mutual Exclusion Algorithm *********************/
2 /* This example implements a centralized mutual exclusion algorithm. */
3 /* LTL property checked : !(GFcs) */
4 /******************************************************************************/
8 #include "xbt/automaton.h"
9 #include "centralized_liveness.h"
11 XBT_LOG_NEW_DEFAULT_CATEGORY(centralized, "my log messages");
20 int coordinator(int argc, char **argv);
21 int client(int argc, char **argv);
23 int coordinator(int argc, char *argv[])
25 xbt_dynar_t requests = xbt_dynar_new(sizeof(char *), NULL); // dynamic vector storing requests (which are char*)
26 int CS_used = 0; // initially the CS is idle
29 msg_task_t task = NULL;
30 MSG_task_receive(&task, "coordinator");
31 const char *kind = MSG_task_get_name(task); //is it a request or a release?
32 if (!strcmp(kind, "request")) { // that's a request
33 char *req = MSG_task_get_data(task);
34 if (CS_used) { // need to push the request in the vector
35 XBT_INFO("CS already used. Queue the request");
36 xbt_dynar_push(requests, &req);
37 } else { // can serve it immediatly
38 XBT_INFO("CS idle. Grant immediatly");
39 msg_task_t answer = MSG_task_create("grant", 0, 1000, NULL);
40 MSG_task_send(answer, req);
43 } else { // that's a release. Check if someone was waiting for the lock
44 if (!xbt_dynar_is_empty(requests)) {
45 XBT_INFO("CS release. Grant to queued requests (queue size: %lu)",
46 xbt_dynar_length(requests));
48 xbt_dynar_pop(requests, &req);
49 MSG_task_send(MSG_task_create("grant", 0, 1000, NULL), req);
50 } else { // nobody wants it
51 XBT_INFO("CS release. resource now idle");
55 MSG_task_destroy(task);
61 int client(int argc, char *argv[])
63 int my_pid = MSG_process_get_PID(MSG_process_self());
64 char *my_mailbox = bprintf("%s", argv[1]);
68 XBT_INFO("Client (%s) asks the request", my_mailbox);
69 MSG_task_send(MSG_task_create("request", 0, 1000, my_mailbox),
72 msg_task_t grant = NULL;
73 MSG_task_receive(&grant, my_mailbox);
75 MSG_task_destroy(grant);
76 XBT_INFO("Client (%s) got the answer. Sleep a bit and release it", my_mailbox);
78 if(!strcmp(my_mailbox, "1"))
81 /*MSG_process_sleep(my_pid);
82 MSG_task_send(MSG_task_create("release", 0, 1000, NULL),
84 XBT_INFO("Client (%s) releases the CS", my_mailbox);
86 if(!strcmp(my_mailbox, "1"))
89 MSG_process_sleep(my_pid);
96 int main(int argc, char *argv[])
99 MSG_init(&argc, argv);
101 MSG_config("model-check/property","promela_centralized_liveness");
102 MC_automaton_new_propositional_symbol("cs", &predCS);
104 MSG_create_environment("../msg_platform.xml");
105 MSG_function_register("coordinator", coordinator);
106 MSG_function_register("client", client);
107 MSG_launch_application("deploy_centralized_liveness.xml");