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