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
026import org.eclipse.aether.named.NamedLockKey;
027
028/**
029 * Named lock support implementation that is using {@link ReadWriteLock} instances. The adapted lock MUST SUPPORT
030 * reentrancy, non re-entrant locks will NOT work. It is the responsibility of an adapting lock, to ensure that
031 * above lock requirement stands.
032 */
033public class ReadWriteLockNamedLock extends NamedLockSupport {
034    private enum Step {
035        /**
036         * Step when {@link ReadWriteLock#readLock()} was locked
037         */
038        SHARED,
039
040        /**
041         * Step when {@link ReadWriteLock#writeLock()} was locked
042         */
043        EXCLUSIVE
044    }
045
046    private final ThreadLocal<Deque<Step>> threadSteps;
047
048    private final ReadWriteLock readWriteLock;
049
050    public ReadWriteLockNamedLock(
051            final NamedLockKey key, final NamedLockFactorySupport factory, final ReadWriteLock readWriteLock) {
052        super(key, factory);
053        this.threadSteps = ThreadLocal.withInitial(ArrayDeque::new);
054        this.readWriteLock = readWriteLock;
055    }
056
057    @Override
058    protected boolean doLockShared(final long time, final TimeUnit unit) throws InterruptedException {
059        Deque<Step> steps = threadSteps.get();
060        if (readWriteLock.readLock().tryLock(time, unit)) {
061            steps.push(Step.SHARED);
062            return true;
063        }
064        return false;
065    }
066
067    @Override
068    protected boolean doLockExclusively(final long time, final TimeUnit unit) throws InterruptedException {
069        Deque<Step> steps = threadSteps.get();
070        if (!steps.isEmpty()) { // we already own shared or exclusive lock
071            if (!steps.contains(Step.EXCLUSIVE)) {
072                throw new LockUpgradeNotSupportedException(this); // Lock upgrade not supported
073            }
074        }
075        if (readWriteLock.writeLock().tryLock(time, unit)) {
076            steps.push(Step.EXCLUSIVE);
077            return true;
078        }
079        return false;
080    }
081
082    @Override
083    protected void doUnlock() {
084        Deque<Step> steps = threadSteps.get();
085        if (steps.isEmpty()) {
086            throw new IllegalStateException("Wrong API usage: unlock without lock");
087        }
088        Step step = steps.pop();
089        if (Step.SHARED == step) {
090            readWriteLock.readLock().unlock();
091        } else if (Step.EXCLUSIVE == step) {
092            readWriteLock.writeLock().unlock();
093        }
094    }
095}