* Attempts to grab the lock, blocking if already held by another thread or process. * * @param [processLock] whether to use file for process level locking or not. */
| 64 | * @param [processLock] whether to use file for process level locking or not. |
| 65 | */ |
| 66 | fun lock(processLock: Boolean = this.processLock) { |
| 67 | threadLock.lock() |
| 68 | if (processLock) { |
| 69 | try { |
| 70 | if (lockFile == null) { |
| 71 | throw IOException("No lock directory was provided.") |
| 72 | } |
| 73 | // Verify parent dir |
| 74 | val parentDir = lockFile.parentFile |
| 75 | parentDir?.mkdirs() |
| 76 | lockChannel = FileOutputStream(lockFile).channel.apply { lock() } |
| 77 | } catch (e: IOException) { |
| 78 | lockChannel = null |
| 79 | Log.w(TAG, "Unable to grab file lock.", e) |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Releases the lock. |