IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
Thread.cs
Go to the documentation of this file.
1/*
2 Copyright (C) 2007-2014 Jeroen Frijters
3
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
11
12 1. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
16 2. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
18 3. This notice may not be removed or altered from any source distribution.
19
20 Jeroen Frijters
21 jeroen@frijters.net
22
23*/
24
25#pragma warning disable CS0618 // Type or member is obsolete
26
27using System;
28using System.Collections.Generic;
29using System.Diagnostics;
30using System.Threading;
31
32using IKVM.Runtime;
35
37{
38
42 static class Thread
43 {
44
45#if FIRST_PASS == false
46
47 static AccessControllerAccessor accessControllerAccessor;
48 static ThreadAccessor threadAccessor;
49
50 static AccessControllerAccessor AccessControllerAccessor => JVM.Internal.BaseAccessors.Get(ref accessControllerAccessor);
51
52 static ThreadAccessor ThreadAccessor => JVM.Internal.BaseAccessors.Get(ref threadAccessor);
53
54#endif
55
60 public static object getMainThreadGroup()
61 {
62#if FIRST_PASS
63 throw new NotImplementedException();
64#else
65 return JVM.MainThreadGroup;
66#endif
67 }
68
69 // this is called from JniInterface.cs
70 internal static void WaitUntilLastJniThread()
71 {
72#if FIRST_PASS
73 throw new NotImplementedException();
74#else
75 int count = ThreadAccessor.InvokeIsDaemon(ThreadAccessor.InvokeCurrentThread()) ? 0 : 1;
76 while (Interlocked.CompareExchange(ref global::java.lang.Thread.nonDaemonCount[0], 0, 0) > count)
77 global::System.Threading.Thread.Sleep(1);
78#endif
79 }
80
81 // this is called from JniInterface.cs
82 internal static void AttachThreadFromJni(object threadGroup)
83 {
84#if FIRST_PASS
85 throw new NotImplementedException();
86#else
87 if (ThreadAccessor.GetCurrent() == null)
88 ThreadAccessor.Init(threadGroup ?? JVM.MainThreadGroup);
89#endif
90 }
91
97 public static global::java.lang.StackTraceElement[] getStackTrace(StackTrace stack)
98 {
99#if FIRST_PASS
100 throw new NotImplementedException();
101#else
102 var stackTrace = new List<global::java.lang.StackTraceElement>();
103 ExceptionHelper.ExceptionInfoHelper.Append(JVM.Context.ExceptionHelper, stackTrace, stack, 0, true);
104 return stackTrace.ToArray();
105#endif
106 }
107
112 public static object getThreads()
113 {
114#if FIRST_PASS
115 throw new NotImplementedException();
116#else
117 return AccessControllerAccessor.InvokeDoPrivileged(new FuncPrivilegedAction<object[]>(() =>
118 {
119 var root = (global::java.lang.ThreadGroup)JVM.MainThreadGroup;
120 while (true)
121 {
122 var threads = new global::java.lang.Thread[root.activeCount()];
123 if (root.enumerate(threads) == threads.Length)
124 return threads;
125 }
126 }));
127#endif
128 }
129
135 public static object dumpThreads(object[] threads)
136 {
137#if FIRST_PASS
138 throw new NotImplementedException();
139#else
140 var stacks = new global::java.lang.StackTraceElement[threads.Length][];
141 for (int i = 0; i < threads.Length; i++)
142 {
143 var nativeThread = ThreadAccessor.GetNativeThread(threads[i]);
144 if (nativeThread == null)
145 {
146 stacks[i] = Array.Empty<global::java.lang.StackTraceElement>();
147 }
148 else
149 {
150#if NETFRAMEWORK
151 try
152 {
153 var suspended = false;
154 if ((nativeThread.ThreadState & global::System.Threading.ThreadState.Suspended) == 0 && nativeThread != global::System.Threading.Thread.CurrentThread)
155 {
156 suspended = true;
157 nativeThread.Suspend();
158 }
159
160 StackTrace stack;
161 try
162 {
163 stack = new StackTrace(nativeThread, true);
164 }
165 finally
166 {
167 if (suspended)
168 nativeThread.Resume();
169 }
170
171 stacks[i] = getStackTrace(stack);
172 }
173 catch (ThreadStateException)
174 {
175 stacks[i] = Array.Empty<global::java.lang.StackTraceElement>();
176 }
177#else
178 if (nativeThread == global::System.Threading.Thread.CurrentThread)
179 {
180 stacks[i] = getStackTrace(new StackTrace(true));
181 }
182 else
183 {
184 stacks[i] = Array.Empty<global::java.lang.StackTraceElement>();
185 }
186#endif
187 }
188 }
189
190 return stacks;
191#endif
192 }
193
194 }
195
196}
System.Threading.Interlocked Interlocked
Implements the native mthods of 'java.lang.Thread'.
Definition Thread.cs:43
static object getMainThreadGroup()
Implements the native method 'getMainThreadGroup'.
Definition Thread.cs:60
static global::java.lang.StackTraceElement[] getStackTrace(StackTrace stack)
Implements the native method 'getStackTrace'.
Definition Thread.cs:97
static object getThreads()
Implements the native method 'getThreads'.
Definition Thread.cs:112
static object dumpThreads(object[] threads)
Implements the native method 'dumpThreads'.
Definition Thread.cs:135
Main state of the running JVM.
ExceptionHelper ExceptionHelper
Gets the ExceptionHelper associated with this instance of the runtime.
Implementation of global::java.security.PrivilegedAction that invokes a delegate.