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