KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > core > SharedLock


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Core License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by SMB are
5
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
6
//
7
// $Id: SharedLock.java,v 1.4 2002/09/18 06:54:15 per_nyfelt Exp $
8

9 package org.ozoneDB.core;
10
11 import org.ozoneDB.DxLib.*;
12 import org.ozoneDB.util.LogWriter;
13
14
15 /**
16  * This class implements a non-exclusive lock policy. Multiple transactions may
17  * hold this lock.
18  *
19  *
20  * @author <a HREF="http://www.softwarebuero.de/">SMB</a>
21  * @version $Revision: 1.4 $Date: 2002/09/18 06:54:15 $
22  */

23 public final class SharedLock extends AbstractLock {
24
25     protected final static long serialVersionUID = 1;
26     protected final static byte subSerialVersionUID = 1;
27
28     private int level = LEVEL_NONE;
29
30     private transient int prevLevel = LEVEL_NONE;
31
32     /**
33      * All transactions that hold this lock. Holds TransactionIDs.
34      */

35     public DxSet lockers;
36
37
38     public SharedLock() {
39         reset();
40     }
41
42
43     public synchronized void reset() {
44         level = prevLevel = LEVEL_NONE;
45         lockers = new DxHashSet( 8 );
46     }
47
48
49     public int tryAcquire( Transaction ta, int newLevel ) {
50         if (false&&ta.env.logWriter.hasTarget( LogWriter.DEBUG3 )) {
51             ta.env.logWriter.newEntry( this, "tryAcquire(): current:" + level + " new:" + newLevel, LogWriter.DEBUG3 );
52         }
53         synchronized (this) {
54             int prevLevel = level( ta );
55             lockers.add( ta.taID() );
56
57             if (!lockers.contains( ta.taID() )) {
58                 throw new RuntimeException JavaDoc( "tryAcquire(): unable to add ta to lockers." );
59             //ta.env.logWriter.newEntry (this, "tryAcquire(): unable to add ta to lockers.", LogWriter.WARN);
60
}
61             level = newLevel > level ? newLevel : level;
62             return prevLevel;
63         }
64     }
65
66
67     public void release( Transaction ta ) {
68         if (false&&ta.env.logWriter.hasTarget( LogWriter.DEBUG3 )) {
69             ta.env.logWriter.newEntry( this, "release()", LogWriter.DEBUG3 );
70         }
71         synchronized (this) {
72             if (!lockers.remove( ta.taID )) {
73                 throw new RuntimeException JavaDoc( "release(): transaction does not hold the lock." );
74             }
75             if (lockers.isEmpty()) {
76                 level = LEVEL_NONE;
77             }
78             if (false&&ta.env.logWriter.hasTarget( LogWriter.DEBUG3 )) {
79                 ta.env.logWriter.newEntry( this, "release(): count=" + lockers.count() + ", level=" + level, LogWriter.DEBUG3 );
80             }
81         }
82     }
83
84
85     public boolean isAcquiredBy( Transaction ta ) {
86         return lockers != null ? lockers.contains( ta.taID() ) : false;
87     }
88
89
90     public DxCollection lockerIDs() {
91         return lockers != null ? lockers : new DxHashSet();
92     }
93
94
95     public int level( Transaction ta ) {
96         if (ta != null) {
97             return isAcquiredBy( ta ) ? level : LEVEL_NONE;
98         } else {
99             return level;
100         }
101     }
102
103
104     public int previousLevel() {
105         return prevLevel;
106     }
107
108     public boolean areMultipleLockersHoldingLocks() {
109         return lockers.count()>=2;
110     }
111
112
113     public boolean hasChanged() {
114         return level > prevLevel;
115     }
116
117     public String JavaDoc toString() {
118         return "SharedLock[lockers="+lockers+"]";
119     }
120 }
121
Popular Tags