/// <summary> /// Update Scheduler. /// </summary> public void Update() { //Dont forget any multithreaded or coroutine tasks must //tell the scheduler when they are finished from their Run //function by calling FinishedRunning(). TasksRanThisUpdate = 0; TasksFinishedThisUpdate = 0; //clean up any tasks that have finished since last time //scheduler was updated. FinishTasks(); while (TasksRanThisUpdate < MaxTasksPerUpdate) { if (ScheduledTasks() > 0) { IThreadedTask task = m_scheduledTasks.First.Value; m_scheduledTasks.RemoveFirst(); RunTask(task); } //If a task running on another thread or in a //coroutine has thrown a exception rethrow it here. CheckForException(); if (ScheduledTasks() == 0) { break; } } FinishTasks(); }
/// <summary> /// If a task is not running on the main thread or uses a /// coroutine it needs to tell the scheduler when it is finished. /// The task will then be cleaned up and have its end function called. /// </summary> public void Finished(IThreadedTask task) { lock (m_lock) { #if CETO_DEBUG_SCHEDULER if (IsFinishing(task)) { throw new ArgumentException("Task has already been added to finished list."); } if (IsScheduled(task)) { throw new ArgumentException("Task is currently scheduled."); } if (IsWaiting(task)) { throw new ArgumentException("Task is currently waiting."); } #endif m_runningTasks.Remove(task); if (!m_shutingDown && !task.NoFinish && !task.Cancelled) { m_finishedTasks.AddLast(task); } } }
public void CreateAndCacheCondition(int fourierSize, float windSpeed, float windDir, float waveAge) { if (this.m_conditionCache == null) { return; } if (this.m_conditionCache.Count >= this.m_maxConditionCacheSize) { Ocean.LogWarning("Condition cache full. Condition not cached."); return; } if (!Mathf.IsPowerOfTwo(fourierSize) || fourierSize < 32 || fourierSize > 512) { Ocean.LogWarning("Fourier size must be a pow2 number from 32 to 512. Condition not cached."); return; } WaveSpectrumCondition waveSpectrumCondition = this.NewSpectrumCondition(fourierSize, windSpeed, windDir, waveAge); if (this.m_conditionCache.ContainsKey(waveSpectrumCondition.Key)) { return; } IThreadedTask createSpectrumConditionTask = waveSpectrumCondition.GetCreateSpectrumConditionTask(); createSpectrumConditionTask.Start(); createSpectrumConditionTask.Run(); createSpectrumConditionTask.End(); this.m_conditionCache.AddFirst(waveSpectrumCondition.Key, waveSpectrumCondition); }
/// <summary> /// Finish all tasks in the finished list /// by calling there end function and removing /// the from the running list. /// </summary> public void FinishTasks() { if (TasksFinishedThisUpdate >= MaxFinishPerUpdate) { return; } lock (m_lock) { m_haveRan.Clear(); //Get a list of all tasks that have ran. var e1 = m_runningTasks.GetEnumerator(); while (e1.MoveNext()) { IThreadedTask task = e1.Current; if (task.Ran) { m_haveRan.AddLast(task); } } //Remove from running list and add to finished list. var e2 = m_haveRan.GetEnumerator(); while (e2.MoveNext()) { Finished(e2.Current); } if (m_finishedTasks.Count == 0) { return; } //Get task at start of queue IThreadedTask finished = m_finishedTasks.First.Value; m_finishedTasks.RemoveFirst(); while (finished != null) { //Clean up task. finished.End(); TasksFinishedThisUpdate++; if (m_finishedTasks.Count == 0 || TasksFinishedThisUpdate >= MaxFinishPerUpdate) { finished = null; } else { finished = m_finishedTasks.First.Value; m_finishedTasks.RemoveFirst(); } } m_haveRan.Clear(); } }
/// <summary> /// Removes a task from the waiting queue and /// adds it to the scheduled queue were it will be run. /// </summary> public void StopWaiting(IThreadedTask task, bool run) { lock (m_lock) { if (m_shutingDown) { return; } #if CETO_DEBUG_SCHEDULER if (IsScheduled(task)) { throw new ArgumentException("Task has already been scheduled."); } if (IsRunning(task)) { throw new ArgumentException("Task is currently running."); } if (IsFinishing(task)) { throw new ArgumentException("Task is currently finishing."); } if (task.Started) { throw new ArgumentException("Task has already been started."); } if (task.Ran) { throw new ArgumentException("Task has already been ran."); } if (task.Done) { throw new ArgumentException("Task has already been done."); } if (task.Cancelled) { throw new ArgumentException("Task has been cancelled."); } #endif m_waitingTasks.Remove(task); if (run) { RunTask(task); } else { m_scheduledTasks.AddLast(task); } } }
/// <summary> /// Returns true if the tasks has been added to /// the scheduler and is finishing. /// </summary> public bool IsFinishing(IThreadedTask task) { bool b; lock (m_lock) { b = m_finishedTasks.Contains(task); } return(b); }
/// <summary> /// Returns true if the tasks has been added to /// the scheduler and is waiting. /// </summary> public bool IsWaiting(IThreadedTask task) { bool b; lock (m_lock) { b = m_waitingTasks.Contains(task); } return(b); }
/// <summary> /// Returns true if the tasks has been added to /// the scheduler and has not been run. /// </summary> public bool IsScheduled(IThreadedTask task) { bool b; lock (m_lock) { b = m_scheduledTasks.Contains(task); } return(b); }
public bool IsFinishing(IThreadedTask task) { object @lock = this.m_lock; bool result; lock (@lock) { result = this.m_finishedTasks.Contains(task); } return(result); }
public bool IsScheduled(IThreadedTask task) { object @lock = this.m_lock; bool result; lock (@lock) { result = this.m_scheduledTasks.Contains(task); } return(result); }
public void AddWaiting(IThreadedTask task) { object @lock = this.m_lock; lock (@lock) { if (!this.m_shutingDown) { task.Scheduler = this; this.m_waitingTasks.AddLast(task); } } }
public void Finished(IThreadedTask task) { object @lock = this.m_lock; lock (@lock) { this.m_runningTasks.Remove(task); if (!this.m_shutingDown && !task.NoFinish && !task.Cancelled) { this.m_finishedTasks.AddLast(task); } } }
/// <summary> /// Add a task to scheduler and run immediately. /// </summary> public void Run(IThreadedTask task) { lock (m_lock) { if (m_shutingDown) { return; } #if CETO_DEBUG_SCHEDULER if (Contains(task)) { throw new ArgumentException("Scheduler already contains task."); } if (task.Started) { throw new ArgumentException("Task has already been started."); } if (task.Ran) { throw new ArgumentException("Task has already been ran."); } if (task.Done) { throw new ArgumentException("Task has already been done."); } if (task.Cancelled) { throw new ArgumentException("Task has been cancelled."); } #endif task.Scheduler = this; if (TasksRanThisUpdate >= MaxTasksPerUpdate) { Add(task); } else { RunTask(task); } } }
/// <summary> /// Cancel a task. Task will have its cancel function called /// if it is cancelled. Tasks that are already running or /// finishing can not be cancelled. /// </summary> public void Cancel(IThreadedTask task) { lock (m_lock) { if (m_scheduledTasks.Contains(task)) { task.Cancel(); m_scheduledTasks.Remove(task); } else if (m_waitingTasks.Contains(task)) { task.Cancel(); m_waitingTasks.Remove(task); } } }
public void FinishTasks() { if (this.TasksFinishedThisUpdate >= this.MaxFinishPerUpdate) { return; } object @lock = this.m_lock; lock (@lock) { this.m_haveRan.Clear(); LinkedList <IThreadedTask> .Enumerator enumerator = this.m_runningTasks.GetEnumerator(); while (enumerator.MoveNext()) { IThreadedTask current = enumerator.Current; if (current.Ran) { this.m_haveRan.AddLast(current); } } LinkedList <IThreadedTask> .Enumerator enumerator2 = this.m_haveRan.GetEnumerator(); while (enumerator2.MoveNext()) { this.Finished(enumerator2.Current); } if (this.m_finishedTasks.Count != 0) { IThreadedTask threadedTask = this.m_finishedTasks.First.Value; this.m_finishedTasks.RemoveFirst(); while (threadedTask != null) { threadedTask.End(); this.TasksFinishedThisUpdate++; if (this.m_finishedTasks.Count == 0 || this.TasksFinishedThisUpdate >= this.MaxFinishPerUpdate) { threadedTask = null; } else { threadedTask = this.m_finishedTasks.First.Value; this.m_finishedTasks.RemoveFirst(); } } this.m_haveRan.Clear(); } } }
/// <summary> /// Runs a threaded task /// </summary> void RunThreaded(object o) { IThreadedTask task = o as IThreadedTask; if (task == null) { Throw(new InvalidCastException("Object is not a ITask or is null")); } else { try { task.Run(); } catch (Exception e) { Throw(e); } } }
public void Cancel(IThreadedTask task) { object @lock = this.m_lock; lock (@lock) { if (this.m_scheduledTasks.Contains(task)) { task.Cancel(); this.m_scheduledTasks.Remove(task); } else if (this.m_waitingTasks.Contains(task)) { task.Cancel(); this.m_waitingTasks.Remove(task); } } }
public void FinishTasks() { if (this.TasksFinishedThisUpdate >= this.MaxFinishPerUpdate) { return; } object @lock = this.m_lock; lock (@lock) { this.m_haveRan.Clear(); foreach (IThreadedTask threadedTask in this.m_runningTasks) { if (threadedTask.Ran) { this.m_haveRan.AddLast(threadedTask); } } foreach (IThreadedTask task in this.m_haveRan) { this.Finished(task); } if (this.m_finishedTasks.Count != 0) { IThreadedTask threadedTask2 = this.m_finishedTasks.First.Value; this.m_finishedTasks.RemoveFirst(); while (threadedTask2 != null) { threadedTask2.End(); this.TasksFinishedThisUpdate++; if (this.m_finishedTasks.Count == 0 || this.TasksFinishedThisUpdate >= this.MaxFinishPerUpdate) { threadedTask2 = null; } else { threadedTask2 = this.m_finishedTasks.First.Value; this.m_finishedTasks.RemoveFirst(); } } this.m_haveRan.Clear(); } } }
/// <summary> /// Runs a threaded task /// </summary> void RunThreaded(object o) { IThreadedTask task = o as IThreadedTask; //OYM: 判断是不是正确的形式,如果不正确的话就是null if (task == null) { Throw(new InvalidCastException("Object is not a ITask or is null")); } else { try { task.Run();//OYM: 执行 } catch (Exception e) { Throw(e); //OYM: 报错 } } }
private void CreateConditions() { int size = this.m_bufferSettings.size; WaveSpectrumConditionKey waveSpectrumConditionKey = this.NewSpectrumConditionKey(size, this.windSpeed, this.m_ocean.windDir, this.waveAge); if (this.m_conditions[0] == null) { if (this.m_conditionCache.ContainsKey(waveSpectrumConditionKey)) { this.m_conditions[0] = this.m_conditionCache[waveSpectrumConditionKey]; this.m_conditionCache.Remove(waveSpectrumConditionKey); } else { this.m_conditions[0] = this.NewSpectrumCondition(size, this.windSpeed, this.m_ocean.windDir, this.waveAge); IThreadedTask createSpectrumConditionTask = this.m_conditions[0].GetCreateSpectrumConditionTask(); createSpectrumConditionTask.Start(); createSpectrumConditionTask.Run(); createSpectrumConditionTask.End(); } } else if (this.m_conditions[1] != null && this.m_conditions[1].Done) { this.CacheCondition(this.m_conditions[0]); this.m_conditions[0] = this.m_conditions[1]; this.m_conditions[1] = null; } else if (this.m_conditions[1] == null && this.m_conditions[0].Done && waveSpectrumConditionKey != this.m_conditions[0].Key) { if (this.m_conditionCache.ContainsKey(waveSpectrumConditionKey)) { this.m_conditions[0] = this.m_conditionCache[waveSpectrumConditionKey]; this.m_conditionCache.Remove(waveSpectrumConditionKey); } else { this.m_conditions[1] = this.NewSpectrumCondition(size, this.windSpeed, this.m_ocean.windDir, this.waveAge); IThreadedTask createSpectrumConditionTask2 = this.m_conditions[1].GetCreateSpectrumConditionTask(); this.m_scheduler.Add(createSpectrumConditionTask2); } } }
public void StopWaiting(IThreadedTask task, bool run) { object @lock = this.m_lock; lock (@lock) { if (!this.m_shutingDown) { this.m_waitingTasks.Remove(task); if (run) { this.RunTask(task); } else { this.m_scheduledTasks.AddLast(task); } } } }
public void Run(IThreadedTask task) { object @lock = this.m_lock; lock (@lock) { if (!this.m_shutingDown) { task.Scheduler = this; if (this.TasksRanThisUpdate >= this.MaxTasksPerUpdate) { this.Add(task); } else { this.RunTask(task); } } } }
/// <summary> /// Does the scheduler contain the task in any of /// its queues. /// </summary> public bool Contains(IThreadedTask task) { if (IsScheduled(task)) { return(true); } if (IsWaiting(task)) { return(true); } if (IsRunning(task)) { return(true); } if (IsFinishing(task)) { return(true); } return(false); }
public void Update() { this.TasksRanThisUpdate = 0; this.TasksFinishedThisUpdate = 0; this.FinishTasks(); while (this.TasksRanThisUpdate < this.MaxTasksPerUpdate) { if (this.ScheduledTasks() > 0) { IThreadedTask value = this.m_scheduledTasks.First.Value; this.m_scheduledTasks.RemoveFirst(); this.RunTask(value); } this.CheckForException(); if (this.ScheduledTasks() == 0) { break; } } this.FinishTasks(); }
/// <summary> /// Add a task to scheduler. The task will be queued and /// will be run when it reaches the front of queue. /// </summary> public void Add(IThreadedTask task) { lock (m_lock) { if (m_shutingDown) { return; } #if CETO_DEBUG_SCHEDULER if (Contains(task)) { throw new ArgumentException("Scheduler already contains task."); } if (task.Started) { throw new ArgumentException("Task has already been started."); } if (task.Ran) { throw new ArgumentException("Task has already been ran."); } if (task.Done) { throw new ArgumentException("Task has already been done."); } if (task.Cancelled) { throw new ArgumentException("Task has been cancelled."); } #endif task.Scheduler = this; m_scheduledTasks.AddLast(task); } }
private void RunTask(IThreadedTask task) { object @lock = this.m_lock; lock (@lock) { this.TasksRanThisUpdate++; if (!task.IsThreaded || this.DisableMultithreading) { task.Start(); IEnumerator enumerator = task.Run(); if (enumerator != null) { if (this.m_coroutine == null) { throw new InvalidOperationException("Scheduler trying to run a coroutine task when coroutine interface is null"); } this.m_coroutine.RunCoroutine(enumerator); if (!this.IsFinishing(task)) { this.m_runningTasks.AddLast(task); } } else { task.End(); } } else { task.Start(); this.m_runningTasks.AddLast(task); ThreadPool.QueueUserWorkItem(new WaitCallback(this.RunThreaded), task); } } }
/// <summary> /// If a task is not running on the main thread or uses a /// coroutine it needs to tell the scheduler when it is finished. /// The task will then be cleaned up and have its end function called. /// </summary> public void Finished(IThreadedTask task) { lock(m_lock) { #if CETO_DEBUG_SCHEDULER if (IsFinishing(task)) throw new ArgumentException("Task has already been added to finished list."); if (IsScheduled(task)) throw new ArgumentException("Task is currently scheduled."); if (IsWaiting(task)) throw new ArgumentException("Task is currently waiting."); #endif m_runningTasks.Remove(task); if(!m_shutingDown && !task.NoFinish && !task.Cancelled) m_finishedTasks.AddLast(task); } }
public Task ScheduleTask(IThreadedTask threadedTask) { threadedTask.Run(new DummyProgress()); return(Task.CompletedTask); }
public void ScheduleTask(IThreadedTask threadedTask) { }
/// <summary> /// Adds a task to the waiting queue. A task will stop /// waiting on some predefined event like another task /// finishing. /// </summary> public void AddWaiting(IThreadedTask task) { lock(m_lock) { if(m_shutingDown) return; #if CETO_DEBUG_SCHEDULER if (Contains(task)) throw new ArgumentException("Scheduler already contains task."); if (task.Started) throw new ArgumentException("Task has already been started."); if (task.Ran) throw new ArgumentException("Task has already been ran."); if (task.Done) throw new ArgumentException("Task has already been done."); if (task.Cancelled) throw new ArgumentException("Task has been cancelled."); #endif task.Scheduler = this; m_waitingTasks.AddLast(task); } }
/// <summary> /// Runs next task. /// </summary> void RunTask(IThreadedTask task) { lock (m_lock) { #if CETO_DEBUG_SCHEDULER if (Contains(task)) { throw new ArgumentException("Scheduler already contains task."); } if (task.Started) { throw new ArgumentException("Task has already been started."); } if (task.Ran) { throw new ArgumentException("Task has already been ran."); } if (task.Done) { throw new ArgumentException("Task has already been done."); } if (task.Cancelled) { throw new ArgumentException("Task has been cancelled."); } #endif TasksRanThisUpdate++; if (!task.IsThreaded || DisableMultithreading) { //Start task. task.Start(); //Run task IEnumerator e = task.Run(); //If task returned a enumerator //the task is a coroutine. if (e != null) { if (m_coroutine == null) { throw new InvalidOperationException("Scheduler trying to run a coroutine task when coroutine interface is null"); } //Start coroutine and add to running queue if it has //not been added to the finishing queue. //If a task has ran quickly it may have already //been added to the finished queue. m_coroutine.RunCoroutine(e); if (!IsFinishing(task)) { m_runningTasks.AddLast(task); } } else { //Clean up task task.End(); } } else { //Start task task.Start(); //Run task on separate thread //and add to running tasks list. m_runningTasks.AddLast(task); ThreadPool.QueueUserWorkItem(new WaitCallback(RunThreaded), task); } } }
/// <summary> /// Does the scheduler contain the task in any of /// its queues. /// </summary> public bool Contains(IThreadedTask task) { if(IsScheduled(task)) return true; if(IsWaiting(task)) return true; if(IsRunning(task)) return true; if(IsFinishing(task)) return true; return false; }
/// <summary> /// Cancel a task. Task will have its cancel function called /// if it is cancelled. Tasks that are already running or /// finishing can not be cancelled. /// </summary> public void Cancel(IThreadedTask task) { lock(m_lock) { if (m_scheduledTasks.Contains(task)) { task.Cancel(); m_scheduledTasks.Remove(task); } else if (m_waitingTasks.Contains(task)) { task.Cancel(); m_waitingTasks.Remove(task); } } }
public bool Contains(IThreadedTask task) { return(this.IsScheduled(task) || this.IsWaiting(task) || this.IsRunning(task) || this.IsFinishing(task)); }
/// <summary> /// Returns true if the tasks has been added to /// the scheduler and is finishing. /// </summary> public bool IsFinishing(IThreadedTask task) { bool b; lock(m_lock) { b = m_finishedTasks.Contains(task); } return b; }
/// <summary> /// Returns true if the tasks has been added to /// the scheduler and has not been run. /// </summary> public bool IsScheduled(IThreadedTask task) { bool b; lock(m_lock) { b = m_scheduledTasks.Contains(task); } return b; }
/// <summary> /// Returns true if the tasks has been added to /// the scheduler and is waiting. /// </summary> public bool IsWaiting(IThreadedTask task) { bool b; lock(m_lock) { b = m_waitingTasks.Contains(task); } return b; }
/// <summary> /// Add a task to scheduler and run immediately. /// </summary> public void Run(IThreadedTask task) { lock(m_lock) { if(m_shutingDown) return; #if CETO_DEBUG_SCHEDULER if (Contains(task)) throw new ArgumentException("Scheduler already contains task."); if (task.Started) throw new ArgumentException("Task has already been started."); if (task.Ran) throw new ArgumentException("Task has already been ran."); if (task.Done) throw new ArgumentException("Task has already been done."); if (task.Cancelled) throw new ArgumentException("Task has been cancelled."); #endif task.Scheduler = this; if (TasksRanThisUpdate >= MaxTasksPerUpdate) Add(task); else RunTask(task); } }
/// <summary> /// Removes a task from the waiting queue and /// adds it to the scheduled queue were it will be run. /// </summary> public void StopWaiting(IThreadedTask task, bool run) { lock(m_lock) { if(m_shutingDown) return; #if CETO_DEBUG_SCHEDULER if (IsScheduled(task)) throw new ArgumentException("Task has already been scheduled."); if (IsRunning(task)) throw new ArgumentException("Task is currently running."); if (IsFinishing(task)) throw new ArgumentException("Task is currently finishing."); if (task.Started) throw new ArgumentException("Task has already been started."); if (task.Ran) throw new ArgumentException("Task has already been ran."); if (task.Done) throw new ArgumentException("Task has already been done."); if (task.Cancelled) throw new ArgumentException("Task has been cancelled."); #endif m_waitingTasks.Remove(task); if(run) RunTask(task); else m_scheduledTasks.AddLast(task); } }
/// <summary> /// Runs next task. /// </summary> void RunTask(IThreadedTask task) { lock(m_lock) { #if CETO_DEBUG_SCHEDULER if (Contains(task)) throw new ArgumentException("Scheduler already contains task."); if (task.Started) throw new ArgumentException("Task has already been started."); if (task.Ran) throw new ArgumentException("Task has already been ran."); if (task.Done) throw new ArgumentException("Task has already been done."); if (task.Cancelled) throw new ArgumentException("Task has been cancelled."); #endif TasksRanThisUpdate++; if (!task.IsThreaded || DisableMultithreading) { //Start task. task.Start(); //Run task IEnumerator e = task.Run(); //If task returned a enumerator //the task is a coroutine. if(e != null) { if(m_coroutine == null) throw new InvalidOperationException("Scheduler trying to run a coroutine task when coroutine interface is null"); //Start coroutine and add to running queue if it has //not been added to the finishing queue. //If a task has ran quickly it may have already //been added to the finished queue. m_coroutine.RunCoroutine(e); if(!IsFinishing(task)) { m_runningTasks.AddLast(task); } } else { //Clean up task task.End(); } } else { //Start task task.Start(); //Run task on separate thread //and add to running tasks list. m_runningTasks.AddLast(task); ThreadPool.QueueUserWorkItem(new WaitCallback(RunThreaded), task); } } }