private boolean processCommands ( int timeout , boolean throttle ) { Command cmd ; if ( timeout != 0 ) { // If we are asked to wait, simply ask mailbox to wait. cmd = mailbox . recv ( timeout ) ; } else { // If we are asked not to wait, check whether we haven't processed // commands recently, so that we can throttle the new commands. // Get the CPU's tick counter. If 0, the counter is not available. long tsc = 0 ; // Clock.rdtsc(); // Optimized version of command processing - it doesn't have to check // for incoming commands each time. It does so only if certain time // elapsed since last command processing. Command delay varies // depending on CPU speed: It's ~1ms on 3GHz CPU, ~2ms on 1.5GHz CPU // etc. The optimization makes sense only on platforms where getting // a timestamp is a very cheap operation (tens of nanoseconds). if ( tsc != 0 && throttle ) { // Check whether TSC haven't jumped backwards (in case of migration // between CPU cores) and whether certain time have elapsed since // last command processing. If it didn't do nothing. if ( tsc >= lastTsc && tsc - lastTsc <= Config . MAX_COMMAND_DELAY . getValue ( ) ) { return true ; } lastTsc = tsc ; } // Check whether there are any commands pending for this thread. cmd = mailbox . recv ( 0 ) ; } // Process all the commands available at the moment. while ( cmd != null ) { cmd . process ( ) ; cmd = mailbox . recv ( 0 ) ; } if ( errno . get ( ) == ZError . EINTR ) { return false ; } assert ( errno . get ( ) == ZError . EAGAIN ) : errno ; if ( ctxTerminated ) { errno . set ( ZError . ETERM ) ; // Do not raise exception at the blocked operation return false ; } return true ; }
in a predefined time period .