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

Fix | Fix SharedTimer implementation to use class level lock for thread safety #1046

Merged
merged 3 commits into from
May 15, 2019
Merged
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
48 changes: 27 additions & 21 deletions src/main/java/com/microsoft/sqlserver/jdbc/SharedTimer.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;


Expand All @@ -16,19 +17,21 @@ class SharedTimer implements Serializable {
* Always update serialVersionUID when prompted
*/
private static final long serialVersionUID = -4069361613863955760L;

static final String CORE_THREAD_PREFIX = "mssql-jdbc-shared-timer-core-";
private static final AtomicLong CORE_THREAD_COUNTER = new AtomicLong();
private static SharedTimer instance;

private static final AtomicLong CORE_THREAD_COUNTER = new AtomicLong();
private static final Object lock = new Object();
/**
* Unique ID of this SharedTimer
*/
private final long id = CORE_THREAD_COUNTER.getAndIncrement();
/**
* Number of outstanding references to this SharedTimer
*/
private int refCount = 0;
private final AtomicInteger refCount = new AtomicInteger();

private static volatile SharedTimer instance;
private ScheduledThreadPoolExecutor executor;

private SharedTimer() {
Expand All @@ -47,7 +50,7 @@ public long getId() {
/**
* @return Whether there is an instance of the SharedTimer currently allocated.
*/
static synchronized boolean isRunning() {
static boolean isRunning() {
return instance != null;
}

Expand All @@ -56,16 +59,17 @@ static synchronized boolean isRunning() {
*
* If the reference count reaches zero then the underlying executor will be shutdown so that its thread stops.
*/
public synchronized void removeRef() {
if (refCount <= 0) {
throw new IllegalStateException("removeRef() called more than actual references");
}
refCount -= 1;
if (refCount == 0) {
// Removed last reference so perform cleanup
executor.shutdownNow();
executor = null;
instance = null;
public void removeRef() {
cheenamalhotra marked this conversation as resolved.
Show resolved Hide resolved
synchronized (lock) {
if (refCount.get() <= 0) {
throw new IllegalStateException("removeRef() called more than actual references");
cheenamalhotra marked this conversation as resolved.
Show resolved Hide resolved
}
if (refCount.decrementAndGet() == 0) {
// Removed last reference so perform cleanup
executor.shutdownNow();
executor = null;
instance = null;
}
}
}

Expand All @@ -76,13 +80,15 @@ public synchronized void removeRef() {
*
* When the caller is finished with the SharedTimer it must be released via {@link#removeRef}
*/
public static synchronized SharedTimer getTimer() {
if (instance == null) {
// No shared object exists so create a new one
instance = new SharedTimer();
public static SharedTimer getTimer() {
cheenamalhotra marked this conversation as resolved.
Show resolved Hide resolved
synchronized (lock) {
if (instance == null) {
// No shared object exists so create a new one
instance = new SharedTimer();
}
instance.refCount.getAndIncrement();
return instance;
}
instance.refCount += 1;
return instance;
}

/**
Expand Down