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