Exemplo n.º 1
0
        private static void TaskSchedulerBody()
        {
            do
            {
                lock (_queueLock)
                {
                    // 1. Remove aborted tasks
                    while (_readyQueue.Count > 0 && _readyQueue.Peek().Task == null)
                    {
                        _readyQueue.Dequeue();
                    }

                    int readyTaskCount = _readyQueue.Count;

                    for (int i = 0; i < readyTaskCount; i++)
                    {
                        TaskHandle readyTask = _readyQueue.Peek();
                        bool       added     = false;

                        // 2. Try to assign a new task to a free handler
                        lock (_poolLock)
                        {
                            foreach (TaskItem taskItem in _pool)
                            {
                                if (taskItem.State == TaskState.Completed)
                                {
                                    taskItem.Handle = readyTask;
                                    taskItem.State  = TaskState.NotStarted;

                                    added = true;
                                    _readyQueue.Dequeue();
                                    break;
                                }
                            }
                        }

                        // 3. Try to create a new handler
                        if (!added && _pool.Count < _maxCount)
                        {
                            AddTaskToPool(readyTask);

                            added = true;
                            _readyQueue.Dequeue();
                        }

                        if (!added)
                        {
                            break;
                        }
                    }
                }

                // 4. Sleep for a while to not waste CPU
                Thread.Sleep(SchedulingInterval);
            } while (true);
        }
Exemplo n.º 2
0
        public static ClientHandle QueueUserTask(UserTask task, object userState, Action <TaskStatus> callback)
        {
            InitializeThreadPool();

            ClientHandle clientHandle = new ClientHandle(Guid.NewGuid());
            TaskHandle   taskHandle   = new TaskHandle(clientHandle, task, userState, callback);

            lock (_queueLock)
            {
                _readyQueue.Enqueue(taskHandle);
            }

            return(clientHandle);
        }
Exemplo n.º 3
0
        private static void AddTaskToPool(TaskHandle taskHandle)
        {
            DistributedThread handler = new DistributedThread(TaskBody);

            if (_pool.Count < Environment.ProcessorCount)
            {
                handler.ProcessorAffinity = 1 << _pool.Count;
            }

            handler.ManagedThread.Name         = "Thread Pool Thread";
            handler.ManagedThread.IsBackground = true;

            TaskItem taskItem = new TaskItem(taskHandle, handler);

            taskItem.Handler.Start(taskItem);

            lock (_poolLock)
            {
                _pool.Add(taskItem);
            }
        }
Exemplo n.º 4
0
 public TaskItem(TaskHandle handle, DistributedThread handler)
 {
     Handle  = handle;
     Handler = handler;
     State   = TaskState.NotStarted;
 }