public <T> T useCache(String operationDisplayName, Factory<? extends T> factory) { if (lockOptions != null && lockOptions.getMode() == FileLockManager.LockMode.Shared) { throw new UnsupportedOperationException("Not implemented yet."); } takeOwnership(operationDisplayName); boolean wasStarted = false; try { wasStarted = onStartWork(); return factory.create(); } finally { lock.lock(); try { try { if (wasStarted) { onEndWork(); } } finally { releaseOwnership(); } } finally { lock.unlock(); } } }
private boolean onStartWork() { if (fileLock != null) { return false; } fileLock = lockManager.lock( lockFile, lockOptions.withMode(Exclusive), cacheDisplayName, operations.getDescription()); stateAtOpen = fileLock.getState(); for (UnitOfWorkParticipant cache : caches) { cache.onStartWork(operations.getDescription(), stateAtOpen); } lockManager.allowContention(fileLock, whenContended()); return true; }
/** * Opens this cache access with the given lock mode. Calling this with {@link * org.gradle.cache.internal.FileLockManager.LockMode#Exclusive} will lock the cache for exclusive * access from all other threads (including those in this process and all other processes), until * {@link #close()} is called. * * @param lockOptions */ public void open(LockOptions lockOptions) { lock.lock(); try { if (owner != null) { throw new IllegalStateException( String.format("Cannot open the %s, as it is already in use.", cacheDisplayName)); } this.lockOptions = lockOptions; if (lockOptions.getMode() == FileLockManager.LockMode.None) { return; } if (fileLock != null) { throw new IllegalStateException("File lock " + lockFile + " is already open."); } fileLock = lockManager.lock(lockFile, lockOptions, cacheDisplayName); stateAtOpen = fileLock.getState(); takeOwnership(String.format("Access %s", cacheDisplayName)); lockManager.allowContention(fileLock, whenContended()); } finally { lock.unlock(); } }