thread safe event system

This commit is contained in:
liujiahua123123 2019-08-17 15:53:38 +08:00
parent 5a7570001e
commit 69b97a3b24
3 changed files with 44 additions and 12 deletions

View File

@ -1,8 +1,10 @@
package net.mamoe.mirai.event;
import lombok.Getter;
import lombok.Setter;
import net.mamoe.mirai.event.events.Cancellable;
import net.mamoe.mirai.event.events.MiraiEvent;
import net.mamoe.mirai.event.events.server.ServerDisableEvent;
import java.util.function.Consumer;
import java.util.function.Predicate;
@ -21,6 +23,10 @@ public class MiraiEventHook<T extends MiraiEvent> {
@Getter
private volatile boolean ignoreCancelled = true;
@Getter
@Setter
private volatile boolean mount = false;
/**
* return true -> this hook need to be removed
*/
@ -74,4 +80,23 @@ public class MiraiEventHook<T extends MiraiEvent> {
return this.valid.test((T)event);
}
/**
* 更加安全高效的方式
* Remember to use {@link this.mount()} at last
* */
public static <D extends MiraiEvent> MiraiEventHook<D> onEvent(Class<D> event){
return new MiraiEventHook<>(event);
}
public void mount(){
MiraiEventManager.getInstance().registerHook(this);
}
public void mountOnce(){
MiraiEventManager.getInstance().hookOnce(this);
}
}

View File

@ -26,17 +26,11 @@ public class MiraiEventManager {
private Map<Class<? extends MiraiEvent>, List<MiraiEventHook<? extends MiraiEvent>>> hooks = new HashMap<>();
public <D extends MiraiEvent> void hookUntil(MiraiEventHook<D> hook, Predicate<D> toRemove){
hooksLock.lock();
hooks.putIfAbsent(hook.getEventClass(),new ArrayList<>());
hooks.get(hook.getEventClass()).add(hook.setValidUntil(toRemove));
hooksLock.unlock();
this.mountHook(hook.setValidUntil(toRemove));
}
public <D extends MiraiEvent> void hookWhile(MiraiEventHook<D> hook, Predicate<D> toKeep){
hooksLock.lock();
hooks.putIfAbsent(hook.getEventClass(),new ArrayList<>());
hooks.get(hook.getEventClass()).add(hook.setValidWhile(toKeep));
hooksLock.unlock();
this.mountHook(hook.setValidWhile(toKeep));
}
public <D extends MiraiEvent> void hookOnce(MiraiEventHook<D> hook){
@ -44,15 +38,25 @@ public class MiraiEventManager {
}
public <D extends MiraiEvent> void registerHook(MiraiEventHook<D> hook){
this.hookUntil(hook,(a) -> false);
this.mountHook(hook);
}
private <D extends MiraiEvent> void mountHook(MiraiEventHook<D> hook){
if(!hook.isMount()) {
hook.setMount(true);
hooksLock.lock();
hooks.putIfAbsent(hook.getEventClass(), new ArrayList<>());
hooks.get(hook.getEventClass()).add(hook);
hooksLock.unlock();
}
}
/**
* 不推荐onEvent
* 非线程安全
* 由于不能保证Hook的原子性 非线程安全
* 不能保证下一个 D event发生时handler就位
* @author NaturalHG Aug27
* use {@link MiraiEventHook::onEvent()} to replace
*/
@Deprecated
@ -85,6 +89,7 @@ public class MiraiEventManager {
public void boardcastEvent(MiraiEvent event){
hooksLock.lock();
if(hooks.containsKey(event.getClass())){

View File

@ -2,6 +2,7 @@ package net.mamoe.mirai.task;
import net.mamoe.mirai.MiraiServer;
import net.mamoe.mirai.event.MiraiEventHook;
import net.mamoe.mirai.event.events.server.ServerDisableEvent;
import java.util.concurrent.Callable;
@ -26,9 +27,10 @@ public class MiraiTaskManager {
private MiraiTaskManager() {
this.pool = new MiraiThreadPool();
MiraiServer.getInstance().getEventManager()
MiraiEventHook
.onEvent(ServerDisableEvent.class)
.setHandler(a -> this.pool.close());
.setHandler(a -> this.pool.close())
.mount();
}