Skip to content
Permalink
Browse files
Make sleep interrupts more robust by using Semaphore.
  • Loading branch information
headius committed Dec 11, 2014
1 parent 540b635 commit 39996efb46b9059de30dc346b20fe94bd45e84a3
Showing with 15 additions and 8 deletions.
  1. +15 −8 core/src/main/java/org/jruby/RubyThread.java
@@ -63,9 +63,12 @@

import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.Lock;

import org.jruby.anno.JRubyMethod;
import org.jruby.anno.JRubyClass;
import org.jruby.runtime.ClassIndex;
@@ -1152,7 +1155,9 @@ public synchronized IRubyObject run() {
*/
public boolean sleep(long millis) throws InterruptedException {
assert this == getRuntime().getCurrentContext().getThread();
if (executeTask(getContext(), new Object[]{this, millis, 0}, SLEEP_TASK2) >= millis) {
Semaphore sem = new Semaphore(1);
sem.acquire();
if (executeTask(getContext(), new Object[]{sem, millis, 0}, SLEEP_TASK2) >= millis) {
return true;
} else {
return false;
@@ -1221,20 +1226,22 @@ public void wakeup() {
private static final class SleepTask2 implements Task<Object[], Long> {
@Override
public Long run(ThreadContext context, Object[] data) throws InterruptedException {
Object syncObj = data[0];
long millis = (Long)data[1];
int nanos = (Integer)data[2];
synchronized (syncObj) {
long start = System.currentTimeMillis();
syncObj.wait(millis, nanos);
// TODO: nano handling?
return System.currentTimeMillis() - start;

long start = System.currentTimeMillis();
// TODO: nano handling?
if (millis == 0) {
((Semaphore) data[0]).acquire();
} else {
((Semaphore) data[0]).tryAcquire(millis, TimeUnit.MILLISECONDS);
}
return System.currentTimeMillis() - start;
}

@Override
public void wakeup(RubyThread thread, Object[] data) {
thread.getNativeThread().interrupt();
((Semaphore)data[0]).release();
}
}

0 comments on commit 39996ef

Please sign in to comment.