1 package org.sourceforge.jemm.client;
2
3 import java.util.concurrent.ConcurrentHashMap;
4 import java.util.concurrent.ConcurrentMap;
5 import java.util.concurrent.CountDownLatch;
6
7 import org.sourceforge.jemm.database.ClientId;
8 import org.sourceforge.jemm.database.ClientThreadId;
9 import org.sourceforge.jemm.database.LockAcquiredListener;
10 import org.sourceforge.jemm.types.ID;
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 public class SynchronousLockDecorator extends DelegatingObjectDatabase
30 implements LockAcquiredListener {
31 ClientId clientId;
32 ConcurrentMap<ClientThreadId, CountDownLatch> waitingThreads;
33
34 public SynchronousLockDecorator(ClientId clientId, ObjectDatabase delegate) {
35 super(delegate);
36 this.clientId = clientId;
37 delegate.setClientLockAcquiredListener(clientId, this);
38
39 waitingThreads = new ConcurrentHashMap<ClientThreadId, CountDownLatch>();
40 }
41
42
43
44
45
46
47
48
49
50
51
52
53 public void acquireLock(ClientThreadId threadId, ID jemmId) {
54 CountDownLatch lock = new CountDownLatch(1);
55 waitingThreads.put(threadId, lock);
56 super.acquireLock(threadId, jemmId);
57
58 awaitLock(lock);
59 waitingThreads.remove(threadId);
60 }
61
62 private void awaitLock(CountDownLatch lock) {
63 try {
64 lock.await();
65 } catch (InterruptedException e) {
66 }
67 }
68
69
70
71
72 public void removeLockAcquiredListener(ClientId clientId) {
73 }
74
75
76
77
78 public void setClientLockAcquiredListener(ClientId clientId,
79 LockAcquiredListener listener) {
80 }
81
82
83
84
85
86 @Override
87 public void lockAcquired(ClientThreadId threadId, ID object) {
88 CountDownLatch lock = waitingThreads.get(threadId);
89
90 lock.countDown();
91
92 }
93 }