-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4799 from MovingBlocks/refactor/remove-usage-Thre…
…adManagerSubsystem
- Loading branch information
Showing
13 changed files
with
139 additions
and
123 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
engine/src/main/java/org/terasology/engine/core/GameScheduler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright 2021 The Terasology Foundation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package org.terasology.engine.core; | ||
|
||
import org.terasology.engine.monitoring.ThreadActivity; | ||
import org.terasology.engine.monitoring.ThreadMonitor; | ||
import org.terasology.gestalt.module.sandbox.API; | ||
import reactor.core.Disposable; | ||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
import reactor.core.scheduler.Scheduler; | ||
import reactor.core.scheduler.Schedulers; | ||
|
||
import java.security.AccessController; | ||
import java.security.PrivilegedAction; | ||
|
||
/** Schedulers to asynchronously run tasks on other threads. */ | ||
@API | ||
public final class GameScheduler { | ||
|
||
private static final Scheduler MAIN; | ||
|
||
static { | ||
MAIN = Schedulers.fromExecutor(GameThread::asynch); | ||
|
||
// Calling Reactor scheduler once to make sure it's initialized. | ||
// doPriviledged in case this class isn't initialized before the security policy is installed. | ||
AccessController.doPrivileged((PrivilegedAction<Scheduler>) Schedulers::parallel); | ||
} | ||
|
||
private GameScheduler() { | ||
} | ||
|
||
/** | ||
* A Scheduler to run tasks on the main thread. | ||
* <p> | ||
* <b>⚠</b> Use this only when necessary, as anything executed on the main thread will delay the core game loop. | ||
*/ | ||
public static Scheduler gameMain() { | ||
return MAIN; | ||
} | ||
|
||
/** | ||
* A Scheduler to run tasks off the main thread. | ||
* <p> | ||
* You can use this {@link Scheduler} with a | ||
* <ul> | ||
* <li>{@link Runnable}, to run a function with no return value. | ||
* <li>{@link Mono}, to run an operation one time, providing a future result. | ||
* <li>{@link Flux}, to asynchronously generate a stream of events over time. | ||
* </ul> | ||
* <p> | ||
* You can expect this to always return the <em>same</em> scheduler; it does not create a new scheduler instance or thread | ||
* on every call. | ||
* | ||
* @return (singleton) | ||
* @see <a href="https://projectreactor.io/docs/core/release/reference/#core-features">Reactor Core Features</a> | ||
*/ | ||
public static Scheduler parallel() { | ||
return Schedulers.parallel(); | ||
} | ||
|
||
/** | ||
* Run a task asynchronously, named for monitoring. | ||
* <p> | ||
* The task will be run on the {@link #parallel()} scheduler. | ||
*/ | ||
@SuppressWarnings("UnusedReturnValue") | ||
public static Disposable scheduleParallel(String name, Runnable task) { | ||
Mono<?> mono = Mono.using( | ||
() -> ThreadMonitor.startThreadActivity(name), | ||
activity -> Mono.fromRunnable(task), | ||
ThreadActivity::close | ||
) | ||
.subscribeOn(Schedulers.parallel()); | ||
return mono.subscribe(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 0 additions & 12 deletions
12
engine/src/main/java/org/terasology/engine/core/subsystem/common/ThreadManager.java
This file was deleted.
Oops, something went wrong.
82 changes: 0 additions & 82 deletions
82
engine/src/main/java/org/terasology/engine/core/subsystem/common/ThreadManagerSubsystem.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.