2010-03-23 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / tests / interlocked.cs
1 using System;
2 using System.Threading;
3
4 public class InterlockTest
5 {
6         public int test;
7         public int add;
8         public int rem;
9
10         static int s_test;
11         
12         public static int Main() {
13                 int a,b;
14
15                 InterlockTest it = new InterlockTest ();
16
17                 it.test = 0;
18                 int c = Interlocked.Exchange (ref it.test, 1);
19                 if (c != 0)
20                         return 1;
21
22                 if (it.test != 1)
23                         return 2;
24
25                 it.test = -2;
26                 c = Interlocked.CompareExchange (ref it.test, 1, -2);
27                 if (c != -2)
28                         return 3;
29
30                 if (it.test != 1)
31                         return 4;
32
33                 a = 1;
34                 b = Interlocked.Increment (ref a);
35                 if (a != 2)
36                         return 5;
37                 if (b != 2)
38                         return 6;
39
40                 a = 2;
41                 b = Interlocked.Decrement (ref a);
42                 if (b != 1)
43                         return 7;
44                 if (a != 1)
45                         return 8;
46
47                 string s = IncTest ();
48                 if (s != "A1")
49                         return 9;
50
51                 s = IncTest ();
52                 if (s != "A2")
53                         return 10;
54
55                 Thread.MemoryBarrier ();
56
57                 Console.WriteLine ("done!");
58
59                 return 0;
60         }
61
62         public static string IncTest () {
63                 return "A" + Interlocked.Increment (ref s_test);
64         }
65 }