1 /* ****************************************************************************
\r
3 * Copyright (c) Microsoft Corporation. All rights reserved.
\r
5 * This software is subject to the Microsoft Public License (Ms-PL).
\r
6 * A copy of the license can be found in the license.htm file included
\r
7 * in this distribution.
\r
9 * You must not remove this notice, or any other, from this software.
\r
11 * ***************************************************************************/
\r
13 namespace System.Web.Mvc.Async {
\r
15 using System.Threading;
\r
17 public sealed class OperationCounter {
\r
23 return Thread.VolatileRead(ref _count);
\r
27 public event EventHandler Completed;
\r
29 private int AddAndExecuteCallbackIfCompleted(int value) {
\r
30 int newCount = Interlocked.Add(ref _count, value);
\r
31 if (newCount == 0) {
\r
38 public int Decrement() {
\r
39 return AddAndExecuteCallbackIfCompleted(-1);
\r
42 public int Decrement(int value) {
\r
43 return AddAndExecuteCallbackIfCompleted(-value);
\r
46 public int Increment() {
\r
47 return AddAndExecuteCallbackIfCompleted(1);
\r
50 public int Increment(int value) {
\r
51 return AddAndExecuteCallbackIfCompleted(value);
\r
54 private void OnCompleted() {
\r
55 EventHandler handler = Completed;
\r
56 if (handler != null) {
\r
57 handler(this, EventArgs.Empty);
\r