From 864f371034fd49653c45a0ed72d1dcf5f2441c59 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Wed, 9 Dec 2020 21:04:20 +0100 Subject: [PATCH] Cut out a couple of middlemen --- .../java/org/syndicate_lang/actors/Actor.java | 54 +++++++++---------- .../org/syndicate_lang/actors/Remote.java | 17 ++++-- 2 files changed, 39 insertions(+), 32 deletions(-) diff --git a/src/main/java/org/syndicate_lang/actors/Actor.java b/src/main/java/org/syndicate_lang/actors/Actor.java index c0c47a2..c34e8a7 100644 --- a/src/main/java/org/syndicate_lang/actors/Actor.java +++ b/src/main/java/org/syndicate_lang/actors/Actor.java @@ -201,7 +201,32 @@ public class Actor implements Executor { } public void execute(Runnable work, Runnable ifNotAlive) { - this.later(0, work, ifNotAlive); + { + WorkItem i = new WorkItem(work, ifNotAlive); + tail.getAndSet(i).set(i); + } + if (workItemCount.getAndIncrement() == 0) { + _executor.execute(() -> { + synchronized (this) { + _currentActor.set(this); + try { + long batch = workItemCount.get(); + while (batch > 0) { + for (int count = 0; count < batch; count++) { + WorkItem i = null; + while (i == null) i = head.get(); + head = i; + this._perform(i.work, i.ifNotAlive); + i.clear(); + } + batch = workItemCount.addAndGet(-batch); + } + } finally { + _currentActor.set(null); + } + } + }); + } } public void later(long delayMilliseconds, Runnable work) { @@ -210,32 +235,7 @@ public class Actor implements Executor { public void later(long delayMilliseconds, Runnable work, Runnable ifNotAlive) { if (delayMilliseconds == 0) { - { - WorkItem i = new WorkItem(work, ifNotAlive); - tail.getAndSet(i).set(i); - } - if (workItemCount.getAndIncrement() == 0) { - _executor.execute(() -> { - synchronized (this) { - _currentActor.set(this); - try { - long batch = workItemCount.get(); - while (batch > 0) { - for (int count = 0; count < batch; count++) { - WorkItem i = null; - while (i == null) i = head.get(); - head = i; - this._perform(i.work, i.ifNotAlive); - i.clear(); - } - batch = workItemCount.addAndGet(-batch); - } - } finally { - _currentActor.set(null); - } - } - }); - } + this.execute(work, ifNotAlive); } else { _scheduledExecutor.schedule(() -> this._performSync(work, ifNotAlive), delayMilliseconds, TimeUnit.MILLISECONDS); } diff --git a/src/main/java/org/syndicate_lang/actors/Remote.java b/src/main/java/org/syndicate_lang/actors/Remote.java index 7da734a..bbf4ac0 100644 --- a/src/main/java/org/syndicate_lang/actors/Remote.java +++ b/src/main/java/org/syndicate_lang/actors/Remote.java @@ -18,18 +18,21 @@ public class Remote { } public void async(Consumer f) { - this.async(0, f); + this._actor.execute(() -> f.accept(this._target)); } public void async(long delayMilliseconds, Consumer f) { this._actor.later(delayMilliseconds, () -> f.accept(this._target)); } - public Promise syncVoid(Consumer f) { - return this.syncVoid(0, f); + public Promise syncVoid(Consumer f) { + return this.sync((t) -> { + f.accept(t); + return null; + }); } - public Promise syncVoid(long delayMilliseconds, Consumer f) { + public Promise syncVoid(long delayMilliseconds, Consumer f) { return this.sync(delayMilliseconds, (t) -> { f.accept(t); return null; @@ -37,7 +40,11 @@ public class Remote { } public Promise sync(Function f) { - return this.sync(0, f); + Promise p = new Promise<>(); + this._actor.execute( + () -> p.resolveWith(f.apply(this._target)), + () -> p.rejectWith(this._actor.getExitReason())); + return p; } public Promise sync(long delayMilliseconds, Function f) {