KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > perseus > concurrency > lib > RWLockValue


1 package org.objectweb.perseus.concurrency.lib;
2
3
4 /**
5  * Copyright (C) 2003-2004
6  * - France Telecom R&D
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser 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  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  *
22  * Release: 1.0
23  *
24  * Authors: Olivier Lobry (olivier.lobry@rd.francetelecom.com)
25  *
26  */

27 /**
28  * Simple lock value comparator. Possible values are:
29  * - nolock (compatible w/ any lock level)
30  * - read (compatible w/ nolocks and other reads)
31  * - upgrade (compatible only w/ nolocks)
32  * - write (compatible only w/ nolocks)
33  * From a compatibility point of view upgrade lock requests are equivalent
34  * to exclusive lock request. However, distinguishing the two can
35  * be convenient in the treatement of requests (two upgrades on the same
36  * object automatically leads to a deadlock)
37  */

38 public class RWLockValue implements LockValue {
39
40     public static final byte READ = 1;
41     public static final byte UPGRADE = 2;
42     public static final byte WRITE = 3;
43
44
45 /*
46  * 3 2 1 0
47  * X U S N
48  * 0: N 1 1 1 1 = 15
49  * 1: S 0 0 1 1 = 3
50  * 2: U 0 0 0 1 = 1
51  * 3: X 0 0 0 1 = 1
52  */

53
54     static final byte compTable[] = { 15, 3, 1, 1 };
55
56
57     public byte maxValue() {
58         return WRITE;
59     }
60
61     public boolean isCompatibleWith(byte l1, byte l2) {
62         boolean res = (((1 << l1) & compTable[l2]) != 0);
63         return res;
64     }
65
66     public byte getCompatibleWith(byte l1, byte l2) {
67         while (!isCompatibleWith(l1, l2)) l1--;
68         return l1;
69     }
70
71     public String JavaDoc str(byte l) {
72         switch(l) {
73         case NOLOCK: return "NOLOCK";
74         case READ: return "READ";
75         case UPGRADE: return "UPGRADE";
76         case WRITE: return "WRITE";
77         default: return "UNDEFINED";
78         }
79     }
80 }
81
Popular Tags