Exemple #1
0
        /// <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();
        }
Exemple #2
0
        /// <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);
                }
            }
        }
Exemple #3
0
        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);
        }
Exemple #4
0
        /// <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();
            }
        }
Exemple #5
0
        /// <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);
                }
            }
        }
Exemple #6
0
        /// <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);
        }
Exemple #7
0
        /// <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);
        }
Exemple #8
0
        /// <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);
        }
Exemple #9
0
        public bool IsFinishing(IThreadedTask task)
        {
            object @lock = this.m_lock;
            bool   result;

            lock (@lock)
            {
                result = this.m_finishedTasks.Contains(task);
            }
            return(result);
        }
Exemple #10
0
        public bool IsScheduled(IThreadedTask task)
        {
            object @lock = this.m_lock;
            bool   result;

            lock (@lock)
            {
                result = this.m_scheduledTasks.Contains(task);
            }
            return(result);
        }
Exemple #11
0
        public void AddWaiting(IThreadedTask task)
        {
            object @lock = this.m_lock;

            lock (@lock)
            {
                if (!this.m_shutingDown)
                {
                    task.Scheduler = this;
                    this.m_waitingTasks.AddLast(task);
                }
            }
        }
Exemple #12
0
        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);
                }
            }
        }
Exemple #13
0
        /// <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);
                }
            }
        }
Exemple #14
0
 /// <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);
         }
     }
 }
Exemple #15
0
        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();
                }
            }
        }
Exemple #16
0
        /// <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);
                }
            }
        }
Exemple #17
0
        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);
                }
            }
        }
Exemple #18
0
        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();
                }
            }
        }
Exemple #19
0
        /// <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:  报错
                }
            }
        }
Exemple #20
0
        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);
                }
            }
        }
Exemple #21
0
        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);
                    }
                }
            }
        }
Exemple #22
0
        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);
                    }
                }
            }
        }
Exemple #23
0
        /// <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);
        }
Exemple #24
0
 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();
 }
Exemple #25
0
        /// <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);
            }
        }
Exemple #26
0
        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);
                }
            }
        }
Exemple #27
0
        /// <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);

            }
        }
Exemple #28
0
 public Task ScheduleTask(IThreadedTask threadedTask)
 {
     threadedTask.Run(new DummyProgress());
     return(Task.CompletedTask);
 }
Exemple #29
0
 public void ScheduleTask(IThreadedTask threadedTask)
 {
 }
Exemple #30
0
        /// <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);
            }
        }
Exemple #31
0
        /// <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);
                }
            }
        }
Exemple #32
0
        /// <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;
        }
Exemple #33
0
 /// <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);
         }
     }
 }
Exemple #34
0
 public bool Contains(IThreadedTask task)
 {
     return(this.IsScheduled(task) || this.IsWaiting(task) || this.IsRunning(task) || this.IsFinishing(task));
 }
Exemple #35
0
 /// <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;
 }
Exemple #36
0
 /// <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;
 }
Exemple #37
0
 /// <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;
 }
Exemple #38
0
        /// <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);

            }
        }
Exemple #39
0
        /// <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);
            }
        }
Exemple #40
0
        /// <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);
                }
            }
        }