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