1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.kernel.concurrent;
16  
17  import java.util.concurrent.ConcurrentHashMap;
18  import java.util.concurrent.ConcurrentMap;
19  import java.util.concurrent.locks.Lock;
20  import java.util.concurrent.locks.ReadWriteLock;
21  import java.util.concurrent.locks.ReentrantReadWriteLock;
22  
23  /**
24   * <a href="ReadWriteLockRegistry.java.html"><b><i>View Source</i></b></a>
25   *
26   * Registry for {@link ReadWriteLock} objects with {@link ReadWriteLockKey} as
27   * keys. The behavior of acquiring and releasing locks is provided by a {@link
28   * ConcurrentHashMap}. This class is completely thread safe and ensures that
29   * only one {@link ReadWriteLock} exists per key. <a
30   * href="ReadWriteLockRegistry.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Shuyang Zhou
33   * @see    ReadWriteLock
34   * @see    ReadWriteLockKey
35   */
36  public class ReadWriteLockRegistry {
37  
38      public Lock acquireLock(ReadWriteLockKey<?> readWriteLockKey) {
39          ReadWriteLock readWriteLock = _readWriteLockMap.get(readWriteLockKey);
40  
41          if (readWriteLock == null) {
42              ReadWriteLock newReadWriteLock = new ReentrantReadWriteLock();
43  
44              readWriteLock = _readWriteLockMap.putIfAbsent(
45                  readWriteLockKey, newReadWriteLock);
46  
47              if (readWriteLock == null) {
48                  readWriteLock = newReadWriteLock;
49              }
50          }
51  
52          if (readWriteLockKey.isWriteLock()) {
53              return readWriteLock.writeLock();
54          }
55          else {
56              return readWriteLock.readLock();
57          }
58      }
59  
60      public void releaseLock(ReadWriteLockKey<?> readWriteLockKey) {
61          if (readWriteLockKey.isWriteLock()) {
62              _readWriteLockMap.remove(readWriteLockKey);
63          }
64      }
65  
66      private ConcurrentMap<ReadWriteLockKey<?>, ReadWriteLock>
67          _readWriteLockMap = new ConcurrentHashMap
68              <ReadWriteLockKey<?>, ReadWriteLock>();
69  
70  }