2010-04-19 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mono / tests / interlocked.cs
blob3fe5ea57b1c4be25e2a26b77907576d652758944
1 using System;
2 using System.Threading;
4 public class InterlockTest
6 public int test;
7 public int add;
8 public int rem;
10 static int s_test;
12 public static int Main() {
13 int a,b;
15 InterlockTest it = new InterlockTest ();
17 it.test = 0;
18 int c = Interlocked.Exchange (ref it.test, 1);
19 if (c != 0)
20 return 1;
22 if (it.test != 1)
23 return 2;
25 it.test = -2;
26 c = Interlocked.CompareExchange (ref it.test, 1, -2);
27 if (c != -2)
28 return 3;
30 if (it.test != 1)
31 return 4;
33 a = 1;
34 b = Interlocked.Increment (ref a);
35 if (a != 2)
36 return 5;
37 if (b != 2)
38 return 6;
40 a = 2;
41 b = Interlocked.Decrement (ref a);
42 if (b != 1)
43 return 7;
44 if (a != 1)
45 return 8;
47 string s = IncTest ();
48 if (s != "A1")
49 return 9;
51 s = IncTest ();
52 if (s != "A2")
53 return 10;
55 Thread.MemoryBarrier ();
57 Console.WriteLine ("done!");
59 return 0;
62 public static string IncTest () {
63 return "A" + Interlocked.Increment (ref s_test);