package de.renew.engine.thread;

import java.util.concurrent.Callable;

import de.renew.util.Lock;

/**
 * This is a specialized lock for simulation threads managed by the
 * {@link SimulationThreadPool}. A simulation thread is allowed to reenter a
 * lock that has been acquired by its ancestor thread. An ancestor thread is
 * defined as the thread that called
 * {@link SimulationThreadPool#executeAndWait(Runnable)} or
 * {@link SimulationThreadPool#submitAndWait(Callable)} and is waiting for the
 * completion of the simulation thread that executes the respective Runnable or
 * Callable.
 *
 * @author Matthias Wester-Ebbinghaus
 * @author Michael Duvigneau
 * @since Renew 2.2
 */
class InheritableSimulationThreadLock extends Lock {
    @Override
    protected boolean mayLock(Thread lockingThread) {
        if (lockingThread == Thread.currentThread()) {
            return true;
        } else if (Thread.currentThread() instanceof SimulationThread simulationThread) {

            return (lockingThread == simulationThread.getAncestor());
        }
        return false;
    }
}