Skip to content

Commit 4458259

Browse files
authored
Merge pull request #95 from mohamedhafez/eagain_pr
waitSelect improvements
2 parents 75ff3df + 33daff7 commit 4458259

File tree

2 files changed

+48
-43
lines changed

2 files changed

+48
-43
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ pkg
44
target
55
build.log
66
.idea
7+
*.iml
78
*.lock
89
lib/org

src/main/java/org/jruby/ext/openssl/SSLSocket.java

Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -386,49 +386,53 @@ private Object waitSelect(final int operations, final boolean blocking, final bo
386386
final SelectionKey key = channel.register(selector, operations);
387387

388388
try {
389-
io.addBlockingThread(thread);
390-
391389
final int[] result = new int[1];
392390

393-
thread.executeBlockingTask(new RubyThread.BlockingTask() {
394-
public void run() throws InterruptedException {
395-
try {
396-
if ( ! blocking ) {
397-
result[0] = selector.selectNow();
398-
399-
if ( result[0] == 0 ) {
400-
if ((operations & SelectionKey.OP_READ) != 0 && (operations & SelectionKey.OP_WRITE) != 0) {
401-
if ( key.isReadable() ) {
402-
writeWouldBlock(runtime, exception, result); return;
403-
}
404-
//else if ( key.isWritable() ) {
405-
// readWouldBlock(runtime, exception, result);
406-
//}
407-
else { //neither, pick one
408-
readWouldBlock(runtime, exception, result); return;
409-
}
410-
}
411-
else if ((operations & SelectionKey.OP_READ) != 0) {
412-
readWouldBlock(runtime, exception, result); return;
413-
}
414-
else if ((operations & SelectionKey.OP_WRITE) != 0) {
415-
writeWouldBlock(runtime, exception, result); return;
416-
}
391+
if ( ! blocking ) {
392+
try {
393+
result[0] = selector.selectNow();
394+
395+
if ( result[0] == 0 ) {
396+
if ((operations & SelectionKey.OP_READ) != 0 && (operations & SelectionKey.OP_WRITE) != 0) {
397+
if ( key.isReadable() ) {
398+
writeWouldBlock(runtime, exception, result);
417399
}
400+
//else if ( key.isWritable() ) {
401+
// readWouldBlock(runtime, exception, result);
402+
//}
403+
else { //neither, pick one
404+
readWouldBlock(runtime, exception, result);
405+
}
406+
}
407+
else if ((operations & SelectionKey.OP_READ) != 0) {
408+
readWouldBlock(runtime, exception, result);
418409
}
419-
else {
410+
else if ((operations & SelectionKey.OP_WRITE) != 0) {
411+
writeWouldBlock(runtime, exception, result);
412+
}
413+
}
414+
}
415+
catch (IOException ioe) {
416+
throw runtime.newRuntimeError("Error with selector: " + ioe.getMessage());
417+
}
418+
} else {
419+
io.addBlockingThread(thread);
420+
thread.executeBlockingTask(new RubyThread.BlockingTask() {
421+
public void run() throws InterruptedException {
422+
try {
420423
result[0] = selector.select();
421424
}
425+
catch (IOException ioe) {
426+
throw runtime.newRuntimeError("Error with selector: " + ioe.getMessage());
427+
}
422428
}
423-
catch (IOException ioe) {
424-
throw runtime.newRuntimeError("Error with selector: " + ioe.getMessage());
429+
430+
public void wakeup() {
431+
selector.wakeup();
425432
}
426-
}
433+
});
434+
}
427435

428-
public void wakeup() {
429-
selector.wakeup();
430-
}
431-
});
432436

433437
switch ( result[0] ) {
434438
case READ_WOULD_BLOCK_RESULT :
@@ -437,11 +441,9 @@ public void wakeup() {
437441
return runtime.newSymbol("wait_writable"); // exception: false
438442
case 0 : return Boolean.FALSE;
439443
default :
440-
if ( result[0] >= 1 ) {
441-
Set<SelectionKey> keySet = selector.selectedKeys();
442-
if ( keySet.iterator().next() == key ) return Boolean.TRUE;
443-
}
444-
return Boolean.FALSE;
444+
//key should always be contained in selectedKeys() here, however there is a bug in
445+
//JRuby <= 9.1.2.0 that makes this not always the case, so we have to check
446+
return selector.selectedKeys().contains(key) ? Boolean.TRUE : Boolean.FALSE;
445447
}
446448
}
447449
catch (InterruptedException interrupt) { return Boolean.FALSE; }
@@ -471,11 +473,13 @@ public void wakeup() {
471473
debugStackTrace(runtime, e);
472474
}
473475

474-
// remove this thread as a blocker against the given IO
475-
io.removeBlockingThread(thread);
476+
if (blocking) {
477+
// remove this thread as a blocker against the given IO
478+
io.removeBlockingThread(thread);
476479

477-
// clear thread state from blocking call
478-
thread.afterBlockingCall();
480+
// clear thread state from blocking call
481+
thread.afterBlockingCall();
482+
}
479483
}
480484
}
481485

0 commit comments

Comments
 (0)