Skip to content

Commit f67f649

Browse files
committed
Make task executor lifecycled to platform readiness.
Closes #2459.
1 parent a9819ca commit f67f649

File tree

3 files changed

+38
-11
lines changed

3 files changed

+38
-11
lines changed

worldedit-core/src/main/java/com/sk89q/worldedit/WorldEdit.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import com.google.common.collect.Lists;
2727
import com.google.common.collect.SetMultimap;
2828
import com.google.common.util.concurrent.ListeningExecutorService;
29-
import com.google.common.util.concurrent.MoreExecutors;
3029
import com.sk89q.worldedit.blocks.BaseItem;
3130
import com.sk89q.worldedit.entity.Player;
3231
import com.sk89q.worldedit.event.platform.BlockInteractEvent;
@@ -55,7 +54,6 @@
5554
import com.sk89q.worldedit.util.Direction;
5655
import com.sk89q.worldedit.util.Location;
5756
import com.sk89q.worldedit.util.asset.AssetLoaders;
58-
import com.sk89q.worldedit.util.concurrency.EvenMoreExecutors;
5957
import com.sk89q.worldedit.util.eventbus.EventBus;
6058
import com.sk89q.worldedit.util.formatting.text.TextComponent;
6159
import com.sk89q.worldedit.util.formatting.text.TranslatableComponent;
@@ -124,8 +122,6 @@ public final class WorldEdit {
124122
@Deprecated
125123
private final EditSessionFactory editSessionFactory = new EditSessionFactory.EditSessionFactoryImpl();
126124
private final SessionManager sessions = new SessionManager(this);
127-
private final ListeningExecutorService executorService = MoreExecutors.listeningDecorator(
128-
EvenMoreExecutors.newBoundedCachedThreadPool(0, 1, 20, "WorldEdit Task Executor - %s"));
129125
private final Supervisor supervisor = new SimpleSupervisor();
130126
private final AssetLoaders assetLoaders = new AssetLoaders(this);
131127

@@ -192,7 +188,7 @@ public Supervisor getSupervisor() {
192188
* @return the executor service
193189
*/
194190
public ListeningExecutorService getExecutorService() {
195-
return executorService;
191+
return platformManager.getExecutorService();
196192
}
197193

198194
/**

worldedit-core/src/main/java/com/sk89q/worldedit/event/platform/PlatformInitializeEvent.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
/**
2525
* Fired when configuration has been loaded and the platform is in the
26-
* intialization stage.
26+
* initialization stage.
2727
*
2828
* <p>This event is fired once.</p>
2929
*/

worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformManager.java

+36-5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919

2020
package com.sk89q.worldedit.extension.platform;
2121

22+
import com.google.common.collect.Maps;
23+
import com.google.common.util.concurrent.ListeningExecutorService;
24+
import com.google.common.util.concurrent.MoreExecutors;
2225
import com.sk89q.worldedit.LocalConfiguration;
2326
import com.sk89q.worldedit.LocalSession;
2427
import com.sk89q.worldedit.WorldEdit;
@@ -41,7 +44,9 @@
4144
import com.sk89q.worldedit.util.HandSide;
4245
import com.sk89q.worldedit.util.Location;
4346
import com.sk89q.worldedit.util.SideEffect;
47+
import com.sk89q.worldedit.util.concurrency.EvenMoreExecutors;
4448
import com.sk89q.worldedit.util.eventbus.Subscribe;
49+
import com.sk89q.worldedit.util.lifecycle.SimpleLifecycled;
4550
import com.sk89q.worldedit.world.World;
4651
import org.apache.logging.log4j.Logger;
4752

@@ -69,7 +74,8 @@ public class PlatformManager {
6974

7075
private final WorldEdit worldEdit;
7176
private final PlatformCommandManager platformCommandManager;
72-
private final List<Platform> platforms = new ArrayList<>();
77+
private final SimpleLifecycled<ListeningExecutorService> executorService;
78+
private final Map<Platform, Boolean> platforms = Maps.newHashMap();
7379
private final Map<Capability, Platform> preferences = new EnumMap<>(Capability.class);
7480
private @Nullable String firstSeenVersion;
7581
private final AtomicBoolean initialized = new AtomicBoolean();
@@ -84,6 +90,7 @@ public PlatformManager(WorldEdit worldEdit) {
8490
checkNotNull(worldEdit);
8591
this.worldEdit = worldEdit;
8692
this.platformCommandManager = new PlatformCommandManager(worldEdit, this);
93+
this.executorService = SimpleLifecycled.invalid();
8794

8895
// Register this instance for events
8996
worldEdit.getEventBus().register(this);
@@ -101,7 +108,7 @@ public synchronized void register(Platform platform) {
101108

102109
// Just add the platform to the list of platforms: we'll pick favorites
103110
// once all the platforms have been loaded
104-
platforms.add(platform);
111+
platforms.put(platform, false);
105112

106113
// Make sure that versions are in sync
107114
if (firstSeenVersion != null) {
@@ -126,7 +133,7 @@ public synchronized void register(Platform platform) {
126133
public synchronized boolean unregister(Platform platform) {
127134
checkNotNull(platform);
128135

129-
boolean removed = platforms.remove(platform);
136+
boolean removed = platforms.remove(platform) != null;
130137

131138
if (removed) {
132139
LOGGER.info("Unregistering " + platform.getClass().getCanonicalName() + " from WorldEdit");
@@ -212,7 +219,7 @@ private synchronized void choosePreferred() {
212219
Platform preferred = null;
213220
Preference highest = null;
214221

215-
for (Platform platform : platforms) {
222+
for (Platform platform : platforms.keySet()) {
216223
Preference preference = platform.getCapabilities().get(capability);
217224
if (preference != null && (highest == null || preference.isPreferredOver(highest))) {
218225
preferred = platform;
@@ -231,7 +238,7 @@ private synchronized void choosePreferred() {
231238
* @return a list of platforms
232239
*/
233240
public synchronized List<Platform> getPlatforms() {
234-
return new ArrayList<>(platforms);
241+
return new ArrayList<>(platforms.keySet());
235242
}
236243

237244
/**
@@ -284,6 +291,21 @@ public PlatformCommandManager getPlatformCommandManager() {
284291
return platformCommandManager;
285292
}
286293

294+
/**
295+
* Get the executor service. Internal, not for API use.
296+
*
297+
* @return the executor service
298+
*/
299+
public ListeningExecutorService getExecutorService() {
300+
return executorService.valueOrThrow();
301+
}
302+
303+
private static ListeningExecutorService createExecutor() {
304+
return MoreExecutors.listeningDecorator(
305+
EvenMoreExecutors.newBoundedCachedThreadPool(
306+
0, 1, 20, "WorldEdit Task Executor - %s"));
307+
}
308+
287309
/**
288310
* Get the current configuration.
289311
*
@@ -340,6 +362,10 @@ public void handlePlatformsRegistered(PlatformsRegisteredEvent event) {
340362
@Subscribe
341363
public void handleNewPlatformReady(PlatformReadyEvent event) {
342364
preferences.forEach((cap, platform) -> cap.ready(this, platform));
365+
platforms.put(event.getPlatform(), true);
366+
if (!executorService.isValid()) {
367+
executorService.newValue(createExecutor());
368+
}
343369
}
344370

345371
/**
@@ -348,6 +374,11 @@ public void handleNewPlatformReady(PlatformReadyEvent event) {
348374
@Subscribe
349375
public void handleNewPlatformUnready(PlatformUnreadyEvent event) {
350376
preferences.forEach((cap, platform) -> cap.unready(this, platform));
377+
platforms.put(event.getPlatform(), false);
378+
if (!platforms.containsValue(true)) {
379+
executorService.value().ifPresent(ListeningExecutorService::shutdownNow);
380+
executorService.invalidate();
381+
}
351382
}
352383

353384
@Subscribe

0 commit comments

Comments
 (0)