[2019-12] [threads] Add back mono_threads_attach_tools_thread as a public API (#18074)
[mono-project.git] / mono / tests / interlocked-2.2.cs
blobc16f700f75facc8e1f56e0643e1e9003924e3e58
1 using System;
2 using System.Threading;
4 public class InterlockTest
6 public int test;
7 public long ltest;
9 public static int Main() {
10 int a,b;
11 long la, lb;
13 InterlockTest it = new InterlockTest ();
15 /* int */
16 it.test = 2;
17 int c = Interlocked.Add (ref it.test, 1);
18 if (c != 3)
19 return 1;
21 if (it.test != 3)
22 return 2;
24 a = 1;
25 b = Interlocked.Add (ref a, 1);
26 if (a != 2)
27 return 3;
28 if (b != 2)
29 return 4;
31 /* long */
32 it.ltest = 2;
33 long lc = Interlocked.Add (ref it.ltest, 1);
34 if (lc != 3)
35 return 5;
37 if (it.ltest != 3)
38 return 6;
40 la = 1;
41 lb = Interlocked.Add (ref la, 1);
42 if (la != 2)
43 return 7;
44 if (lb != 2)
45 return 8;
47 if (Interlocked.Read (ref la) != 2)
48 return 9;
50 la = 1;
51 lc = Interlocked.Exchange (ref la, 2);
52 if (lc != 1)
53 return 10;
55 if (la != 2)
56 return 11;
58 /* Generics */
59 InterlockTest o1 = new InterlockTest ();
60 InterlockTest o2 = new InterlockTest ();
61 InterlockTest o = o1;
63 InterlockTest o3 = Interlocked.CompareExchange (ref o, o2, o2);
64 if (o3 != o1)
65 return 12;
66 if (o != o1)
67 return 13;
69 InterlockTest o4 = Interlocked.CompareExchange (ref o, o2, o1);
70 if (o4 != o1)
71 return 14;
72 if (o != o2)
73 return 15;
75 /* long increment/decrement */
76 la = 0x12345678;
77 lb = Interlocked.Increment (ref la);
78 if (la != 0x12345679)
79 return 16;
80 if (lb != 0x12345679)
81 return 16;
82 lb = Interlocked.Decrement (ref la);
83 if (la != 0x12345678)
84 return 17;
85 if (lb != 0x12345678)
86 return 18;
88 la = 1;
89 lb = Interlocked.CompareExchange (ref la, 2, 1);
90 if (la != 2)
91 return 19;
92 if (lb != 1)
93 return 20;
95 Console.WriteLine ("done!");
97 return 0;