View Javadoc

1   /***
2    *  Copyright 2003-2010 Terracotta, Inc.
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package net.sf.ehcache.concurrent;
17  
18  import java.util.concurrent.locks.ReentrantReadWriteLock;
19  import java.util.concurrent.locks.Lock;
20  import java.util.concurrent.TimeUnit;
21  import java.util.concurrent.locks.ReadWriteLock;
22  
23  /***
24   * A simple ReadWriteLock synchronizer.
25   *
26   * @author Alex Snaps
27   */
28  public class ReadWriteLockSync implements Sync {
29  
30      private final ReentrantReadWriteLock rrwl;
31  
32      /***
33       * default constructor.
34       */
35      public ReadWriteLockSync() {
36          this(new ReentrantReadWriteLock());
37      }
38  
39      /***
40       * Constructor.
41       * @param lock
42       */
43      public ReadWriteLockSync(ReentrantReadWriteLock lock) {
44          this.rrwl = lock;
45      }
46      /***
47       * {@inheritDoc}
48       */
49      public void lock(final LockType type) {
50          getLock(type).lock();
51      }
52  
53      /***
54       * {@inheritDoc}
55       */
56      public boolean tryLock(final LockType type, final long msec) throws InterruptedException {
57          return getLock(type).tryLock(msec, TimeUnit.MILLISECONDS);
58      }
59  
60      /***
61       * {@inheritDoc}
62       */
63      public void unlock(final LockType type) {
64          getLock(type).unlock();
65      }
66  
67      private Lock getLock(final LockType type) {
68          switch (type) {
69              case READ:
70                  return rrwl.readLock();
71              case WRITE:
72                  return rrwl.writeLock();
73              default:
74                  throw new IllegalArgumentException("We don't support any other lock type than READ or WRITE!");
75          }
76      }
77  
78      /***
79       * Gets the {@code ReadWriteLock} backing this sync.
80       * 
81       * @return the backing {@code ReadWriteLock}
82       */
83      public ReadWriteLock getReadWriteLock() {
84        return rrwl;
85      }
86      
87      /***
88       * {@inheritDoc}
89       */
90      public boolean isHeldByCurrentThread(LockType type) {
91          switch (type) {
92              case READ:
93                  throw new UnsupportedOperationException("Querying of read lock is not supported.");
94              case WRITE:
95                  return rrwl.isWriteLockedByCurrentThread();
96              default:
97                  throw new IllegalArgumentException("We don't support any other lock type than READ or WRITE!");
98          }
99      }
100 }