2018-12-09 01:53:47 +01:00
|
|
|
/*
|
2018-12-19 09:05:25 +01:00
|
|
|
* Copyright (c) 2018, 7u83
|
2018-12-09 01:53:47 +01:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
package opensesim.world.scheduler;
|
|
|
|
|
2018-12-19 09:05:25 +01:00
|
|
|
import java.util.ArrayList;
|
2018-12-19 19:52:23 +01:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.SortedMap;
|
|
|
|
import java.util.TreeMap;
|
2018-12-19 09:05:25 +01:00
|
|
|
import opensesim.world.scheduler.EventListener;
|
2018-12-09 01:53:47 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2018-12-19 09:05:25 +01:00
|
|
|
* @author tohe
|
2018-12-09 01:53:47 +01:00
|
|
|
*/
|
2018-12-19 09:05:25 +01:00
|
|
|
public class Scheduler {
|
2018-12-19 19:52:23 +01:00
|
|
|
|
|
|
|
private final SortedMap<Long, LinkedList<Event>> event_queue = new TreeMap<>();
|
|
|
|
|
|
|
|
private class Worker extends Thread {
|
|
|
|
|
|
|
|
boolean terminate = false;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
while (!terminate) {
|
|
|
|
|
|
|
|
long delay = getDelay();
|
2018-12-20 17:21:06 +01:00
|
|
|
//System.out.printf("Worker %d has delay %d\n",Thread.currentThread().getId(), delay);
|
|
|
|
if (delay > 0 || delay==-1) {
|
|
|
|
//System.out.printf("Worker %d sleeps for %d\n",Thread.currentThread().getId(), delay);
|
2018-12-19 19:52:23 +01:00
|
|
|
synchronized (clock) {
|
|
|
|
try {
|
|
|
|
if (delay != -1 && !clock.isPause()) {
|
|
|
|
clock.wait(delay);
|
|
|
|
} else {
|
|
|
|
clock.wait();
|
|
|
|
}
|
|
|
|
} catch (InterruptedException e) {
|
2018-12-20 17:21:06 +01:00
|
|
|
//System.out.printf("Interrupted\n");
|
2018-12-19 19:52:23 +01:00
|
|
|
}
|
|
|
|
}
|
2018-12-20 10:51:16 +01:00
|
|
|
continue;
|
2018-12-19 19:52:23 +01:00
|
|
|
}
|
|
|
|
Event e = getNextEvent();
|
2018-12-20 17:21:06 +01:00
|
|
|
|
2018-12-20 09:42:07 +01:00
|
|
|
if (e == null) {
|
2018-12-19 19:52:23 +01:00
|
|
|
continue;
|
|
|
|
}
|
2018-12-20 17:21:06 +01:00
|
|
|
//System.out.printf("Worker %d got event %d\n",Thread.currentThread().getId(), e.t);
|
2018-12-19 19:52:23 +01:00
|
|
|
e.listener.receive(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ArrayList<Worker> workers;
|
|
|
|
final private Clock clock = new Clock();
|
2018-12-20 10:51:16 +01:00
|
|
|
// int next = 0;
|
2018-12-19 19:52:23 +01:00
|
|
|
|
|
|
|
public Scheduler(int nthreads) {
|
|
|
|
workers = new ArrayList<>();
|
|
|
|
for (int i = 0; i < nthreads; i++) {
|
|
|
|
workers.add(i, new Worker());
|
2018-12-09 01:53:47 +01:00
|
|
|
}
|
|
|
|
}
|
2018-12-19 19:52:23 +01:00
|
|
|
|
|
|
|
public Scheduler() {
|
2018-12-20 10:51:16 +01:00
|
|
|
this(10);
|
2018-12-09 01:53:47 +01:00
|
|
|
}
|
2018-12-19 19:52:23 +01:00
|
|
|
|
|
|
|
public void start() {
|
|
|
|
for (Worker w : workers) {
|
|
|
|
w.start();
|
2018-12-09 01:53:47 +01:00
|
|
|
}
|
|
|
|
}
|
2018-12-19 19:52:23 +01:00
|
|
|
|
2018-12-20 09:42:07 +01:00
|
|
|
public Event startTimerTask(EventListener listener, long time) {
|
|
|
|
Event e = new Event(listener);
|
|
|
|
long t = time + clock.currentTimeMillis();
|
2018-12-20 10:51:16 +01:00
|
|
|
e.t=t;
|
2018-12-20 09:42:07 +01:00
|
|
|
synchronized (event_queue) {
|
|
|
|
LinkedList<Event> s = event_queue.get(t);
|
|
|
|
if (s == null) {
|
|
|
|
s = new LinkedList<>();
|
|
|
|
event_queue.put(t, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
s.add(e);
|
|
|
|
}
|
|
|
|
synchronized (clock) {
|
|
|
|
clock.notifyAll();
|
|
|
|
}
|
|
|
|
return e;
|
|
|
|
|
2018-12-19 19:52:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected long getDelay() {
|
|
|
|
synchronized (event_queue) {
|
2018-12-20 17:21:06 +01:00
|
|
|
|
|
|
|
|
2018-12-19 19:52:23 +01:00
|
|
|
if (event_queue.isEmpty()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
long t = event_queue.firstKey();
|
2018-12-20 17:21:06 +01:00
|
|
|
//System.out.printf("Worker: %d - queu is not empty: cur millis %d til %d\n", Thread.currentThread().getId(), clock.currentTimeMillis(),t);
|
2018-12-19 19:52:23 +01:00
|
|
|
return clock.getDelay(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected Event getNextEvent() {
|
|
|
|
|
|
|
|
// System.out.printf("RunEvents in Thread %d\n",Thread.currentThread().getId());
|
|
|
|
synchronized (event_queue) {
|
|
|
|
if (event_queue.isEmpty()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
long t = event_queue.firstKey();
|
|
|
|
LinkedList<Event> s = event_queue.get(t);
|
|
|
|
|
|
|
|
Event e = s.pop();
|
|
|
|
if (s.isEmpty()) {
|
|
|
|
event_queue.remove(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
return e;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-12-18 01:10:08 +01:00
|
|
|
}
|
2018-12-19 19:52:23 +01:00
|
|
|
|
2018-12-20 17:21:06 +01:00
|
|
|
public long currentTimeMillis(){
|
|
|
|
return clock.currentTimeMillis();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setAcceleration(double a){
|
|
|
|
clock.setAcceleration(a);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-12-09 01:53:47 +01:00
|
|
|
}
|