View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.fulcrum.yaafi.framework.locking;
21  
22  
23  /**
24   * Convenience implementation of a read/write lock based on {@link GenericLock}.
25   * <br>
26   * <br>
27   * Reads are shared which means there can be any number of concurrent read
28   * accesses allowed by this lock. Writes are exclusive. This means when there is
29   * a write access no other access neither read nor write are allowed by this
30   * lock. Additionally, writes are preferred over reads in order to avoid starvation. The idea
31   * is that there are many readers, but few writers and if things work out bad the writer would
32   * never be served at all. That's why it is preferred.<br>
33   * <br>
34   * Calls to both {@link #acquireRead(Object, long)}and
35   * {@link #acquireWrite(Object, long)}are blocking and reentrant. Blocking
36   * means they will wait if they can not acquire the descired access, reentrant means that a lock
37   * request by a specific owner will always be compatible with other accesses on this lock by the
38   * same owner. E.g. if you already have a lock for writing and you try to acquire write access
39   * again you will not be blocked by this first lock, while others of course will be. This is the
40   * natural way you already know from Java monitors and synchronized blocks.
41   *
42   * @version $Revision: 1.1 $
43   * @see GenericLock
44   */
45  public class ReadWriteLock extends GenericLock {
46  
47      public static final int NO_LOCK = 0;
48  
49      public static final int READ_LOCK = 1;
50  
51      public static final int WRITE_LOCK = 2;
52  
53      /**
54       * Creates a new read/write lock.
55       *
56       * @param resourceId
57       *            identifier for the resource associated to this lock
58       * @param logger
59       *            generic logger used for all kind of debug logging
60       */
61      public ReadWriteLock(Object resourceId, LoggerFacade logger) {
62          super(resourceId, WRITE_LOCK, logger);
63      }
64  
65      /**
66       * Tries to acquire a blocking, reentrant read lock. A read lock is
67       * compatible with other read locks, but not with a write lock.
68       *
69       * @param ownerId
70       *            a unique id identifying the entity that wants to acquire a
71       *            certain lock level on this lock
72       * @param timeoutMSecs
73       *            if blocking is enabled by the <code>wait</code> parameter
74       *            this specifies the maximum wait time in milliseconds
75       * @return <code>true</code> if the lock actually was acquired
76       * @throws InterruptedException
77       *             when the thread waiting on this method is interrupted
78       */
79      public boolean acquireRead(Object ownerId, long timeoutMSecs) throws InterruptedException {
80          return acquire(ownerId, READ_LOCK, false, timeoutMSecs);
81      }
82  
83      /**
84       * Tries to acquire a blocking, reentrant write lock. A write lock is
85       * incompatible with any another read or write lock and is thus exclusive.
86       *
87       * @param ownerId
88       *            a unique id identifying the entity that wants to acquire a
89       *            certain lock level on this lock
90       * @param timeoutMSecs
91       *            if blocking is enabled by the <code>wait</code> parameter
92       *            this specifies the maximum wait time in milliseconds
93       * @return <code>true</code> if the lock actually was acquired
94       * @throws InterruptedException
95       *             when the thread waiting on this method is interrupted
96       */
97      public boolean acquireWrite(Object ownerId, long timeoutMSecs) throws InterruptedException {
98          return acquire(ownerId, WRITE_LOCK, true, timeoutMSecs);
99      }
100 }