01: /*
02: * Created on 19-Apr-2004
03: *
04: */
05: package com.jofti.locking;
06:
07: import com.jofti.btree.Node;
08: import com.jofti.exception.JoftiException;
09:
10: import com.jofti.oswego.concurrent.ReadWriteLock;
11:
12: ;
13:
14: /**
15: * Provides the locking and unlocking functions for the BTree nodes.<p>
16: *
17: * @author Steve Woodcock
18: *
19: */
20: public class LockManager {
21:
22: public static final int WRITE_LOCK = 1;
23: public static final int READ_LOCK = 2;
24:
25: private LockManager() {
26: // not implemented as is a static helper
27: }
28:
29: public static void acquireLock(Node node, int lockType)
30: throws JoftiException {
31: if (node != null) {
32: try {
33:
34: if (lockType == WRITE_LOCK) {
35: node.nodeLock.writeLock().acquire();
36: } else {
37: node.nodeLock.readLock().acquire();
38: }
39: } catch (InterruptedException ie) {
40: throw new JoftiException(
41: "Unable to acquire node lock on node " + node);
42: }
43: }
44: }
45:
46: public static boolean attemptLock(Node node, int lockType, long time)
47: throws JoftiException {
48: if (node != null) {
49: try {
50: if (lockType == WRITE_LOCK) {
51: return node.nodeLock.writeLock().attempt(time);
52: } else {
53: return node.nodeLock.readLock().attempt(time);
54: }
55: } catch (InterruptedException ie) {
56: throw new JoftiException(
57: "Unable to acquire node lock on node " + node);
58: }
59: }
60: return false;
61: }
62:
63: public static void acquireRWLock(ReadWriteLock lock, int lockType)
64: throws JoftiException {
65: if (lock != null) {
66: try {
67: if (lockType == WRITE_LOCK) {
68: lock.writeLock().acquire();
69: } else {
70: lock.readLock().acquire();
71: }
72: } catch (InterruptedException ie) {
73: throw new JoftiException(
74: "Unable to acquire RW lock on lock " + lock);
75: }
76: }
77: }
78:
79: public static void releaseRWLock(ReadWriteLock lock, int lockType) {
80: if (lockType == WRITE_LOCK) {
81: lock.writeLock().release();
82: } else {
83: lock.readLock().release();
84: }
85: }
86:
87: public static void releaseLock(Node node, int lockType) {
88: if (lockType == WRITE_LOCK) {
89: node.nodeLock.writeLock().release();
90: } else {
91: node.nodeLock.readLock().release();
92: }
93: }
94:
95: }
|