1 /* Copyright (c) 2012-2014. 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 package mutualExclusion;
8 import java.util.LinkedList;
10 import org.simgrid.msg.Host;
11 import org.simgrid.msg.Msg;
12 import org.simgrid.msg.MsgException;
13 import org.simgrid.msg.Task;
14 import org.simgrid.msg.Process;
17 public class Coordinator extends Process {
18 public Coordinator(Host host, String name, String[]args) {
19 super(host,name,args);
21 LinkedList<RequestTask> waitingQueue=new LinkedList<RequestTask>();
24 public void main(String[] args) throws MsgException {
25 CsToServe = Integer.parseInt(args[0]);
27 while (CsToServe >0) {
28 task = Task.receive("coordinator");
29 if (task instanceof RequestTask) {
30 RequestTask t = (RequestTask) task;
31 if (waitingQueue.isEmpty()) {
32 Msg.info("Got a request from "+t.from+". Queue empty: grant it");
33 GrantTask tosend = new GrantTask();
36 waitingQueue.addFirst(t);
38 } else if (task instanceof ReleaseTask) {
39 if (!waitingQueue.isEmpty()) {
40 RequestTask req = waitingQueue.removeLast();
41 GrantTask tosend = new GrantTask();
42 tosend.send(req.from);
45 if (waitingQueue.isEmpty() && CsToServe==0) {
46 Msg.info("we should shutdown the simulation now");