@GuardedBy annotations.

This commit is contained in:
akwizgran
2018-12-20 22:34:52 +00:00
parent 8c25732d13
commit 189ec874cc
11 changed files with 45 additions and 27 deletions

View File

@@ -14,6 +14,7 @@ import java.io.File;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import javax.annotation.concurrent.GuardedBy;
import javax.inject.Inject; import javax.inject.Inject;
import static java.util.logging.Logger.getLogger; import static java.util.logging.Logger.getLogger;
@@ -39,7 +40,7 @@ class AndroidAccountManager extends AccountManagerImpl
appContext = app.getApplicationContext(); appContext = app.getApplicationContext();
} }
// Locking: stateChangeLock @GuardedBy("stateChangeLock")
@Override @Override
@Nullable @Nullable
protected String loadEncryptedDatabaseKey() { protected String loadEncryptedDatabaseKey() {
@@ -49,7 +50,7 @@ class AndroidAccountManager extends AccountManagerImpl
return key; return key;
} }
// Locking: stateChangeLock @GuardedBy("stateChangeLock")
@Nullable @Nullable
private String getDatabaseKeyFromPreferences() { private String getDatabaseKeyFromPreferences() {
String key = prefs.getString(PREF_DB_KEY, null); String key = prefs.getString(PREF_DB_KEY, null);
@@ -58,7 +59,7 @@ class AndroidAccountManager extends AccountManagerImpl
return key; return key;
} }
// Locking: stateChangeLock @GuardedBy("stateChangeLock")
private void migrateDatabaseKeyToFile(String key) { private void migrateDatabaseKeyToFile(String key) {
if (storeEncryptedDatabaseKey(key)) { if (storeEncryptedDatabaseKey(key)) {
if (prefs.edit().remove(PREF_DB_KEY).commit()) if (prefs.edit().remove(PREF_DB_KEY).commit())
@@ -83,7 +84,7 @@ class AndroidAccountManager extends AccountManagerImpl
return PreferenceManager.getDefaultSharedPreferences(appContext); return PreferenceManager.getDefaultSharedPreferences(appContext);
} }
// Locking: stateChangeLock @GuardedBy("stateChangeLock")
private void deleteAppData(SharedPreferences... clear) { private void deleteAppData(SharedPreferences... clear) {
// Clear and commit shared preferences // Clear and commit shared preferences
for (SharedPreferences prefs : clear) { for (SharedPreferences prefs : clear) {

View File

@@ -10,6 +10,7 @@ import java.util.concurrent.TimeUnit;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import javax.annotation.concurrent.GuardedBy;
import javax.annotation.concurrent.ThreadSafe; import javax.annotation.concurrent.ThreadSafe;
import static java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.concurrent.TimeUnit.MILLISECONDS;
@@ -37,10 +38,12 @@ public class RenewableWakeLock {
private final Runnable renewTask; private final Runnable renewTask;
private final Object lock = new Object(); private final Object lock = new Object();
@GuardedBy("lock")
@Nullable @Nullable
private PowerManager.WakeLock wakeLock; // Locking: lock private PowerManager.WakeLock wakeLock;
@GuardedBy("lock")
@Nullable @Nullable
private ScheduledFuture future; // Locking: lock private ScheduledFuture future;
public RenewableWakeLock(PowerManager powerManager, public RenewableWakeLock(PowerManager powerManager,
ScheduledExecutorService scheduler, int levelAndFlags, String tag, ScheduledExecutorService scheduler, int levelAndFlags, String tag,

View File

@@ -17,6 +17,7 @@ import java.io.InputStreamReader;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import javax.annotation.concurrent.GuardedBy;
import javax.inject.Inject; import javax.inject.Inject;
import static java.util.logging.Level.WARNING; import static java.util.logging.Level.WARNING;
@@ -67,7 +68,7 @@ class AccountManagerImpl implements AccountManager {
return databaseKey; return databaseKey;
} }
// Locking: stateChangeLock @GuardedBy("stateChangeLock")
@Nullable @Nullable
protected String loadEncryptedDatabaseKey() { protected String loadEncryptedDatabaseKey() {
String key = readDbKeyFromFile(dbKeyFile); String key = readDbKeyFromFile(dbKeyFile);
@@ -82,7 +83,7 @@ class AccountManagerImpl implements AccountManager {
return key; return key;
} }
// Locking: stateChangeLock @GuardedBy("stateChangeLock")
@Nullable @Nullable
private String readDbKeyFromFile(File f) { private String readDbKeyFromFile(File f) {
if (!f.exists()) { if (!f.exists()) {
@@ -101,7 +102,7 @@ class AccountManagerImpl implements AccountManager {
} }
} }
// Locking: stateChangeLock @GuardedBy("stateChangeLock")
boolean storeEncryptedDatabaseKey(String hex) { boolean storeEncryptedDatabaseKey(String hex) {
LOG.info("Storing database key in file"); LOG.info("Storing database key in file");
// Create the directory if necessary // Create the directory if necessary
@@ -139,7 +140,7 @@ class AccountManagerImpl implements AccountManager {
} }
} }
// Locking: stateChangeLock @GuardedBy("stateChangeLock")
private void writeDbKeyToFile(String key, File f) throws IOException { private void writeDbKeyToFile(String key, File f) throws IOException {
FileOutputStream out = new FileOutputStream(f); FileOutputStream out = new FileOutputStream(f);
out.write(key.getBytes("UTF-8")); out.write(key.getBytes("UTF-8"));
@@ -169,7 +170,7 @@ class AccountManagerImpl implements AccountManager {
} }
} }
// Locking: stateChangeLock @GuardedBy("stateChangeLock")
private boolean encryptAndStoreDatabaseKey(SecretKey key, String password) { private boolean encryptAndStoreDatabaseKey(SecretKey key, String password) {
byte[] plaintext = key.getBytes(); byte[] plaintext = key.getBytes();
byte[] ciphertext = crypto.encryptWithPassword(plaintext, password); byte[] ciphertext = crypto.encryptWithPassword(plaintext, password);
@@ -196,7 +197,7 @@ class AccountManagerImpl implements AccountManager {
} }
} }
// Locking: stateChangeLock @GuardedBy("stateChangeLock")
@Nullable @Nullable
private SecretKey loadAndDecryptDatabaseKey(String password) { private SecretKey loadAndDecryptDatabaseKey(String password) {
String hex = loadEncryptedDatabaseKey(); String hex = loadEncryptedDatabaseKey();

View File

@@ -52,6 +52,7 @@ import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import javax.annotation.concurrent.GuardedBy;
import static java.lang.System.arraycopy; import static java.lang.System.arraycopy;
import static java.sql.Types.INTEGER; import static java.sql.Types.INTEGER;
@@ -319,11 +320,13 @@ abstract class JdbcDatabase implements Database<Connection> {
private final Clock clock; private final Clock clock;
private final DatabaseTypes dbTypes; private final DatabaseTypes dbTypes;
// Locking: connectionsLock @GuardedBy("connectionsLock")
private final LinkedList<Connection> connections = new LinkedList<>(); private final LinkedList<Connection> connections = new LinkedList<>();
private int openConnections = 0; // Locking: connectionsLock @GuardedBy("connectionsLock")
private boolean closed = false; // Locking: connectionsLock private int openConnections = 0;
@GuardedBy("connectionsLock")
private boolean closed = false;
protected abstract Connection createConnection() throws SQLException; protected abstract Connection createConnection() throws SQLException;

View File

@@ -36,6 +36,7 @@ import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.annotation.concurrent.GuardedBy;
import javax.annotation.concurrent.ThreadSafe; import javax.annotation.concurrent.ThreadSafe;
import static java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.concurrent.TimeUnit.MILLISECONDS;
@@ -59,7 +60,8 @@ class Poller implements EventListener {
private final SecureRandom random; private final SecureRandom random;
private final Clock clock; private final Clock clock;
private final Lock lock; private final Lock lock;
private final Map<TransportId, ScheduledPollTask> tasks; // Locking: lock @GuardedBy("lock")
private final Map<TransportId, ScheduledPollTask> tasks;
Poller(@IoExecutor Executor ioExecutor, Poller(@IoExecutor Executor ioExecutor,
@Scheduler ScheduledExecutorService scheduler, @Scheduler ScheduledExecutorService scheduler,

View File

@@ -28,6 +28,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.annotation.concurrent.GuardedBy;
import javax.annotation.concurrent.ThreadSafe; import javax.annotation.concurrent.ThreadSafe;
import static java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.concurrent.TimeUnit.MILLISECONDS;
@@ -111,7 +112,7 @@ class TransportKeyManagerImpl implements TransportKeyManager {
return rotationResult; return rotationResult;
} }
// Locking: lock @GuardedBy("lock")
private void addKeys(Collection<KeySet> keys) { private void addKeys(Collection<KeySet> keys) {
for (KeySet ks : keys) { for (KeySet ks : keys) {
addKeys(ks.getKeySetId(), ks.getContactId(), addKeys(ks.getKeySetId(), ks.getContactId(),
@@ -119,7 +120,7 @@ class TransportKeyManagerImpl implements TransportKeyManager {
} }
} }
// Locking: lock @GuardedBy("lock")
private void addKeys(KeySetId keySetId, ContactId contactId, private void addKeys(KeySetId keySetId, ContactId contactId,
MutableTransportKeys m) { MutableTransportKeys m) {
MutableKeySet ks = new MutableKeySet(keySetId, contactId, m); MutableKeySet ks = new MutableKeySet(keySetId, contactId, m);
@@ -130,7 +131,7 @@ class TransportKeyManagerImpl implements TransportKeyManager {
considerReplacingOutgoingKeys(ks); considerReplacingOutgoingKeys(ks);
} }
// Locking: lock @GuardedBy("lock")
private void encodeTags(KeySetId keySetId, ContactId contactId, private void encodeTags(KeySetId keySetId, ContactId contactId,
MutableIncomingKeys inKeys) { MutableIncomingKeys inKeys) {
for (long streamNumber : inKeys.getWindow().getUnseen()) { for (long streamNumber : inKeys.getWindow().getUnseen()) {
@@ -143,7 +144,7 @@ class TransportKeyManagerImpl implements TransportKeyManager {
} }
} }
// Locking: lock @GuardedBy("lock")
private void considerReplacingOutgoingKeys(MutableKeySet ks) { private void considerReplacingOutgoingKeys(MutableKeySet ks) {
// Use the active outgoing keys with the highest key set ID // Use the active outgoing keys with the highest key set ID
if (ks.getTransportKeys().getCurrentOutgoingKeys().isActive()) { if (ks.getTransportKeys().getCurrentOutgoingKeys().isActive()) {

View File

@@ -22,6 +22,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import javax.annotation.concurrent.GuardedBy;
import javax.annotation.concurrent.ThreadSafe; import javax.annotation.concurrent.ThreadSafe;
import static com.sun.jna.Library.OPTION_FUNCTION_MAPPER; import static com.sun.jna.Library.OPTION_FUNCTION_MAPPER;
@@ -45,7 +46,8 @@ class WindowsShutdownManagerImpl extends ShutdownManagerImpl {
private final Map<String, Object> options; private final Map<String, Object> options;
private boolean initialised = false; // Locking: lock @GuardedBy("lock")
private boolean initialised = false;
WindowsShutdownManagerImpl() { WindowsShutdownManagerImpl() {
// Use the Unicode versions of Win32 API calls // Use the Unicode versions of Win32 API calls
@@ -71,7 +73,7 @@ class WindowsShutdownManagerImpl extends ShutdownManagerImpl {
return new StartOnce(r); return new StartOnce(r);
} }
// Locking: lock @GuardedBy("lock")
private void initialise() { private void initialise() {
if (isWindows()) { if (isWindows()) {
new EventLoop().start(); new EventLoop().start();

View File

@@ -182,7 +182,7 @@ class ModemImpl implements Modem, WriteHandler, SerialPortEventListener {
} }
} }
// Locking: stateChange @GuardedBy("stateChange")
private void hangUpInner() throws IOException { private void hangUpInner() throws IOException {
ReliabilityLayer reliability; ReliabilityLayer reliability;
lock.lock(); lock.lock();

View File

@@ -10,6 +10,7 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import java.util.Locale; import java.util.Locale;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import javax.annotation.concurrent.GuardedBy;
import static android.os.Build.VERSION.SDK_INT; import static android.os.Build.VERSION.SDK_INT;
import static org.briarproject.briar.android.settings.SettingsFragment.LANGUAGE; import static org.briarproject.briar.android.settings.SettingsFragment.LANGUAGE;
@@ -17,7 +18,7 @@ import static org.briarproject.briar.android.settings.SettingsFragment.LANGUAGE;
@NotNullByDefault @NotNullByDefault
public class Localizer { public class Localizer {
// Locking: class @GuardedBy("Localizer.class")
@Nullable @Nullable
private static Localizer INSTANCE; private static Localizer INSTANCE;
private final Locale systemLocale; private final Locale systemLocale;

View File

@@ -9,6 +9,7 @@ import java.util.TimeZone;
import java.util.logging.Formatter; import java.util.logging.Formatter;
import java.util.logging.LogRecord; import java.util.logging.LogRecord;
import javax.annotation.concurrent.GuardedBy;
import javax.annotation.concurrent.ThreadSafe; import javax.annotation.concurrent.ThreadSafe;
import static java.util.Locale.US; import static java.util.Locale.US;
@@ -18,8 +19,10 @@ import static java.util.Locale.US;
public class BriefLogFormatter extends Formatter { public class BriefLogFormatter extends Formatter {
private final Object lock = new Object(); private final Object lock = new Object();
private final DateFormat dateFormat; // Locking: lock @GuardedBy("lock")
private final Date date; // Locking: lock private final DateFormat dateFormat;
@GuardedBy("lock")
private final Date date;
public BriefLogFormatter() { public BriefLogFormatter() {
synchronized (lock) { synchronized (lock) {

View File

@@ -9,6 +9,7 @@ import java.util.Queue;
import java.util.logging.Handler; import java.util.logging.Handler;
import java.util.logging.LogRecord; import java.util.logging.LogRecord;
import javax.annotation.concurrent.GuardedBy;
import javax.annotation.concurrent.ThreadSafe; import javax.annotation.concurrent.ThreadSafe;
@ThreadSafe @ThreadSafe
@@ -18,7 +19,7 @@ public class CachingLogHandler extends Handler {
private static final int MAX_RECENT_RECORDS = 100; private static final int MAX_RECENT_RECORDS = 100;
private final Object lock = new Object(); private final Object lock = new Object();
// Locking: lock @GuardedBy("lock")
private final Queue<LogRecord> recent = new LinkedList<>(); private final Queue<LogRecord> recent = new LinkedList<>();
@Override @Override