Top Description Inners Fields Constructors Methods
org.jruby.internal.runtime

public Class ThreadService

extends ThreadLocal<SoftReference<ThreadContext>>
Class Inheritance
Imports
java.lang.ref.SoftReference, java.util.concurrent.atomic.AtomicLong, java.util.concurrent.locks.ReentrantLock, java.util.ArrayList, .Collections, .Map, .WeakHashMap, org.jruby.Ruby, .RubyThread, org.jruby.exceptions.RaiseException, org.jruby.ext.fiber.ThreadFiber, org.jruby.runtime.builtin.IRubyObject, org.jruby.runtime.ThreadContext

ThreadService maintains references to all JRuby-specific thread data structures needed for Ruby's threading API and for JRuby's execution. The main structures are: In order to ensure these structures do not linger after the thread has terminated, most of them are either weakly or softly referenced. The references associated with these structures are: These data structures can come to life in one of two ways:

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class

Field Summary

Modifier and TypeField and Description
private final ReentrantLock
private ThreadContext
mainContext

A hard reference to the "main" context, so we always have one waiting for "main" thread execution.

private final ThreadGroup
rubyThreadGroup

The Java thread group into which we register all Ruby threads.

private final Map<Thread, RubyThread>
rubyThreadMap

A map from a Java Thread to its RubyThread instance.

private final Ruby
private final AtomicLong

Constructor Summary

AccessConstructor and Description
public
ThreadService(final Ruby runtime)

Method Summary

Modifier and TypeMethod and Description
private void
acquireCritical()
Deprecated

private RubyThread
private static ThreadContext
public void
associateThread(Thread thread, RubyThread rubyThread)

private static ThreadContext
public void
deliverEvent(RubyThread sender, RubyThread target, ThreadService.Event event)
Deprecated

public void
disposeCurrentThread()
Deprecated

public synchronized void
dissociateThread(Object thread)
Deprecated

public RubyThread[]
public boolean
getCritical()
Deprecated

public final ThreadContext

Returns:

The ThreadContext instance for the current thread, or a new one if none has previously been created or the old ThreadContext has been collected.
getCurrentContext
()

In order to provide an appropriate execution context for a given thread, we store ThreadContext instances in a threadlocal.

public static ThreadContext
public RubyThread
public boolean
getPolling()
Deprecated

public ThreadGroup
getRubyThreadGroup()
Deprecated

public Map<Object, RubyThread>
getRubyThreadMap()
Deprecated

public ThreadContext
public long
public void
public ThreadContext
private void
releaseCritical()
Deprecated

public void
setCritical(boolean critical)
Deprecated

public final void
setCurrentContext(ThreadContext context)
Deprecated

public void
setMainThread(Thread thread, RubyThread rubyThread)

public void
public void
public void
private void
unregisterThreadImpl(ThreadContext context, Thread nativeThread)

Inherited from java.lang.ThreadLocal:
getinitialValueremovesetwithInitial

Field Detail

criticalLockback to summary
private final ReentrantLock criticalLock
mainContextback to summary
private ThreadContext mainContext

A hard reference to the "main" context, so we always have one waiting for "main" thread execution.

rubyThreadGroupback to summary
private final ThreadGroup rubyThreadGroup

The Java thread group into which we register all Ruby threads. This is distinct from the RubyThreadGroup, which is simply a mutable collection of threads.

rubyThreadMapback to summary
private final Map<Thread, RubyThread> rubyThreadMap

A map from a Java Thread to its RubyThread instance. This is a synchronized WeakHashMap, so it weakly references its keys; this means that when the Thread goes away, eventually its entry in this map will follow.

runtimeback to summary
private final Ruby runtime
threadCountback to summary
private final AtomicLong threadCount

Constructor Detail

ThreadServiceback to summary
public ThreadService(final Ruby runtime)

Method Detail

acquireCriticalback to summary
private void acquireCritical()

Deprecated

Annotations
@Deprecated
adoptCurrentThreadback to summary
private RubyThread adoptCurrentThread()
adoptLoopback to summary
private static ThreadContext adoptLoop(ThreadService service)
associateThreadback to summary
public void associateThread(Thread thread, RubyThread rubyThread)
contextFromAdoptback to summary
private static ThreadContext contextFromAdopt(ThreadService service)
deliverEventback to summary
public void deliverEvent(RubyThread sender, RubyThread target, ThreadService.Event event)

Deprecated

Annotations
@Deprecated
disposeCurrentThreadback to summary
public void disposeCurrentThread()

Deprecated

Annotations
@Deprecated
dissociateThreadback to summary
public synchronized void dissociateThread(Object thread)

Deprecated

Annotations
@Deprecated
getActiveRubyThreadsback to summary
public RubyThread[] getActiveRubyThreads()
getCriticalback to summary
public boolean getCritical()

Deprecated

Annotations
@Deprecated
getCurrentContextback to summary
public final ThreadContext getCurrentContext()

