|
1
|
|
|
package io.mcarle.sciurus.lock; |
|
2
|
|
|
|
|
3
|
|
|
import io.mcarle.sciurus.annotation.Lock; |
|
4
|
|
|
import io.mcarle.sciurus.ExecutionIdentifier; |
|
5
|
|
|
import org.aspectj.lang.ProceedingJoinPoint; |
|
6
|
|
|
import org.slf4j.Logger; |
|
7
|
|
|
import org.slf4j.LoggerFactory; |
|
8
|
|
|
|
|
9
|
|
|
import java.util.Collections; |
|
10
|
|
|
import java.util.HashMap; |
|
11
|
|
|
import java.util.Map; |
|
12
|
|
|
import java.util.concurrent.locks.ReentrantLock; |
|
13
|
|
|
|
|
14
|
1 |
|
class LockAspectHandler { |
|
15
|
|
|
|
|
16
|
1 |
|
private static final Logger LOG = LoggerFactory.getLogger(LockAspectHandler.class); |
|
17
|
1 |
|
private static final Map<ExecutionIdentifier, Execution> ACTIVE_EXECUTIONS = Collections.synchronizedMap(new HashMap<>()); |
|
18
|
1 |
|
private static final ReentrantLock REENTRANT_LOCK = new ReentrantLock(true); |
|
19
|
|
|
|
|
20
|
|
|
static Object checkLockAndExecute(ProceedingJoinPoint joinPoint, Lock lock) throws Throwable { |
|
21
|
1 |
|
Execution execution = new Execution(joinPoint, lock); |
|
22
|
1 |
|
return checkLockAndExecute(execution); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
private static Object checkLockAndExecute(Execution exec) throws Throwable { |
|
26
|
1 |
|
Execution oldExecution = ACTIVE_EXECUTIONS.putIfAbsent(exec.getId(), exec); |
|
27
|
1 |
|
if (oldExecution == null || oldExecution.getActivator().get() == Boolean.TRUE) { |
|
28
|
1 |
|
LOG.trace("Create lock: {}", exec); |
|
29
|
1 |
|
synchronized (exec.getLock()) { |
|
30
|
|
|
try { |
|
31
|
1 |
|
exec.getActivator().set(Boolean.FALSE); |
|
32
|
1 |
|
return exec.getJoinPoint().proceed(); |
|
33
|
|
|
} finally { |
|
34
|
1 |
|
exec.getWaitingThreads().remove(Thread.currentThread().getId()); |
|
35
|
1 |
|
REENTRANT_LOCK.lock(); |
|
36
|
1 |
|
if (exec.getWaitingThreads().isEmpty()) { |
|
37
|
1 |
|
LOG.trace("Remove finished thread from waiting thread map: {}", exec); |
|
38
|
1 |
|
ACTIVE_EXECUTIONS.remove(exec.getId()); |
|
39
|
|
|
} |
|
40
|
1 |
|
REENTRANT_LOCK.unlock(); |
|
41
|
1 |
|
LOG.trace("Release lock: {}", exec); |
|
42
|
1 |
|
exec.getLock().notifyAll(); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
} else { |
|
46
|
1 |
|
REENTRANT_LOCK.lock(); |
|
47
|
1 |
|
Execution lockedExec = ACTIVE_EXECUTIONS.get(exec.getId()); |
|
48
|
1 |
|
if (lockedExec != null) { |
|
49
|
1 |
|
if (lockedExec.getActivator().get() == null) { |
|
50
|
1 |
|
lockedExec.getWaitingThreads().add(Thread.currentThread().getId()); |
|
51
|
1 |
|
REENTRANT_LOCK.unlock(); |
|
52
|
1 |
|
LOG.trace("Wait for lock release: {}", lockedExec); |
|
53
|
1 |
|
synchronized (lockedExec.getLock()) { |
|
54
|
1 |
|
while (lockedExec.getWaitingThreads().indexOf(Thread.currentThread().getId()) > 0) { |
|
55
|
1 |
|
lockedExec.getLock().wait(100); |
|
56
|
1 |
|
LOG.trace("Wait released or reached timeout: {}", lockedExec); |
|
57
|
|
|
} |
|
58
|
1 |
|
lockedExec.setJoinPointAndActivate(exec.getJoinPoint()); |
|
59
|
|
|
} |
|
60
|
1 |
|
LOG.trace("Got lock release: {}", lockedExec); |
|
61
|
1 |
|
return checkLockAndExecute(lockedExec); |
|
62
|
|
|
} else { |
|
63
|
1 |
|
REENTRANT_LOCK.unlock(); |
|
64
|
1 |
|
LOG.trace("Recursive call, proceed: {}", lockedExec); |
|
65
|
1 |
|
return exec.getJoinPoint().proceed(); |
|
66
|
|
|
} |
|
67
|
|
|
} else { |
|
68
|
|
|
/* |
|
69
|
|
|
* Should only happen when oldExecution was still running, but until getting the lockedExec, |
|
70
|
|
|
* that thread finished its work and removed itself from the active execution map. (i.e. rarely) |
|
71
|
|
|
*/ |
|
72
|
|
|
REENTRANT_LOCK.unlock(); |
|
73
|
|
|
LOG.trace("No active execution found, retry: {}", exec); |
|
74
|
|
|
return checkLockAndExecute(exec); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|