Skip to content

Commit

Permalink
remove use of TimeoutError, since it was controversial on ruby-core
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.codehaus.org/jruby/trunk/jruby@6186 961051c9-f516-0410-bf72-c9f7e237a7b7
  • Loading branch information
MenTaLguY committed Mar 12, 2008
1 parent 2c83092 commit 3750f3a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 34 deletions.
6 changes: 3 additions & 3 deletions lib/ruby/1.8/monitor.rb
Expand Up @@ -260,11 +260,11 @@ def mon_wait_for_cond(condition, timeout)
condition.instance_eval { @mon_n_waiters += 1 }
begin
mon_release
begin
if timeout
condition.wait(@mon_mutex, timeout)
else
condition.wait(@mon_mutex)
true
rescue TimeoutError
false
end
ensure
@mon_total_waiting += 1
Expand Down
9 changes: 8 additions & 1 deletion lib/ruby/1.8/timeout.rb
Expand Up @@ -32,7 +32,8 @@ module Timeout
##
# Raised by Timeout#timeout when the block times out.

Error = ::TimeoutError # TimeoutError is now part of core
class Error<Interrupt
end

##
# Executes the method's block. If the block execution terminates before +sec+
Expand Down Expand Up @@ -75,6 +76,12 @@ def timeout(n, e=Timeout::Error, &block) # :nodoc:
Timeout::timeout(n, e, &block)
end

##
# Another name for Timeout::Error, defined for backwards compatibility with
# earlier versions of timeout.rb.

TimeoutError = Timeout::Error # :nodoc:

if __FILE__ == $0
p timeout(5) {
45
Expand Down
5 changes: 2 additions & 3 deletions src/org/jruby/Ruby.java
Expand Up @@ -1003,7 +1003,6 @@ private void initExceptions() {
scriptError = defineClassIfAllowed("ScriptError", exceptionClass);
rangeError = defineClassIfAllowed("RangeError", standardError);
signalException = defineClassIfAllowed("SignalException", exceptionClass);
interruptError = defineClassIfAllowed("Interrupt", signalException);

if (profile.allowClass("NameError")) {
nameError = RubyNameError.createNameErrorClass(this, standardError);
Expand All @@ -1025,7 +1024,7 @@ private void initExceptions() {
}

defineClassIfAllowed("Fatal", exceptionClass);
defineClassIfAllowed("TimeoutError", interruptError);
defineClassIfAllowed("Interrupt", signalException);
defineClassIfAllowed("TypeError", standardError);
defineClassIfAllowed("ArgumentError", standardError);
defineClassIfAllowed("IndexError", standardError);
Expand Down Expand Up @@ -2614,7 +2613,7 @@ public ExecutorService getExecutor() {
groupStruct,
procStatusClass, exceptionClass, runtimeError, ioError,
scriptError, nameError, signalException, standardError,
interruptError, systemCallError, rangeError;
systemCallError, rangeError;

/**
* All the core modules we keep direct references to, for quick access and
Expand Down
57 changes: 30 additions & 27 deletions src/org/jruby/libraries/ThreadLibrary.java
Expand Up @@ -60,32 +60,20 @@ public void load(final Ruby runtime, boolean wrap) throws IOException {
SizedQueue.setup(runtime);
}

static void wait_timeout(IRubyObject o, Double timeout) throws InterruptedException {
boolean success = false;
try {
if ( timeout != null ) {
long delay_ns = (long)(timeout * 1000000000.0);
long start_ns = System.nanoTime();
if (delay_ns > 0) {
long delay_ms = delay_ns / 1000000;
int delay_ns_remainder = (int)( delay_ns % 1000000 );
o.wait(delay_ms, delay_ns_remainder);
}
long end_ns = System.nanoTime();
if ( ( end_ns - start_ns ) > delay_ns ) {
throw new RaiseException(o.getRuntime(), o.getRuntime().fastGetClass("TimeoutError"), "wait timed out", false);
}
} else {
o.wait();
}
success = true;
} finally {
// An exception may have caused us to miss a notify that we
// consumed, so do another notify in case someone else would
// have been ready to pick it up instead
if (!success) {
o.notify();
static boolean wait_timeout(IRubyObject o, Double timeout) throws InterruptedException {
if ( timeout != null ) {
long delay_ns = (long)(timeout * 1000000000.0);
long start_ns = System.nanoTime();
if (delay_ns > 0) {
long delay_ms = delay_ns / 1000000;
int delay_ns_remainder = (int)( delay_ns % 1000000 );
o.wait(delay_ms, delay_ns_remainder);
}
long end_ns = System.nanoTime();
return ( end_ns - start_ns ) <= delay_ns;
} else {
o.wait();
return true;
}
}

Expand Down Expand Up @@ -219,15 +207,30 @@ public IRubyObject wait_ruby(IRubyObject args[]) throws InterruptedException {
if (Thread.interrupted()) {
throw new InterruptedException();
}
boolean success = false;
try {
synchronized (this) {
mutex.unlock();
ThreadLibrary.wait_timeout(this, timeout);
try {
success = ThreadLibrary.wait_timeout(this, timeout);
} finally {
// An interrupt or timeout may have caused us to miss
// a notify that we consumed, so do another notify in
// case someone else is available to pick it up.
if (!success) {
this.notify();
}
}
}
} finally {
mutex.lock();
}
return getRuntime().getNil();
if (timeout != null) {
return getRuntime().newBoolean(success);
} else {
// backwards-compatibility
return getRuntime().getNil();
}
}

public synchronized IRubyObject broadcast() {
Expand Down

0 comments on commit 3750f3a

Please sign in to comment.