Beispiel #1
0
        public void TestThreeTasksOnTwoThreadsPreemptingEachOther()
        {
            int numOfThreads = 2;
            SimpleTaskScheduler taskScheduler = SimpleTaskScheduler.CreatePreemptive(numOfThreads);

            SimpleTaskScheduler.ExecutionToken token1 = taskScheduler.GetExecutionToken();
            SimpleTaskScheduler.ExecutionToken token2 = taskScheduler.GetExecutionToken();
            SimpleTaskScheduler.ExecutionToken token3 = taskScheduler.GetExecutionToken();
            Object resource = new object();
            Task   t1       = CreateTaskWithResourceLock(id: 1, token: token1, resource: resource);
            Task   t2       = CreateTaskWithResourceLock(id: 2, token: token2, resource: resource);
            Task   t3       = CreateTask(id: 3, token: token3);

            taskScheduler.Register(t1, 5, 2500, token1);
            taskScheduler.Register(t2, 4, 2500, token2);
            taskScheduler.Register(t3, 3, 2500, token3);

            t1.Start(taskScheduler);
            Task.Delay(500).Wait();
            t2.Start(taskScheduler);
            t3.Start(taskScheduler);

            Task.Delay(5000).Wait();
            Assert.AreEqual(taskScheduler.CurrentlyExecutingTasksCount(), 0);
        }
            internal override void Execute()
            {
                int currentlyExecutingTasksCount = scheduler.CurrentlyExecutingTasksCount();

                Task minPriorityNonExecutingTask = scheduler.FindMinimumPriorityNonExecutingTask();

                if (minPriorityNonExecutingTask == null)
                {
                    return;
                }

                if (currentlyExecutingTasksCount < scheduler.MaxParallelTasks)
                {
                    //start min priority non executing task, we have a free thread
                    scheduler.ContinueStartTask(task: minPriorityNonExecutingTask, threadId: scheduler.FindFirstFreeThread());
                }
                else if (scheduler.IsPreemptive)
                {
                    //search for task which could be stopped
                    Task maxPriorityExecutingTask = scheduler.FindMaximumPriorityExecutingTask();

                    if (scheduler.ComparePriorities(minPriorityNonExecutingTask, maxPriorityExecutingTask) < 0)
                    {
                        int threadId = scheduler.GetThreadOfTask(maxPriorityExecutingTask);
                        scheduler.PauseTask(maxPriorityExecutingTask);
                        scheduler.ContinueStartTask(minPriorityNonExecutingTask, threadId);
                    }
                }
            }
Beispiel #3
0
        public void TestTwoTasksWithNoResourceLocking()
        {
            int numOfThreads = 1;
            SimpleTaskScheduler taskScheduler = SimpleTaskScheduler.CreatePreemptive(numOfThreads);

            SimpleTaskScheduler.ExecutionToken token1 = taskScheduler.GetExecutionToken();
            SimpleTaskScheduler.ExecutionToken token2 = taskScheduler.GetExecutionToken();
            Task t1 = CreateTask(id: 1, token: token1);
            Task t2 = CreateTask(id: 2, token: token2);

            taskScheduler.Register(t1, 5, 2500, token1);
            taskScheduler.Register(t2, 4, 2500, token2);

            t1.Start(taskScheduler);
            Task.Delay(500).Wait();
            t2.Start(taskScheduler);
            Assert.AreEqual(taskScheduler.CurrentlyExecutingTasksCount(), 1);
        }