// Path: core/src/main/java/org/mini2Dx/miniscript/core/ThreadPoolProvider.java
// public interface ThreadPoolProvider {
// 
// 	Future<?> submit(Runnable task);
// 
// 	ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit);
// 
// 	public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit);
// 
// 	void shutdown(boolean interruptThreads);
// 
// }
// 
// Path: core/src/main/java/org/mini2Dx/miniscript/core/util/ScheduledTaskQueue.java
// public class ScheduledTaskQueue extends ReadWritePriorityQueue<ScheduledTask> {
// 
// 	public ScheduledTaskQueue() {
// 		this(Integer.MAX_VALUE);
// 	}
// 
// 	public ScheduledTaskQueue(int maxCapacity) {
// 		super(maxCapacity);
// 	}
// 
// 	@Override
// 	public ScheduledTask take() throws InterruptedException {
// 		ScheduledTask head = super.peek();
// 		while(head == null || head.getScheduledStartTimeNanos() > System.nanoTime()) {
// 			if(head == null) {
// 				synchronized(waitingForItemsMonitor) {
// 					waitingForItemsMonitor.wait();
// 				}
// 			} else {
// 				long currentTimeNanos = System.nanoTime();
// 				long delayMillis = Math.max (0, (head.getScheduledStartTimeNanos() - currentTimeNanos) / TimeUnit.MILLISECONDS.toNanos(1));
// 				int delayNanos = Math.max (0, (int) ((head.getScheduledStartTimeNanos() - currentTimeNanos) % TimeUnit.MILLISECONDS.toNanos(1)));
// 
// 				synchronized(waitingForItemsMonitor) {
// 					waitingForItemsMonitor.wait(delayMillis, delayNanos);
// 				}
// 			}
// 
// 			head = super.peek();
// 		}
// 
// 		lock.lockWrite();
// 		ScheduledTask result = super.peek();
// 		while (isEmpty() || result == null || result.getScheduledStartTimeNanos() > System.nanoTime()) {
// 			lock.unlockWrite();
// 			synchronized(waitingForItemsMonitor) {
// 				if(result == null) {
// 					waitingForItemsMonitor.wait();
// 				} else {
// 					long currentTimeNanos = System.nanoTime();
// 					long delayMillis = Math.max (0, (head.getScheduledStartTimeNanos() - currentTimeNanos) / TimeUnit.MILLISECONDS.toNanos(1));
// 					int delayNanos = Math.max (0, (int) ((head.getScheduledStartTimeNanos() - currentTimeNanos) % TimeUnit.MILLISECONDS.toNanos(1)));
// 					waitingForItemsMonitor.wait(delayMillis, delayNanos);
// 				}
// 			}
// 			lock.lockWrite();
// 			result = super.peek();
// 		}
// 		result = super.poll();
// 		lock.unlockWrite();
// 
// 		synchronized(waitingForRemovalMonitor) {
// 			waitingForRemovalMonitor.notify();
// 		}
// 		return result;
// 	}
// }


import org.mini2Dx.miniscript.core.ThreadPoolProvider;
import org.mini2Dx.miniscript.core.util.ScheduledTaskQueue;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
/**
 * The MIT License (MIT)
 *
 * Copyright (c) 2021 Thomas Cashman
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.mini2Dx.miniscript.core.threadpool;



/**
 * {@link ThreadPoolProvider} implementation for Kava-based runtimes
 */
public class KavaThreadPoolProvider implements Runnable, ThreadPoolProvider {
	private final AtomicBoolean running = new AtomicBoolean(true);
	private final Thread [] threads;

// Path: core/src/main/java/org/mini2Dx/miniscript/core/ThreadPoolProvider.java
// public interface ThreadPoolProvider {
// 
// 	Future<?> submit(Runnable task);
// 
// 	ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit);
// 
// 	public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit);
// 
// 	void shutdown(boolean interruptThreads);
// 
// }
// 
// Path: core/src/main/java/org/mini2Dx/miniscript/core/util/ScheduledTaskQueue.java
// public class ScheduledTaskQueue extends ReadWritePriorityQueue<ScheduledTask> {
// 
// 	public ScheduledTaskQueue() {
// 		this(Integer.MAX_VALUE);
// 	}
// 
// 	public ScheduledTaskQueue(int maxCapacity) {
// 		super(maxCapacity);
// 	}
// 
// 	@Override
// 	public ScheduledTask take() throws InterruptedException {
// 		ScheduledTask head = super.peek();
// 		while(head == null || head.getScheduledStartTimeNanos() > System.nanoTime()) {
// 			if(head == null) {
// 				synchronized(waitingForItemsMonitor) {
// 					waitingForItemsMonitor.wait();
// 				}
// 			} else {
// 				long currentTimeNanos = System.nanoTime();
// 				long delayMillis = Math.max (0, (head.getScheduledStartTimeNanos() - currentTimeNanos) / TimeUnit.MILLISECONDS.toNanos(1));
// 				int delayNanos = Math.max (0, (int) ((head.getScheduledStartTimeNanos() - currentTimeNanos) % TimeUnit.MILLISECONDS.toNanos(1)));
// 
// 				synchronized(waitingForItemsMonitor) {
// 					waitingForItemsMonitor.wait(delayMillis, delayNanos);
// 				}
// 			}
// 
// 			head = super.peek();
// 		}
// 
// 		lock.lockWrite();
// 		ScheduledTask result = super.peek();
// 		while (isEmpty() || result == null || result.getScheduledStartTimeNanos() > System.nanoTime()) {
// 			lock.unlockWrite();
// 			synchronized(waitingForItemsMonitor) {
// 				if(result == null) {
// 					waitingForItemsMonitor.wait();
// 				} else {
// 					long currentTimeNanos = System.nanoTime();
// 					long delayMillis = Math.max (0, (head.getScheduledStartTimeNanos() - currentTimeNanos) / TimeUnit.MILLISECONDS.toNanos(1));
// 					int delayNanos = Math.max (0, (int) ((head.getScheduledStartTimeNanos() - currentTimeNanos) % TimeUnit.MILLISECONDS.toNanos(1)));
// 					waitingForItemsMonitor.wait(delayMillis, delayNanos);
// 				}
// 			}
// 			lock.lockWrite();
// 			result = super.peek();
// 		}
// 		result = super.poll();
// 		lock.unlockWrite();
// 
// 		synchronized(waitingForRemovalMonitor) {
// 			waitingForRemovalMonitor.notify();
// 		}
// 		return result;
// 	}
// }


// Path: core/src/main/java/org/mini2Dx/miniscript/core/threadpool/KavaThreadPoolProvider.java
import org.mini2Dx.miniscript.core.ThreadPoolProvider;
import org.mini2Dx.miniscript.core.util.ScheduledTaskQueue;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * The MIT License (MIT)
 *
 * Copyright (c) 2021 Thomas Cashman
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.mini2Dx.miniscript.core.threadpool;



/**
 * {@link ThreadPoolProvider} implementation for Kava-based runtimes
 */
public class KavaThreadPoolProvider implements Runnable, ThreadPoolProvider {
	private final AtomicBoolean running = new AtomicBoolean(true);
	private final Thread [] threads;

	private final ScheduledTaskQueue scheduledTaskQueue = new ScheduledTaskQueue();