Skip to content

Commit

Permalink
Just use a normal reentrant lock. read + write acquire hangs.
Browse files Browse the repository at this point in the history
  • Loading branch information
headius committed Nov 9, 2014
1 parent 7dd4cb6 commit 1148ce4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 30 deletions.
12 changes: 6 additions & 6 deletions core/src/main/java/org/jruby/RubyIO.java
Expand Up @@ -1476,11 +1476,11 @@ public RubyBoolean sync(ThreadContext context) {

RubyIO io = GetWriteIO();
fptr = io.getOpenFileChecked();
fptr.lockReadOnly();
fptr.lock();
try {
return (fptr.getMode() & OpenFile.SYNC) != 0 ? runtime.getTrue() : runtime.getFalse();
} finally {
fptr.unlockReadOnly();
fptr.unlock();
}
}

Expand Down Expand Up @@ -1806,13 +1806,13 @@ public RubyBoolean tty_p(ThreadContext context) {

fptr = getOpenFileChecked();

fptr.lockReadOnly();
fptr.lock();
try {
if (fptr.isStdio()) return runtime.getTrue();
if (runtime.getPosix().isNative() && runtime.getPosix().libc().isatty(fptr.getFileno()) != 0)
return runtime.getTrue();
} finally {
fptr.unlockReadOnly();
fptr.unlock();
}

return runtime.getFalse();
Expand All @@ -1837,7 +1837,7 @@ public IRubyObject initialize_copy(IRubyObject _io){
fptr = dest.MakeOpenFile();

// orig is the visible one here
orig.lockReadOnly();
orig.lock();
try {
io.flush(context);

Expand All @@ -1860,7 +1860,7 @@ public IRubyObject initialize_copy(IRubyObject _io){
if (0 <= pos)
fptr.seek(context, pos, PosixShim.SEEK_SET);
} finally {
orig.unlockReadOnly();
orig.unlock();
}

if (fptr.isBinmode()) {
Expand Down
39 changes: 15 additions & 24 deletions core/src/main/java/org/jruby/util/io/OpenFile.java
Expand Up @@ -41,6 +41,7 @@
import java.nio.channels.WritableByteChannel;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class OpenFile implements Finalizable {
Expand Down Expand Up @@ -130,9 +131,7 @@ public static class Buffer {
public boolean writeconvInitialized;

public volatile ReentrantReadWriteLock write_lock;
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
private final ReentrantReadWriteLock.WriteLock writeLock = lock.writeLock();
private final ReentrantReadWriteLock.ReadLock readLock = lock.readLock();
private final ReentrantLock lock = new ReentrantLock();

public final Buffer wbuf = new Buffer(), rbuf = new Buffer(), cbuf = new Buffer();

Expand Down Expand Up @@ -410,7 +409,7 @@ public void checkReadable(ThreadContext context) {

// io_fflush
public int io_fflush(ThreadContext context) {
assert writeLock.isHeldByCurrentThread();
assert lock.isHeldByCurrentThread();

checkClosed();

Expand All @@ -430,7 +429,7 @@ public int io_fflush(ThreadContext context) {

// rb_io_wait_writable
public boolean waitWritable(ThreadContext context, long timeout) {
assert writeLock.isHeldByCurrentThread();
assert lock.isHeldByCurrentThread();

if (posix.errno == null) return false;

Expand All @@ -457,7 +456,7 @@ public boolean waitWritable(ThreadContext context) {

// rb_io_wait_readable
public boolean waitReadable(ThreadContext context, long timeout) {
assert writeLock.isHeldByCurrentThread();
assert lock.isHeldByCurrentThread();

if (posix.errno == null) return false;

Expand Down Expand Up @@ -495,7 +494,7 @@ public boolean ready(ThreadContext context, int ops) {
* @return
*/
public boolean ready(Ruby runtime, RubyThread thread, int ops, long timeout) {
assert writeLock.isHeldByCurrentThread();
assert lock.isHeldByCurrentThread();

try {
if (fd.chSelect != null) {
Expand Down Expand Up @@ -637,15 +636,15 @@ boolean wsplit()

// io_seek
public long seek(ThreadContext context, long offset, int whence) {
assert writeLock.isHeldByCurrentThread();
assert lock.isHeldByCurrentThread();

flushBeforeSeek(context);
return posix.lseek(fd, offset, whence);
}

// flush_before_seek
private void flushBeforeSeek(ThreadContext context) {
assert writeLock.isHeldByCurrentThread();
assert lock.isHeldByCurrentThread();

if (io_fflush(context) < 0)
throw context.runtime.newErrnoFromErrno(posix.errno, "");
Expand Down Expand Up @@ -2393,12 +2392,12 @@ public boolean isStdio() {
}

public int readPending() {
lockReadOnly();
lock();
try {
if (READ_CHAR_PENDING()) return 1;
return READ_DATA_PENDING_COUNT();
} finally {
unlockReadOnly();
unlock();
}
}

Expand Down Expand Up @@ -2618,30 +2617,22 @@ public int remainSize() {
return siz;
}

public void lockReadOnly() {
readLock.lock();
}

public void unlockReadOnly() {
readLock.unlock();
}

public boolean lock() {
if (writeLock.isHeldByCurrentThread()) {
if (lock.isHeldByCurrentThread()) {
return false;
} else {
writeLock.lock();
lock.lock();
return true;
}
}

public void unlock() {
assert writeLock.isHeldByCurrentThread();
assert lock.isHeldByCurrentThread();

writeLock.unlock();
lock.unlock();
}

public boolean lockedByMe() {
return writeLock.isHeldByCurrentThread();
return lock.isHeldByCurrentThread();
}
}

0 comments on commit 1148ce4

Please sign in to comment.