GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

checkLockAndExecute(Execution)   B
last analyzed

Complexity

Conditions 7

Size

Total Lines 50
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 7.0336

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 40
c 1
b 0
f 0
dl 0
loc 50
ccs 31
cts 34
cp 0.9118
crap 7.0336
rs 7.52
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
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