3c1f479b9d
Former-commit-id: 806294f5ded97629b74c85c09952f2a74fe182d9
51 lines
1.2 KiB
C#
51 lines
1.2 KiB
C#
namespace System.Web.Mvc.Async {
|
|
using System;
|
|
using System.Threading;
|
|
|
|
public sealed class OperationCounter {
|
|
|
|
private int _count;
|
|
|
|
public int Count {
|
|
get {
|
|
return Volatile.Read(ref _count);
|
|
}
|
|
}
|
|
|
|
public event EventHandler Completed;
|
|
|
|
private int AddAndExecuteCallbackIfCompleted(int value) {
|
|
int newCount = Interlocked.Add(ref _count, value);
|
|
if (newCount == 0) {
|
|
OnCompleted();
|
|
}
|
|
|
|
return newCount;
|
|
}
|
|
|
|
public int Decrement() {
|
|
return AddAndExecuteCallbackIfCompleted(-1);
|
|
}
|
|
|
|
public int Decrement(int value) {
|
|
return AddAndExecuteCallbackIfCompleted(-value);
|
|
}
|
|
|
|
public int Increment() {
|
|
return AddAndExecuteCallbackIfCompleted(1);
|
|
}
|
|
|
|
public int Increment(int value) {
|
|
return AddAndExecuteCallbackIfCompleted(value);
|
|
}
|
|
|
|
private void OnCompleted() {
|
|
EventHandler handler = Completed;
|
|
if (handler != null) {
|
|
handler(this, EventArgs.Empty);
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|