From e97df5a48881482598b9eb61ccd74980d15570a3 Mon Sep 17 00:00:00 2001 From: huangli Date: Wed, 25 Dec 2024 22:00:51 +0800 Subject: [PATCH] chore refactor: use prepare to clean Fiber.source, move signal0(Fiber) to FiberCondition --- .../github/dtprj/dongting/fiber/Dispatcher.java | 1 - .../com/github/dtprj/dongting/fiber/Fiber.java | 1 + .../dtprj/dongting/fiber/FiberCondition.java | 14 +++++++++++++- .../dtprj/dongting/fiber/FiberFuture.java | 1 + .../github/dtprj/dongting/fiber/FiberLock.java | 1 + .../dtprj/dongting/fiber/FiberReadLock.java | 1 + .../github/dtprj/dongting/fiber/WaitSource.java | 17 ++--------------- 7 files changed, 19 insertions(+), 17 deletions(-) diff --git a/server/src/main/java/com/github/dtprj/dongting/fiber/Dispatcher.java b/server/src/main/java/com/github/dtprj/dongting/fiber/Dispatcher.java index e3bb0b161..9ddd99743 100644 --- a/server/src/main/java/com/github/dtprj/dongting/fiber/Dispatcher.java +++ b/server/src/main/java/com/github/dtprj/dongting/fiber/Dispatcher.java @@ -226,7 +226,6 @@ private void processScheduleFibers() { if (f.source != null) { f.source.removeWaiter(f); f.source.prepare(f, true); - f.source = null; } f.cleanSchedule(); f.fiberGroup.tryMakeFiberReady(f, false); diff --git a/server/src/main/java/com/github/dtprj/dongting/fiber/Fiber.java b/server/src/main/java/com/github/dtprj/dongting/fiber/Fiber.java index 0b509dd39..7e14db652 100644 --- a/server/src/main/java/com/github/dtprj/dongting/fiber/Fiber.java +++ b/server/src/main/java/com/github/dtprj/dongting/fiber/Fiber.java @@ -145,6 +145,7 @@ protected void prepare(Fiber waitFiber, boolean timeout) { } else { waitFiber.inputObj = null; } + waitFiber.source = null; } public FrameCallResult join(FrameCall resumePoint) { diff --git a/server/src/main/java/com/github/dtprj/dongting/fiber/FiberCondition.java b/server/src/main/java/com/github/dtprj/dongting/fiber/FiberCondition.java index 32f62a81f..7fd04e010 100644 --- a/server/src/main/java/com/github/dtprj/dongting/fiber/FiberCondition.java +++ b/server/src/main/java/com/github/dtprj/dongting/fiber/FiberCondition.java @@ -31,6 +31,11 @@ public String toString() { return "Condition:" + name + "@" + Integer.toHexString(hashCode()); } + @Override + protected void prepare(Fiber waitFiber, boolean timeout) { + waitFiber.source = null; + } + public void signal() { Dispatcher.getCurrentFiberAndCheck(fiberGroup); signal0(true); @@ -43,7 +48,14 @@ public void signalLater() { public void signal(Fiber targetFiber) { Dispatcher.getCurrentFiberAndCheck(fiberGroup); - signal0(targetFiber); + if (fiberGroup.finished) { + return; + } + if (targetFiber.source != this) { + return; + } + removeWaiter(targetFiber); + signalFiber(targetFiber, true); } public void signalAll() { diff --git a/server/src/main/java/com/github/dtprj/dongting/fiber/FiberFuture.java b/server/src/main/java/com/github/dtprj/dongting/fiber/FiberFuture.java index cec83f870..97a551265 100644 --- a/server/src/main/java/com/github/dtprj/dongting/fiber/FiberFuture.java +++ b/server/src/main/java/com/github/dtprj/dongting/fiber/FiberFuture.java @@ -60,6 +60,7 @@ protected void prepare(Fiber currentFiber, boolean timeout) { currentFiber.inputObj = execResult; } } + currentFiber.source = null; } public T getResult() { diff --git a/server/src/main/java/com/github/dtprj/dongting/fiber/FiberLock.java b/server/src/main/java/com/github/dtprj/dongting/fiber/FiberLock.java index 55bc4c7df..018dfa177 100644 --- a/server/src/main/java/com/github/dtprj/dongting/fiber/FiberLock.java +++ b/server/src/main/java/com/github/dtprj/dongting/fiber/FiberLock.java @@ -42,6 +42,7 @@ protected void prepare(Fiber fiber, boolean timeout) { fiber.inputObj = null; } updateOwnerAndHeldCount(fiber); + fiber.source = null; } private void updateOwnerAndHeldCount(Fiber fiber) { diff --git a/server/src/main/java/com/github/dtprj/dongting/fiber/FiberReadLock.java b/server/src/main/java/com/github/dtprj/dongting/fiber/FiberReadLock.java index 553582f12..8120195ea 100644 --- a/server/src/main/java/com/github/dtprj/dongting/fiber/FiberReadLock.java +++ b/server/src/main/java/com/github/dtprj/dongting/fiber/FiberReadLock.java @@ -42,6 +42,7 @@ protected void prepare(Fiber fiber, boolean timeout) { fiber.inputObj = null; } heldCount++; + fiber.source = null; } @Override diff --git a/server/src/main/java/com/github/dtprj/dongting/fiber/WaitSource.java b/server/src/main/java/com/github/dtprj/dongting/fiber/WaitSource.java index ddb7f76f4..f9bacacb2 100644 --- a/server/src/main/java/com/github/dtprj/dongting/fiber/WaitSource.java +++ b/server/src/main/java/com/github/dtprj/dongting/fiber/WaitSource.java @@ -29,8 +29,7 @@ public WaitSource(String name, FiberGroup group) { this.name = name; } - protected void prepare(Fiber waitFiber, boolean timeout){ - } + protected abstract void prepare(Fiber waitFiber, boolean timeout); void addWaiter(Fiber f) { @@ -93,17 +92,6 @@ Fiber popHeadWaiter() { return result; } - void signal0(Fiber f) { - if (fiberGroup.finished) { - return; - } - if (f.source != this) { - return; - } - removeWaiter(f); - signalFiber(f, true); - } - void signal0(boolean addFirst) { if (fiberGroup.finished) { return; @@ -114,12 +102,11 @@ void signal0(boolean addFirst) { } } - private void signalFiber(Fiber f, boolean addFirst) { + void signalFiber(Fiber f, boolean addFirst) { if (f.scheduleTimeoutMillis > 0) { fiberGroup.dispatcher.removeFromScheduleQueue(f); } prepare(f, false); - f.source = null; f.cleanSchedule(); fiberGroup.tryMakeFiberReady(f, addFirst); }