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 #include "xbt/automaton.h"
10 #include "xbt/automatonparse_promela.h"
11 #include "example_liveness_with_cycle.h"
14 #define AMOUNT_OF_CLIENTS 2
15 #define CS_PER_PROCESS 1
17 XBT_LOG_NEW_DEFAULT_CATEGORY(example_liveness_with_cycle, "my log messages");
30 int coordinator(int argc, char *argv[])
32 xbt_dynar_t requests = xbt_dynar_new(sizeof(char *), NULL); // dynamic vector storing requests (which are char*)
33 int CS_used = 0; // initially the CS is idle
34 int todo = AMOUNT_OF_CLIENTS * CS_PER_PROCESS; // amount of releases we are expecting
37 MSG_task_receive(&task, "coordinator");
38 const char *kind = MSG_task_get_name(task); //is it a request or a release?
39 if (!strcmp(kind, "request")) { // that's a request
40 char *req = MSG_task_get_data(task);
41 //XBT_INFO("Req %s", req);
42 if (CS_used) { // need to push the request in the vector
43 XBT_INFO("CS already used. Queue the request");
44 xbt_dynar_push(requests, &req);
45 } else { // can serve it immediatly
46 XBT_INFO("CS idle. Grant immediatly");
47 if(strcmp(req, "1") == 0){
48 m_task_t answer = MSG_task_create("grant", 0, 1000, NULL);
49 MSG_task_send(answer, req);
52 m_task_t answer = MSG_task_create("notgrant", 0, 1000, NULL);
53 MSG_task_send(answer, req);
57 } else { // that's a release. Check if someone was waiting for the lock
58 if (xbt_dynar_length(requests) > 0) {
59 XBT_INFO("CS release. Grant to queued requests (queue size: %lu)",
60 xbt_dynar_length(requests));
62 if(strcmp(req, "1") == 0){
63 xbt_dynar_pop(requests, &req);
64 MSG_task_send(MSG_task_create("grant", 0, 1000, NULL), req);
67 xbt_dynar_pop(requests, &req);
68 MSG_task_send(MSG_task_create("notgrant", 0, 1000, NULL), req);
71 } else { // nobody wants it
72 XBT_INFO("CS release. resource now idle");
77 MSG_task_destroy(task);
79 XBT_INFO("Received all releases, quit now");
83 int client(int argc, char *argv[])
85 int my_pid = MSG_process_get_PID(MSG_process_self());
86 // use my pid as name of mailbox to contact me
87 char *my_mailbox = bprintf("%s", argv[1]);
88 // request the CS 2 times, sleeping a bit in between
90 for (i = 0; i < CS_PER_PROCESS; i++) {
93 XBT_INFO("Ask the request");
94 MSG_task_send(MSG_task_create("request", 0, 1000, my_mailbox),
96 if(strcmp(my_mailbox, "1") == 0){
101 m_task_t grant = NULL;
102 MSG_task_receive(&grant, my_mailbox);
103 const char *kind = MSG_task_get_name(grant);
104 if(!strcmp(kind, "grant")){
105 if(strcmp(my_mailbox, "2") == 0){
110 MSG_task_destroy(grant);
111 XBT_INFO("got the answer. Sleep a bit and release it");
112 MSG_process_sleep(1);
113 MSG_task_send(MSG_task_create("release", 0, 1000,NULL ),
116 MSG_task_destroy(grant);
117 XBT_INFO("Negative answer");
118 MSG_process_sleep(1);
119 MSG_task_send(MSG_task_create("notrelease", 0, 1000,NULL ),
123 MSG_process_sleep(my_pid);
127 //XBT_INFO("Got all the CS I wanted (%s), quit now", my_mailbox);
131 int main(int argc, char *argv[])
135 automaton = get_automaton();
136 xbt_propositional_symbol_t ps = xbt_new_propositional_symbol(automaton,"p", &predP);
137 ps = xbt_new_propositional_symbol(automaton,"q", &predQ);
139 MSG_global_init(&argc, argv);
140 MSG_create_environment("../msg_platform.xml");
141 MSG_function_register("coordinator", coordinator);
142 MSG_function_register("client", client);
143 MSG_launch_application("deploy_mutex2.xml");
144 MSG_main_liveness_stateless(automaton);