summaryrefslogtreecommitdiff
path: root/Source/ExecutionEngine/ThreadTaskScheduler.cs
blob: 0b2c859476bda4b4734bac061f11adee07b6e11b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.Boogie
{
  // Implementation of System.Threading.Tasks.TaskScheduler which creates a unique thread per
  // task, and allows each thread to have its own custom stack size.  The standard
  // scheduler uses the .NET threadpool, which in turn inherits stack size from the EXE.
  public class ThreadTaskScheduler : TaskScheduler
  {
    private object tasklock; // Guards acess to the "tasks" queue
    private Queue<Task> tasks;

    private Thread[] dispatchers;
    private ManualResetEvent eventsWaiting;
    private int stackSize;

    public ThreadTaskScheduler(int StackReserveSize)
    {
      int MaxThreads = System.Environment.ProcessorCount;
      Initialize(StackReserveSize, MaxThreads);
    }

    public ThreadTaskScheduler(int StackReserveSize, int MaxThreads) 
    {
      Initialize(StackReserveSize, MaxThreads);
    }

    void Initialize(int StackReserveSize, int MaxThreads) 
    {
      Contract.Requires(StackReserveSize >= 0);
      Contract.Requires(MaxThreads > 0);

      tasklock = new object();
      tasks = new Queue<Task>();
      eventsWaiting = new ManualResetEvent(false);
      stackSize = StackReserveSize;
      dispatchers = new Thread[MaxThreads];
      for (int i = 0; i < MaxThreads; ++i) 
      {
        dispatchers[i] = new Thread(new ThreadStart(DispatcherMain));
        dispatchers[i].IsBackground = true;
        dispatchers[i].Start();
      }
    }

    protected override IEnumerable<Task> GetScheduledTasks() 
    {
      IEnumerable<Task> r;
      lock(tasklock)
      {
        r=tasks.ToArray<Task>();
      }
      return r;
    }

    protected override void QueueTask(Task task) 
    {
      lock (tasklock) 
      {
        tasks.Enqueue(task);
        eventsWaiting.Set();
      }
    }

    protected override bool TryExecuteTaskInline(Task task, bool taskWasPreviouslyQueued) 
    {
      return false;
    }

    private void DispatcherMain() 
    {
      while (true) 
      {
        Task t = null;
        lock (tasklock) 
        {
          if (tasks.Count > 0) 
          {
            t = tasks.Dequeue();
            if (tasks.Count == 0) 
            {
              eventsWaiting.Reset();
            }
          }
        }

        if (t != null) 
        {
          Thread th = new Thread(TaskMain, stackSize);
          th.Start(t);
          th.Join();
        }
        else 
        {
          eventsWaiting.WaitOne();
        }
      }
    }

    private void TaskMain(object data) 
    {
      Task t = (Task)data;
      TryExecuteTask(t);
    }
  }
}