mirror of
https://github.com/Minestom/Minestom.git
synced 2024-12-31 21:48:08 +01:00
Reuse lock code
This commit is contained in:
parent
0043cc7275
commit
34d10e57fa
@ -42,17 +42,6 @@ public interface Acquirable<T> {
|
||||
return new AcquirableImpl<>(value);
|
||||
}
|
||||
|
||||
default void sync(@NotNull Consumer<T> consumer) {
|
||||
final Thread currentThread = Thread.currentThread();
|
||||
final TickThread tickThread = getHandler().getTickThread();
|
||||
Acquisition.acquire(currentThread, tickThread, () -> consumer.accept(unwrap()));
|
||||
}
|
||||
|
||||
default void async(@NotNull Consumer<T> consumer) {
|
||||
// TODO per-thread list
|
||||
AsyncUtils.runAsync(() -> sync(consumer));
|
||||
}
|
||||
|
||||
default @NotNull Acquired<T> lock() {
|
||||
var optional = optional();
|
||||
if (optional.isPresent()) {
|
||||
@ -65,6 +54,17 @@ public interface Acquirable<T> {
|
||||
}
|
||||
}
|
||||
|
||||
default void sync(@NotNull Consumer<T> consumer) {
|
||||
var acquired = lock();
|
||||
consumer.accept(acquired.get());
|
||||
acquired.unlock();
|
||||
}
|
||||
|
||||
default void async(@NotNull Consumer<T> consumer) {
|
||||
// TODO per-thread list
|
||||
AsyncUtils.runAsync(() -> sync(consumer));
|
||||
}
|
||||
|
||||
default @NotNull Optional<T> optional() {
|
||||
final Thread currentThread = Thread.currentThread();
|
||||
final TickThread tickThread = getHandler().getTickThread();
|
||||
|
Loading…
Reference in New Issue
Block a user