16af9f66a7a56143f92ae0e5edaef17a50b4a803
[mono.git] / mono / tests / thread-native-exit.cs
1
2 using System;
3 using System.Runtime.InteropServices;
4 using System.Threading;
5
6 class Driver
7 {
8         [DllImport ("libc")]
9         extern static void pthread_exit (IntPtr value);
10
11         [DllImport ("kernel32")]
12         extern static void ExitThread (IntPtr value);
13
14         static Thread GetThread1 ()
15         {
16                 return new Thread (() => {
17                         /* Exit bypassing completely the runtime */
18                         try {
19                                 pthread_exit (IntPtr.Zero);
20                         } catch (EntryPointNotFoundException) {
21                         }
22
23                         try {
24                                 ExitThread (IntPtr.Zero);
25                         } catch (EntryPointNotFoundException) {
26                         }
27                 });
28         }
29
30         static Thread GetThread2 ()
31         {
32                 return new Thread (() => {
33                         /* Exit without returning from the ThreadStart delegate */
34                         Thread.CurrentThread.Abort ();
35                 });
36         }
37
38         static Thread GetThread3 ()
39         {
40                 return new Thread (() => {
41                         /* Exit by returning from the ThreadStart delegate */
42                         return;
43                 });
44         }
45
46         static Thread[] CreateThreads ()
47         {
48                 return new Thread [] {
49                         GetThread1 (),
50                         GetThread2 (),
51                         GetThread3 (),
52                 };
53         }
54
55         public static void Main ()
56         {
57                 Thread[] threads;
58
59                 {
60                         threads = CreateThreads ();
61
62                         for (int i = 0; i < threads.Length; ++i)
63                                 threads [i].Start ();
64
65                         for (int i = 0; i < threads.Length; ++i)
66                                 threads [i].Join ();
67                 }
68
69                 {
70                         threads = CreateThreads ();
71
72                         for (int i = 0; i < threads.Length; ++i)
73                                 threads [i].Start ();
74                 }
75         }
76 }