Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows client-server fixes #1230

Merged
merged 5 commits into from
Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions main/client/src/mill/main/client/Lock.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package mill.main.client;
public abstract class Lock implements AutoCloseable{
abstract public Locked lock() throws Exception;
abstract public Locked tryLock() throws Exception;

public void await() throws Exception{
public abstract class Lock implements AutoCloseable {

public abstract Locked lock() throws Exception;

public abstract Locked tryLock() throws Exception;

public void await() throws Exception {
lock().release();
}

/**
* Returns `true` if the lock is *available for taking*
*/
abstract public boolean probe() throws Exception;
public abstract boolean probe() throws Exception;
}
6 changes: 1 addition & 5 deletions main/client/src/mill/main/client/Locked.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package mill.main.client;

import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.util.concurrent.locks.ReentrantLock;
public interface Locked {


public interface Locked{
public void release() throws Exception;
}
66 changes: 40 additions & 26 deletions main/client/src/mill/main/client/Locks.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@
import java.nio.channels.FileChannel;
import java.util.concurrent.locks.ReentrantLock;

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

public Lock processLock;
public Lock serverLock;
public Lock clientLock;
public static Locks files(String lockBase) throws Exception{

public static Locks files(String lockBase) throws Exception {
return new Locks(){{
processLock = new FileLock(lockBase + "/pid");

serverLock = new FileLock(lockBase + "/serverLock");

clientLock = new FileLock(lockBase + "/clientLock");
}};
}
public static Locks memory(){

public static Locks memory() {
return new Locks(){{
this.processLock = new MemoryLock();
this.serverLock = new MemoryLock();
Expand All @@ -32,35 +33,41 @@ public void close() throws Exception {
clientLock.close();
}
}
class FileLocked implements Locked{

class FileLocked implements Locked {

private java.nio.channels.FileLock lock;
public FileLocked(java.nio.channels.FileLock lock){

public FileLocked(java.nio.channels.FileLock lock) {
this.lock = lock;
}
public void release() throws Exception{

public void release() throws Exception {
this.lock.release();
}
}

class FileLock extends Lock{
String path;
RandomAccessFile raf;
FileChannel chan;
public FileLock(String path) throws Exception{
this.path = path;
class FileLock extends Lock {

private RandomAccessFile raf;
private FileChannel chan;

public FileLock(String path) throws Exception {
raf = new RandomAccessFile(path, "rw");
chan = raf.getChannel();
}

public Locked lock() throws Exception{
public Locked lock() throws Exception {
return new FileLocked(chan.lock());
}
public Locked tryLock() throws Exception{

public Locked tryLock() throws Exception {
java.nio.channels.FileLock l = chan.tryLock();
if (l == null) return null;
else return new FileLocked(l);
}
public boolean probe()throws Exception{

public boolean probe() throws Exception {
java.nio.channels.FileLock l = chan.tryLock();
if (l == null) return false;
else {
Expand All @@ -71,30 +78,37 @@ public boolean probe()throws Exception{

@Override
public void close() throws Exception {
raf.close();
chan.close();
raf.close();
}
}
class MemoryLocked implements Locked{
java.util.concurrent.locks.Lock l;
public MemoryLocked(java.util.concurrent.locks.Lock l){

class MemoryLocked implements Locked {

private java.util.concurrent.locks.Lock l;

public MemoryLocked(java.util.concurrent.locks.Lock l) {
this.l = l;
}
public void release() throws Exception{

public void release() throws Exception {
l.unlock();
}
}

class MemoryLock extends Lock{
ReentrantLock innerLock = new ReentrantLock(true);
class MemoryLock extends Lock {

private ReentrantLock innerLock = new ReentrantLock();

public boolean probe(){
return !innerLock.isLocked();
public boolean probe() {
return !innerLock.isLocked();
}

public Locked lock() {
innerLock.lock();
return new MemoryLocked(innerLock);
}

public Locked tryLock() {
if (innerLock.tryLock()) return new MemoryLocked(innerLock);
else return null;
Expand Down
Loading