[System.ServiceModel] Prevent crash in Dispatcher.ListenerLoopManager… (#7136)
[mono-project.git] / mono / tests / monitor.cs
blob7b96860bbb4fced1fbf2f9b8fbdc6d5de44d3893
1 using System;
2 using System.Collections.Generic;
3 using System.Reflection;
4 using System.Runtime.InteropServices;
5 using System.Threading;
7 public class Tests {
8 static void Main ()
10 TestDriver.RunTests (typeof (Tests));
13 // Check that try-catch clauses are not enlarged to encompass a Monitor.Enter
14 public static int test_0_enter_catch_clause () {
15 try {
16 Monitor.Enter (null);
17 try {
18 Console.WriteLine ();
19 } catch (Exception ex) {
20 return 1;
22 } catch (Exception ex) {
23 return 0;
25 return 1;
28 const int thread_count = 3;
30 // #651546
31 public static int test_0_enter_abort_race () {
32 AppDomain ad = AppDomain.CreateDomain ("foo");
33 Thread t = new Thread (StartAppDomain);
34 t.Start (ad);
35 Thread.Sleep (thread_count * 100 * 2);
36 // This will abort the threads created by StartAppDomain
37 AppDomain.Unload (ad);
38 return 0;
41 static void StartAppDomain (object dummy)
43 ((AppDomain) dummy).DoCallBack (Main2);
46 static void Main2 ()
48 Thread[] t = new Thread [thread_count];
49 for (int i = 0; i < t.Length; i++) {
50 t[i] = new Thread (LockMe);
51 t[i].Start (i);
52 Thread.Sleep (100); // this is just so that gdb's [New Thread ...] message are properly coupled with our "Thread # entered" messages
54 Thread.Sleep ((int) (thread_count * 100 * 1.5));
57 static object the_lock = new object ();
59 static void LockMe (object thread_id)
61 bool unlocked = false;
62 try {
63 Monitor.Enter (the_lock);
64 try {
65 Thread.Sleep (thread_count * 1000);
66 } finally {
67 unlocked = true;
68 Monitor.Exit (the_lock);
71 } catch (Exception ex) {
72 if (!unlocked) {
74 } finally {