Skip to content

Commit

Permalink
Do not lock webcam when locking is disabled, closes #131
Browse files Browse the repository at this point in the history
  • Loading branch information
sarxos committed Aug 5, 2013
1 parent 32bc81a commit a2ee2f3
Showing 1 changed file with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public LockUpdater() {
@Override
public void run() {
do {
if (disabled.get()) {
return;
}
update();
try {
Thread.sleep(INTERVAL);
Expand Down Expand Up @@ -79,6 +82,11 @@ public void run() {
*/
private AtomicBoolean locked = new AtomicBoolean(false);

/**
* Is lock completely disabled.
*/
private AtomicBoolean disabled = new AtomicBoolean(false);

/**
* Lock file.
*/
Expand All @@ -102,6 +110,10 @@ private String getLockName() {

private void write(long value) {

if (disabled.get()) {
return;
}

String name = getLockName();

File tmp = null;
Expand Down Expand Up @@ -215,6 +227,10 @@ private void write(long value) {

private long read() {

if (disabled.get()) {
return -1;
}

DataInputStream dis = null;

long value = -1;
Expand Down Expand Up @@ -249,6 +265,11 @@ private long read() {
}

private void update() {

if (disabled.get()) {
return;
}

write(System.currentTimeMillis());
}

Expand All @@ -257,6 +278,10 @@ private void update() {
*/
public void lock() {

if (disabled.get()) {
return;
}

if (isLocked()) {
throw new WebcamLockException(String.format("Webcam %s has already been locked", webcam.getName()));
}
Expand All @@ -273,11 +298,30 @@ public void lock() {
updater.start();
}

/**
* Completely disable locking mechanism. After this method is invoked, the
* lock will not have any effect on the webcam runtime.
*/
public void disable() {
if (disabled.compareAndSet(false, true)) {
LOG.info("Locking mechanism has been disabled in {}", webcam);
if (updater != null) {
updater.interrupt();
}
}
}

/**
* Unlock webcam.
*/
public void unlock() {

// do nothing when lock disabled

if (disabled.get()) {
return;
}

if (!locked.compareAndSet(true, false)) {
return;
}
Expand All @@ -300,6 +344,12 @@ public void unlock() {
*/
public boolean isLocked() {

// always return false when lock is disabled

if (disabled.get()) {
return false;
}

// check if locked by current process

if (locked.get()) {
Expand Down

0 comments on commit a2ee2f3

Please sign in to comment.