001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2003-2006, GeoTools Project Managment Committee (PMC)
005: * (C) 2002, Centre for Computational Geography
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation;
010: * version 2.1 of the License.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.index;
018:
019: /**
020: * DOCUMENT ME!
021: *
022: * @author Tommaso Nolli
023: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/plugin/shapefile/src/main/java/org/geotools/index/LockManager.java $
024: */
025: public class LockManager {
026: private static final int EXCLUSIVE_LOCK_TIMEOUT = 20;
027: private static final int SHARED_LOCK_TIMEOUT = 10;
028: public static final short READ = 1;
029: public static final short WRITE = 2;
030: private Lock exclusiveLock;
031: private int leases;
032:
033: public LockManager() {
034: }
035:
036: public synchronized void release(Lock lock) {
037: LockImpl li = (LockImpl) lock;
038:
039: if (li.getType() == Lock.EXCLUSIVE) {
040: this .exclusiveLock = null;
041: } else {
042: this .leases--;
043: }
044:
045: this .notify();
046: }
047:
048: /**
049: * DOCUMENT ME!
050: *
051: *
052: * @throws LockTimeoutException DOCUMENT ME!
053: */
054: public synchronized Lock aquireExclusive()
055: throws LockTimeoutException {
056: int cnt = 0;
057:
058: while (((this .exclusiveLock != null) || (this .leases > 0))
059: && (cnt < EXCLUSIVE_LOCK_TIMEOUT)) {
060: cnt++;
061:
062: try {
063: this .wait(500);
064: } catch (InterruptedException e) {
065: throw new LockTimeoutException(e);
066: }
067: }
068:
069: if ((this .exclusiveLock != null) || (this .leases > 0)) {
070: throw new LockTimeoutException(
071: "Timeout aquiring exclusive lock");
072: }
073:
074: this .exclusiveLock = new LockImpl(Lock.EXCLUSIVE);
075:
076: return this .exclusiveLock;
077: }
078:
079: /**
080: * DOCUMENT ME!
081: *
082: *
083: * @throws LockTimeoutException DOCUMENT ME!
084: */
085: public synchronized Lock aquireShared() throws LockTimeoutException {
086: int cnt = 0;
087:
088: while ((this .exclusiveLock != null)
089: && (cnt < SHARED_LOCK_TIMEOUT)) {
090: cnt++;
091:
092: try {
093: this .wait(500);
094: } catch (InterruptedException e) {
095: throw new LockTimeoutException(e);
096: }
097: }
098:
099: if (this .exclusiveLock != null) {
100: throw new LockTimeoutException(
101: "Timeout aquiring shared lock");
102: }
103:
104: this .leases++;
105:
106: return new LockImpl(Lock.SHARED);
107: }
108:
109: /**
110: * DOCUMENT ME!
111: *
112: * @author Tommaso Nolli
113: */
114: private class LockImpl implements Lock {
115: private short type;
116:
117: /**
118: * DOCUMENT ME!
119: *
120: * @param type
121: */
122: public LockImpl(short type) {
123: this .type = type;
124: }
125:
126: /**
127: * @see org.geotools.index.Lock#getType()
128: */
129: public short getType() {
130: return this.type;
131: }
132: }
133: }
|