1
|
|
|
package io.mcarle.sciurus.lock; |
2
|
|
|
|
3
|
|
|
import io.mcarle.sciurus.ExecutionIdentifier; |
4
|
|
|
import io.mcarle.sciurus.annotation.Lock; |
5
|
|
|
import org.aspectj.lang.ProceedingJoinPoint; |
6
|
|
|
|
7
|
|
|
import java.util.ArrayList; |
8
|
|
|
import java.util.Collections; |
9
|
|
|
import java.util.List; |
10
|
|
|
|
11
|
|
|
public class Execution { |
12
|
|
|
private final ExecutionIdentifier id; |
13
|
|
|
private final Object lock; |
14
|
|
|
private final ThreadLocal<Boolean> activator; |
15
|
|
|
private final List<Long> waitingThreads; |
16
|
|
|
private ProceedingJoinPoint joinPoint; |
17
|
|
|
|
18
|
1 |
|
Execution(ProceedingJoinPoint joinPoint, Lock lock) { |
19
|
1 |
|
this.joinPoint = joinPoint; |
20
|
1 |
|
this.lock = new Object(); |
21
|
1 |
|
this.activator = new ThreadLocal<>(); |
22
|
1 |
|
this.activator.set(Boolean.FALSE); |
23
|
1 |
|
this.waitingThreads = Collections.synchronizedList(new ArrayList<>()); |
24
|
1 |
|
this.waitingThreads.add(Thread.currentThread().getId()); |
25
|
1 |
|
List<Object> tmp = new ArrayList<>(); |
26
|
1 |
|
for (int i : lock.on()) { |
27
|
1 |
|
tmp.add(joinPoint.getArgs()[i]); |
28
|
|
|
} |
29
|
1 |
|
this.id = new ExecutionIdentifier(joinPoint.getSignature().toString(), tmp.toArray()); |
30
|
1 |
|
} |
31
|
|
|
|
32
|
|
|
public void setJoinPointAndActivate(ProceedingJoinPoint joinPoint) { |
33
|
1 |
|
this.joinPoint = joinPoint; |
34
|
1 |
|
this.activator.set(Boolean.TRUE); |
35
|
1 |
|
} |
36
|
|
|
|
37
|
|
|
public ExecutionIdentifier getId() { |
38
|
1 |
|
return id; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public Object getLock() { |
42
|
1 |
|
return lock; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public ThreadLocal<Boolean> getActivator() { |
46
|
1 |
|
return activator; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public List<Long> getWaitingThreads() { |
50
|
1 |
|
return waitingThreads; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public ProceedingJoinPoint getJoinPoint() { |
54
|
1 |
|
return joinPoint; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
@Override |
58
|
|
|
public String toString() { |
59
|
1 |
|
return "ID: " + id + " - Waiting: " + waitingThreads + " - Active? " + (activator.get() != null); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|