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