001: ///////////////////////////////////////////////////////////////////////////////
002: //
003: // Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
004: //
005: // All Rights Reserved
006: //
007: // This program is free software; you can redistribute it and/or modify
008: // it under the terms of the GNU General Public License and GNU Library
009: // General Public License as published by the Free Software Foundation;
010: // either version 2, or (at your option) any later version.
011: //
012: // This program 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
015: // GNU General Public License and GNU Library General Public License
016: // for more details.
017: //
018: // You should have received a copy of the GNU General Public License
019: // and GNU Library General Public License along with this program; if
020: // not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
021: // MA 02139, USA.
022: //
023: ///////////////////////////////////////////////////////////////////////////////
024: package org.myoodb.core;
025:
026: import java.io.*;
027: import org.myoodb.exception.*;
028:
029: public final class Lock implements AbstractLock {
030: private int m_level;
031: private long m_identifier;
032:
033: public Lock() {
034: reset();
035: }
036:
037: public int tryAcquire(AbstractTransaction tx, int level) {
038: synchronized (this ) {
039: int prevLevel = getLockLevel(tx);
040:
041: if ((m_identifier == -1) || (isAcquiredBy(tx) == true)) {
042: m_level = level > m_level ? level : m_level;
043: m_identifier = tx.getTransactionIdentifier();
044: return prevLevel;
045: } else {
046: return org.myoodb.MyOodbAccess.NONE;
047: }
048: }
049: }
050:
051: public void release(AbstractTransaction tx) {
052: /* TODO: make work with nested transactions
053: if (tx != null)
054: {
055: if (isAcquiredBy(tx) == false)
056: {
057: throw new PermissionException("Invalid release (Lock not owned): " + m_identifier);
058: }
059: }
060: */
061:
062: reset();
063: }
064:
065: public boolean isAcquiredBy(AbstractTransaction tx) {
066: return (m_identifier != -1) ? (m_identifier == tx
067: .getTransactionIdentifier()) : false;
068: }
069:
070: public long getLockIdentifier() {
071: return m_identifier;
072: }
073:
074: public int getLockLevel(AbstractTransaction tx) {
075: if (tx != null) {
076: return isAcquiredBy(tx) ? m_level
077: : org.myoodb.MyOodbAccess.READ;
078: } else {
079: return m_level;
080: }
081: }
082:
083: public int getLockLevel() {
084: return m_level;
085: }
086:
087: public void reset() {
088: synchronized (this ) {
089: m_identifier = -1;
090: m_level = org.myoodb.MyOodbAccess.READ;
091: }
092: }
093:
094: public void writeExternal(ObjectOutput out) throws IOException {
095: out.writeInt(m_level);
096: out.writeLong(m_identifier);
097: }
098:
099: public void readExternal(ObjectInput in) throws IOException,
100: ClassNotFoundException {
101: m_level = in.readInt();
102: m_identifier = in.readLong();
103: }
104: }
|