[loader] Init MonoClass:sizes.element_size lazily (Fixes #43563) (#5559)
[mono.git] / mono / tests / thread2.cs
1
2 using System;
3 using System.Threading;
4
5 public class Test {
6         static LocalDataStoreSlot slot;
7         
8         private void Thread_func() {
9                 //Throws undocumented exception :-(
10                 //LocalDataStoreSlot namedslot=Thread.AllocateNamedDataSlot("data-slot");
11                 LocalDataStoreSlot namedslot=Thread.GetNamedDataSlot("data-slot");
12                 
13                 Console.WriteLine("In a thread!");
14                 
15                 Thread thr=Thread.CurrentThread;
16                 Console.WriteLine("Found thread!");
17                 thr.Name="wobble";
18                 Thread otherthr=Thread.CurrentThread;
19                 Console.WriteLine("Other subthread is " + otherthr.Name);
20
21                 Thread.SetData(slot, thr);
22                 Thread storedthr=(Thread)Thread.GetData(slot);
23                 Console.WriteLine("Stored subthread is " + storedthr.Name);
24
25                 Thread.SetData(namedslot, thr);
26                 storedthr=(Thread)Thread.GetData(namedslot);
27                 Console.WriteLine("Stored subthread is " + storedthr.Name);
28                 
29                 Console.WriteLine("Locking thr for 1.5s");
30                 lock(thr) {
31                         Thread.Sleep(1500);
32                 }
33                 
34                 Console.WriteLine("Waiting for signal");
35                 lock(thr) {
36                         Monitor.Wait(thr);
37                         Console.WriteLine("Thread signalled!");
38                 }
39                 
40                 Console.WriteLine("Sleeping for 10s");
41                 Thread.Sleep(10000);
42
43                 Thread storedthr2=(Thread)Thread.GetData(slot);
44                 Console.WriteLine("Stored subthread is still " + storedthr2.Name);
45         }
46         
47         public static int Main () {
48                 Console.WriteLine ("Hello, World!");
49                 slot=Thread.AllocateDataSlot();
50                 LocalDataStoreSlot namedslot=Thread.AllocateNamedDataSlot("data-slot");
51
52                 Test test = new Test();
53                 Thread thr=new Thread(new ThreadStart(test.Thread_func));
54                 thr.Start();
55                 Thread.Sleep(1000);
56                 Thread main=Thread.CurrentThread;
57                 main.Name="wibble";
58                 Thread othermain=Thread.CurrentThread;
59                 Console.WriteLine("Other name " + othermain.Name);
60                 Thread.Sleep(0);
61                 
62                 Console.WriteLine("In the main line!");
63
64                 Console.WriteLine("Trying to enter lock");
65                 if(Monitor.TryEnter(thr, 100)==true) {
66                         Console.WriteLine("Returned lock");
67                         Monitor.Exit(thr);
68                 } else {
69                         Console.WriteLine("Didn't get lock");
70                 }
71
72                 Thread.SetData(slot, main);
73                 Thread storedthr=(Thread)Thread.GetData(slot);
74                 Console.WriteLine("Stored subthread is " + storedthr.Name);
75
76                 Thread.SetData(namedslot, main);
77                 storedthr=(Thread)Thread.GetData(namedslot);
78                 Console.WriteLine("Stored subthread is " + storedthr.Name);
79                 
80                 if(thr.Join(5000)) {
81                         Console.WriteLine("Joined thread");
82                 } else {
83                         Console.WriteLine("Didn't join thread");
84                 }
85                 
86                 lock(thr) {
87                         Monitor.Pulse(thr);
88                         Console.WriteLine("Signalled thread");
89                 }
90                 
91                 thr.Join();
92                 
93                 return 0;
94         }
95 }
96