public boolean isAlive(){ return threadImpl.isAlive() && status.get() != Status.DEAD; }
private boolean isCurrent() { return threadImpl.isCurrent(); }
public Thread getNativeThread() { return threadImpl.nativeThread(); }
@JRubyMethod(name = "priority=", required = 1) public IRubyObject priority_set(IRubyObject priority) { int iPriority = RubyNumeric.fix2int(priority); if (iPriority < RUBY_MIN_THREAD_PRIORITY) { iPriority = RUBY_MIN_THREAD_PRIORITY; } else if (iPriority > RUBY_MAX_THREAD_PRIORITY) { iPriority = RUBY_MAX_THREAD_PRIORITY; } if (threadImpl.isAlive()) { int jPriority = rubyPriorityToJavaPriority(iPriority); if (jPriority < Thread.MIN_PRIORITY) { jPriority = Thread.MIN_PRIORITY; } else if (jPriority > Thread.MAX_PRIORITY) { jPriority = Thread.MAX_PRIORITY; } threadImpl.setPriority(jPriority); } return RubyFixnum.newFixnum(getRuntime(), iPriority); }
@JRubyMethod public RubyFixnum priority() { return RubyFixnum.newFixnum(getRuntime(), javaPriorityToRubyPriority(threadImpl.getPriority())); }
private IRubyObject startThread(ThreadContext context, Runnable runnable) throws RaiseException, OutOfMemoryError { final Ruby runtime = context.runtime; try { Thread thread = new Thread(runnable); thread.setDaemon(true); this.file = context.getFile(); this.line = context.getLine(); initThreadName(runtime, thread, file, line); threadImpl = new NativeThread(this, thread); addToCorrectThreadGroup(context); // JRUBY-2380, associate thread early so it shows up in Thread.list right away, in case it doesn't run immediately runtime.getThreadService().associateThread(thread, this); // copy parent thread's interrupt masks interruptMaskStack.addAll(context.getThread().interruptMaskStack); threadImpl.start(); // We yield here to hopefully permit the target thread to schedule // MRI immediately schedules it, so this is close but not exact Thread.yield(); return this; } catch (OutOfMemoryError oome) { if (oome.getMessage().equals("unable to create new native thread")) { throw runtime.newThreadError(oome.getMessage()); } throw oome; } catch (SecurityException ex) { throw runtime.newThreadError(ex.getMessage()); } }
/** * Dispose of the current thread by tidying up connections to other stuff */ public void dispose() { if (disposed) return; synchronized (this) { if (disposed) return; disposed = true; // remove from parent thread group threadGroup.remove(this); // unlock all locked locks unlockAll(); // reset thread priority to initial if pooling if (Options.THREADPOOL_ENABLED.load()) { threadImpl.setPriority(initialPriority); } // mark thread as DEAD beDead(); } // unregister from runtime's ThreadService getRuntime().getThreadService().unregisterThread(this); }
@JRubyMethod(name = "name") public IRubyObject getName() { Ruby runtime = getRuntime(); CharSequence rubyName = threadImpl.getRubyName(); if (rubyName == null) return runtime.getNil(); return RubyString.newString(runtime, rubyName); }
protected void printReportExceptionWarning() { Ruby runtime = getRuntime(); String name = threadImpl.getReportName(); runtime.getErrorStream().println("warning: thread \"" + name + "\" terminated with exception (report_on_exception is true):"); }
@JRubyMethod(name = "name=", required = 1) public IRubyObject setName(IRubyObject name) { final Ruby runtime = getRuntime(); if (!name.isNil()) { RubyString nameStr = StringSupport.checkEmbeddedNulls(runtime, name); Encoding enc = nameStr.getEncoding(); if (!enc.isAsciiCompatible()) { throw runtime.newArgumentError("ASCII incompatible encoding (" + enc + ")"); } threadImpl.setRubyName(runtime.freezeAndDedupString(nameStr).asJavaString()); } else { threadImpl.setRubyName(null); } return name; }
@JRubyMethod(name = "priority=", required = 1) public IRubyObject priority_set(IRubyObject priority) { int iPriority = RubyNumeric.fix2int(priority); if (iPriority < RUBY_MIN_THREAD_PRIORITY) { iPriority = RUBY_MIN_THREAD_PRIORITY; } else if (iPriority > RUBY_MAX_THREAD_PRIORITY) { iPriority = RUBY_MAX_THREAD_PRIORITY; } if (threadImpl.isAlive()) { int jPriority = rubyPriorityToJavaPriority(iPriority); if (jPriority < Thread.MIN_PRIORITY) { jPriority = Thread.MIN_PRIORITY; } else if (jPriority > Thread.MAX_PRIORITY) { jPriority = Thread.MAX_PRIORITY; } threadImpl.setPriority(jPriority); } return RubyFixnum.newFixnum(getRuntime(), iPriority); }
@JRubyMethod(name = "priority") public RubyFixnum priority() { return RubyFixnum.newFixnum(getRuntime(), javaPriorityToRubyPriority(threadImpl.getPriority())); }
/** * Dispose of the current thread by tidying up connections to other stuff */ public void dispose() { if (disposed) return; synchronized (this) { if (disposed) return; disposed = true; // remove from parent thread group threadGroup.remove(this); // unlock all locked locks unlockAll(); // reset thread priority to initial if pooling if (Options.THREADPOOL_ENABLED.load()) { threadImpl.setPriority(initialPriority); } // mark thread as DEAD beDead(); } // unregister from runtime's ThreadService getRuntime().getThreadService().unregisterThread(this); }
@JRubyMethod(name = "name") public IRubyObject getName() { Ruby runtime = getRuntime(); CharSequence rubyName = threadImpl.getRubyName(); if (rubyName == null) return runtime.getNil(); return RubyString.newString(runtime, rubyName); }
protected void printReportExceptionWarning() { Ruby runtime = getRuntime(); String name = threadImpl.getReportName(); runtime.getErrorStream().println("warning: thread \"" + name + "\" terminated with exception (report_on_exception is true):"); }
@JRubyMethod(name = "name=", required = 1) public IRubyObject setName(IRubyObject name) { final Ruby runtime = getRuntime(); if (!name.isNil()) { RubyString nameStr = StringSupport.checkEmbeddedNulls(runtime, name); Encoding enc = nameStr.getEncoding(); if (!enc.isAsciiCompatible()) { throw runtime.newArgumentError("ASCII incompatible encoding (" + enc + ")"); } threadImpl.setRubyName(runtime.freezeAndDedupString(nameStr).asJavaString()); } else { threadImpl.setRubyName(null); } return name; }
@JRubyMethod(name = "priority=", required = 1) public IRubyObject priority_set(IRubyObject priority) { int iPriority = RubyNumeric.fix2int(priority); if (iPriority < RUBY_MIN_THREAD_PRIORITY) { iPriority = RUBY_MIN_THREAD_PRIORITY; } else if (iPriority > RUBY_MAX_THREAD_PRIORITY) { iPriority = RUBY_MAX_THREAD_PRIORITY; } if (threadImpl.isAlive()) { int jPriority = rubyPriorityToJavaPriority(iPriority); if (jPriority < Thread.MIN_PRIORITY) { jPriority = Thread.MIN_PRIORITY; } else if (jPriority > Thread.MAX_PRIORITY) { jPriority = Thread.MAX_PRIORITY; } threadImpl.setPriority(jPriority); } return RubyFixnum.newFixnum(getRuntime(), iPriority); }