sclass FileBasedLock implements AutoCloseable { File lockFile; double timeout = 60.0; bool haveLock; java.util.Timer touchTimer; // returns true iff lock was acquired synchronized bool tryToLock() { if (haveLock) ret; double age = fileAgeInSeconds(lockFile); if (age >= timeout) { print("Deleting old lock file: " + lockFile + " (age: " + iround(age) + " seconds)"); deleteFile(lockFile); } try { Files.createFile(lockFile); set haveLock; startTouchTimer(); true; } catch e { printExceptionShort(e); false; } } synchronized void startTouchTimer { if (timer != null) ret; timer = doEvery(timeout/2, r doTouch); } synchronized void doTouch { if (haveLock) touchFile(lockFile); } public void close { dispose touchTimer; } }