2017-05-07 00:18:59 +00:00
|
|
|
/*
|
2017-05-01 13:32:39 +00:00
|
|
|
* This file is part of ComputerCraft - http://www.computercraft.info
|
2019-01-01 01:10:18 +00:00
|
|
|
* Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
|
2017-05-01 13:32:39 +00:00
|
|
|
* Send enquiries to dratcliffe@gmail.com
|
|
|
|
*/
|
|
|
|
|
|
|
|
package dan200.computercraft.core.computer;
|
|
|
|
|
2018-04-24 11:26:26 +00:00
|
|
|
import dan200.computercraft.core.tracking.Tracking;
|
|
|
|
|
|
|
|
import java.util.ArrayDeque;
|
|
|
|
import java.util.Queue;
|
2017-05-01 13:32:39 +00:00
|
|
|
|
|
|
|
public class MainThread
|
|
|
|
{
|
|
|
|
private static final int MAX_TASKS_PER_TICK = 1000;
|
|
|
|
private static final int MAX_TASKS_TOTAL = 50000;
|
|
|
|
|
2018-04-24 11:26:26 +00:00
|
|
|
private static final Queue<ITask> m_outstandingTasks = new ArrayDeque<>();
|
2017-05-01 13:32:39 +00:00
|
|
|
private static final Object m_nextUnusedTaskIDLock = new Object();
|
|
|
|
private static long m_nextUnusedTaskID = 0;
|
|
|
|
|
|
|
|
public static long getUniqueTaskID()
|
|
|
|
{
|
|
|
|
synchronized( m_nextUnusedTaskIDLock )
|
|
|
|
{
|
|
|
|
return ++m_nextUnusedTaskID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean queueTask( ITask task )
|
|
|
|
{
|
|
|
|
synchronized( m_outstandingTasks )
|
|
|
|
{
|
|
|
|
if( m_outstandingTasks.size() < MAX_TASKS_TOTAL )
|
|
|
|
{
|
2018-04-24 11:26:26 +00:00
|
|
|
m_outstandingTasks.offer( task );
|
2017-05-01 13:32:39 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void executePendingTasks()
|
|
|
|
{
|
|
|
|
int tasksThisTick = 0;
|
|
|
|
while( tasksThisTick < MAX_TASKS_PER_TICK )
|
|
|
|
{
|
|
|
|
ITask task = null;
|
|
|
|
synchronized( m_outstandingTasks )
|
|
|
|
{
|
2018-04-24 11:26:26 +00:00
|
|
|
task = m_outstandingTasks.poll();
|
2017-05-01 13:32:39 +00:00
|
|
|
}
|
|
|
|
if( task != null )
|
|
|
|
{
|
2018-04-24 11:26:26 +00:00
|
|
|
long start = System.nanoTime();
|
2017-05-01 13:32:39 +00:00
|
|
|
task.execute();
|
2018-04-24 11:26:26 +00:00
|
|
|
|
|
|
|
long stop = System.nanoTime();
|
|
|
|
Computer computer = task.getOwner();
|
|
|
|
if( computer != null ) Tracking.addServerTiming( computer, stop - start );
|
|
|
|
|
2017-05-01 13:32:39 +00:00
|
|
|
++tasksThisTick;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|