summaryrefslogtreecommitdiff
path: root/tests/src/JIT/jit64/opt/rngchk/ArrayWithThread.cs
blob: 07e8901a7fc44c13c7da00a5a1a9fa219af64c7d (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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Threading;
using System.Runtime.CompilerServices;

namespace ArrayWithThread
{
    public delegate void RngTest(ref int a);
    internal class Class1
    {
        public static int val = 0;
        public static AutoResetEvent myResetEvent = new AutoResetEvent(false);
        private static int Main()
        {
            int retVal = 100;
            int testNum = 0;
            RngTest[] Tests ={  new RngTest(Test.Test1),
                        new RngTest(Test.Test2)};
            foreach (RngTest test in Tests)
            {
                testNum++;
                if (DoTest(test))
                {
                    Console.WriteLine("Test {0} Passed", testNum);
                }
                else
                {
                    Console.WriteLine("Test {0} Failed", testNum);
                    retVal = 1;
                }
            }
            return retVal;
        }

        private static bool DoTest(RngTest Test)
        {
            bool bResult = false;
            myResetEvent.Reset();
            try
            {
                Thread t = new Thread(new ThreadStart(Class1.ThreadFunc));
                t.Start();
                Test(ref val);
                t.Join();
            }
            catch (System.IndexOutOfRangeException)
            {
                bResult = true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            return bResult;
        }
        private static void ThreadFunc()
        {
            myResetEvent.WaitOne();
            Class1.val = 101;
            return;
        }
    }
    internal class Test
    {
        [MethodImplAttribute(MethodImplOptions.NoInlining)]
        public static void Test1(ref int index)
        {
            int[] numbers = new int[100];
            for (; index < numbers.Length; index++)
            {
                Class1.myResetEvent.Set();
                Thread.Sleep(1);
                numbers[index] = index * index;
            }
        }

        [MethodImplAttribute(MethodImplOptions.NoInlining)]
        public static void Test2(ref int upper)
        {
            int[] numbers = new int[100];
            int index = 0;
            upper = numbers.Length;
            for (; index < upper; index++)
            {
                Class1.myResetEvent.Set();
                Thread.Sleep(1);
                numbers[index] = index * index;
            }
        }
    }
}