4 * Copyright 2010. The SimGrid Team. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package.
10 package mutualExclusion.centralized;
11 import java.util.LinkedList;
13 import org.simgrid.msg.Host;
14 import org.simgrid.msg.Msg;
15 import org.simgrid.msg.MsgException;
16 import org.simgrid.msg.Task;
17 import org.simgrid.msg.Process;
20 public class Coordinator extends Process {
21 public Coordinator(Host host, String name, String[]args) {
22 super(host,name,args);
24 LinkedList<RequestTask> waitingQueue=new LinkedList<RequestTask>();
27 public void main(String[] args) throws MsgException {
28 CsToServe = Integer.parseInt(args[0]);
30 while (CsToServe >0) {
31 task = Task.receive("coordinator");
32 if (task instanceof RequestTask) {
33 RequestTask t = (RequestTask) task;
34 if (waitingQueue.isEmpty()) {
35 Msg.info("Got a request from "+t.from+". Queue empty: grant it");
36 GrantTask tosend = new GrantTask();
39 waitingQueue.addFirst(t);
41 } else if (task instanceof ReleaseTask) {
42 if (!waitingQueue.isEmpty()) {
43 RequestTask req = waitingQueue.removeLast();
44 GrantTask tosend = new GrantTask();
45 tosend.send(req.from);
48 if (waitingQueue.isEmpty() && CsToServe==0) {
49 Msg.info("we should shutdown the simulation now");