01: /*
02: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tcclient.cache;
05:
06: import com.tc.object.bytecode.Manager;
07: import com.tc.util.Assert;
08:
09: /**
10: * TODO: Merge with com.terracotta.session.util.Lock.java.
11: *
12: */
13: public class Lock {
14:
15: // Resources
16: private final Manager manager;
17:
18: // Config
19: private final String lockId;
20: private final int lockType;
21:
22: // State
23: private boolean isLocked = false;
24:
25: public Lock(final String lockId, Manager manager) {
26: this (lockId, Manager.LOCK_TYPE_WRITE, manager);
27: }
28:
29: public Lock(final String lockId, final int lockType, Manager manager) {
30: this .manager = manager;
31:
32: if (lockType != Manager.LOCK_TYPE_SYNCHRONOUS_WRITE
33: && lockType != Manager.LOCK_TYPE_WRITE) {
34: throw new AssertionError("Trying to set lockType to "
35: + lockType
36: + " -- must be either write or synchronous-write");
37: }
38: this .lockType = lockType;
39:
40: Assert.pre(lockId != null && lockId.length() > 0);
41: this .lockId = lockId;
42: }
43:
44: public void commitLock() {
45: manager.commitLock(lockId);
46: isLocked = false;
47: }
48:
49: public void getWriteLock() {
50: manager.beginLock(lockId, lockType);
51: isLocked = true;
52: }
53:
54: public void writeLock() {
55: manager.beginLock(lockId, lockType);
56: }
57:
58: public boolean tryWriteLock() {
59: isLocked = manager.tryBeginLock(lockId, lockType);
60: return isLocked;
61: }
62:
63: public String getLockId() {
64: return lockId;
65: }
66:
67: public boolean isLocked() {
68: return isLocked;
69: }
70: }
|