001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.named.support;
020
021import java.util.ArrayDeque;
022import java.util.Deque;
023import java.util.concurrent.TimeUnit;
024import java.util.concurrent.locks.ReadWriteLock;
025
026/**
027 * Named lock support implementation that is using {@link ReadWriteLock} instances. The adapted lock MUST SUPPORT
028 * reentrancy, non re-entrant locks will NOT work. It is the responsibility of an adapting lock, to ensure that
029 * above lock requirement stands.
030 */
031public class ReadWriteLockNamedLock extends NamedLockSupport {
032    private enum Step {
033        /**
034         * Step when {@link ReadWriteLock#readLock()} was locked
035         */
036        SHARED,
037
038        /**
039         * Step when {@link ReadWriteLock#writeLock()} was locked
040         */
041        EXCLUSIVE
042    }
043
044    private final ThreadLocal<Deque<Step>> threadSteps;
045
046    private final ReadWriteLock readWriteLock;
047
048    public ReadWriteLockNamedLock(
049            final String name, final NamedLockFactorySupport factory, final ReadWriteLock readWriteLock) {
050        super(name, factory);
051        this.threadSteps = ThreadLocal.withInitial(ArrayDeque::new);
052        this.readWriteLock = readWriteLock;
053    }
054
055    @Override
056    protected boolean doLockShared(final long time, final TimeUnit unit) throws InterruptedException {
057        Deque<Step> steps = threadSteps.get();
058        if (readWriteLock.readLock().tryLock(time, unit)) {
059            steps.push(Step.SHARED);
060            return true;
061        }
062        return false;
063    }
064
065    @Override
066    protected boolean doLockExclusively(final long time, final TimeUnit unit) throws InterruptedException {
067        Deque<Step> steps = threadSteps.get();
068        if (!steps.isEmpty()) { // we already own shared or exclusive lock
069            if (!steps.contains(Step.EXCLUSIVE)) {
070                throw new LockUpgradeNotSupportedException(this); // Lock upgrade not supported
071            }
072        }
073        if (readWriteLock.writeLock().tryLock(time, unit)) {
074            steps.push(Step.EXCLUSIVE);
075            return true;
076        }
077        return false;
078    }
079
080    @Override
081    protected void doUnlock() {
082        Deque<Step> steps = threadSteps.get();
083        if (steps.isEmpty()) {
084            throw new IllegalStateException("Wrong API usage: unlock without lock");
085        }
086        Step step = steps.pop();
087        if (Step.SHARED == step) {
088            readWriteLock.readLock().unlock();
089        } else if (Step.EXCLUSIVE == step) {
090            readWriteLock.writeLock().unlock();
091        }
092    }
093}