summaryrefslogtreecommitdiff
path: root/tests/src/baseservices/threading/interlocked/compareexchange/compareexchangelong.cs
blob: d4e55a4867654e13d9c938584470fc0030d94ab9 (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
107
108
109
110
111
112
113
114
115
116
// 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;

namespace CompareExchangeLong
{
    class CompareExchangeLong
    {
        static int Main(string[] args)
        {
            // Check number of args
            if(args.Length != 2)
            {
                Console.WriteLine("USAGE:  CompareExchangeLong " +
                    "/loops:<int> /addVal:<long>");
                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.ToInt64(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;
        public ThreadSafe(): this(100,Int64.MaxValue) { }
        public ThreadSafe(int loops, long value)
        {
            signal = new ManualResetEvent(false);
            numberOfIterations = loops;
            valueToAdd = value;
        }

        public void Signal()
        {
            signal.Set();
        }

        public void ThreadWorker()
        {
            signal.Set();
            for(int i=0;i<numberOfIterations;i++)
                AddToTotal(valueToAdd);
        }

        public long Expected
        {
            get
            {
                return (numberOfIterations * valueToAdd);
            }
        }

        public long Total
        {
            get { return totalValue; }
        }

        private long AddToTotal(long addend)
        {
            long initialValue, computedValue;
            signal.WaitOne();
            do
            {
                initialValue = totalValue;
                computedValue = initialValue + addend;
            } 
            while (initialValue != Interlocked.CompareExchange(ref totalValue, 
                computedValue, initialValue));
            return computedValue;
        }
    }    
}