90
90
import java .util .Map ;
91
91
import java .util .Set ;
92
92
import java .util .concurrent .TimeUnit ;
93
- import java .util .function .Function ;
94
93
import java .util .logging .Level ;
95
94
96
95
import org .graalvm .nativeimage .ImageInfo ;
@@ -425,9 +424,10 @@ public int[] pipeMessage() throws PosixException {
425
424
public SelectResult select (int [] readfds , int [] writefds , int [] errorfds , Timeval timeout ) throws PosixException {
426
425
SelectableChannel [] readChannels = getSelectableChannels (readfds );
427
426
SelectableChannel [] writeChannels = getSelectableChannels (writefds );
428
- SelectableChannel [] errChannels = getSelectableChannels (errorfds );
427
+ // Java doesn't support any exceptional conditions we could apply on errfds
428
+ // TODO raise exception if errfds is not a subset of readfds & writefds?
429
429
430
- boolean [] wasBlocking = new boolean [readChannels .length + writeChannels .length + errChannels . length ];
430
+ boolean [] wasBlocking = new boolean [readChannels .length + writeChannels .length ];
431
431
int i = 0 ;
432
432
433
433
for (SelectableChannel channel : readChannels ) {
@@ -436,25 +436,19 @@ public SelectResult select(int[] readfds, int[] writefds, int[] errorfds, Timeva
436
436
for (SelectableChannel channel : writeChannels ) {
437
437
wasBlocking [i ++] = channel .isBlocking ();
438
438
}
439
- for ( SelectableChannel channel : errChannels ) {
440
- wasBlocking [ i ++] = channel . isBlocking () ;
441
- }
439
+
440
+ final int readOps = SelectionKey . OP_READ | SelectionKey . OP_ACCEPT ;
441
+ final int writeOps = SelectionKey . OP_WRITE ;
442
442
443
443
try (Selector selector = Selector .open ()) {
444
444
for (SelectableChannel channel : readChannels ) {
445
445
channel .configureBlocking (false );
446
- channel .register (selector , ( SelectionKey . OP_READ | SelectionKey . OP_ACCEPT ) & channel .validOps ());
446
+ channel .register (selector , readOps & channel .validOps ());
447
447
}
448
448
449
449
for (SelectableChannel channel : writeChannels ) {
450
450
channel .configureBlocking (false );
451
- channel .register (selector , SelectionKey .OP_WRITE );
452
- }
453
-
454
- for (SelectableChannel channel : errChannels ) {
455
- // TODO(fa): not sure if these ops are representing "exceptional condition pending"
456
- channel .configureBlocking (false );
457
- channel .register (selector , SelectionKey .OP_CONNECT );
451
+ channel .register (selector , writeOps );
458
452
}
459
453
460
454
// IMPORTANT: The meaning of the timeout value is slightly different: 'timeout == 0.0'
@@ -478,9 +472,9 @@ public SelectResult select(int[] readfds, int[] writefds, int[] errorfds, Timeva
478
472
int selected = useSelectNow ? selector .selectNow () : selector .select (timeoutMs );
479
473
480
474
// remove non-selected channels from given lists
481
- boolean [] resReadfds = createSelectedMap (readfds , readChannels , selector , SelectionKey :: isReadable );
482
- boolean [] resWritefds = createSelectedMap (writefds , writeChannels , selector , SelectionKey :: isWritable );
483
- boolean [] resErrfds = createSelectedMap ( errorfds , errChannels , selector , key -> key . isAcceptable () || key . isConnectable ()) ;
475
+ boolean [] resReadfds = createSelectedMap (readfds , readChannels , selector , readOps );
476
+ boolean [] resWritefds = createSelectedMap (writefds , writeChannels , selector , writeOps );
477
+ boolean [] resErrfds = new boolean [ errorfds . length ] ;
484
478
485
479
assert selected == countSelected (resReadfds ) + countSelected (resWritefds ) + countSelected (resErrfds );
486
480
return new SelectResult (resReadfds , resWritefds , resErrfds );
@@ -499,23 +493,18 @@ public SelectResult select(int[] readfds, int[] writefds, int[] errorfds, Timeva
499
493
channel .configureBlocking (true );
500
494
}
501
495
}
502
- for (SelectableChannel channel : errChannels ) {
503
- if (wasBlocking [i ++]) {
504
- channel .configureBlocking (true );
505
- }
506
- }
507
496
} catch (IOException e ) {
508
497
// We didn't manage to restore the blocking status, ignore
509
498
}
510
499
}
511
500
}
512
501
513
- private static boolean [] createSelectedMap (int [] fds , SelectableChannel [] channels , Selector selector , Function < SelectionKey , Boolean > selectedPredicate ) {
502
+ private static boolean [] createSelectedMap (int [] fds , SelectableChannel [] channels , Selector selector , int op ) {
514
503
boolean [] result = new boolean [fds .length ];
515
504
for (int i = 0 ; i < channels .length ; i ++) {
516
505
SelectableChannel channel = channels [i ];
517
506
SelectionKey selectionKey = channel .keyFor (selector );
518
- result [i ] = selectedPredicate . apply (selectionKey ) ;
507
+ result [i ] = (selectionKey . readyOps () & op ) != 0 ;
519
508
}
520
509
return result ;
521
510
}
0 commit comments