summaryrefslogtreecommitdiff
path: root/tests/src/baseservices/threading/interlocked/add/interlockedaddlongwithsubtract.cs
blob: bc641afdb170e36e7efe6b798f26d053ad8c7861 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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; }
        }
    }
}