In order to provide an appropriate execution context for a given thread, we store ThreadContext instances in a threadlocal. This method is a utility to get at that threadlocal context from anywhere in the program it may not be immediately available. This method should be used sparingly, and if it is possible to pass ThreadContext on the argument list, it is preferable. Description of behavior The threadlocal does not actually contain the ThreadContext directly; instead, it contains a SoftReference that holds the ThreadContext. This is to allow new threads to enter the system and execute Ruby code with a valid context, but still allow that context to garbage collect if the thread stays alive much longer. We use SoftReference here because WeakReference is collected too quickly, resulting in very expensive ThreadContext churn (and this originally lead to JRUBY-2261's leak of adopted RubyThread instances).

Returns:ThreadContext

The ThreadContext instance for the current thread, or a new one if none has previously been created or the old ThreadContext has been collected.

getCurrentContextback to summary
public static ThreadContext getCurrentContext(final ThreadService service)
getMainThreadback to summary
public RubyThread getMainThread()
getPollingback to summary
public boolean getPolling()

Deprecated

Annotations
@Deprecated
getRubyThreadGroupback to summary
public ThreadGroup getRubyThreadGroup()

Deprecated

Annotations
@Deprecated
getRubyThreadMapback to summary
public Map<Object, RubyThread> getRubyThreadMap()

Deprecated

Annotations
@Deprecated
getThreadContextForThreadback to summary
public ThreadContext getThreadContextForThread(RubyThread thread)

Deprecated

Annotations
@Deprecated
incrementAndGetThreadCountback to summary
public long incrementAndGetThreadCount()
initMainThreadback to summary
public void initMainThread()
registerNewThreadback to summary
public ThreadContext registerNewThread(RubyThread thread)
releaseCriticalback to summary
private void releaseCritical()

Deprecated

Annotations
@Deprecated
setCriticalback to summary
public void setCritical(boolean critical)

Deprecated

Annotations
@Deprecated
setCurrentContextback to summary
public final void setCurrentContext(ThreadContext context)

Deprecated

Annotations
@Deprecated
setMainThreadback to summary
public void setMainThread(Thread thread, RubyThread rubyThread)
teardownback to summary
public void teardown()
unregisterCurrentThreadback to summary
public void unregisterCurrentThread(ThreadContext context)
unregisterThreadback to summary
public void unregisterThread(RubyThread thread)
unregisterThreadImplback to summary
private void unregisterThreadImpl(ThreadContext context, Thread nativeThread)
org.jruby.internal.runtime back to summary

public Class ThreadService.Event

extends Object
Class Inheritance
Annotations
@Deprecated

Deprecated

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static enum

Field Summary

Modifier and TypeField and Description
public final String
public final IRubyObject
public final ThreadService.Event.Type

Constructor Summary

AccessConstructor and Description
public
Event(String description, ThreadService.Event.Type type)

public
Event(String description, ThreadService.Event.Type type, IRubyObject exception)

Method Summary

Modifier and TypeMethod and Description
public static ThreadService.Event
kill(RubyThread sender, RubyThread target, ThreadService.Event.Type type)
Deprecated

public static ThreadService.Event
raise(RubyThread sender, RubyThread target, ThreadService.Event.Type type, IRubyObject exception)
Deprecated

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

public static ThreadService.Event
wakeup(RubyThread sender, RubyThread target, ThreadService.Event.Type type)
Deprecated

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

descriptionback to summary
public final String description
exceptionback to summary
public final IRubyObject exception
typeback to summary
public final ThreadService.Event.Type type

Constructor Detail

Eventback to summary
public Event(String description, ThreadService.Event.Type type)
Eventback to summary
public Event(String description, ThreadService.Event.Type type, IRubyObject exception)

Method Detail

killback to summary
public static ThreadService.Event kill(RubyThread sender, RubyThread target, ThreadService.Event.Type type)

Deprecated

Annotations
@Deprecated
raiseback to summary
public static ThreadService.Event raise(RubyThread sender, RubyThread target, ThreadService.Event.Type type, IRubyObject exception)

Deprecated

Annotations
@Deprecated
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

wakeupback to summary
public static ThreadService.Event wakeup(RubyThread sender, RubyThread target, ThreadService.Event.Type type)

Deprecated

Annotations
@Deprecated
org.jruby.internal.runtime back to summary

public final Enum ThreadService.Event.Type

extends Enum<ThreadService.Event.Type>
Class Inheritance

Field Summary

Modifier and TypeField and Description
public static final ThreadService.Event.Type
public static final ThreadService.Event.Type
public static final ThreadService.Event.Type

Constructor Summary

AccessConstructor and Description
private
Type()

Method Summary

Modifier and TypeMethod and Description
public static ThreadService.Event.Type
public static ThreadService.Event.Type[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

KILLback to summary
public static final ThreadService.Event.Type KILL
RAISEback to summary
public static final ThreadService.Event.Type RAISE
WAKEUPback to summary
public static final ThreadService.Event.Type WAKEUP

Constructor Detail

Typeback to summary
private Type()

Method Detail

valueOfback to summary
public static ThreadService.Event.Type valueOf(String name)
valuesback to summary
public static ThreadService.Event.Type[] values()