KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > concurrency > TransactionLocks


1 package org.jacorb.concurrency;
2
3 /*
4  * JacORB concurrency control service - a free CCS for JacORB
5  *
6  * Copyright (C) 1999-2004 LogicLand group, Viacheslav Tararin.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22
23 import org.omg.CosTransactions.*;
24 import org.omg.CosConcurrencyControl.*;
25
26 public class TransactionLocks
27 {
28     TransactionCoordinator current;
29     private int read = 0;
30     private int write = 0;
31     private int upgrade = 0;
32     private int intention_read = 0;
33     private int intention_write = 0;
34
35     TransactionLocks( TransactionCoordinator current )
36     {
37         this.current = current;
38     };
39
40     synchronized boolean no_conflict( lock_mode mode )
41     {
42         if( mode.equals( lock_mode.read ) ){
43             return write == 0 && intention_write == 0;
44         } else if( mode.equals( lock_mode.write ) ) {
45             return write == 0 && read == 0 && upgrade == 0 && intention_read == 0 && intention_write == 0;
46         } else if( mode.equals(lock_mode.upgrade ) ) {
47             return upgrade == 0 && intention_write == 0 && write == 0;
48         } else if( mode.equals ( lock_mode.intention_read ) ) {
49             return write == 0;
50         } else if( mode.equals( lock_mode.intention_write ) ) {
51             return write == 0 && read == 0 && upgrade == 0;
52         }
53         return false;
54     };
55
56     synchronized void lock( lock_mode mode )
57     {
58         if( mode.equals( lock_mode.read ) ) {
59             read++;
60         } else if( mode.equals( lock_mode.write ) ) {
61             write++;
62         } else if( mode.equals( lock_mode.upgrade ) ) {
63             upgrade++;
64         } else if( mode.equals( lock_mode.intention_read ) ) {
65             intention_read++;
66         } else if( mode.equals( lock_mode.intention_write ) ) {
67             intention_write++;
68         }
69     };
70
71     synchronized void unlock( lock_mode mode )
72         throws LockNotHeld
73     {
74         if( mode.equals( lock_mode.read ) ) {
75             check_held( read );
76             read--;
77         } else if( mode.equals( lock_mode.write ) ) {
78             check_held( write );
79             write--;
80         } else if( mode.equals( lock_mode.upgrade ) ) {
81             check_held( upgrade );
82             upgrade--;
83         } else if( mode.equals( lock_mode.intention_read ) ) {
84             check_held( intention_read );
85             intention_read--;
86         } else if( mode.equals( lock_mode.intention_write ) ) {
87             check_held( intention_write );
88             intention_write--;
89         }
90     }
91
92     private void check_held( int i )
93         throws LockNotHeld
94     {
95         if ( i == 0 ){
96             throw new LockNotHeld();
97         }
98     };
99
100     boolean is_held( lock_mode mode )
101     {
102         if( mode.equals( lock_mode.read ) ) {
103             return read > 0;
104         } else if( mode.equals( lock_mode.write ) ) {
105             return write > 0;
106         } else if( mode.equals( lock_mode.upgrade ) ) {
107             return upgrade > 0;
108         } else if( mode.equals( lock_mode.intention_read ) ) {
109             return intention_read > 0;
110         } else if( mode.equals( lock_mode.intention_write ) ) {
111             return intention_write > 0;
112         }
113         return false;
114     };
115
116     boolean any_locks()
117     {
118         return read!=0 || write!=0 || upgrade!=0 || intention_read!=0 || intention_write!=0;
119     };
120
121     public String JavaDoc toString()
122     {
123         return current.get_coordinator().get_transaction_name()+": "+
124             " read="+read+
125             " write="+write+
126             " upgrade="+upgrade+
127             " intention_read="+intention_read+
128             " intention_write="+intention_write;
129     };
130
131
132 }
133
134
135
Popular Tags