summaryrefslogtreecommitdiff
path: root/tests/src/JIT/Methodical/tailcall/Desktop/thread-race.cs
blob: 9746d34790b10a1faace2a03038b1d5bb6995db7 (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;
using System.Runtime.CompilerServices;

internal class Repro
{
    private static volatile bool s_threadsCompleted;
    private static volatile Mutex s_myMutex;
    private static volatile int[] s_threadSum;

    public static int Main()
    {
        ThreadStart ts = new ThreadStart(FibThread);
        Thread t1 = new Thread(ts);
        Thread t2 = new Thread(ts);
        Thread t3 = new Thread(ts);
        long threadValue;

        s_threadsCompleted = false;
        s_threadSum = new int[3];
        s_threadSum[0] = 0;
        s_threadSum[1] = 0;
        s_threadSum[2] = 0;
        s_myMutex = new Mutex();

        t1.Start();
        t2.Start();
        t3.Start();

        while (!s_threadsCompleted)
        {
            GC.Collect();
        }

        t1.Join();
        t2.Join();
        t3.Join();

        threadValue = (s_threadSum[0] + s_threadSum[1] + s_threadSum[2]);

        if (((long)54018518 * 3) != threadValue)
        {
            Console.WriteLine("FALSE: {0} != {1}", ((long)439201 * 3), threadValue);
            return 0;
        }
        else
        {
            Console.WriteLine("PASS");
            return 100;
        }
    }

    public static void FibThread()
    {
        int sum = 0;
        const int length = 35;

        for (int i = 0; i <= length; i++)
        {
            sum += fib(0, i);
            Console.WriteLine("" + i + ": " + sum);
        }

        s_threadsCompleted = true;

        s_myMutex.WaitOne();
        if (0 == s_threadSum[0]) s_threadSum[0] = sum;
        if (0 == s_threadSum[1]) s_threadSum[1] = sum;
        if (0 == s_threadSum[2]) s_threadSum[2] = sum;
        s_myMutex.ReleaseMutex();
    }

    public static int fib(int sum, int num)
    {
        if (num <= 2)
        {
            return simple(sum, num);
        }

        return fib(fib(sum, num - 1), num - 2);
    }

    public static int simple(int sum, int num)
    {
        if (num == 0)
        {
            return sum + 0;
        }

        if (num == 1)
        {
            return sum + 1;
        }

        if (num == 2)
        {
            return sum + 3;
        }

        return 555555;
    }
}