001: /*
002:
003: Derby - Class org.apache.derbyTesting.unitTests.services.T_L2
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derbyTesting.unitTests.services;
023:
024: import org.apache.derby.iapi.services.sanity.SanityManager;
025: import java.util.Hashtable;
026: import org.apache.derby.iapi.services.locks.*;
027:
028: /**
029: A semaphore that implements Lockable for unit testing.
030: */
031: class T_L2 implements Lockable {
032:
033: private int allowed;
034: private Object[] lockers;
035: private int[] counts;
036:
037: T_L2(int allowed) {
038: this .allowed = allowed;
039: lockers = new Object[allowed];
040: counts = new int[allowed];
041: }
042:
043: /*
044: ** Lockable methods (Simple, qualifier assumed to be null), allows
045: ** up to 'allowed' lockers in at the same time.
046: */
047:
048: public void lockEvent(Latch lockInfo) {
049:
050: int empty = -1;
051: for (int i = 0; i < allowed; i++) {
052: if (lockers[i] == lockInfo.getCompatabilitySpace()) {
053: counts[i]++;
054: return;
055: }
056:
057: if (lockers[i] == null)
058: empty = i;
059: }
060:
061: if (SanityManager.DEBUG)
062: SanityManager.ASSERT(empty != -1);
063: lockers[empty] = lockInfo.getCompatabilitySpace();
064: counts[empty] = 1;
065:
066: }
067:
068: public boolean requestCompatible(Object requestedQualifier,
069: Object grantedQualifier) {
070: return false;
071: }
072:
073: public boolean lockerAlwaysCompatible() {
074: return true;
075: }
076:
077: public void unlockEvent(Latch lockInfo) {
078:
079: for (int i = 0; i < allowed; i++) {
080:
081: if (lockers[i] == lockInfo.getCompatabilitySpace()) {
082: counts[i]--;
083: if (SanityManager.DEBUG)
084: SanityManager.ASSERT(counts[i] >= 0);
085: if (counts[i] == 0) {
086: lockers[i] = null;
087: return;
088: }
089:
090: return;
091: }
092: }
093:
094: if (SanityManager.DEBUG)
095: SanityManager
096: .THROWASSERT("unlocked by a compatability space that does not exist");
097: }
098:
099: public boolean lockAttributes(int flag, Hashtable t) {
100: return false;
101: }
102:
103: }
|