Merge pull request #5406 from kumpera/fix_12157
[mono.git] / mono / tests / thread-static.cs
1 using System;
2 using System.Threading;
3 using System.Reflection;
4
5 class T {
6         [ThreadStatic]
7         static int var = 5;
8
9         static bool tfailed = true;
10
11         static void thread () {
12                 tfailed = false;
13                 if (var != 0)
14                         tfailed = true;
15                 Console.WriteLine ("start value: {0}", var);
16                 for (int i = 0; i < 10; ++i) {
17                         var += 10;
18                         Thread.Sleep (5);
19                 }
20                 Console.WriteLine ("end value: {0}", var);
21                 if (var != 100)
22                         tfailed = true;
23         }
24         
25         static int Main () {
26                 bool failed = false;
27                 var  = 10;
28                 Thread thr = new Thread (new ThreadStart (thread));
29                 thr.Start ();
30                 if (var != 10)
31                         failed = true;
32                 var = 20;
33                 Console.WriteLine ("value in main thread: {0}", var);
34                 thr.Join ();
35                 Console.WriteLine ("value in main thread after join: {0}", var);
36                 if (var != 20)
37                         failed = true;
38
39                 if (failed)
40                         return 1;
41                 if (tfailed)
42                         return 2;
43
44                 /* Test access though reflection */
45                 var = 42;
46                 FieldInfo fi = typeof (T).GetField ("var", BindingFlags.NonPublic|BindingFlags.Static);
47                 if ((int)fi.GetValue (null) != 42)
48                         return 3;
49                 fi.SetValue (null, 43);
50                 if (var != 43)
51                         return 4;
52
53                 return 0;
54         }
55 }