Skip to content

Commit

Permalink
Tighten up mill/main/client/lock files (#3366)
Browse files Browse the repository at this point in the history
follow up for #3363

* Remove the `public`-ness from `FileTryLocked` and `MemoryTryLocked`.
They do not need to be public, as people should only access them through
the `TryLocked` interface

* Add `final` to the fields that do not need to be mutated (i.e. all of
them)

* Make `Locks` `final`, and replace weird anonymous class initializers
with a proper constructor
  • Loading branch information
lihaoyi authored Aug 12, 2024
1 parent a097392 commit 89c93e9
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 22 deletions.
4 changes: 2 additions & 2 deletions main/client/src/mill/main/client/lock/FileLock.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

class FileLock extends Lock {

private RandomAccessFile raf;
private FileChannel chan;
final private RandomAccessFile raf;
final private FileChannel chan;

public FileLock(String path) throws Exception {
raf = new RandomAccessFile(path, "rw");
Expand Down
2 changes: 1 addition & 1 deletion main/client/src/mill/main/client/lock/FileLocked.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class FileLocked implements Locked {

protected java.nio.channels.FileLock lock;
final protected java.nio.channels.FileLock lock;

public FileLocked(java.nio.channels.FileLock lock) {
this.lock = lock;
Expand Down
2 changes: 1 addition & 1 deletion main/client/src/mill/main/client/lock/FileTryLocked.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package mill.main.client.lock;

public class FileTryLocked extends FileLocked implements TryLocked{
class FileTryLocked extends FileLocked implements TryLocked{
public FileTryLocked(java.nio.channels.FileLock lock) {
super(lock);
}
Expand Down
34 changes: 20 additions & 14 deletions main/client/src/mill/main/client/lock/Locks.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,32 @@

import mill.main.client.ServerFiles;

public class Locks implements AutoCloseable {
final public class Locks implements AutoCloseable {

public Lock processLock;
public Lock serverLock;
public Lock clientLock;
final public Lock processLock;
final public Lock serverLock;
final public Lock clientLock;

public Locks(Lock processLock, Lock serverLock, Lock clientLock){
this.processLock = processLock;
this.serverLock = serverLock;
this.clientLock = clientLock;
}

public static Locks files(String lockBase) throws Exception {
return new Locks(){{
processLock = new FileLock(lockBase + "/" + ServerFiles.processLock);
serverLock = new FileLock(lockBase + "/" + ServerFiles.serverLock);
clientLock = new FileLock(lockBase + "/" + ServerFiles.clientLock);
}};
return new Locks(
new FileLock(lockBase + "/" + ServerFiles.processLock),
new FileLock(lockBase + "/" + ServerFiles.serverLock),
new FileLock(lockBase + "/" + ServerFiles.clientLock)
);
}

public static Locks memory() {
return new Locks(){{
this.processLock = new MemoryLock();
this.serverLock = new MemoryLock();
this.clientLock = new MemoryLock();
}};
return new Locks(
new MemoryLock(),
new MemoryLock(),
new MemoryLock()
);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion main/client/src/mill/main/client/lock/MemoryLock.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class MemoryLock extends Lock {

private ReentrantLock innerLock = new ReentrantLock();
final private ReentrantLock innerLock = new ReentrantLock();

public boolean probe() {
return !innerLock.isLocked();
Expand Down
2 changes: 1 addition & 1 deletion main/client/src/mill/main/client/lock/MemoryLocked.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class MemoryLocked implements Locked {

protected java.util.concurrent.locks.Lock lock;
final protected java.util.concurrent.locks.Lock lock;

public MemoryLocked(java.util.concurrent.locks.Lock lock) {
this.lock = lock;
Expand Down
2 changes: 1 addition & 1 deletion main/client/src/mill/main/client/lock/MemoryTryLocked.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package mill.main.client.lock;

public class MemoryTryLocked extends MemoryLocked implements TryLocked {
class MemoryTryLocked extends MemoryLocked implements TryLocked {
public MemoryTryLocked(java.util.concurrent.locks.Lock lock) {
super(lock);
}
Expand Down
2 changes: 1 addition & 1 deletion main/client/src/mill/main/client/lock/TryLocked.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package mill.main.client.lock;

public interface TryLocked extends Locked {
public boolean isLocked();
boolean isLocked();
}

0 comments on commit 89c93e9

Please sign in to comment.