Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
* Factor out real IO calc in IOOutputStream constructor
* Revert to RubyThread.getStatus to preserve native status
* Clean up some C-style declarations
  • Loading branch information
headius committed Mar 2, 2021
1 parent 9987e64 commit 5cbea1a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
14 changes: 6 additions & 8 deletions core/src/main/java/org/jruby/RubyIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -1548,26 +1548,24 @@ public int write(ThreadContext context, byte[] bytes, int start, int length, En
* @return the count of bytes written
*/
public int write(ThreadContext context, byte[] bytes, int start, int length, Encoding encoding, boolean nosync) {
Ruby runtime = context.runtime;
OpenFile fptr;
int n;

if (length == 0) return 0;

fptr = getOpenFileChecked();
OpenFile fptr = getOpenFileChecked();

boolean locked = fptr.lock();
try {
fptr = getOpenFileChecked();
fptr.checkWritable(context);

n = fptr.fwrite(context, bytes, start, length, encoding, nosync);
if (n == -1) throw runtime.newErrnoFromErrno(fptr.errno(), fptr.getPath());
int n = fptr.fwrite(context, bytes, start, length, encoding, nosync);

if (n == -1) throw context.runtime.newErrnoFromErrno(fptr.errno(), fptr.getPath());

return n;
} finally {
if (locked) fptr.unlock();
}

return n;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/org/jruby/RubyThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -1255,7 +1255,7 @@ public RubyString inspect(ThreadContext context) {
result.catString(Integer.toString(line + 1));
}
result.cat(' ');
result.catString(status.toString().toLowerCase());
result.catString(getStatus().toString().toLowerCase());
result.cat('>');
return result;
}
Expand Down Expand Up @@ -1673,7 +1673,7 @@ public void enterSleep() {
}

public void exitSleep() {
if (status != Status.ABORTING) {
if (getStatus() != Status.ABORTING) {
STATUS.set(this, Status.RUN);
}
}
Expand Down
30 changes: 17 additions & 13 deletions core/src/main/java/org/jruby/util/IOOutputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,7 @@ public IOOutputStream(final IRubyObject io, Encoding encoding, boolean checkAppe
this.runtime = io.getRuntime();

// If we can get a real IO from the object, we can do fast writes
RubyIO realIO = null;
if (io instanceof RubyIO) {
RubyIO tmpIO = (RubyIO) io;
if (fastWritable(tmpIO)) {
tmpIO = tmpIO.GetWriteIO();

// recheck write IO for IOness
if (tmpIO == io || fastWritable(tmpIO)) {
realIO = tmpIO;
}
}
}
this.realIO = realIO;
RubyIO realIO = this.realIO = getRealIO(io);

if (realIO == null || verifyCanWrite) {
final String site;
Expand All @@ -103,6 +91,22 @@ public IOOutputStream(final IRubyObject io, Encoding encoding, boolean checkAppe
this.encoding = encoding;
}

protected RubyIO getRealIO(IRubyObject io) {
RubyIO realIO = null;
if (io instanceof RubyIO) {
RubyIO tmpIO = (RubyIO) io;
if (fastWritable(tmpIO)) {
tmpIO = tmpIO.GetWriteIO();

// recheck write IO for IOness
if (tmpIO == io || fastWritable(tmpIO)) {
realIO = tmpIO;
}
}
}
return realIO;
}

protected boolean fastWritable(RubyIO io) {
return !io.isClosed() &&
io.isBuiltin("write");
Expand Down

0 comments on commit 5cbea1a

Please sign in to comment.