public class RubyThread extends RubyObject implements ExecutionContext
Thread class. Each Ruby thread is
mapped to an underlying Java Virtual Machine thread.
Thread encapsulates the behavior of a thread of execution, including the main
thread of the Ruby script. In the descriptions that follow, the parameter
aSymbol refers to a symbol, which is either a quoted string or a
Symbol (such as :name).
Note: For CVS history, see ThreadClass.java.
| Modifier and Type | Class and Description |
|---|---|
static interface |
RubyThread.BlockingTask |
static class |
RubyThread.Location |
static class |
RubyThread.SleepTask |
static class |
RubyThread.Status
Thread statuses
|
RubyObject.DataRubyBasicObject.Finalizer| Modifier and Type | Field and Description |
|---|---|
private boolean |
abortOnException
Whether this thread should try to abort the program on exception
|
private BlockingIO.Condition |
blockingIO |
private java.lang.ref.WeakReference<ThreadContext> |
contextRef
Weak reference to the ThreadContext for this thread.
|
private java.util.Map<java.lang.Object,IRubyObject> |
contextVariables
Context-local variables, internal-ish thread locals
|
private RubyThread.BlockingTask |
currentBlockingTask
The current task blocking a thread, to allow interrupting it in an appropriate way
|
private java.nio.channels.Selector |
currentSelector |
private static boolean |
DEBUG |
private boolean |
disposed
Whether or not this thread has been disposed of
|
private IRubyObject |
errorInfo
Per-thread "current exception"
|
private RaiseException |
exitingException
The exception currently being raised out of the thread.
|
private IRubyObject |
finalResult
The final value resulting from the thread's execution
|
private java.util.List<java.util.concurrent.locks.Lock> |
heldLocks
The list of locks this thread currently holds, so they can be released on exit
|
private int |
initialPriority
The thread's initial priority, for use in thread pooled mode
|
private static Logger |
LOG |
private java.util.concurrent.atomic.AtomicReference<ThreadService.Event> |
mail
Mail slot for cross-thread events
|
private int |
RUBY_MAX_THREAD_PRIORITY |
private int |
RUBY_MIN_THREAD_PRIORITY |
private java.util.concurrent.atomic.AtomicReference<RubyThread.Status> |
status
Current status in an atomic reference
|
private RubyThreadGroup |
threadGroup
The ThreadGroup to which this thread belongs
|
private ThreadLike |
threadImpl
The thread-like think that is actually executing
|
private java.util.Map<IRubyObject,IRubyObject> |
threadLocalVariables
Normal thread-local variables
|
FIELD_ALLOCATED_CLASSES, FIELD_ALLOCATORS, IVAR_INSPECTING_OBJECT_ALLOCATOR, OBJECT_ALLOCATOR, OBJECT_VAR0_ALLOCATOR, OBJECT_VAR1_ALLOCATOR, OBJECT_VAR2_ALLOCATOR, OBJECT_VAR3_ALLOCATOR, OBJECT_VAR4_ALLOCATOR, OBJECT_VAR5_ALLOCATOR, OBJECT_VAR6_ALLOCATOR, OBJECT_VAR7_ALLOCATOR, OBJECT_VAR8_ALLOCATOR, OBJECT_VAR9_ALLOCATOR, REIFYING_OBJECT_ALLOCATORALL_F, BASICOBJECT_ALLOCATOR, COMPARE_BY_IDENTITY_F, ERR_INSECURE_SET_INST_VAR, FALSE_F, FL_USHIFT, flags, FROZEN_F, metaClass, NEVER, NIL_F, STAMP_OFFSET, TAINTED_F, UNDEF, UNTRUSTED_F, USER0_F, USER1_F, USER2_F, USER3_F, USER4_F, USER5_F, USER6_F, USER7_F, USER8_F, VAR_TABLE_OFFSET, varTable, varTableStampNULL_ARRAY| Modifier | Constructor and Description |
|---|---|
protected |
RubyThread(Ruby runtime,
RubyClass type) |
|
RubyThread(Ruby runtime,
RubyClass klass,
ThreadedRunnable runnable) |
| Modifier and Type | Method and Description |
|---|---|
static IRubyObject |
abort_on_exception_set_x(IRubyObject recv,
IRubyObject value) |
IRubyObject |
abort_on_exception_set(IRubyObject val) |
static RubyBoolean |
abort_on_exception_x(IRubyObject recv)
Returns the status of the global ``abort on exception'' condition.
|
RubyBoolean |
abort_on_exception() |
private boolean |
abortOnException(Ruby runtime) |
private void |
addToCorrectThreadGroup(ThreadContext context) |
static RubyThread |
adopt(IRubyObject recv,
java.lang.Thread t) |
private static RubyThread |
adoptThread(IRubyObject recv,
java.lang.Thread t,
Block block) |
void |
afterBlockingCall() |
RubyBoolean |
alive_p() |
IRubyObject |
backtrace_locations(ThreadContext context,
IRubyObject[] args) |
IRubyObject |
backtrace(ThreadContext context) |
IRubyObject |
backtrace20(ThreadContext context,
IRubyObject[] args) |
void |
beDead() |
void |
beforeBlockingCall() |
void |
beforeStart()
Perform pre-execution tasks once the native thread is running, but we
have not yet called the Ruby code for the thread.
|
void |
checkMail(ThreadContext context) |
void |
cleanTerminate(IRubyObject result) |
private void |
clearThreadLocals() |
static RubyClass |
createThreadClass(Ruby runtime) |
static IRubyObject |
critical_set(IRubyObject receiver,
IRubyObject value) |
static IRubyObject |
critical(IRubyObject receiver) |
static RubyThread |
current(IRubyObject recv) |
private static void |
debug(RubyThread thread,
java.lang.String message) |
void |
dieFromFinalizer()
Used for finalizers that need to kill a Ruby thread.
|
void |
dispose()
Dispose of the current thread by tidying up connections to other stuff
|
void |
enterSleep() |
boolean |
equals(java.lang.Object obj)
This method is just a wrapper around the Ruby "==" method,
provided so that RubyObjects can be used as keys in the Java
HashMap object underlying RubyHash.
|
void |
exceptionRaised(RaiseException exception) |
void |
exceptionRaised(java.lang.Throwable exception)
For handling all non-Ruby exceptions bubbling out of threads
|
void |
executeBlockingTask(RubyThread.BlockingTask task) |
static IRubyObject |
exit(IRubyObject receiver,
Block block) |
void |
exitSleep() |
ThreadContext |
getContext() |
java.util.Map<java.lang.Object,IRubyObject> |
getContextVariables() |
IRubyObject |
getErrorInfo() |
java.lang.Thread |
getNativeThread() |
private java.nio.channels.Selector |
getSelector(java.nio.channels.SelectableChannel channel) |
private IRubyObject |
getSymbolKey(IRubyObject originalKey) |
RubyThreadGroup |
getThreadGroup() |
private java.util.Map<IRubyObject,IRubyObject> |
getThreadLocals() |
IRubyObject |
group() |
int |
hashCode()
Override the Object#hashCode method to make sure that the Ruby
hash is actually used as the hashcode for Ruby objects.
|
private java.lang.String |
identityString() |
IRubyObject |
initialize(ThreadContext context,
IRubyObject[] args,
Block block) |
IRubyObject |
inspect()
rb_obj_inspect
call-seq:
obj.inspect => string
Returns a string containing a human-readable representation of
obj.
|
void |
internalRaise(IRubyObject[] args)
This is intended to be used to raise exceptions in Ruby threads from non-
Ruby threads like Timeout's thread.
|
void |
interrupt() |
boolean |
isAlive() |
private boolean |
isCurrent() |
java.lang.StackTraceElement[] |
javaBacktrace() |
private int |
javaPriorityToRubyPriority(int javaPriority) |
IRubyObject |
join(IRubyObject[] args) |
RubyBoolean |
key_p(IRubyObject key) |
RubyArray |
keys() |
IRubyObject |
kill_bang() |
IRubyObject |
kill() |
static IRubyObject |
kill(IRubyObject receiver,
IRubyObject rubyThread,
Block block) |
static RubyArray |
list(IRubyObject recv) |
void |
lock(java.util.concurrent.locks.Lock lock)
Acquire the given lock, holding a reference to it for cleanup on thread
termination.
|
void |
lockInterruptibly(java.util.concurrent.locks.Lock lock)
Acquire the given lock interruptibly, holding a reference to it for cleanup
on thread termination.
|
static RubyThread |
main(IRubyObject recv) |
static RubyThread |
mainThread(IRubyObject receiver) |
static IRubyObject |
newInstance(IRubyObject recv,
IRubyObject[] args,
Block block)
Thread.new |
IRubyObject |
op_aref(IRubyObject key) |
IRubyObject |
op_aset(IRubyObject key,
IRubyObject value) |
static IRubyObject |
pass(IRubyObject recv) |
void |
pollThreadEvents() |
void |
pollThreadEvents(ThreadContext context) |
private IRubyObject |
prepareRaiseException(Ruby runtime,
IRubyObject[] args,
Block block) |
IRubyObject |
priority_set(IRubyObject priority) |
RubyFixnum |
priority() |
IRubyObject |
raise(IRubyObject exception)
Simplified utility method for just raising an existing exception in this
thread.
|
IRubyObject |
raise(IRubyObject[] args,
Block block) |
private void |
receivedAnException(ThreadContext context,
IRubyObject exception) |
void |
receiveMail(ThreadService.Event event) |
private int |
rubyPriorityToJavaPriority(int rubyPriority) |
IRubyObject |
run() |
IRubyObject |
safe_level() |
boolean |
select(java.nio.channels.Channel channel,
RubyIO io,
int ops) |
boolean |
select(java.nio.channels.Channel channel,
RubyIO io,
int ops,
long timeout) |
boolean |
select(RubyIO io,
int ops) |
boolean |
select(RubyIO io,
int ops,
long timeout) |
boolean |
selectForAccept(RubyIO io)
Deprecated.
|
void |
setContext(ThreadContext context) |
IRubyObject |
setErrorInfo(IRubyObject errorInfo) |
(package private) void |
setThreadGroup(RubyThreadGroup rubyThreadGroup) |
boolean |
sleep(long millis)
We can never be sure if a wait will finish because of a Java "spurious wakeup".
|
static RubyThread |
start(IRubyObject recv,
IRubyObject[] args,
Block block)
Basically the same as Thread.new .
|
static RubyThread |
start19(IRubyObject recv,
IRubyObject[] args,
Block block) |
private static RubyThread |
startThread(IRubyObject recv,
IRubyObject[] args,
boolean callInit,
Block block) |
private IRubyObject |
startWith(ThreadedRunnable runnable) |
IRubyObject |
status() |
private IRubyObject |
status(Ruby runtime) |
IRubyObject |
status(ThreadContext context) |
RubyBoolean |
stop_p() |
static IRubyObject |
stop(ThreadContext context,
IRubyObject receiver) |
private static void |
throwThreadKill() |
java.lang.String |
toString()
The default toString method is just a wrapper that calls the
Ruby "to_s" method.
|
boolean |
tryLock(java.util.concurrent.locks.Lock lock)
Try to acquire the given lock, adding it to a list of held locks for cleanup
on thread termination if it is acquired.
|
void |
unlock(java.util.concurrent.locks.Lock lock)
Release the given lock and remove it from the list of locks to be released
on thread termination.
|
void |
unlockAll()
Release all locks held.
|
IRubyObject |
value() |
boolean |
wait_timeout(IRubyObject o,
java.lang.Double timeout) |
boolean |
waitForIO(ThreadContext context,
RubyIO io,
int ops) |
RubyThread |
wakeup() |
attachToObjectSpace, callInit, callInit, callInit, callInit, callInit, callInit, callInit, callInit, callInit, callInit, convertToType, createObjectClass, eqlInternal, equalInternal, getNativeTypeIndex, initialize, initialize, inspect, op_eqq, puts, specificEvaladdFinalizer, anyToString, asJavaString, asString, callMethod, callMethod, callMethod, callMethod, callMethod, callMethod, callMethod, callMethod, callSuper, checkArrayType, checkCallMethod, checkFrozen, checkStringType, checkStringType19, compareTo, convertToArray, convertToFloat, convertToHash, convertToInteger, convertToInteger, convertToInteger, convertToString, copyInstanceVariablesInto, copySpecialInstanceVariables, createBasicObjectClass, dataGetStruct, dataGetStructChecked, dataWrapStruct, display, dup, ensureInstanceVariablesSettable, eql_p, eql, equal_p, equal_p19, evalUnder, extend, fastGetInstanceVariable, fastGetInternalVariable, fastHasInstanceVariable, fastHasInternalVariable, fastSetInstanceVariable, fastSetInternalVariable, freeze, frozen_p, getFFIHandle, getFlag, getInstanceEvalClass, getInstanceVariable, getInstanceVariableList, getInstanceVariableNameList, getInstanceVariables, getInternalVariable, getInternalVariables, getJavaClass, getMetaClass, getNativeHandle, getObjectId, getRuntime, getSingletonClass, getSingletonClassClone, getType, getVariable, getVariableCount, getVariableList, getVariableNameList, hash, hashyInspect, hasInstanceVariable, hasInternalVariable, hasVariables, id_deprecated, id, infectBy, infectBy, infectBy, initialize_copy, initialize19, inspectHashCode, instance_eval, instance_eval, instance_eval, instance_eval, instance_eval19, instance_eval19, instance_eval19, instance_eval19, instance_exec, instance_exec19, instance_of_p, instance_variable_defined_p, instance_variable_get, instance_variable_set, instance_variables, instance_variables19, isBuiltin, isClass, isFalse, isFrozen, isImmediate, isModule, isNil, isTaint, isTrue, isUntrusted, kind_of_p, makeMetaClass, method_missing19, method, method19, methods, methods, methods19, nil_p, op_cmp, op_equal_19, op_equal, op_match, op_match19, op_not_equal, op_not_match, op_not, private_methods, private_methods19, protected_methods, protected_methods19, public_methods, public_methods19, rbClone, recacheBuiltinMethods, remove_instance_variable, removeFinalizers, removeInstanceVariable, removeInternalVariable, respond_to_p, respond_to_p, respond_to_p19, respond_to_p19, respondsTo, respondsToMissing, respondsToMissing, send, send, send, send, send, send19, send19, send19, send19, setFFIHandle, setFlag, setFrozen, setInstanceVariable, setInternalVariable, setMetaClass, setNativeHandle, setTaint, setUntrusted, setVariable, singleton_method_added19, singleton_method_removed19, singleton_method_undefined19, singleton_methods, singleton_methods19, specificEval, specificEval, specificEval, specificEval, syncVariables, syncVariables, taint, taint, tainted_p, testFrozen, testFrozen, to_a, to_s, toJava, trust, type_deprecated, type, untaint, untrust, untrusted_p, validateInstanceVariable, variableTableContains, variableTableFastContains, variableTableFastFetch, variableTableFastStore, variableTableFetch, variableTableRemove, variableTableStore, variableTableSync, yieldUnder, yieldUnderprivate static final Logger LOG
private ThreadLike threadImpl
private transient java.util.Map<IRubyObject,IRubyObject> threadLocalVariables
private final java.util.Map<java.lang.Object,IRubyObject> contextVariables
private boolean abortOnException
private IRubyObject finalResult
private RaiseException exitingException
private RubyThreadGroup threadGroup
private IRubyObject errorInfo
private volatile java.lang.ref.WeakReference<ThreadContext> contextRef
private static final boolean DEBUG
private int RUBY_MIN_THREAD_PRIORITY
private int RUBY_MAX_THREAD_PRIORITY
private final java.util.concurrent.atomic.AtomicReference<RubyThread.Status> status
private final java.util.concurrent.atomic.AtomicReference<ThreadService.Event> mail
private volatile RubyThread.BlockingTask currentBlockingTask
private final java.util.List<java.util.concurrent.locks.Lock> heldLocks
private volatile boolean disposed
private int initialPriority
private volatile java.nio.channels.Selector currentSelector
private volatile BlockingIO.Condition blockingIO
public RubyThread(Ruby runtime, RubyClass klass, ThreadedRunnable runnable)
public void receiveMail(ThreadService.Event event)
public void checkMail(ThreadContext context)
public IRubyObject getErrorInfo()
public IRubyObject setErrorInfo(IRubyObject errorInfo)
public void setContext(ThreadContext context)
public ThreadContext getContext()
public java.lang.Thread getNativeThread()
public void beforeStart()
public void dispose()
public static IRubyObject newInstance(IRubyObject recv, IRubyObject[] args, Block block)
Thread.new
Thread.new( [ arg ]* ) {| args | block } -> aThread
Creates a new thread to execute the instructions given in block, and begins running it. Any arguments passed to Thread.new are passed into the block.
x = Thread.new { sleep .1; print "x"; print "y"; print "z" }
a = Thread.new { print "a"; print "b"; sleep .2; print "c" }
x.join # Let the threads finish before
a.join # main thread exits...
produces: abxyzcpublic static RubyThread start(IRubyObject recv, IRubyObject[] args, Block block)
public static RubyThread start19(IRubyObject recv, IRubyObject[] args, Block block)
public static RubyThread adopt(IRubyObject recv, java.lang.Thread t)
private static RubyThread adoptThread(IRubyObject recv, java.lang.Thread t, Block block)
public IRubyObject initialize(ThreadContext context, IRubyObject[] args, Block block)
private IRubyObject startWith(ThreadedRunnable runnable) throws RaiseException, java.lang.OutOfMemoryError
RaiseExceptionjava.lang.OutOfMemoryErrorprivate static RubyThread startThread(IRubyObject recv, IRubyObject[] args, boolean callInit, Block block)
public void cleanTerminate(IRubyObject result)
public void beDead()
public void pollThreadEvents()
public void pollThreadEvents(ThreadContext context)
private static void throwThreadKill()
public static RubyBoolean abort_on_exception_x(IRubyObject recv)
public static IRubyObject abort_on_exception_set_x(IRubyObject recv, IRubyObject value)
public static RubyThread current(IRubyObject recv)
public static RubyThread main(IRubyObject recv)
public static IRubyObject pass(IRubyObject recv)
public static RubyArray list(IRubyObject recv)
private void addToCorrectThreadGroup(ThreadContext context)
private IRubyObject getSymbolKey(IRubyObject originalKey)
private java.util.Map<IRubyObject,IRubyObject> getThreadLocals()
private void clearThreadLocals()
public final java.util.Map<java.lang.Object,IRubyObject> getContextVariables()
getContextVariables in interface ExecutionContextpublic boolean isAlive()
public IRubyObject op_aref(IRubyObject key)
public IRubyObject op_aset(IRubyObject key, IRubyObject value)
public RubyBoolean abort_on_exception()
public IRubyObject abort_on_exception_set(IRubyObject val)
public RubyBoolean alive_p()
public IRubyObject join(IRubyObject[] args)
public IRubyObject value()
public IRubyObject group()
void setThreadGroup(RubyThreadGroup rubyThreadGroup)
public IRubyObject inspect()
RubyBasicObjectto_s method to
generate the string.
[ 1, 2, 3..4, 'five' ].inspect #=> "[1, 2, 3..4, \"five\"]"
Time.new.inspect #=> "Wed Apr 09 08:54:39 CDT 2003"inspect in interface IRubyObjectinspect in class RubyBasicObjectpublic RubyBoolean key_p(IRubyObject key)
public RubyArray keys()
public static IRubyObject critical_set(IRubyObject receiver, IRubyObject value)
public static IRubyObject critical(IRubyObject receiver)
public static IRubyObject stop(ThreadContext context, IRubyObject receiver)
public static IRubyObject kill(IRubyObject receiver, IRubyObject rubyThread, Block block)
public static IRubyObject exit(IRubyObject receiver, Block block)
public RubyBoolean stop_p()
public RubyThread wakeup()
public RubyFixnum priority()
public IRubyObject priority_set(IRubyObject priority)
private int javaPriorityToRubyPriority(int javaPriority)
private int rubyPriorityToJavaPriority(int rubyPriority)
public IRubyObject raise(IRubyObject exception)
exception - the exception to raisepublic IRubyObject raise(IRubyObject[] args, Block block)
public void internalRaise(IRubyObject[] args)
args - Same args as for Thread#raiseblock - Same as for Thread#raiseprivate IRubyObject prepareRaiseException(Ruby runtime, IRubyObject[] args, Block block)
public IRubyObject run()
public boolean sleep(long millis)
throws java.lang.InterruptedException
java.lang.InterruptedExceptionpublic IRubyObject status()
public IRubyObject status(ThreadContext context)
private IRubyObject status(Ruby runtime)
public void executeBlockingTask(RubyThread.BlockingTask task) throws java.lang.InterruptedException
java.lang.InterruptedExceptionpublic void enterSleep()
public void exitSleep()
public IRubyObject kill()
public void dieFromFinalizer()
private static void debug(RubyThread thread, java.lang.String message)
public IRubyObject kill_bang()
public IRubyObject safe_level()
public IRubyObject backtrace(ThreadContext context)
public IRubyObject backtrace20(ThreadContext context, IRubyObject[] args)
public IRubyObject backtrace_locations(ThreadContext context, IRubyObject[] args)
public java.lang.StackTraceElement[] javaBacktrace()
private boolean isCurrent()
public void exceptionRaised(RaiseException exception)
public void exceptionRaised(java.lang.Throwable exception)
exception - private boolean abortOnException(Ruby runtime)
public static RubyThread mainThread(IRubyObject receiver)
@Deprecated public boolean selectForAccept(RubyIO io)
private java.nio.channels.Selector getSelector(java.nio.channels.SelectableChannel channel)
throws java.io.IOException
java.io.IOExceptionpublic boolean select(RubyIO io, int ops)
public boolean select(RubyIO io, int ops, long timeout)
public boolean select(java.nio.channels.Channel channel,
RubyIO io,
int ops)
public boolean select(java.nio.channels.Channel channel,
RubyIO io,
int ops,
long timeout)
public void interrupt()
public boolean waitForIO(ThreadContext context, RubyIO io, int ops)
public void beforeBlockingCall()
public void afterBlockingCall()
private void receivedAnException(ThreadContext context, IRubyObject exception)
public boolean wait_timeout(IRubyObject o, java.lang.Double timeout) throws java.lang.InterruptedException
java.lang.InterruptedExceptionpublic RubyThreadGroup getThreadGroup()
public boolean equals(java.lang.Object obj)
RubyObjectequals in class RubyObjectpublic int hashCode()
RubyObjecthashCode in class RubyObjectpublic java.lang.String toString()
RubyObjecttoString in class RubyObjectpublic void lock(java.util.concurrent.locks.Lock lock)
lock - the lock to acquire, released on thread terminationpublic void lockInterruptibly(java.util.concurrent.locks.Lock lock)
throws java.lang.InterruptedException
lock - the lock to acquire, released on thread terminationjava.lang.InterruptedException - if the lock acquisition is interruptedpublic boolean tryLock(java.util.concurrent.locks.Lock lock)
lock - the lock to acquire, released on thread terminationpublic void unlock(java.util.concurrent.locks.Lock lock)
lock - the lock to release and dereferencespublic void unlockAll()
private java.lang.String identityString()