1 package org.eclipse.aether.named.support;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.util.ArrayDeque;
23 import java.util.Deque;
24 import java.util.concurrent.TimeUnit;
25 import java.util.concurrent.locks.ReadWriteLock;
26
27 /**
28 * Named lock support implementation that is using {@link ReadWriteLock} instances. The adapted lock MUST SUPPORT
29 * reentrancy, non re-entrant locks will NOT work. It is the responsibility of an adapting lock, to ensure that
30 * above lock requirement stands.
31 */
32 public class ReadWriteLockNamedLock
33 extends NamedLockSupport
34 {
35 private enum Step
36 {
37 /**
38 * Step when {@link ReadWriteLock#readLock()} was locked
39 */
40 SHARED,
41
42 /**
43 * Step when {@link ReadWriteLock#writeLock()} was locked
44 */
45 EXCLUSIVE
46 }
47
48 private final ThreadLocal<Deque<Step>> threadSteps;
49
50 private final ReadWriteLock readWriteLock;
51
52 public ReadWriteLockNamedLock( final String name,
53 final NamedLockFactorySupport factory,
54 final ReadWriteLock readWriteLock )
55 {
56 super( name, factory );
57 this.threadSteps = ThreadLocal.withInitial( ArrayDeque::new );
58 this.readWriteLock = readWriteLock;
59 }
60
61 @Override
62 public boolean lockShared( final long time, final TimeUnit unit ) throws InterruptedException
63 {
64 Deque<Step> steps = threadSteps.get();
65 if ( readWriteLock.readLock().tryLock( time, unit ) )
66 {
67 steps.push( Step.SHARED );
68 return true;
69 }
70 return false;
71 }
72
73 @Override
74 public boolean lockExclusively( final long time, final TimeUnit unit ) throws InterruptedException
75 {
76 Deque<Step> steps = threadSteps.get();
77 if ( !steps.isEmpty() )
78 { // we already own shared or exclusive lock
79 if ( !steps.contains( Step.EXCLUSIVE ) )
80 {
81 return false; // Lock upgrade not supported
82 }
83 }
84 if ( readWriteLock.writeLock().tryLock( time, unit ) )
85 {
86 steps.push( Step.EXCLUSIVE );
87 return true;
88 }
89 return false;
90 }
91
92 @Override
93 public void unlock()
94 {
95 Deque<Step> steps = threadSteps.get();
96 if ( steps.isEmpty() )
97 {
98 throw new IllegalStateException( "Wrong API usage: unlock without lock" );
99 }
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 }