001package jmri.jmrit.logixng.implementation;
002
003/**
004 * Protect the DefaultConditionalNG.execute() method.
005 * That method may be called recursively from different threads.
006 */
007public class ExecuteLock {
008
009    private boolean lock = false;
010    private boolean again = false;
011    
012    /**
013     * Get the status of the lock.
014     * If the call succeeds, the caller is responsible to loop while the method
015     * loop() returns true.
016     * @return true if the caller gets the lock.
017     */
018    public boolean once() {
019        synchronized(this) {
020            again = true;
021            if (! lock) {
022                lock = true;
023                return true;
024            } else {
025                return false;
026            }
027        }
028    }
029    
030    /**
031     * Get the status of the lock during loop.
032     * The caller is responsible to loop while the method returns true.
033     * @return true if the caller still has the lock.
034     */
035    public boolean loop() {
036        synchronized(this) {
037            if (again) {
038                again = false;
039                return true;
040            } else {
041                lock = false;
042                return false;
043            }
044        }
045    }
046    
047}