Skip to content

Commit

Permalink
Make task executor lifecycled to platform readiness.
Browse files Browse the repository at this point in the history
Closes #2459.
  • Loading branch information
wizjany committed Feb 17, 2025
1 parent dc92f6f commit 81f5fcd
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.google.common.collect.Lists;
import com.google.common.collect.SetMultimap;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import com.sk89q.worldedit.blocks.BaseItem;
import com.sk89q.worldedit.entity.Player;
import com.sk89q.worldedit.event.platform.BlockInteractEvent;
Expand Down Expand Up @@ -55,7 +54,6 @@
import com.sk89q.worldedit.util.Direction;
import com.sk89q.worldedit.util.Location;
import com.sk89q.worldedit.util.asset.AssetLoaders;
import com.sk89q.worldedit.util.concurrency.EvenMoreExecutors;
import com.sk89q.worldedit.util.eventbus.EventBus;
import com.sk89q.worldedit.util.formatting.text.TextComponent;
import com.sk89q.worldedit.util.formatting.text.TranslatableComponent;
Expand Down Expand Up @@ -124,8 +122,6 @@ public final class WorldEdit {
@Deprecated
private final EditSessionFactory editSessionFactory = new EditSessionFactory.EditSessionFactoryImpl();
private final SessionManager sessions = new SessionManager(this);
private final ListeningExecutorService executorService = MoreExecutors.listeningDecorator(
EvenMoreExecutors.newBoundedCachedThreadPool(0, 1, 20, "WorldEdit Task Executor - %s"));
private final Supervisor supervisor = new SimpleSupervisor();
private final AssetLoaders assetLoaders = new AssetLoaders(this);

Expand Down Expand Up @@ -192,7 +188,7 @@ public Supervisor getSupervisor() {
* @return the executor service
*/
public ListeningExecutorService getExecutorService() {
return executorService;
return platformManager.getExecutorService();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

/**
* Fired when configuration has been loaded and the platform is in the
* intialization stage.
* initialization stage.
*
* <p>This event is fired once.</p>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

package com.sk89q.worldedit.extension.platform;

import com.google.common.collect.Maps;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import com.sk89q.worldedit.LocalConfiguration;
import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.WorldEdit;
Expand All @@ -41,7 +44,9 @@
import com.sk89q.worldedit.util.HandSide;
import com.sk89q.worldedit.util.Location;
import com.sk89q.worldedit.util.SideEffect;
import com.sk89q.worldedit.util.concurrency.EvenMoreExecutors;
import com.sk89q.worldedit.util.eventbus.Subscribe;
import com.sk89q.worldedit.util.lifecycle.SimpleLifecycled;
import com.sk89q.worldedit.world.World;
import org.apache.logging.log4j.Logger;

Expand Down Expand Up @@ -69,7 +74,8 @@ public class PlatformManager {

private final WorldEdit worldEdit;
private final PlatformCommandManager platformCommandManager;
private final List<Platform> platforms = new ArrayList<>();
private final SimpleLifecycled<ListeningExecutorService> executorService;
private final Map<Platform, Boolean> platforms = Maps.newHashMap();
private final Map<Capability, Platform> preferences = new EnumMap<>(Capability.class);
private @Nullable String firstSeenVersion;
private final AtomicBoolean initialized = new AtomicBoolean();
Expand All @@ -84,6 +90,7 @@ public PlatformManager(WorldEdit worldEdit) {
checkNotNull(worldEdit);
this.worldEdit = worldEdit;
this.platformCommandManager = new PlatformCommandManager(worldEdit, this);
this.executorService = SimpleLifecycled.invalid();

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

// Just add the platform to the list of platforms: we'll pick favorites
// once all the platforms have been loaded
platforms.add(platform);
platforms.put(platform, false);

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

boolean removed = platforms.remove(platform);
boolean removed = platforms.remove(platform) != null;

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

for (Platform platform : platforms) {
for (Platform platform : platforms.keySet()) {
Preference preference = platform.getCapabilities().get(capability);
if (preference != null && (highest == null || preference.isPreferredOver(highest))) {
preferred = platform;
Expand All @@ -231,7 +238,7 @@ private synchronized void choosePreferred() {
* @return a list of platforms
*/
public synchronized List<Platform> getPlatforms() {
return new ArrayList<>(platforms);
return new ArrayList<>(platforms.keySet());
}

/**
Expand Down Expand Up @@ -284,6 +291,21 @@ public PlatformCommandManager getPlatformCommandManager() {
return platformCommandManager;
}

/**
* Get the executor service. Internal, not for API use.
*
* @return the executor service
*/
public ListeningExecutorService getExecutorService() {
return executorService.valueOrThrow();
}

private static ListeningExecutorService createExecutor() {
return MoreExecutors.listeningDecorator(
EvenMoreExecutors.newBoundedCachedThreadPool(
0, 1, 20, "WorldEdit Task Executor - %s"));
}

/**
* Get the current configuration.
*
Expand Down Expand Up @@ -340,6 +362,10 @@ public void handlePlatformsRegistered(PlatformsRegisteredEvent event) {
@Subscribe
public void handleNewPlatformReady(PlatformReadyEvent event) {
preferences.forEach((cap, platform) -> cap.ready(this, platform));
platforms.put(event.getPlatform(), true);
if (!executorService.isValid()) {
executorService.newValue(createExecutor());
}
}

/**
Expand All @@ -348,6 +374,11 @@ public void handleNewPlatformReady(PlatformReadyEvent event) {
@Subscribe
public void handleNewPlatformUnready(PlatformUnreadyEvent event) {
preferences.forEach((cap, platform) -> cap.unready(this, platform));
platforms.put(event.getPlatform(), false);
if (!platforms.containsValue(true)) {
executorService.value().ifPresent(ListeningExecutorService::shutdownNow);
executorService.invalidate();
}
}

@Subscribe
Expand Down

0 comments on commit 81f5fcd

Please sign in to comment.