From dc932764eceef434a1c338a5f1296b66f779d52b Mon Sep 17 00:00:00 2001
From: akwizgran <michael@briarproject.org>
Date: Sat, 24 Nov 2012 12:32:45 +0000
Subject: [PATCH] You can't use an AtomicBoolean as a semaphore.

---
 src/net/sf/briar/plugins/modem/ModemImpl.java | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/src/net/sf/briar/plugins/modem/ModemImpl.java b/src/net/sf/briar/plugins/modem/ModemImpl.java
index 6440269455..da217a881c 100644
--- a/src/net/sf/briar/plugins/modem/ModemImpl.java
+++ b/src/net/sf/briar/plugins/modem/ModemImpl.java
@@ -11,6 +11,7 @@ import java.io.OutputStream;
 import java.util.concurrent.BlockingQueue;
 import java.util.concurrent.Executor;
 import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.Semaphore;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.logging.Logger;
 
@@ -33,7 +34,8 @@ class ModemImpl implements Modem, SerialPortEventListener {
 	private final Executor executor;
 	private final Callback callback;
 	private final SerialPort port;
-	private final AtomicBoolean initialised, offHook, connected;
+	private final AtomicBoolean initialised, connected;
+	private final Semaphore offHook;
 	private final BlockingQueue<byte[]> received;
 	private final byte[] line;
 
@@ -45,7 +47,7 @@ class ModemImpl implements Modem, SerialPortEventListener {
 		this.callback = callback;
 		port = new SerialPort(portName);
 		initialised = new AtomicBoolean(false);
-		offHook = new AtomicBoolean(false);
+		offHook = new Semaphore(1);
 		connected = new AtomicBoolean(false);
 		received = new LinkedBlockingQueue<byte[]>();
 		line = new byte[MAX_LINE_LENGTH];
@@ -94,7 +96,7 @@ class ModemImpl implements Modem, SerialPortEventListener {
 	}
 
 	public boolean dial(String number) throws IOException {
-		if(offHook.getAndSet(true)) {
+		if(!offHook.tryAcquire()) {
 			if(LOG.isLoggable(INFO))
 				LOG.info("Not dialling - call in progress");
 			return false;
@@ -140,7 +142,7 @@ class ModemImpl implements Modem, SerialPortEventListener {
 		}
 		received.add(new byte[0]); // Empty buffer indicates EOF
 		connected.set(false);
-		offHook.set(false);
+		offHook.release();
 	}
 
 	public void serialEvent(SerialPortEvent ev) {
@@ -208,7 +210,7 @@ class ModemImpl implements Modem, SerialPortEventListener {
 	}
 
 	private void answer() throws IOException {
-		if(offHook.getAndSet(true)) {
+		if(offHook.tryAcquire()) {
 			if(LOG.isLoggable(INFO))
 				LOG.info("Not answering - call in progress");
 			return;
-- 
GitLab