-
Notifications
You must be signed in to change notification settings - Fork 3.6k
=act Extract AtomicCancellable in Scheduler. #31915
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ import scala.concurrent.ExecutionContext | |
| import scala.concurrent.duration._ | ||
| import scala.util.control.NoStackTrace | ||
|
|
||
| import akka.actor.Scheduler.AtomicCancellable | ||
| import akka.annotation.InternalApi | ||
| import akka.util.JavaDurationConverters | ||
|
|
||
|
|
@@ -70,51 +71,25 @@ trait Scheduler { | |
| * Note: For scheduling within actors `with Timers` should be preferred. | ||
| */ | ||
| def scheduleWithFixedDelay(initialDelay: FiniteDuration, delay: FiniteDuration)(runnable: Runnable)( | ||
| implicit executor: ExecutionContext): Cancellable = { | ||
| try new AtomicReference[Cancellable](Cancellable.initialNotCancelled) with Cancellable { self => | ||
| compareAndSet( | ||
| Cancellable.initialNotCancelled, | ||
| implicit executor: ExecutionContext): Cancellable = | ||
| new AtomicCancellable(Cancellable.initialNotCancelled) { | ||
| final override protected def scheduleFirst(): Cancellable = | ||
| scheduleOnce( | ||
| initialDelay, | ||
| new Runnable { | ||
| override def run(): Unit = { | ||
| try { | ||
| runnable.run() | ||
| if (self.get != null) | ||
| if (get != null) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in the other place it is
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because I need to access the |
||
| swap(scheduleOnce(delay, this)) | ||
| } catch { | ||
| // ignore failure to enqueue or terminated target actor | ||
| case _: SchedulerException => | ||
| case e: IllegalStateException if e.getCause != null && e.getCause.isInstanceOf[SchedulerException] => | ||
| } | ||
| } | ||
| })) | ||
|
|
||
| @tailrec private def swap(c: Cancellable): Unit = { | ||
| get match { | ||
| case null => if (c != null) c.cancel() | ||
| case old => if (!compareAndSet(old, c)) swap(c) | ||
| } | ||
| } | ||
|
|
||
| final def cancel(): Boolean = { | ||
| @tailrec def tailrecCancel(): Boolean = { | ||
| get match { | ||
| case null => false | ||
| case c => | ||
| if (c.cancel()) compareAndSet(c, null) | ||
| else compareAndSet(c, null) || tailrecCancel() | ||
| } | ||
| } | ||
|
|
||
| tailrecCancel() | ||
| } | ||
|
|
||
| override def isCancelled: Boolean = get == null | ||
| } catch { | ||
| case SchedulerException(msg) => throw new IllegalStateException(msg) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Java API: Schedules a `Runnable` to be run repeatedly with an initial delay and | ||
|
|
@@ -561,4 +536,45 @@ object Scheduler { | |
| * a custom implementation of `Scheduler` must also implement this. | ||
| */ | ||
| trait TaskRunOnClose extends Runnable | ||
|
|
||
| /** | ||
| * INTERNAL API | ||
| */ | ||
| @InternalApi | ||
| private[akka] abstract class AtomicCancellable(initialValue: Cancellable) | ||
He-Pin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| extends AtomicReference[Cancellable](initialValue) | ||
| with Cancellable { | ||
| try { | ||
| compareAndSet(initialValue, scheduleFirst()) | ||
| } catch { | ||
| case cause @ SchedulerException(msg) => throw new IllegalStateException(msg, cause) | ||
| } | ||
|
|
||
| protected def scheduleFirst(): Cancellable | ||
|
|
||
| @tailrec final protected def swap(c: Cancellable): Unit = { | ||
| get match { | ||
| case null => if (c != null) c.cancel() | ||
| case old => | ||
| if (!compareAndSet(old, c)) | ||
| swap(c) | ||
| } | ||
| } | ||
|
|
||
| final def cancel(): Boolean = { | ||
| @tailrec def tailrecCancel(): Boolean = { | ||
| get match { | ||
| case null => false | ||
| case c => | ||
| if (c.cancel()) compareAndSet(c, null) | ||
| else compareAndSet(c, null) || tailrecCancel() | ||
| } | ||
| } | ||
|
|
||
| tailrecCancel() | ||
| } | ||
|
|
||
| final override def isCancelled: Boolean = get == null | ||
|
|
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self refs to the AtomicReference.