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

net.openhft.chronicle.algo.locks.AcquisitionStrategies Maven / Gradle / Ivy

There is a newer version: 2.27ea0
Show newest version
/*
 * Copyright 2014 Higher Frequency Trading http://www.higherfrequencytrading.com
 *
 * 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.openhft.chronicle.algo.locks;

import net.openhft.chronicle.algo.bytes.Access;
import net.openhft.chronicle.core.threads.ThreadHints;

import java.util.concurrent.TimeUnit;

public final class AcquisitionStrategies {

    private AcquisitionStrategies() {
    }

    public static 
    AcquisitionStrategy spinLoop(long duration, TimeUnit unit) {
        return new SpinLoopAcquisitionStrategy<>(duration, unit);
    }

    public static 
    AcquisitionStrategy spinLoopOrFail(long duration, TimeUnit unit) {
        return new SpinLoopOrFailAcquisitionStrategy<>(duration, unit);
    }

    public static 
    AcquisitionStrategy spinLoopRegisteringWaitOrFail(
            long duration, TimeUnit unit) {
        return new SpinLoopWriteWithWaitsAcquisitionStrategy<>(duration, unit);
    }

    private static class SpinLoopAcquisitionStrategy
            implements AcquisitionStrategy {
        private final long durationNanos;

        private SpinLoopAcquisitionStrategy(long duration, TimeUnit unit) {
            durationNanos = unit.toNanos(duration);
        }

        @Override
        public  boolean acquire(TryAcquireOperation operation, S strategy,
                                   Access access, T t, long offset) {
            if (operation.tryAcquire(strategy, access, t, offset))
                return true;
            long deadLineNanos = System.nanoTime() + durationNanos;
            beforeLoop(strategy, access, t, offset);
            do {
                if (operation.tryAcquire(strategy, access, t, offset))
                    return true;
                ThreadHints.onSpinWait();
            } while (deadLineNanos - System.nanoTime() >= 0L); // overflow-cautious
            afterLoop(strategy, access, t, offset);
            return end();
        }

         void beforeLoop(S strategy, Access access, T t, long offset) {
        }

         void afterLoop(S strategy, Access access, T t, long offset) {
        }

        boolean end() {
            return false;
        }
    }

    private static class SpinLoopOrFailAcquisitionStrategy
            extends SpinLoopAcquisitionStrategy {

        private SpinLoopOrFailAcquisitionStrategy(long duration, TimeUnit unit) {
            super(duration, unit);
        }

        @Override
        boolean end() {
            throw new IllegalStateException("Failed to acquire the lock");
        }
    }

    private static class SpinLoopWriteWithWaitsAcquisitionStrategy<
            S extends ReadWriteWithWaitsLockingStrategy>
            extends SpinLoopOrFailAcquisitionStrategy {

        private SpinLoopWriteWithWaitsAcquisitionStrategy(long duration, TimeUnit unit) {
            super(duration, unit);
        }

        @Override
         void beforeLoop(S strategy, Access access, T t, long offset) {
            strategy.registerWait(access, t, offset);
        }

        @Override
         void afterLoop(S strategy, Access access, T t, long offset) {
            strategy.deregisterWait(access, t, offset);
        }
    }
}