Reduce TLS accesses. (#11487)
[mono-project.git] / mono / tests / exception-invokes.cs
blob5d7a6a5932825490e83fda6eaee17ea7ca6766e6
1 using System;
2 using System.Linq;
3 using System.Runtime.CompilerServices;
4 using System.Reflection;
6 class C
8 const int StepSize = 5;
9 const int Iterations = 8;
11 public static void Main ()
13 AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(HandleException);
14 var args = new object[] {C.Iterations * C.StepSize};
15 typeof (C).GetMethod ("InvokeChain", BindingFlags.NonPublic | BindingFlags.Instance).Invoke (new C (), args);
18 public static void HandleException (object sender, UnhandledExceptionEventArgs e)
20 var ex = e.ExceptionObject as Exception;
22 int iterations = 0;
23 while (ex != null) {
24 string fullTrace = ex.StackTrace;
26 string[] frames = fullTrace.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
27 // Console.WriteLine("Split into {0} lines", frames.Length);
29 int count = 0;
30 for (int i=0; i < frames.Length; i++)
32 var words = frames [i].Split((char []) null, StringSplitOptions.RemoveEmptyEntries);
33 if (words.Length > 1 && words [0] == "at" && words [1].StartsWith("C.InvokeChain"))
34 count++;
35 // Console.WriteLine("Line: {0} {1}", frames [i], words.Length);
38 if (count != 0)
40 if (count > 1 && count != C.StepSize)
42 Console.WriteLine("Fail step size");
43 Environment.Exit(1);
45 if (count == C.StepSize)
46 iterations += 1;
49 ex = ex.InnerException;
52 if (iterations != C.Iterations) {
53 Console.WriteLine ("{0} iterations", iterations);
54 Environment.Exit (1);
57 // Prevents the runtime from printing the exception
58 Environment.Exit (0);
62 [MethodImpl(MethodImplOptions.NoInlining)]
63 private void InvokeChain (int depth)
65 if (depth == 0) {
66 Base ();
67 } else if (depth % C.StepSize == 0) {
68 //Console.WriteLine ("InvokeChain {0} indirect", depth);
69 typeof (C).GetMethod ("InvokeChain", BindingFlags.NonPublic | BindingFlags.Instance).Invoke (this, new object[] {depth - 1});
70 } else {
71 //Console.WriteLine ("InvokeChain {0} direct", depth);
72 InvokeChain (depth - 1);
76 [MethodImpl(MethodImplOptions.NoInlining)]
77 private void Base ()
79 throw new NotImplementedException ();