IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
ThreadImpl.cs
Go to the documentation of this file.
1/*
2 Copyright (C) 2011-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
26{
27
28 static class ThreadImpl
29 {
30
31 public static object getThreads()
32 {
34 }
35
36 private const int JVMTI_THREAD_STATE_ALIVE = 0x0001;
37 private const int JVMTI_THREAD_STATE_TERMINATED = 0x0002;
38 private const int JVMTI_THREAD_STATE_RUNNABLE = 0x0004;
39 private const int JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER = 0x0400;
40 private const int JVMTI_THREAD_STATE_WAITING_INDEFINITELY = 0x0010;
41 private const int JVMTI_THREAD_STATE_WAITING_WITH_TIMEOUT = 0x0020;
42 private const int JMM_THREAD_STATE_FLAG_SUSPENDED = 0x00100000;
43 private const int JMM_THREAD_STATE_FLAG_NATIVE = 0x00400000;
44
45 public static void getThreadInfo1(long[] ids, int maxDepth, object result)
46 {
47#if !FIRST_PASS
48 System.Reflection.ConstructorInfo[] constructors = typeof(global::java.lang.management.ThreadInfo).GetConstructors(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
49 foreach (System.Reflection.ConstructorInfo constructor in constructors)
50 {
51 if (constructor.GetParameters().Length == 9)
52 {
53 global::java.lang.Thread[] threads = (global::java.lang.Thread[])getThreads();
54 global::java.lang.management.ThreadInfo[] threadInfos = (global::java.lang.management.ThreadInfo[])result;
55 for (int i = 0; i < ids.Length; i++)
56 {
57 long id = ids[i];
58 for (int t = 0; t < threads.Length; t++)
59 {
60 if (threads[t].getId() == id)
61 {
62 global::java.lang.Thread thread = threads[t];
63
64 int state;
65 // invers to sun.misc.VM.toThreadState
66 switch (thread.getState().ordinal())
67 {
68 case (int)global::java.lang.Thread.State.__Enum.RUNNABLE:
69 state = JVMTI_THREAD_STATE_RUNNABLE;
70 break;
71 case (int)global::java.lang.Thread.State.__Enum.BLOCKED:
72 state = JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER;
73 break;
74 case (int)global::java.lang.Thread.State.__Enum.WAITING:
75 state = JVMTI_THREAD_STATE_WAITING_INDEFINITELY;
76 break;
77 case (int)global::java.lang.Thread.State.__Enum.TIMED_WAITING:
78 state = JVMTI_THREAD_STATE_WAITING_WITH_TIMEOUT;
79 break;
80 case (int)global::java.lang.Thread.State.__Enum.TERMINATED:
81 state = JVMTI_THREAD_STATE_TERMINATED;
82 break;
83 case (int)global::java.lang.Thread.State.__Enum.NEW:
84 state = JVMTI_THREAD_STATE_ALIVE;
85 break;
86 default:
87 state = 0;
88 break;
89 }
90 //TODO set in state JMM_THREAD_STATE_FLAG_SUSPENDED if the thread is suspended
91
92 global::java.lang.StackTraceElement[] stacktrace = thread.getStackTrace();
93 if (maxDepth >= 0 && maxDepth < stacktrace.Length)
94 {
95 global::java.lang.StackTraceElement[] temp = new global::java.lang.StackTraceElement[maxDepth];
96 System.Array.Copy(stacktrace, temp, temp.Length);
97 stacktrace = temp;
98 }
99
100 object[] parameters = new object[9];
101 parameters[0] = thread; // thread
102 parameters[1] = state; // state
103 // lockObj
104 // lockOwner
105 parameters[4] = 0; // blockedCount
106 parameters[5] = 0; // blockedTime
107 parameters[6] = -1; // waitedCount
108 parameters[7] = 0; // waitedTime
109 parameters[8] = stacktrace; // stackTrace
110 threadInfos[i] = (global::java.lang.management.ThreadInfo)constructor.Invoke(parameters);
111 break;
112 }
113 }
114 }
115 return;
116 }
117 }
118 throw new global::java.lang.InternalError("Constructor for java.lang.management.ThreadInfo not find.");
119#endif
120 }
121
122 private static int GetCurrentThreadId()
123 {
124#pragma warning disable 618
125 // On the CLR and Mono on Windows this is the (obsolete) equivalent of kernel32!GetCurrentThreadId
126 return System.AppDomain.GetCurrentThreadId();
127#pragma warning restore 618
128 }
129
130 public static long getThreadTotalCpuTime0(long id)
131 {
132 if (id == 0)
133 {
134 int currentId = GetCurrentThreadId();
135 System.Diagnostics.ProcessThreadCollection threads = System.Diagnostics.Process.GetCurrentProcess().Threads;
136 foreach (System.Diagnostics.ProcessThread t in threads)
137 {
138 if (t.Id == currentId)
139 {
140 return (long)(t.TotalProcessorTime.Ticks * 100);
141 }
142 }
143 return 0;
144 }
145 else
146 {
147 throw new System.NotImplementedException("Only current Thread is supported.");
148 }
149 }
150
151 public static void getThreadTotalCpuTime1(long[] ids, long[] result)
152 {
153 throw new System.NotImplementedException();
154 }
155
156 public static long getThreadUserCpuTime0(long id)
157 {
158 throw new System.NotImplementedException();
159 }
160
161 public static void getThreadUserCpuTime1(long[] ids, long[] result)
162 {
163 throw new System.NotImplementedException();
164 }
165
166 public static void getThreadAllocatedMemory1(long[] ids, long[] result)
167 {
168 throw new System.NotImplementedException();
169 }
170
171 public static void setThreadCpuTimeEnabled0(bool enable)
172 {
173 //ignoring, we need nothing to enable
174 }
175
176 public static void setThreadAllocatedMemoryEnabled0(bool enable)
177 {
178 throw new System.NotImplementedException();
179 }
180
181 public static void setThreadContentionMonitoringEnabled0(bool enable)
182 {
183 throw new System.NotImplementedException();
184 }
185
186 public static object findMonitorDeadlockedThreads0()
187 {
188 throw new System.NotImplementedException();
189 }
190
191 public static object findDeadlockedThreads0()
192 {
193 throw new System.NotImplementedException();
194 }
195
196 public static void resetPeakThreadCount0()
197 {
198 throw new System.NotImplementedException();
199 }
200
201 public static object dumpThreads0(long[] ids, bool lockedMonitors, bool lockedSynchronizers)
202 {
203 throw new System.NotImplementedException();
204 }
205
206 public static void resetContentionTimes0(long tid)
207 {
208 throw new System.NotImplementedException();
209 }
210
211 }
212
213}
Implements the native mthods of 'java.lang.Thread'.
Definition Thread.cs:43
static object getThreads()
Implements the native method 'getThreads'.
Definition Thread.cs:112
static void setThreadCpuTimeEnabled0(bool enable)
static void getThreadAllocatedMemory1(long[] ids, long[] result)
static void getThreadInfo1(long[] ids, int maxDepth, object result)
Definition ThreadImpl.cs:45
static object dumpThreads0(long[] ids, bool lockedMonitors, bool lockedSynchronizers)
static void setThreadContentionMonitoringEnabled0(bool enable)
static void setThreadAllocatedMemoryEnabled0(bool enable)
static void getThreadTotalCpuTime1(long[] ids, long[] result)
static void getThreadUserCpuTime1(long[] ids, long[] result)