Merge pull request #249 from pcc/xgetinputfocus
[mono.git] / mcs / class / System.Web.Mvc3 / Mvc / Async / OperationCounter.cs
1 namespace System.Web.Mvc.Async {
2     using System;
3     using System.Threading;
4
5     public sealed class OperationCounter {
6
7         private int _count;
8
9         public int Count {
10             get {
11                 return Thread.VolatileRead(ref _count);
12             }
13         }
14
15         public event EventHandler Completed;
16
17         private int AddAndExecuteCallbackIfCompleted(int value) {
18             int newCount = Interlocked.Add(ref _count, value);
19             if (newCount == 0) {
20                 OnCompleted();
21             }
22
23             return newCount;
24         }
25
26         public int Decrement() {
27             return AddAndExecuteCallbackIfCompleted(-1);
28         }
29
30         public int Decrement(int value) {
31             return AddAndExecuteCallbackIfCompleted(-value);
32         }
33
34         public int Increment() {
35             return AddAndExecuteCallbackIfCompleted(1);
36         }
37
38         public int Increment(int value) {
39             return AddAndExecuteCallbackIfCompleted(value);
40         }
41
42         private void OnCompleted() {
43             EventHandler handler = Completed;
44             if (handler != null) {
45                 handler(this, EventArgs.Empty);
46             }
47         }
48
49     }
50 }