| @@ -1,333 +1,333 | |||
| 
             | 
        1 | 1 | using System; | 
| 
             | 
        2 | 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; | 
| 
             | 
        3 | 3 | using System.Reflection; | 
| 
             | 
        4 | 4 | using System.Threading; | 
| 
             | 
        5 | 5 | using Implab.Parallels; | 
| 
             | 
        6 | 6 | |
| 
             | 
        7 | 7 | namespace Implab.Test { | 
| 
             | 
        8 | 8 | [TestClass] | 
| 
             | 
        9 | 9 | public class AsyncTests { | 
| 
             | 
        10 | 10 | [TestMethod] | 
| 
             | 
        11 | 11 | public void ResolveTest() { | 
| 
             | 
        12 | 12 | int res = -1; | 
| 
             | 
        13 | 13 | var p = new Promise<int>(); | 
| 
             | 
        14 | 14 | p.Then(x => res = x); | 
| 
             | 
        15 | 15 | p.Resolve(100); | 
| 
             | 
        16 | 16 | |
| 
             | 
        17 | 17 | Assert.AreEqual(100, res); | 
| 
             | 
        18 | 18 | } | 
| 
             | 
        19 | 19 | |
| 
             | 
        20 | 20 | [TestMethod] | 
| 
             | 
        21 | 21 | public void RejectTest() { | 
| 
             | 
        22 | 22 | int res = -1; | 
| 
             | 
        23 | 23 | Exception err = null; | 
| 
             | 
        24 | 24 | |
| 
             | 
        25 | 25 | var p = new Promise<int>(); | 
| 
             | 
        26 | 26 | p.Then(x => res = x, e => err = e); | 
| 
             | 
        27 | 27 | p.Reject(new ApplicationException("error")); | 
| 
             | 
        28 | 28 | |
| 
             | 
        29 | 29 | Assert.AreEqual(res, -1); | 
| 
             | 
        30 | 30 | Assert.AreEqual(err.Message, "error"); | 
| 
             | 
        31 | 31 | |
| 
             | 
        32 | 32 | } | 
| 
             | 
        33 | 33 | |
| 
             | 
        34 | 34 | [TestMethod] | 
| 
             | 
        35 | 35 | public void JoinSuccessTest() { | 
| 
             | 
        36 | 36 | var p = new Promise<int>(); | 
| 
             | 
        37 | 37 | p.Resolve(100); | 
| 
             | 
        38 | 38 | Assert.AreEqual(p.Join(), 100); | 
| 
             | 
        39 | 39 | } | 
| 
             | 
        40 | 40 | |
| 
             | 
        41 | 41 | [TestMethod] | 
| 
             | 
        42 | 42 | public void JoinFailTest() { | 
| 
             | 
        43 | 43 | var p = new Promise<int>(); | 
| 
             | 
        44 | 44 | p.Reject(new ApplicationException("failed")); | 
| 
             | 
        45 | 45 | |
| 
             | 
        46 | 46 | try { | 
| 
             | 
        47 | 47 | p.Join(); | 
| 
             | 
        48 | 48 | throw new ApplicationException("WRONG!"); | 
| 
             | 
        49 | 49 | } catch (TargetInvocationException err) { | 
| 
             | 
        50 | 50 | Assert.AreEqual(err.InnerException.Message, "failed"); | 
| 
             | 
        51 | 51 | } catch { | 
| 
             | 
        52 | 52 | Assert.Fail("Got wrong excaption"); | 
| 
             | 
        53 | 53 | } | 
| 
             | 
        54 | 54 | } | 
| 
             | 
        55 | 55 | |
| 
             | 
        56 | 56 | [TestMethod] | 
| 
             | 
        57 | 57 | public void MapTest() { | 
| 
             | 
        58 | 58 | var p = new Promise<int>(); | 
| 
             | 
        59 | 59 | |
| 
             | 
        60 | 60 | var p2 = p.Map(x => x.ToString()); | 
| 
             | 
        61 | 61 | p.Resolve(100); | 
| 
             | 
        62 | 62 | |
| 
             | 
        63 | 63 | Assert.AreEqual(p2.Join(), "100"); | 
| 
             | 
        64 | 64 | } | 
| 
             | 
        65 | 65 | |
| 
             | 
        66 | 66 | [TestMethod] | 
| 
             | 
        67 | 67 | public void FixErrorTest() { | 
| 
             | 
        68 | 68 | var p = new Promise<int>(); | 
| 
             | 
        69 | 69 | |
| 
             | 
        70 | 70 | var p2 = p.Error(e => 101); | 
| 
             | 
        71 | 71 | |
| 
             | 
        72 | 72 | p.Reject(new Exception()); | 
| 
             | 
        73 | 73 | |
| 
             | 
        74 | 74 | Assert.AreEqual(p2.Join(), 101); | 
| 
             | 
        75 | 75 | } | 
| 
             | 
        76 | 76 | |
| 
             | 
        77 | 77 | [TestMethod] | 
| 
             | 
        78 | 78 | public void ChainTest() { | 
| 
             | 
        79 | 79 | var p1 = new Promise<int>(); | 
| 
             | 
        80 | 80 | |
| 
             | 
        81 | 81 | var p3 = p1.Chain(x => { | 
| 
             | 
        82 | 82 | var p2 = new Promise<string>(); | 
| 
             | 
        83 | 83 | p2.Resolve(x.ToString()); | 
| 
             | 
        84 | 84 | return p2; | 
| 
             | 
        85 | 85 | }); | 
| 
             | 
        86 | 86 | |
| 
             | 
        87 | 87 | p1.Resolve(100); | 
| 
             | 
        88 | 88 | |
| 
             | 
        89 | 89 | Assert.AreEqual(p3.Join(), "100"); | 
| 
             | 
        90 | 90 | } | 
| 
             | 
        91 | 91 | |
| 
             | 
        92 | 92 | [TestMethod] | 
| 
             | 
        93 | 93 | public void PoolTest() { | 
| 
             | 
        94 | 94 | var pid = Thread.CurrentThread.ManagedThreadId; | 
| 
             | 
        95 | 95 | var p = AsyncPool.Invoke(() => Thread.CurrentThread.ManagedThreadId); | 
| 
             | 
        96 | 96 | |
| 
             | 
        97 | 97 | Assert.AreNotEqual(pid, p.Join()); | 
| 
             | 
        98 | 98 | } | 
| 
             | 
        99 | 99 | |
| 
             | 
        100 | 100 | [TestMethod] | 
| 
             | 
        101 | 101 | public void WorkerPoolSizeTest() { | 
| 
             | 
        102 | 102 | var pool = new WorkerPool(5, 10, 0); | 
| 
             | 
        103 | 103 | |
| 
             | 
        104 | 104 | Assert.AreEqual(5, pool.PoolSize); | 
| 
             | 
        105 | 105 | |
| 
             | 
        106 | pool.Invoke(() => { Thread.Sleep(1000000); return 10; }); | |
| 
             | 
        107 | pool.Invoke(() => { Thread.Sleep(1000000); return 10; }); | |
| 
             | 
        108 | pool.Invoke(() => { Thread.Sleep(1000000); return 10; }); | |
| 
             | 
        106 | pool.Invoke(() => { Thread.Sleep(100000000); return 10; }); | |
| 
             | 
        107 | pool.Invoke(() => { Thread.Sleep(100000000); return 10; }); | |
| 
             | 
        108 | pool.Invoke(() => { Thread.Sleep(100000000); return 10; }); | |
| 
             | 
        109 | 109 | |
| 
             | 
        110 | 110 | Assert.AreEqual(5, pool.PoolSize); | 
| 
             | 
        111 | 111 | |
| 
             | 
        112 | 112 | for (int i = 0; i < 100; i++) | 
| 
             | 
        113 | pool.Invoke(() => { Thread.Sleep(1000000); return 10; }); | |
| 
             | 
        114 | 
            
                         Thread.Sleep( | 
    |
| 
             | 
        113 | pool.Invoke(() => { Thread.Sleep(100000000); return 10; }); | |
| 
             | 
        114 | Thread.Sleep(200); | |
| 
             | 
        115 | 115 | Assert.AreEqual(10, pool.PoolSize); | 
| 
             | 
        116 | 116 | |
| 
             | 
        117 | 117 | pool.Dispose(); | 
| 
             | 
        118 | 118 | } | 
| 
             | 
        119 | 119 | |
| 
             | 
        120 | 120 | [TestMethod] | 
| 
             | 
        121 | 121 | public void WorkerPoolCorrectTest() { | 
| 
             | 
        122 | 122 | var pool = new WorkerPool(0,1000,100); | 
| 
             | 
        123 | 123 | |
| 
             | 
        124 | 124 | int iterations = 1000; | 
| 
             | 
        125 | 125 | int pending = iterations; | 
| 
             | 
        126 | 126 | var stop = new ManualResetEvent(false); | 
| 
             | 
        127 | 127 | |
| 
             | 
        128 | 128 | var count = 0; | 
| 
             | 
        129 | 129 | for (int i = 0; i < iterations; i++) { | 
| 
             | 
        130 | 130 | pool | 
| 
             | 
        131 | 131 | .Invoke(() => 1) | 
| 
             | 
        132 | 132 | .Then(x => Interlocked.Add(ref count, x)) | 
| 
             | 
        133 | 133 | .Then(x => Math.Log10(x)) | 
| 
             | 
        134 | 134 | .Anyway(() => { | 
| 
             | 
        135 | 135 | Interlocked.Decrement(ref pending); | 
| 
             | 
        136 | 136 | if (pending == 0) | 
| 
             | 
        137 | 137 | stop.Set(); | 
| 
             | 
        138 | 138 | }); | 
| 
             | 
        139 | 139 | } | 
| 
             | 
        140 | 140 | |
| 
             | 
        141 | 141 | stop.WaitOne(); | 
| 
             | 
        142 | 142 | |
| 
             | 
        143 | 143 | Assert.AreEqual(iterations, count); | 
| 
             | 
        144 | 144 | Console.WriteLine("Max threads: {0}", pool.MaxRunningThreads); | 
| 
             | 
        145 | 145 | pool.Dispose(); | 
| 
             | 
        146 | 146 | |
| 
             | 
        147 | 147 | } | 
| 
             | 
        148 | 148 | |
| 
             | 
        149 | 149 | [TestMethod] | 
| 
             | 
        150 | 150 | public void WorkerPoolDisposeTest() { | 
| 
             | 
        151 | 151 | var pool = new WorkerPool(5, 20); | 
| 
             | 
        152 | 152 | Assert.AreEqual(5, pool.PoolSize); | 
| 
             | 
        153 | 153 | pool.Dispose(); | 
| 
             | 
        154 | 154 | Thread.Sleep(500); | 
| 
             | 
        155 | 155 | Assert.AreEqual(0, pool.PoolSize); | 
| 
             | 
        156 | 156 | pool.Dispose(); | 
| 
             | 
        157 | 157 | } | 
| 
             | 
        158 | 158 | |
| 
             | 
        159 | 159 | [TestMethod] | 
| 
             | 
        160 | 160 | public void MTQueueTest() { | 
| 
             | 
        161 | 161 | var queue = new MTQueue<int>(); | 
| 
             | 
        162 | 162 | int res; | 
| 
             | 
        163 | 163 | |
| 
             | 
        164 | 164 | queue.Enqueue(10); | 
| 
             | 
        165 | 165 | Assert.IsTrue(queue.TryDequeue(out res)); | 
| 
             | 
        166 | 166 | Assert.AreEqual(10, res); | 
| 
             | 
        167 | 167 | Assert.IsFalse(queue.TryDequeue(out res)); | 
| 
             | 
        168 | 168 | |
| 
             | 
        169 | 169 | for (int i = 0; i < 1000; i++) | 
| 
             | 
        170 | 170 | queue.Enqueue(i); | 
| 
             | 
        171 | 171 | |
| 
             | 
        172 | 172 | for (int i = 0; i < 1000; i++) { | 
| 
             | 
        173 | 173 | queue.TryDequeue(out res); | 
| 
             | 
        174 | 174 | Assert.AreEqual(i, res); | 
| 
             | 
        175 | 175 | } | 
| 
             | 
        176 | 176 | |
| 
             | 
        177 | 177 | int writers = 0; | 
| 
             | 
        178 | 178 | int readers = 0; | 
| 
             | 
        179 | 179 | var stop = new ManualResetEvent(false); | 
| 
             | 
        180 | 180 | int total = 0; | 
| 
             | 
        181 | 181 | |
| 
             | 
        182 | 182 | int itemsPerWriter = 1000; | 
| 
             | 
        183 | 183 | int writersCount = 3; | 
| 
             | 
        184 | 184 | |
| 
             | 
        185 | 185 | for (int i = 0; i < writersCount; i++) { | 
| 
             | 
        186 | 186 | Interlocked.Increment(ref writers); | 
| 
             | 
        187 | 187 | var wn = i; | 
| 
             | 
        188 | 188 | AsyncPool | 
| 
             | 
        189 | 189 | .InvokeNewThread(() => { | 
| 
             | 
        190 | 190 | for (int ii = 0; ii < itemsPerWriter; ii++) { | 
| 
             | 
        191 | 191 | queue.Enqueue(1); | 
| 
             | 
        192 | 192 | } | 
| 
             | 
        193 | 193 | return 1; | 
| 
             | 
        194 | 194 | }) | 
| 
             | 
        195 | 195 | .Anyway(() => Interlocked.Decrement(ref writers)); | 
| 
             | 
        196 | 196 | } | 
| 
             | 
        197 | 197 | |
| 
             | 
        198 | 198 | for (int i = 0; i < 10; i++) { | 
| 
             | 
        199 | 199 | Interlocked.Increment(ref readers); | 
| 
             | 
        200 | 200 | var wn = i; | 
| 
             | 
        201 | 201 | AsyncPool | 
| 
             | 
        202 | 202 | .InvokeNewThread(() => { | 
| 
             | 
        203 | 203 | int t; | 
| 
             | 
        204 | 204 | do { | 
| 
             | 
        205 | 205 | while (queue.TryDequeue(out t)) | 
| 
             | 
        206 | 206 | Interlocked.Add(ref total, t); | 
| 
             | 
        207 | 207 | } while (writers > 0); | 
| 
             | 
        208 | 208 | return 1; | 
| 
             | 
        209 | 209 | }) | 
| 
             | 
        210 | 210 | .Anyway(() => { | 
| 
             | 
        211 | 211 | Interlocked.Decrement(ref readers); | 
| 
             | 
        212 | 212 | if (readers == 0) | 
| 
             | 
        213 | 213 | stop.Set(); | 
| 
             | 
        214 | 214 | }); | 
| 
             | 
        215 | 215 | } | 
| 
             | 
        216 | 216 | |
| 
             | 
        217 | 217 | stop.WaitOne(); | 
| 
             | 
        218 | 218 | |
| 
             | 
        219 | 219 | Assert.AreEqual(itemsPerWriter * writersCount, total); | 
| 
             | 
        220 | 220 | } | 
| 
             | 
        221 | 221 | |
| 
             | 
        222 | 222 | [TestMethod] | 
| 
             | 
        223 | 223 | public void ParallelMapTest() { | 
| 
             | 
        224 | 224 | |
| 
             | 
        225 | 225 | int count = 100000; | 
| 
             | 
        226 | 226 | |
| 
             | 
        227 | 227 | double[] args = new double[count]; | 
| 
             | 
        228 | 228 | var rand = new Random(); | 
| 
             | 
        229 | 229 | |
| 
             | 
        230 | 230 | for (int i = 0; i < count; i++) | 
| 
             | 
        231 | 231 | args[i] = rand.NextDouble(); | 
| 
             | 
        232 | 232 | |
| 
             | 
        233 | 233 | var t = Environment.TickCount; | 
| 
             | 
        234 | 234 | var res = args.ParallelMap(x => Math.Sin(x*x), 4).Join(); | 
| 
             | 
        235 | 235 | |
| 
             | 
        236 | 236 | Console.WriteLine("Map complete in {0} ms", Environment.TickCount - t); | 
| 
             | 
        237 | 237 | |
| 
             | 
        238 | 238 | t = Environment.TickCount; | 
| 
             | 
        239 | 239 | for (int i = 0; i < count; i++) | 
| 
             | 
        240 | 240 | Assert.AreEqual(Math.Sin(args[i] * args[i]), res[i]); | 
| 
             | 
        241 | 241 | Console.WriteLine("Verified in {0} ms", Environment.TickCount - t); | 
| 
             | 
        242 | 242 | } | 
| 
             | 
        243 | 243 | |
| 
             | 
        244 | 244 | [TestMethod] | 
| 
             | 
        245 | 245 | public void ChainedMapTest() { | 
| 
             | 
        246 | 246 | |
| 
             | 
        247 | 
            
                         using (var pool = new WorkerPool(0 | 
    |
| 
             | 
        247 | using (var pool = new WorkerPool(4,4,0)) { | |
| 
             | 
        248 | 248 | int count = 10000; | 
| 
             | 
        249 | 249 | |
| 
             | 
        250 | 250 | double[] args = new double[count]; | 
| 
             | 
        251 | 251 | var rand = new Random(); | 
| 
             | 
        252 | 252 | |
| 
             | 
        253 | 253 | for (int i = 0; i < count; i++) | 
| 
             | 
        254 | 254 | args[i] = rand.NextDouble(); | 
| 
             | 
        255 | 255 | |
| 
             | 
        256 | 256 | var t = Environment.TickCount; | 
| 
             | 
        257 | 257 | var res = args | 
| 
             | 
        258 | 258 | .ChainedMap( | 
| 
             | 
        259 | 259 | x => pool.Invoke( | 
| 
             | 
        260 | 260 | () => Math.Sin(x * x) | 
| 
             | 
        261 | 261 | ), | 
| 
             | 
        262 | 262 | 4 | 
| 
             | 
        263 | 263 | ) | 
| 
             | 
        264 | 264 | .Join(); | 
| 
             | 
        265 | 265 | |
| 
             | 
        266 | 266 | Console.WriteLine("Map complete in {0} ms", Environment.TickCount - t); | 
| 
             | 
        267 | 267 | |
| 
             | 
        268 | 268 | t = Environment.TickCount; | 
| 
             | 
        269 | 269 | for (int i = 0; i < count; i++) | 
| 
             | 
        270 | 270 | Assert.AreEqual(Math.Sin(args[i] * args[i]), res[i]); | 
| 
             | 
        271 | 271 | Console.WriteLine("Verified in {0} ms", Environment.TickCount - t); | 
| 
             | 
        272 | 272 | Console.WriteLine("Max workers: {0}", pool.MaxRunningThreads); | 
| 
             | 
        273 | 273 | } | 
| 
             | 
        274 | 274 | } | 
| 
             | 
        275 | 275 | |
| 
             | 
        276 | 276 | [TestMethod] | 
| 
             | 
        277 | 277 | public void ParallelForEachTest() { | 
| 
             | 
        278 | 278 | |
| 
             | 
        279 | 279 | int count = 100000; | 
| 
             | 
        280 | 280 | |
| 
             | 
        281 | 281 | int[] args = new int[count]; | 
| 
             | 
        282 | 282 | var rand = new Random(); | 
| 
             | 
        283 | 283 | |
| 
             | 
        284 | 284 | for (int i = 0; i < count; i++) | 
| 
             | 
        285 | 285 | args[i] = (int)(rand.NextDouble() * 100); | 
| 
             | 
        286 | 286 | |
| 
             | 
        287 | 287 | int result = 0; | 
| 
             | 
        288 | 288 | |
| 
             | 
        289 | 289 | var t = Environment.TickCount; | 
| 
             | 
        290 | 290 | args.ParallelForEach(x => Interlocked.Add(ref result, x), 4).Join(); | 
| 
             | 
        291 | 291 | |
| 
             | 
        292 | 292 | Console.WriteLine("Iteration complete in {0} ms, result: {1}", Environment.TickCount - t, result); | 
| 
             | 
        293 | 293 | |
| 
             | 
        294 | 294 | int result2 = 0; | 
| 
             | 
        295 | 295 | |
| 
             | 
        296 | 296 | t = Environment.TickCount; | 
| 
             | 
        297 | 297 | for (int i = 0; i < count; i++) | 
| 
             | 
        298 | 298 | result2 += args[i]; | 
| 
             | 
        299 | 299 | Assert.AreEqual(result2, result); | 
| 
             | 
        300 | 300 | Console.WriteLine("Verified in {0} ms", Environment.TickCount - t); | 
| 
             | 
        301 | 301 | } | 
| 
             | 
        302 | 302 | |
| 
             | 
        303 | 303 | [TestMethod] | 
| 
             | 
        304 | 304 | public void ComplexCase1Test() { | 
| 
             | 
        305 | 305 | var flags = new bool[3]; | 
| 
             | 
        306 | 306 | |
| 
             | 
        307 | 307 | // op1 (aync 200ms) => op2 (async 200ms) => op3 (sync map) | 
| 
             | 
        308 | 308 | |
| 
             | 
        309 | 309 | var p = PromiseHelper | 
| 
             | 
        310 | 310 | .Sleep(200, "Alan") | 
| 
             | 
        311 | 311 | .Cancelled(() => flags[0] = true) | 
| 
             | 
        312 | 312 | .Chain(x => | 
| 
             | 
        313 | 313 | PromiseHelper | 
| 
             | 
        314 | 314 | .Sleep(200, "Hi, " + x) | 
| 
             | 
        315 | 315 | .Map(y => y) | 
| 
             | 
        316 | 316 | .Cancelled(() => flags[1] = true) | 
| 
             | 
        317 | 317 | ) | 
| 
             | 
        318 | 318 | .Cancelled(() => flags[2] = true); | 
| 
             | 
        319 | 319 | Thread.Sleep(300); | 
| 
             | 
        320 | 320 | p.Cancel(); | 
| 
             | 
        321 | 321 | try { | 
| 
             | 
        322 | 322 | Assert.AreEqual(p.Join(), "Hi, Alan"); | 
| 
             | 
        323 | 323 | Assert.Fail("Shouldn't get here"); | 
| 
             | 
        324 | 324 | } catch (OperationCanceledException) { | 
| 
             | 
        325 | 325 | } | 
| 
             | 
        326 | 326 | |
| 
             | 
        327 | 327 | Assert.IsFalse(flags[0]); | 
| 
             | 
        328 | 328 | Assert.IsTrue(flags[1]); | 
| 
             | 
        329 | 329 | Assert.IsTrue(flags[2]); | 
| 
             | 
        330 | 330 | } | 
| 
             | 
        331 | 331 | } | 
| 
             | 
        332 | 332 | } | 
| 
             | 
        333 | 333 | |
| 
             | 
        1 | NO CONTENT: modified file, binary diff hidden | 
| @@ -1,302 +1,330 | |||
| 
             | 
        1 | 1 | using System; | 
| 
             | 
        2 | 2 | using System.Collections.Generic; | 
| 
             | 
        3 | 3 | using System.Linq; | 
| 
             | 
        4 | 4 | using System.Text; | 
| 
             | 
        5 | 5 | using System.Threading; | 
| 
             | 
        6 | 6 | using System.Diagnostics; | 
| 
             | 
        7 | 7 | |
| 
             | 
        8 | 8 | namespace Implab.Parallels { | 
| 
             | 
        9 | 9 | public abstract class DispatchPool<TUnit> : IDisposable { | 
| 
             | 
        10 | 10 | readonly int m_minThreads; | 
| 
             | 
        11 | 11 | readonly int m_maxThreads; | 
| 
             | 
        12 | 12 | |
| 
             | 
        13 | 13 | int m_createdThreads = 0; // the current size of the pool | 
| 
             | 
        14 | 14 | int m_activeThreads = 0; // the count of threads which are active | 
| 
             | 
        15 | 15 | int m_sleepingThreads = 0; // the count of currently inactive threads | 
| 
             | 
        16 | 16 | int m_maxRunningThreads = 0; // the meximum reached size of the pool | 
| 
             | 
        17 | 17 | int m_exitRequired = 0; // the pool is going to shutdown, all unused workers are released | 
| 
             | 
        18 | 18 | int m_releaseTimeout = 100; // the timeout while the working thread will wait for the new tasks before exit | 
| 
             | 
        19 | 19 | int m_wakeEvents = 0; // the count of wake events | 
| 
             | 
        20 | 20 | |
| 
             | 
        21 | 21 | AutoResetEvent m_hasTasks = new AutoResetEvent(false); | 
| 
             | 
        22 | 22 | |
| 
             | 
        23 | 23 | protected DispatchPool(int min, int max) { | 
| 
             | 
        24 | 24 | if (min < 0) | 
| 
             | 
        25 | 25 | throw new ArgumentOutOfRangeException("min"); | 
| 
             | 
        26 | 26 | if (max <= 0) | 
| 
             | 
        27 | 27 | throw new ArgumentOutOfRangeException("max"); | 
| 
             | 
        28 | 28 | |
| 
             | 
        29 | 29 | if (min > max) | 
| 
             | 
        30 | 30 | min = max; | 
| 
             | 
        31 | 31 | m_minThreads = min; | 
| 
             | 
        32 | 32 | m_maxThreads = max; | 
| 
             | 
        33 | 33 | } | 
| 
             | 
        34 | 34 | |
| 
             | 
        35 | 35 | protected DispatchPool(int threads) | 
| 
             | 
        36 | 36 | : this(threads, threads) { | 
| 
             | 
        37 | 37 | } | 
| 
             | 
        38 | 38 | |
| 
             | 
        39 | 39 | protected DispatchPool() { | 
| 
             | 
        40 | 40 | int maxThreads, maxCP; | 
| 
             | 
        41 | 41 | ThreadPool.GetMaxThreads(out maxThreads, out maxCP); | 
| 
             | 
        42 | 42 | |
| 
             | 
        43 | 43 | m_minThreads = 0; | 
| 
             | 
        44 | 44 | m_maxThreads = maxThreads; | 
| 
             | 
        45 | 45 | } | 
| 
             | 
        46 | 46 | |
| 
             | 
        47 | 47 | protected void InitPool() { | 
| 
             | 
        48 | 48 | for (int i = 0; i < m_minThreads; i++) | 
| 
             | 
        49 | 49 | StartWorker(); | 
| 
             | 
        50 | 50 | } | 
| 
             | 
        51 | 51 | |
| 
             | 
        52 | 52 | public int PoolSize { | 
| 
             | 
        53 | 53 | get { | 
| 
             | 
        54 | 54 | return m_createdThreads; | 
| 
             | 
        55 | 55 | } | 
| 
             | 
        56 | 56 | } | 
| 
             | 
        57 | 57 | |
| 
             | 
        58 | 58 | public int ActiveThreads { | 
| 
             | 
        59 | 59 | get { | 
| 
             | 
        60 | 60 | return m_activeThreads; | 
| 
             | 
        61 | 61 | } | 
| 
             | 
        62 | 62 | } | 
| 
             | 
        63 | 63 | |
| 
             | 
        64 | 64 | public int MaxRunningThreads { | 
| 
             | 
        65 | 65 | get { | 
| 
             | 
        66 | 66 | return m_maxRunningThreads; | 
| 
             | 
        67 | 67 | } | 
| 
             | 
        68 | 68 | } | 
| 
             | 
        69 | 69 | |
| 
             | 
        70 | 70 | protected bool IsDisposed { | 
| 
             | 
        71 | 71 | get { | 
| 
             | 
        72 | 72 | return m_exitRequired != 0; | 
| 
             | 
        73 | 73 | } | 
| 
             | 
        74 | 74 | } | 
| 
             | 
        75 | 75 | |
| 
             | 
        76 | 76 | protected abstract bool TryDequeue(out TUnit unit); | 
| 
             | 
        77 | 77 | |
| 
             | 
        78 | 78 | #region thread execution traits | 
| 
             | 
        79 | 79 | int SignalThread() { | 
| 
             | 
        80 | 80 | var signals = Interlocked.Increment(ref m_wakeEvents); | 
| 
             | 
        81 | 81 | if(signals == 1) | 
| 
             | 
        82 | 82 | m_hasTasks.Set(); | 
| 
             | 
        83 | 83 | return signals; | 
| 
             | 
        84 | 84 | } | 
| 
             | 
        85 | 85 | |
| 
             | 
        86 | bool FetchSignalOrWait(int timeout) { | |
| 
             | 
        87 | var start = Environment.TickCount; | |
| 
             | 
        88 | ||
| 
             | 
        89 | // ΠΎΠ·Π½Π°ΡΠ°Π΅Ρ, ΡΡΠΎ ΠΏΠΎΡΠΎΠΊ Π²Π»Π°Π΄Π΅Π΅Ρ Π±Π»ΠΎΠΊΠΈΡΠΎΠ²ΠΊΠΎΠΉ ΠΈ ΠΏΡΠΈ ΡΡΠΏΠ΅ΡΠ½ΠΎΠΌ ΠΏΠΎΠ»ΡΡΠ΅Π½ΠΈΠΈ ΡΠΈΠ³Π½Π°Π»Π° Π΄ΠΎΠ»ΠΆΠ΅Π½ | |
| 
             | 
        90 | // Π΅Π΅ Π²Π΅ΡΠ½ΡΡΡ, ΡΡΠΎΠ±Ρ Π΄ΡΡΠ³ΠΎΠΉ ΠΎΠΆΠΈΠ΄Π°ΡΡΠΈΠΉ ΠΏΠΎΡΠΎΠΊ ΡΠΌΠΎΠ³ | |
| 
             | 
        91 | bool hasLock = false; | |
| 
             | 
        92 | do { | |
| 
             | 
        93 | int signals; | |
| 
             | 
        94 | do { | |
| 
             | 
        95 | signals = m_wakeEvents; | |
| 
             | 
        96 | if (signals == 0) | |
| 
             | 
        97 | break; | |
| 
             | 
        98 | } while (Interlocked.CompareExchange(ref m_wakeEvents, signals - 1, signals) != signals); | |
| 
             | 
        99 | ||
| 
             | 
        100 | if (signals >= 1) { | |
| 
             | 
        101 | if (signals > 1 && hasLock) | |
| 
             | 
        102 | m_hasTasks.Set(); | |
| 
             | 
        103 | return true; | |
| 
             | 
        104 | } | |
| 
             | 
        105 | ||
| 
             | 
        106 | if (timeout != -1) | |
| 
             | 
        107 | timeout = Math.Max(0, timeout - (Environment.TickCount - start)); | |
| 
             | 
        108 | ||
| 
             | 
        109 | // Π΅ΡΠ»ΠΈ ΡΠΈΠ³Π½Π°Π»ΠΎΠ² Π±ΠΎΠ»ΡΡΠ΅ Π½Π΅ ΠΎΡΡΠ°Π»ΠΎΡΡ, ΡΠΎ ΠΏΠ΅ΡΠ²ΡΠΉ ΠΏΠΎΡΠΎΠΊ, ΠΊΠΎΡΠΎΡΡΠΉ Π΄ΠΎΡΠ΅Π» ΡΡΠ΄Π° ΡΠ±ΡΠΎΡΠΈΡ ΡΠΎΠ±ΡΡΠΈΠ΅ | |
| 
             | 
        110 | // ΠΈ ΡΠΉΠ΄Π΅Ρ Π½Π° ΠΏΡΡΡΠΎΠΉ ΡΠΈΠΊΠ», ΠΏΠΎΡΠ»Π΅ ΡΠ΅Π³ΠΎ Π·Π°Π±Π»ΠΎΠΊΠΈΡΡΠ΅ΡΡΡ | |
| 
             | 
        111 | ||
| 
             | 
        112 | hasLock = true; | |
| 
             | 
        113 | } while (m_hasTasks.WaitOne(timeout)); | |
| 
             | 
        114 | ||
| 
             | 
        115 | return false; | |
| 
             | 
        116 | } | |
| 
             | 
        117 | ||
| 
             | 
        86 | 118 | bool Sleep(int timeout) { | 
| 
             | 
        87 | 119 | Interlocked.Increment(ref m_sleepingThreads); | 
| 
             | 
        88 | 
            
                         if ( | 
    |
| 
             | 
        89 | // this is autoreset event, only one thread can run this block simultaneously | |
| 
             | 
        90 | var sleeping = Interlocked.Decrement(ref m_sleepingThreads); | |
| 
             | 
        91 | if (Interlocked.Decrement(ref m_wakeEvents) > 0) | |
| 
             | 
        92 | m_hasTasks.Set(); // wake next worker | |
| 
             | 
        93 | ||
| 
             | 
        120 | if (FetchSignalOrWait(timeout)) { | |
| 
             | 
        121 | Interlocked.Decrement(ref m_sleepingThreads); | |
| 
             | 
        94 | 122 | return true; | 
| 
             | 
        95 | 123 | } else { | 
| 
             | 
        96 | 124 | Interlocked.Decrement(ref m_sleepingThreads); | 
| 
             | 
        97 | 125 | return false; | 
| 
             | 
        98 | 126 | } | 
| 
             | 
        99 | 127 | } | 
| 
             | 
        100 | 128 | #endregion | 
| 
             | 
        101 | 129 | |
| 
             | 
        102 | 130 | /// <summary> | 
| 
             | 
        103 | 131 | /// ΠΠ°ΠΏΡΡΠΊΠ°Π΅Ρ Π»ΠΈΠ±ΠΎ Π½ΠΎΠ²ΡΠΉ ΠΏΠΎΡΠΎΠΊ, Π΅ΡΠ»ΠΈ ΡΠ°Π½ΡΡΠ΅ Π½Π΅ Π±ΡΠ»ΠΎ Π½ΠΈ ΠΎΠ΄Π½ΠΎΠ³ΠΎ ΠΏΠΎΡΠΎΠΊΠ°, Π»ΠΈΠ±ΠΎ ΡΡΡΠ°Π½Π°Π²Π»ΠΈΠ²Π°Π΅Ρ ΡΠΎΠ±ΡΡΠΈΠ΅ ΠΏΡΠΎΠ±ΡΠΆΠ΄Π΅Π½ΠΈΠ΅ ΠΎΠ΄Π½ΠΎΠ³ΠΎ ΡΠΏΡΡΠ΅Π³ΠΎ ΠΏΠΎΡΠΎΠΊΠ° | 
| 
             | 
        104 | 132 | /// </summary> | 
| 
             | 
        105 | 133 | protected void GrowPool() { | 
| 
             | 
        106 | 134 | if (m_exitRequired != 0) | 
| 
             | 
        107 | 135 | return; | 
| 
             | 
        108 | 136 | if (m_sleepingThreads > m_wakeEvents) { | 
| 
             | 
        137 | //Console.WriteLine("Waking threads (sleeps {0}, pending {1})", m_sleepingThreads, m_wakeEvents); | |
| 
             | 
        138 | ||
| 
             | 
        109 | 139 | // all sleeping threads may gone | 
| 
             | 
        110 | 140 | SignalThread(); // wake a sleeping thread; | 
| 
             | 
        111 | 141 | |
| 
             | 
        112 | 142 | // we can't check whether signal has been processed | 
| 
             | 
        113 | 143 | // anyway it may take some time for the thread to start | 
| 
             | 
        114 | 144 | // we will ensure that at least one thread is running | 
| 
             | 
        115 | 145 | |
| 
             | 
        116 | 146 | if (AllocateThreadSlot(1)) { | 
| 
             | 
        117 | 147 | // if there were no threads in the pool | 
| 
             | 
        118 | 148 | var worker = new Thread(this.Worker); | 
| 
             | 
        119 | 149 | worker.IsBackground = true; | 
| 
             | 
        120 | 150 | worker.Start(); | 
| 
             | 
        121 | 151 | } | 
| 
             | 
        122 | 152 | } else { | 
| 
             | 
        123 | 153 | // if there is no sleeping threads in the pool | 
| 
             | 
        124 | 154 | StartWorker(); | 
| 
             | 
        125 | 155 | } | 
| 
             | 
        126 | 156 | } | 
| 
             | 
        127 | 157 | |
| 
             | 
        128 | 158 | private bool Suspend() { | 
| 
             | 
        129 | 159 | //no tasks left, exit if the thread is no longer needed | 
| 
             | 
        130 | 160 | bool last; | 
| 
             | 
        131 | 161 | bool requestExit; | 
| 
             | 
        132 | 162 | |
| 
             | 
        133 | ||
| 
             | 
        134 | ||
| 
             | 
        135 | 163 | // if threads have a timeout before releasing | 
| 
             | 
        136 | 164 | if (m_releaseTimeout > 0) | 
| 
             | 
        137 | 165 | requestExit = !Sleep(m_releaseTimeout); | 
| 
             | 
        138 | 166 | else | 
| 
             | 
        139 | 167 | requestExit = true; | 
| 
             | 
        140 | 168 | |
| 
             | 
        141 | 169 | if (!requestExit) | 
| 
             | 
        142 | 170 | return true; | 
| 
             | 
        143 | 171 | |
| 
             | 
        144 | 172 | // release unsused thread | 
| 
             | 
        145 | 173 | if (requestExit && ReleaseThreadSlot(out last)) { | 
| 
             | 
        146 | 174 | // in case at the moment the last thread was being released | 
| 
             | 
        147 | 175 | // a new task was added to the queue, we need to try | 
| 
             | 
        148 | 176 | // to revoke the thread to avoid the situation when the task is left unprocessed | 
| 
             | 
        149 | 177 | if (last && Sleep(0)) { // Sleep(0) will fetch pending task or will return false | 
| 
             | 
        150 | 178 | if (AllocateThreadSlot(1)) | 
| 
             | 
        151 | 179 | return true; // spin again... | 
| 
             | 
        152 | 180 | else | 
| 
             | 
        153 | 181 | SignalThread(); // since Sleep(0) has fetched the signal we neet to reschedule it | 
| 
             | 
        154 | 182 | |
| 
             | 
        155 | 183 | } | 
| 
             | 
        156 | 184 | |
| 
             | 
        157 | 185 | return false; | 
| 
             | 
        158 | 186 | } | 
| 
             | 
        159 | 187 | |
| 
             | 
        160 | 188 | // wait till infinity | 
| 
             | 
        161 | 189 | Sleep(-1); | 
| 
             | 
        162 | 190 | |
| 
             | 
        163 | 191 | return true; | 
| 
             | 
        164 | 192 | } | 
| 
             | 
        165 | 193 | |
| 
             | 
        166 | 194 | #region thread slots traits | 
| 
             | 
        167 | 195 | |
| 
             | 
        168 | 196 | bool AllocateThreadSlot() { | 
| 
             | 
        169 | 197 | int current; | 
| 
             | 
        170 | 198 | // use spins to allocate slot for the new thread | 
| 
             | 
        171 | 199 | do { | 
| 
             | 
        172 | 200 | current = m_createdThreads; | 
| 
             | 
        173 | 201 | if (current >= m_maxThreads || m_exitRequired != 0) | 
| 
             | 
        174 | 202 | // no more slots left or the pool has been disposed | 
| 
             | 
        175 | 203 | return false; | 
| 
             | 
        176 | 204 | } while (current != Interlocked.CompareExchange(ref m_createdThreads, current + 1, current)); | 
| 
             | 
        177 | 205 | |
| 
             | 
        178 | 206 | UpdateMaxThreads(current + 1); | 
| 
             | 
        179 | 207 | |
| 
             | 
        180 | 208 | return true; | 
| 
             | 
        181 | 209 | } | 
| 
             | 
        182 | 210 | |
| 
             | 
        183 | 211 | bool AllocateThreadSlot(int desired) { | 
| 
             | 
        184 | 212 | if (desired - 1 != Interlocked.CompareExchange(ref m_createdThreads, desired, desired - 1)) | 
| 
             | 
        185 | 213 | return false; | 
| 
             | 
        186 | 214 | |
| 
             | 
        187 | 215 | UpdateMaxThreads(desired); | 
| 
             | 
        188 | 216 | |
| 
             | 
        189 | 217 | return true; | 
| 
             | 
        190 | 218 | } | 
| 
             | 
        191 | 219 | |
| 
             | 
        192 | 220 | bool ReleaseThreadSlot(out bool last) { | 
| 
             | 
        193 | 221 | last = false; | 
| 
             | 
        194 | 222 | int current; | 
| 
             | 
        195 | 223 | // use spins to release slot for the new thread | 
| 
             | 
        196 | 224 | do { | 
| 
             | 
        197 | 225 | current = m_createdThreads; | 
| 
             | 
        198 | 226 | if (current <= m_minThreads && m_exitRequired == 0) | 
| 
             | 
        199 | 227 | // the thread is reserved | 
| 
             | 
        200 | 228 | return false; | 
| 
             | 
        201 | 229 | } while (current != Interlocked.CompareExchange(ref m_createdThreads, current - 1, current)); | 
| 
             | 
        202 | 230 | |
| 
             | 
        203 | 231 | last = (current == 1); | 
| 
             | 
        204 | 232 | |
| 
             | 
        205 | 233 | return true; | 
| 
             | 
        206 | 234 | } | 
| 
             | 
        207 | 235 | |
| 
             | 
        208 | 236 | /// <summary> | 
| 
             | 
        209 | 237 | /// releases thread slot unconditionally, used during cleanup | 
| 
             | 
        210 | 238 | /// </summary> | 
| 
             | 
        211 | 239 | /// <returns>true - no more threads left</returns> | 
| 
             | 
        212 | 240 | bool ReleaseThreadSlotAnyway() { | 
| 
             | 
        213 | 241 | var left = Interlocked.Decrement(ref m_createdThreads); | 
| 
             | 
        214 | 242 | return left == 0; | 
| 
             | 
        215 | 243 | } | 
| 
             | 
        216 | 244 | |
| 
             | 
        217 | 245 | void UpdateMaxThreads(int count) { | 
| 
             | 
        218 | 246 | int max; | 
| 
             | 
        219 | 247 | do { | 
| 
             | 
        220 | 248 | max = m_maxRunningThreads; | 
| 
             | 
        221 | 249 | if (max >= count) | 
| 
             | 
        222 | 250 | break; | 
| 
             | 
        223 | 251 | } while(max != Interlocked.CompareExchange(ref m_maxRunningThreads, count, max)); | 
| 
             | 
        224 | 252 | } | 
| 
             | 
        225 | 253 | |
| 
             | 
        226 | 254 | #endregion | 
| 
             | 
        227 | 255 | |
| 
             | 
        228 | 256 | bool StartWorker() { | 
| 
             | 
        229 | 257 | if (AllocateThreadSlot()) { | 
| 
             | 
        230 | 258 | // slot successfully allocated | 
| 
             | 
        231 | 259 | var worker = new Thread(this.Worker); | 
| 
             | 
        232 | 260 | worker.IsBackground = true; | 
| 
             | 
        233 | 261 | worker.Start(); | 
| 
             | 
        234 | 262 | |
| 
             | 
        235 | 263 | return true; | 
| 
             | 
        236 | 264 | } else { | 
| 
             | 
        237 | 265 | return false; | 
| 
             | 
        238 | 266 | } | 
| 
             | 
        239 | 267 | } | 
| 
             | 
        240 | 268 | |
| 
             | 
        241 | 269 | protected abstract void InvokeUnit(TUnit unit); | 
| 
             | 
        242 | 270 | |
| 
             | 
        243 | 271 | void Worker() { | 
| 
             | 
        244 | 272 | TUnit unit; | 
| 
             | 
        273 | //Console.WriteLine("{0}: Active", Thread.CurrentThread.ManagedThreadId); | |
| 
             | 
        245 | 274 | Interlocked.Increment(ref m_activeThreads); | 
| 
             | 
        246 | Sleep(0); // remove wake request if the new thread is started | |
| 
             | 
        247 | 275 | do { | 
| 
             | 
        248 | 276 | // exit if requested | 
| 
             | 
        249 | 277 | if (m_exitRequired != 0) { | 
| 
             | 
        250 | 278 | // release the thread slot | 
| 
             | 
        251 | 279 | Interlocked.Decrement(ref m_activeThreads); | 
| 
             | 
        252 | 280 | if (ReleaseThreadSlotAnyway()) // it was the last worker | 
| 
             | 
        253 | 281 | m_hasTasks.Dispose(); | 
| 
             | 
        254 | 282 | else | 
| 
             | 
        255 | 283 | SignalThread(); // wake next worker | 
| 
             | 
        256 | 284 | unit = default(TUnit); | 
| 
             | 
        257 | 285 | break; | 
| 
             | 
        258 | 286 | } | 
| 
             | 
        259 | 287 | |
| 
             | 
        260 | 288 | // fetch task | 
| 
             | 
        261 | 289 | if (TryDequeue(out unit)) { | 
| 
             | 
        262 | 290 | InvokeUnit(unit); | 
| 
             | 
        263 | 291 | continue; | 
| 
             | 
        264 | 292 | } | 
| 
             | 
        265 | 293 | |
| 
             | 
        266 | 294 | Interlocked.Decrement(ref m_activeThreads); | 
| 
             | 
        267 | 295 | |
| 
             | 
        268 | 296 | // entering suspend state | 
| 
             | 
        269 | 297 | // keep this thread and wait | 
| 
             | 
        270 | 298 | if (!Suspend()) | 
| 
             | 
        271 | 299 | break; | 
| 
             | 
        272 | ||
| 
             | 
        300 | //Console.WriteLine("{0}: Awake", Thread.CurrentThread.ManagedThreadId); | |
| 
             | 
        273 | 301 | Interlocked.Increment(ref m_activeThreads); | 
| 
             | 
        274 | 302 | } while (true); | 
| 
             | 
        275 | ||
| 
             | 
        303 | //Console.WriteLine("{0}: Exited", Thread.CurrentThread.ManagedThreadId); | |
| 
             | 
        276 | 304 | } | 
| 
             | 
        277 | 305 | |
| 
             | 
        278 | 306 | protected virtual void Dispose(bool disposing) { | 
| 
             | 
        279 | 307 | if (disposing) { | 
| 
             | 
        280 | 308 | if (m_exitRequired == 0) { | 
| 
             | 
        281 | 309 | if (Interlocked.CompareExchange(ref m_exitRequired, 1, 0) != 0) | 
| 
             | 
        282 | 310 | return; | 
| 
             | 
        283 | 311 | |
| 
             | 
        284 | 312 | // wake sleeping threads | 
| 
             | 
        285 | 313 | if (m_createdThreads > 0) | 
| 
             | 
        286 | 314 | SignalThread(); | 
| 
             | 
        287 | 315 | else | 
| 
             | 
        288 | 316 | m_hasTasks.Dispose(); | 
| 
             | 
        289 | 317 | GC.SuppressFinalize(this); | 
| 
             | 
        290 | 318 | } | 
| 
             | 
        291 | 319 | } | 
| 
             | 
        292 | 320 | } | 
| 
             | 
        293 | 321 | |
| 
             | 
        294 | 322 | public void Dispose() { | 
| 
             | 
        295 | 323 | Dispose(true); | 
| 
             | 
        296 | 324 | } | 
| 
             | 
        297 | 325 | |
| 
             | 
        298 | 326 | ~DispatchPool() { | 
| 
             | 
        299 | 327 | Dispose(false); | 
| 
             | 
        300 | 328 | } | 
| 
             | 
        301 | 329 | } | 
| 
             | 
        302 | 330 | } | 
        
        General Comments 0
    
    
  
  
                      You need to be logged in to leave comments.
                      Login now
                    
                