All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.eBus.timer.EScheduledExecutorSpinYield 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 java.util.concurrent.locks.LockSupport;
import javax.annotation.Nullable;
import net.sf.eBus.config.EConfigure;

/**
 * Alternates between spinning while waiting for next timer to
 * expire and {@link LockSupport#park() yielding} the core.
 * This technique provides good timer delivery latency while not
 * requiring this executor thread to have core affinity.
 *
 * @see EScheduledExecutorSpinning
 * @see EScheduledExecutorSpinSleep
 * @see EScheduledExecutorBlocking
 *
 * @author Charles W. Rapp
 */

/* package */ final class EScheduledExecutorSpinYield
    extends EScheduledExecutorAbstractSpin
{
//---------------------------------------------------------------
// Member data.
//

//---------------------------------------------------------------
// Member methods.
//

    //-----------------------------------------------------------
    // Constructors.
    //

    /**
     * Creates a new spin+park scheduled executor based on the
     * given configuration.
     * @param config contains scheduled executor configuration.
     * @param startupSignal decrement signal when scheduled
     * executor thread is up and running.
     * @param shutdownSignal decrement signal when scheduled
     * executor thread is shut down.
     */
    /* package */ EScheduledExecutorSpinYield(final EConfigure.ScheduledExecutor config,
                                              final CountDownLatch startupSignal,
                                              final CountDownLatch shutdownSignal)
    {
        super (config, startupSignal, shutdownSignal);
    } // end of EScheduledExecutorSpinYield(...)

    //
    // end of Constructors.
    //-----------------------------------------------------------

    //-----------------------------------------------------------
    // Abstract Method Implementations.
    //

    @Override
    protected @Nullable ETimerImpl pollTimer()
    {
        long counter = mSpinLimit;
        ETimerImpl retval = null;

        // Is this thread still running?
        // Was an scheduled timer acquired?
        while (mRunFlag && (retval = nextTimer()) == null)
        {
            // Yes, this thread is still running.
            // No, there is no scheduled timer.
            // Spin limit reached?
            if (counter == 0)
            {
                // Yes. Take a nap before continuing.
                LockSupport.park();
                counter = mSpinLimit;
            }

            --counter;
        }

        return (retval);
    } // end of pollTimer()

    @Override
    protected boolean waitForExpiration(final ETimerImpl timer)
    {
        final long expiration = timer.expiration();
        long currNanos;
        long counter = mSpinLimit;

        // Keep spinning until expiration time is reached or
        // timers map is updated.
        while ((currNanos = System.nanoTime()) < expiration &&
               !mUpdateFlag.compareAndSet(true, false))
        {
            // Time to take a nap?
            if (counter == 0)
            {
                // Yes. Nighty, night.
                LockSupport.park();
                counter = mSpinLimit;
            }

            --counter;
        }

        return (currNanos >= expiration);
    } // end of waitForExpiration(ETimerImpl)

    //
    // end of Abstract Method Implementations.
    //-----------------------------------------------------------
} // end of class EScheduledExecutorSpinYield




© 2015 - 2024 Weber Informatics LLC | Privacy Policy