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

net.sf.eBus.timer.EScheduledExecutorSpinSleep 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 net.sf.eBus.config.EConfigure;


/**
 * Alternates between spinning while waiting for next timer to
 * expire and {@link LockSupport#parkNanos(long) parking} for
 * a configured about of time. This technique provides good
 * timer delivery latency while not requiring this executor
 * thread to have core affinity.
 *
 * @see EScheduledExecutorSpinning
 * @see EScheduledExecutorSpinYield
 * @see EScheduledExecutorBlocking
 *
 * @author Charles W. Rapp
 */

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

    //-----------------------------------------------------------
    // Locals.
    //

    /**
     * Park for this nanosecond time.
     */
    private final long mParkTime;

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

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

    /**
     * Creates a new instance of EScheduledExecutorSpinSleep.
     */
    /* package */ EScheduledExecutorSpinSleep(final EConfigure.ScheduledExecutor config,
                                              final CountDownLatch startupSignal,
                                              final CountDownLatch shutdownSignal)
    {
        super (config, startupSignal, shutdownSignal);

        mParkTime = (config.parkTime()).toNanos();
    } // end of EScheduledExecutorSpinSleep(...)

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

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

    @Override
    protected 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(mParkTime);
                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(mParkTime);
                counter = mSpinLimit;
            }

            --counter;
        }

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

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy