1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // ---------------------------------------------------------------------------
10 package cc
.squirreljme
.jvm
.mle
;
12 import cc
.squirreljme
.jvm
.mle
.brackets
.TaskBracket
;
13 import cc
.squirreljme
.jvm
.mle
.brackets
.TracePointBracket
;
14 import cc
.squirreljme
.jvm
.mle
.brackets
.VMThreadBracket
;
15 import cc
.squirreljme
.jvm
.mle
.constants
.ThreadModelType
;
16 import cc
.squirreljme
.jvm
.mle
.exceptions
.MLECallError
;
17 import cc
.squirreljme
.runtime
.cldc
.annotation
.Api
;
18 import cc
.squirreljme
.runtime
.cldc
.annotation
.SquirrelJMEVendorApi
;
19 import org
.intellij
.lang
.annotations
.Flow
;
20 import org
.intellij
.lang
.annotations
.MagicConstant
;
21 import org
.jetbrains
.annotations
.Blocking
;
22 import org
.jetbrains
.annotations
.CheckReturnValue
;
23 import org
.jetbrains
.annotations
.NotNull
;
24 import org
.jetbrains
.annotations
.Range
;
27 * This shelf handles everything regarding threading and otherwise.
29 * @see VMThreadBracket
32 @SuppressWarnings("UnstableApiUsage")
34 public final class ThreadShelf
37 * Returns the number of alive threads.
39 * @param __includeMain Include main threads?
40 * @param __includeDaemon Include daemon threads?
41 * @return The number of alive threads.
45 public static native int aliveThreadCount(boolean __includeMain
,
46 boolean __includeDaemon
);
49 * Creates a virtual machine thread for the given Java thread.
51 * @param __javaThread The Java thread to create under.
52 * @param __name The name of this thread.
53 * @return The virtual machine thread.
54 * @throws MLECallError If {@code __javaThread} is null.
58 public static native VMThreadBracket
createVMThread(
59 @Flow(target
= "this._vmThread") @NotNull Thread __javaThread
,
64 * Returns the exit code for the current process.
66 * @return The exit code for the current process.
70 public static native int currentExitCode();
73 * Returns the current Java thread.
75 * @return The current {@link Thread}.
79 public static native Thread
currentJavaThread();
82 * Returns the current virtual machine thread.
84 * @return The current virtual machine thread.
88 public static native VMThreadBracket
currentVMThread();
91 * Checks if these two threads are the same.
93 * @param __a The first thread.
94 * @param __b The second thread.
95 * @return If these are the same thread.
96 * @throws MLECallError If either arguments are null.
100 public static native boolean equals(VMThreadBracket __a
,
105 * Returns whether the interrupt flag was raised and clears it.
107 * @param __javaThread The Java thread.
108 * @return If the thread was interrupted.
109 * @throws MLECallError If {@code __javaThread} is null.
112 @SquirrelJMEVendorApi
113 public static native boolean javaThreadClearInterrupt(
114 @NotNull Thread __javaThread
)
118 * Marks the thread as being started.
120 * @param __javaThread The thread to mark started.
121 * @throws MLECallError If {@code __javaThread} is null.
124 @SquirrelJMEVendorApi
125 public static native void javaThreadFlagStarted(
126 @NotNull Thread __javaThread
)
130 * Has this Java thread been started?
132 * @param __javaThread The Java thread.
133 * @return If this thread has been started.
134 * @throws MLECallError If {@code __javaThread} is null.
137 @SquirrelJMEVendorApi
138 @Flow(source
= "this._started")
139 public static native boolean javaThreadIsStarted(
140 @NotNull Thread __javaThread
)
144 * Returns the runnable for the given Java thread.
146 * @param __javaThread The Java thread.
147 * @return The {@link Runnable} for the given thread.
148 * @throws MLECallError If {@code __javaThread} is null.
152 @SquirrelJMEVendorApi
153 @Flow(source
= "this._runnable")
154 public static native Runnable
javaThreadRunnable(
155 @NotNull Thread __javaThread
)
159 * Sets if the thread is alive or not.
161 * @param __javaThread The Java thread.
162 * @param __set If this is to be alive or not. If this is {@code true}
163 * then the active count goes up, otherwise it shall go down.
164 * @throws MLECallError If {@code __javaThread} is null.
167 @SquirrelJMEVendorApi
168 public static native void javaThreadSetAlive(@NotNull Thread __javaThread
,
173 * Sets the thread to be a daemon thread, it cannot be started.
175 * @param __javaThread The thread to set as a daemon thread.
176 * @throws MLECallError If {@code __javaThread} is null or is already
180 @SquirrelJMEVendorApi
181 public static native void javaThreadSetDaemon(@NotNull Thread __javaThread
)
185 * Returns the {@link ThreadModelType} of the virtual machine.
187 * @return The {@link ThreadModelType} of the virtual machine.
190 @SquirrelJMEVendorApi
191 @MagicConstant(valuesFromClass
= ThreadModelType
.class)
192 public static native int model();
195 * Runs the main entry point for the current process and gives it all of
196 * the arguments that were specified on program initialization.
200 @SquirrelJMEVendorApi
201 public static native void runProcessMain();
204 * Sets the current process exit code.
206 * @param __code The exit code to use.
209 @SquirrelJMEVendorApi
210 public static native void setCurrentExitCode(int __code
);
213 * Sets the trace of the current task so that it can be requested by
214 * another or launching program.
216 * @param __message The message of the trace.
217 * @param __trace The trace to set.
218 * @throws MLECallError If {@code __message} is {@code null}, or
219 * {@code __trace} or any element within is {@code null}.
222 @SquirrelJMEVendorApi
223 public static native void setTrace(@NotNull String __message
,
224 @NotNull TracePointBracket
[] __trace
)
228 * Sleeps the current thread for the given amount of time.
230 * If both times are zero, this means to yield the thread (give up its
231 * current execution context).
233 * If SquirrelJME is running in cooperative
234 * single threaded mode, this will relinquish control of the current
237 * @param __ms The number of milliseconds.
238 * @param __ns The number of nanoseconds.
239 * @return {@code true} if the thread was interrupted.
240 * @throws MLECallError If either value is negative or the nanoseconds is
244 @SquirrelJMEVendorApi
246 public static native boolean sleep(
247 @Range(from
= 0, to
= Integer
.MAX_VALUE
) int __ms
,
248 @Range(from
= 0, to
= 999999) int __ns
)
252 * Returns the Java thread for the VM thread.
254 * @param __vmThread The VM thread.
255 * @return The Java thread which belongs to this thread.
256 * @throws MLECallError If {@code __thread} is null.
259 @SquirrelJMEVendorApi
260 public static native Thread
toJavaThread(
261 @NotNull VMThreadBracket __vmThread
)
265 * Returns the virtual machine thread from the given Java thread.
267 * @param __thread The Java thread.
268 * @return The VM thread for this thread.
269 * @throws MLECallError If {@code __thread} is null.
272 @SquirrelJMEVendorApi
273 @Flow(source
= "this._vmThread")
274 public static native VMThreadBracket
toVMThread(
275 @NotNull Thread __thread
)
279 * Signals that the thread has ended and is no longer considered to be
282 * @param __vmThread The virtual machine thread.
283 * @throws MLECallError If {@code __vmThread} is null.
286 @SquirrelJMEVendorApi
287 public static native void vmThreadEnd(
288 @NotNull VMThreadBracket __vmThread
)
292 * Returns the thread ID for the given thread.
294 * @param __vmThread The virtual machine thread.
295 * @return The thread ID.
296 * @throws MLECallError If {@code __vmThread} is null.
299 @SquirrelJMEVendorApi
300 public static native int vmThreadId(
301 @NotNull VMThreadBracket __vmThread
)
305 * Performs a hardware interrupt on the thread.
307 * @param __vmThread The virtual machine thread.
308 * @throws MLECallError If {@code __vmThread} is null.
311 @SquirrelJMEVendorApi
312 public static native void vmThreadInterrupt(
313 @NotNull VMThreadBracket __vmThread
)
317 * Checks if the given thread is a main thread.
319 * @param __vmThread The thread to check.
320 * @return {@code true} if the given thread is a main thread.
321 * @throws MLECallError If {@code __vmThread} is null.
324 @SquirrelJMEVendorApi
325 public static native boolean vmThreadIsMain(
326 @NotNull VMThreadBracket __vmThread
)
330 * Sets the thread priority in the same manner as
331 * {@link Thread#setPriority(int)} if this is supported by the hardware.
333 * This may or may not be supported and should only be used as a hint and
336 * @param __vmThread The virtual machine thread.
337 * @param __p The priority to set, this will be the same as
338 * {@link Thread#setPriority(int)}.
339 * @throws MLECallError If {@code __vmThread} is null or {@code __p} is
340 * not within {@link Thread#MIN_PRIORITY} and {@link Thread#MAX_PRIORITY}
344 @SquirrelJMEVendorApi
345 public static native void vmThreadSetPriority(
346 @NotNull VMThreadBracket __vmThread
, int __p
)
350 * Performs the actual start of the given thread.
352 * @param __vmThread The thread to start.
353 * @return If the start of the thread succeeded.
354 * @throws MLECallError If {@code __vmThread} is null.
357 @SquirrelJMEVendorApi
358 public static native boolean vmThreadStart(
359 @NotNull VMThreadBracket __vmThread
)
363 * Returns the task that owns the given thread.
365 * @param __vmThread The thread to get the task of.
366 * @return The task for the given thread.
367 * @throws MLECallError If the thread is not valid.
370 @SquirrelJMEVendorApi
371 public static native TaskBracket
vmThreadTask(
372 @NotNull VMThreadBracket __vmThread
)
376 * Waits for the state of threads to be updated, or just times out.
378 * A thread update is when another thread becomes alive, becomes dead,
381 * If waiting and SquirrelJME is running in cooperative
382 * single threaded mode, this will relinquish control of the current
385 * @param __ms The amount of time to wait for.
386 * @return If the thread was interrupted while waiting.
387 * @throws MLECallError If {@code __ms} is negative.
390 @SquirrelJMEVendorApi
392 public static native boolean waitForUpdate(int __ms
)