Skip to content

Commit d7f6b2a

Browse files
author
James Hagborg
committed
Synchronize stuff in periodicscheduler and preferences
1 parent 5d29f4d commit d7f6b2a

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

src/main/java/org/usfirst/frc/team69/util/PeriodicScheduler.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
* one can add methods to report information to the driver station, or to
99
* check if preferences have been changed.
1010
*
11+
* This class should be thread-safe, provided it is OK to run all events
12+
* from the main thread.
13+
*
1114
* @author James Hagborg
1215
*
1316
*/
@@ -20,7 +23,7 @@ public class PeriodicScheduler {
2023
*
2124
* @return The sinlge instance of {@link PeriodicScheduler}
2225
*/
23-
public static PeriodicScheduler getInstance() {
26+
public static synchronized PeriodicScheduler getInstance() {
2427
if (theInstance == null) {
2528
theInstance = new PeriodicScheduler();
2629
}
@@ -39,15 +42,15 @@ private PeriodicScheduler() {
3942
*
4043
* @param event A {@link Runnable} object representing the task to run
4144
*/
42-
public void addEvent(Runnable event) {
45+
public synchronized void addEvent(Runnable event) {
4346
events.add(event);
4447
}
4548

4649
/**
4750
* Run all of the events which have been added to the scheduler. If you
4851
* are using {@link HYPERRobot}, you do not need to call this manually.
4952
*/
50-
public void run() {
53+
public synchronized void run() {
5154
for (Runnable event : events) {
5255
event.run();
5356
}

src/main/java/org/usfirst/frc/team69/util/pref/PreferencesSet.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ public class PreferencesSet {
2727

2828
private static HashSet<String> setNames = new HashSet<String>();
2929

30+
/**
31+
* Check the set name does not exist and add it if it does. This needs to
32+
* be synchronized to access the global list.
33+
* @param name The name to add and check
34+
*/
35+
private synchronized static void registerSetName(String name) {
36+
if (setNames.contains(name)) {
37+
throw new IllegalStateException("A PreferencesSet named " + name + " already exists");
38+
}
39+
40+
setNames.add(name);
41+
}
42+
3043
/**
3144
* Construct a new {@link PreferencesSet} object with the given name and
3245
* {@link PreferencesListener}. Each time a preference in the set is
@@ -41,11 +54,9 @@ public PreferencesSet(String name, PreferencesListener listener) {
4154
throw new NullPointerException("name == null");
4255
} else if (listener == null) {
4356
throw new NullPointerException("listener == null");
44-
} if (setNames.contains(name)) {
45-
throw new IllegalStateException("A PreferencesSet named " + name + " already exists");
4657
}
4758

48-
setNames.add(name);
59+
registerSetName(name);
4960

5061
m_name = name;
5162
m_listener = listener;
@@ -108,7 +119,7 @@ private void addPreference(String name, Preference pref) {
108119
* @param value The default value
109120
* @return The newly created preference
110121
*/
111-
public DoublePreference addDouble(String name, double value) {
122+
public synchronized DoublePreference addDouble(String name, double value) {
112123
checkName(name);
113124
DoublePreference pref = new DoublePreference(m_name + SEPARATOR + name, value);
114125
addPreference(name, pref);
@@ -122,7 +133,7 @@ public DoublePreference addDouble(String name, double value) {
122133
* @param value The default value
123134
* @return The newly created preference
124135
*/
125-
public IntPreference addInt(String name, int value) {
136+
public synchronized IntPreference addInt(String name, int value) {
126137
checkName(name);
127138
IntPreference pref = new IntPreference(m_name + SEPARATOR + name, value);
128139
addPreference(name, pref);
@@ -136,7 +147,7 @@ public IntPreference addInt(String name, int value) {
136147
* @param value The default value
137148
* @return The newly created preference
138149
*/
139-
public BooleanPreference addBoolean(String name, boolean value) {
150+
public synchronized BooleanPreference addBoolean(String name, boolean value) {
140151
checkName(name);
141152
BooleanPreference pref = new BooleanPreference(m_name + SEPARATOR + name, value);
142153
addPreference(name, pref);
@@ -150,7 +161,7 @@ public BooleanPreference addBoolean(String name, boolean value) {
150161
* @param value The default value
151162
* @return The newly created preference
152163
*/
153-
public StringPreference addString(String name, String value) {
164+
public synchronized StringPreference addString(String name, String value) {
154165
checkName(name);
155166
StringPreference pref = new StringPreference(m_name + SEPARATOR + name, value);
156167
addPreference(name, pref);

0 commit comments

Comments
 (0)