summaryrefslogtreecommitdiff
path: root/tests/src/baseservices/threading/interlocked/add/interlockedaddlongwithsubtract.cs
diff options
context:
space:
mode:
authorJiyoung Yun <jy910.yun@samsung.com>2016-11-23 19:09:09 +0900
committerJiyoung Yun <jy910.yun@samsung.com>2016-11-23 19:09:09 +0900
commit4b4aad7217d3292650e77eec2cf4c198ea9c3b4b (patch)
tree98110734c91668dfdbb126fcc0e15ddbd93738ca /tests/src/baseservices/threading/interlocked/add/interlockedaddlongwithsubtract.cs
parentfa45f57ed55137c75ac870356a1b8f76c84b229c (diff)
downloadcoreclr-4b4aad7217d3292650e77eec2cf4c198ea9c3b4b.tar.gz
coreclr-4b4aad7217d3292650e77eec2cf4c198ea9c3b4b.tar.bz2
coreclr-4b4aad7217d3292650e77eec2cf4c198ea9c3b4b.zip
Imported Upstream version 1.1.0upstream/1.1.0
Diffstat (limited to 'tests/src/baseservices/threading/interlocked/add/interlockedaddlongwithsubtract.cs')
-rw-r--r--tests/src/baseservices/threading/interlocked/add/interlockedaddlongwithsubtract.cs106
1 files changed, 106 insertions, 0 deletions
diff --git a/tests/src/baseservices/threading/interlocked/add/interlockedaddlongwithsubtract.cs b/tests/src/baseservices/threading/interlocked/add/interlockedaddlongwithsubtract.cs
new file mode 100644
index 0000000000..bc641afdb1
--- /dev/null
+++ b/tests/src/baseservices/threading/interlocked/add/interlockedaddlongwithsubtract.cs
@@ -0,0 +1,106 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+using System;
+using System.Threading;
+//Regression for DevDiv Bugs 48020
+namespace ExchangeAdd
+{
+ class InterlockedAddInt
+ {
+ static int Main(string[] args)
+ {
+ // Check number of args
+ if(args.Length != 2)
+ {
+ Console.WriteLine("USAGE: InterlockedAddLongWithSubtract " +
+ "/loops:<int> /addVal:<int>");
+ return -1;
+ }
+
+ // Get the args
+ int loops=100;
+ long valueToAdd = 0;
+
+ for(int i=0;i<args.Length;i++)
+ {
+ if(args[i].ToLower().StartsWith("/loops:"))
+ {
+ loops = Convert.ToInt32(args[i].Substring(7));
+ continue;
+ }
+
+ if(args[i].ToLower().StartsWith("/addval:"))
+ {
+ valueToAdd = Convert.ToInt32(args[i].Substring(8));
+ continue;
+ }
+ }
+
+ int rValue = 0;
+ Thread[] threads = new Thread[100];
+ ThreadSafe tsi = new ThreadSafe(loops, valueToAdd);
+ for (int i = 0; i < threads.Length; i++)
+ {
+ threads[i] = new Thread(new ThreadStart(tsi.ThreadWorker));
+ threads[i].Start();
+ }
+
+ tsi.Signal();
+
+ for (int i = 0; i < threads.Length; i++)
+ threads[i].Join();
+
+ if (tsi.Total == tsi.Expected * threads.Length)
+ rValue = 100;
+ Console.WriteLine("Expected: " + (tsi.Expected * threads.Length));
+ Console.WriteLine("Actual: " + tsi.Total);
+ Console.WriteLine("Test {0}", rValue == 100 ? "Passed" : "Failed");
+ return rValue;
+ }
+ }
+
+ public class ThreadSafe
+ {
+ ManualResetEvent signal;
+ private long totalValue = 0;
+ private int numberOfIterations;
+ private long valueToAdd;
+ private long valueToSubtract;
+ public ThreadSafe(): this(100,100) { }
+ public ThreadSafe(int loops, long iAdd)
+ {
+ signal = new ManualResetEvent(false);
+ numberOfIterations = loops;
+ valueToAdd = iAdd;
+ valueToSubtract = 0-iAdd;
+ }
+
+ public void Signal()
+ {
+ signal.Set();
+ }
+
+ public void ThreadWorker()
+ {
+ signal.WaitOne();
+ for (int i = 0; i < numberOfIterations; i++)
+ {
+ Interlocked.Add(ref totalValue, valueToAdd);
+ Interlocked.Add(ref totalValue, valueToSubtract);
+ }
+
+ }
+ public long Expected
+ {
+ get
+ {
+ return (0);
+ }
+ }
+ public long Total
+ {
+ get { return totalValue; }
+ }
+ }
+} \ No newline at end of file