001: package org.jacorb.concurrency;
002:
003: /*
004: * JacORB concurrency control service - a free CCS for JacORB
005: *
006: * Copyright (C) 1999-2004 LogicLand group, Viacheslav Tararin.
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Library General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Library General Public License for more details.
017: *
018: * You should have received a copy of the GNU Library General Public
019: * License along with this library; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: */
022:
023: import org.omg.CosConcurrencyControl.LockNotHeld;
024: import org.omg.CosConcurrencyControl.lock_mode;
025:
026: public class TransactionLocks {
027: TransactionCoordinator current;
028: private int read = 0;
029: private int write = 0;
030: private int upgrade = 0;
031: private int intention_read = 0;
032: private int intention_write = 0;
033:
034: TransactionLocks(TransactionCoordinator current) {
035: this .current = current;
036: };
037:
038: synchronized boolean no_conflict(lock_mode mode) {
039: if (mode.equals(lock_mode.read)) {
040: return write == 0 && intention_write == 0;
041: } else if (mode.equals(lock_mode.write)) {
042: return write == 0 && read == 0 && upgrade == 0
043: && intention_read == 0 && intention_write == 0;
044: } else if (mode.equals(lock_mode.upgrade)) {
045: return upgrade == 0 && intention_write == 0 && write == 0;
046: } else if (mode.equals(lock_mode.intention_read)) {
047: return write == 0;
048: } else if (mode.equals(lock_mode.intention_write)) {
049: return write == 0 && read == 0 && upgrade == 0;
050: }
051: return false;
052: };
053:
054: synchronized void lock(lock_mode mode) {
055: if (mode.equals(lock_mode.read)) {
056: read++;
057: } else if (mode.equals(lock_mode.write)) {
058: write++;
059: } else if (mode.equals(lock_mode.upgrade)) {
060: upgrade++;
061: } else if (mode.equals(lock_mode.intention_read)) {
062: intention_read++;
063: } else if (mode.equals(lock_mode.intention_write)) {
064: intention_write++;
065: }
066: };
067:
068: synchronized void unlock(lock_mode mode) throws LockNotHeld {
069: if (mode.equals(lock_mode.read)) {
070: check_held(read);
071: read--;
072: } else if (mode.equals(lock_mode.write)) {
073: check_held(write);
074: write--;
075: } else if (mode.equals(lock_mode.upgrade)) {
076: check_held(upgrade);
077: upgrade--;
078: } else if (mode.equals(lock_mode.intention_read)) {
079: check_held(intention_read);
080: intention_read--;
081: } else if (mode.equals(lock_mode.intention_write)) {
082: check_held(intention_write);
083: intention_write--;
084: }
085: }
086:
087: private void check_held(int i) throws LockNotHeld {
088: if (i == 0) {
089: throw new LockNotHeld();
090: }
091: };
092:
093: boolean is_held(lock_mode mode) {
094: if (mode.equals(lock_mode.read)) {
095: return read > 0;
096: } else if (mode.equals(lock_mode.write)) {
097: return write > 0;
098: } else if (mode.equals(lock_mode.upgrade)) {
099: return upgrade > 0;
100: } else if (mode.equals(lock_mode.intention_read)) {
101: return intention_read > 0;
102: } else if (mode.equals(lock_mode.intention_write)) {
103: return intention_write > 0;
104: }
105: return false;
106: };
107:
108: boolean any_locks() {
109: return read != 0 || write != 0 || upgrade != 0
110: || intention_read != 0 || intention_write != 0;
111: };
112:
113: public String toString() {
114: return current.get_coordinator().get_transaction_name() + ": "
115: + " read=" + read + " write=" + write + " upgrade="
116: + upgrade + " intention_read=" + intention_read
117: + " intention_write=" + intention_write;
118: };
119:
120: }
|