net.sf.eBus.timer.EScheduledExecutorSpinning Maven / Gradle / Ivy
The newest version!
//
// Copyright 2024 Charles W. Rapp
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package net.sf.eBus.timer;
import java.util.concurrent.CountDownLatch;
import javax.annotation.Nullable;
import net.sf.eBus.config.EConfigure.ScheduledExecutor;
/**
* Provides the lowest latency delivery of expired timer task its
* eBus object's event queue. This scheduled executor achieves
* this low latency by spinning on a tight loop waiting for the
* next timer to expire.
*
* A spinning scheduled executor is best used with thread
* affinity for a core isolated from the operating system.
*
*
* @see EScheduledExecutorSpinSleep
* @see EScheduledExecutorSpinYield
* @see EScheduledExecutorBlocking
*
* @author Charles W. Rapp
*/
/* package */ final class EScheduledExecutorSpinning
extends EScheduledExecutorAbstractSpin
{
//---------------------------------------------------------------
// Member data.
//
//---------------------------------------------------------------
// Member methods.
//
//-----------------------------------------------------------
// Constructors.
//
/**
* Creates a new spinning eBus scheduled executor instance
* based on the given configuration.
* @param config scheculed executor configuration.
* @param startupSignal decremented when executor thread
* is running.
* @param shutdownSignal decrements when executor thread is
* shut down.
*/
/* package */ EScheduledExecutorSpinning(final ScheduledExecutor config,
final CountDownLatch startupSignal,
final CountDownLatch shutdownSignal)
{
super (config, startupSignal, shutdownSignal);
} // end of EScheduledExecutorSpinning()
//
// end of Constructors.
//-----------------------------------------------------------
//-----------------------------------------------------------
// Abstract Method Implementations.
//
@Override
protected @Nullable ETimerImpl pollTimer()
{
ETimerImpl retval = null;
while (mRunFlag && (retval = nextTimer()) == null)
{
// Keep spinning until a timer is scheduled.
}
return (retval);
} // end of pollTimer()
@Override
protected boolean waitForExpiration(final ETimerImpl timer)
{
final long expiration = timer.expiration();
long currNanos;
// Keep spinning until expiration time is reached or
// timers map is updated.
while ((currNanos = System.nanoTime()) < expiration &&
!mUpdateFlag.compareAndSet(true, false))
{}
return (currNanos >= expiration);
} // end of waitForExpiration(ETimerImpl)
//
// end of Abstract Method Implementations.
//-----------------------------------------------------------
} // end of class EScheduledExecutorSpinning