Index: third_party/grpc/src/csharp/Grpc.Core/Internal/AtomicCounter.cs |
diff --git a/third_party/protobuf/csharp/src/Google.Protobuf/Reflection/PackageDescriptor.cs b/third_party/grpc/src/csharp/Grpc.Core/Internal/AtomicCounter.cs |
similarity index 59% |
copy from third_party/protobuf/csharp/src/Google.Protobuf/Reflection/PackageDescriptor.cs |
copy to third_party/grpc/src/csharp/Grpc.Core/Internal/AtomicCounter.cs |
index e547d834986de8d80703504469e9c39b3d8ae93d..63bea44e0e98161f9c51845dfb02dd1fb653f23a 100644 |
--- a/third_party/protobuf/csharp/src/Google.Protobuf/Reflection/PackageDescriptor.cs |
+++ b/third_party/grpc/src/csharp/Grpc.Core/Internal/AtomicCounter.cs |
@@ -1,7 +1,7 @@ |
#region Copyright notice and license |
-// Protocol Buffers - Google's data interchange format |
-// Copyright 2008 Google Inc. All rights reserved. |
-// https://developers.google.com/protocol-buffers/ |
+ |
+// Copyright 2015, Google Inc. |
+// All rights reserved. |
// |
// Redistribution and use in source and binary forms, with or without |
// modification, are permitted provided that the following conditions are |
@@ -28,41 +28,59 @@ |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
+ |
#endregion |
-namespace Google.Protobuf.Reflection |
+using System; |
+using System.Threading; |
+ |
+namespace Grpc.Core.Internal |
{ |
- /// <summary> |
- /// Represents a package in the symbol table. We use PackageDescriptors |
- /// just as placeholders so that someone cannot define, say, a message type |
- /// that has the same name as an existing package. |
- /// </summary> |
- internal sealed class PackageDescriptor : IDescriptor |
+ internal class AtomicCounter |
{ |
- private readonly string name; |
- private readonly string fullName; |
- private readonly FileDescriptor file; |
+ long counter = 0; |
+ |
+ public AtomicCounter(long initialCount = 0) |
+ { |
+ this.counter = initialCount; |
+ } |
- internal PackageDescriptor(string name, string fullName, FileDescriptor file) |
+ public long Increment() |
{ |
- this.file = file; |
- this.fullName = fullName; |
- this.name = name; |
+ return Interlocked.Increment(ref counter); |
} |
- public string Name |
+ public void IncrementIfNonzero(ref bool success) |
{ |
- get { return name; } |
+ long origValue = counter; |
+ while (true) |
+ { |
+ if (origValue == 0) |
+ { |
+ success = false; |
+ return; |
+ } |
+ long result = Interlocked.CompareExchange(ref counter, origValue + 1, origValue); |
+ if (result == origValue) |
+ { |
+ success = true; |
+ return; |
+ }; |
+ origValue = result; |
+ } |
} |
- public string FullName |
+ public long Decrement() |
{ |
- get { return fullName; } |
+ return Interlocked.Decrement(ref counter); |
} |
- public FileDescriptor File |
+ public long Count |
{ |
- get { return file; } |
+ get |
+ { |
+ return counter; |
+ } |
} |
} |
-} |
+} |