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 extends NamedLockSupport
033{
034    private enum Step
035    {
036        /**
037         * Step when {@link ReadWriteLock#readLock()} was locked
038         */
039        SHARED,
040
041        /**
042         * Step when {@link ReadWriteLock#writeLock()} was locked
043         */
044        EXCLUSIVE
045    }
046
047    private final ThreadLocal<Deque<Step>> threadSteps;
048
049    private final ReadWriteLock readWriteLock;
050
051    public ReadWriteLockNamedLock( final String name, final NamedLockFactorySupport factory,
052                                   final ReadWriteLock readWriteLock )
053    {
054        super( name, factory );
055        this.threadSteps = ThreadLocal.withInitial( ArrayDeque::new );
056        this.readWriteLock = readWriteLock;
057    }
058
059    @Override
060    public boolean lockShared( final long time, final TimeUnit unit ) throws InterruptedException
061    {
062        Deque<Step> steps = threadSteps.get();
063        if ( readWriteLock.readLock().tryLock( time, unit ) )
064        {
065            steps.push( Step.SHARED );
066            return true;
067        }
068        return false;
069    }
070
071    @Override
072    public boolean lockExclusively( final long time, final TimeUnit unit ) throws InterruptedException
073    {
074        Deque<Step> steps = threadSteps.get();
075        if ( !steps.isEmpty() )
076        { // we already own shared or exclusive lock
077            if ( !steps.contains( Step.EXCLUSIVE ) )
078            {
079                return false; // Lock upgrade not supported
080            }
081        }
082        if ( readWriteLock.writeLock().tryLock( time, unit ) )
083        {
084            steps.push( Step.EXCLUSIVE );
085            return true;
086        }
087        return false;
088    }
089
090    @Override
091    public void unlock()
092    {
093        Deque<Step> steps = threadSteps.get();
094        if ( steps.isEmpty() )
095        {
096            throw new IllegalStateException( "Wrong API usage: unlock w/o lock" );
097        }
098        Step step = steps.pop();
099        if ( Step.SHARED == step )
100        {
101            readWriteLock.readLock().unlock();
102        }
103        else if ( Step.EXCLUSIVE == step )
104        {
105            readWriteLock.writeLock().unlock();
106        }
107    }
108}