KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > unitTests > services > T_L2


1 /*
2
3    Derby - Class org.apache.derbyTesting.unitTests.services.T_L2
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.unitTests.services;
23
24 import org.apache.derby.iapi.services.sanity.SanityManager;
25 import java.util.Hashtable JavaDoc;
26 import org.apache.derby.iapi.services.locks.*;
27
28 /**
29     A semaphore that implements Lockable for unit testing.
30 */

31 class T_L2 implements Lockable {
32
33     private int allowed;
34     private Object JavaDoc[] lockers;
35     private int[] counts;
36
37     T_L2(int allowed) {
38         this.allowed = allowed;
39         lockers = new Object JavaDoc[allowed];
40         counts = new int[allowed];
41     }
42
43     /*
44     ** Lockable methods (Simple, qualifier assumed to be null), allows
45     ** up to 'allowed' lockers in at the same time.
46     */

47
48     public void lockEvent(Latch lockInfo) {
49
50         int empty = -1;
51         for (int i = 0; i < allowed; i++) {
52             if (lockers[i] == lockInfo.getCompatabilitySpace()) {
53                 counts[i]++;
54                 return;
55             }
56
57             if (lockers[i] == null)
58                 empty = i;
59         }
60
61         if (SanityManager.DEBUG)
62             SanityManager.ASSERT(empty != -1);
63         lockers[empty] = lockInfo.getCompatabilitySpace();
64         counts[empty] = 1;
65
66     }
67
68     public boolean requestCompatible(Object JavaDoc requestedQualifier, Object JavaDoc grantedQualifier) {
69         return false;
70     }
71
72     public boolean lockerAlwaysCompatible() {
73         return true;
74     }
75
76     public void unlockEvent(Latch lockInfo) {
77
78         for (int i = 0; i < allowed; i++) {
79
80             if (lockers[i] == lockInfo.getCompatabilitySpace()) {
81                 counts[i]--;
82                 if (SanityManager.DEBUG)
83                     SanityManager.ASSERT(counts[i] >= 0);
84                 if (counts[i] == 0) {
85                     lockers[i] = null;
86                     return;
87                 }
88
89                 return;
90             }
91         }
92
93         if (SanityManager.DEBUG)
94             SanityManager.THROWASSERT("unlocked by a compatability space that does not exist");
95     }
96
97     public boolean lockAttributes(int flag, Hashtable JavaDoc t)
98     {
99         return false;
100     }
101     
102 }
103
Popular Tags