Browse Source

Cut out a couple of middlemen

main
Tony Garnock-Jones 11 months ago
parent
commit
864f371034
  1. 54
      src/main/java/org/syndicate_lang/actors/Actor.java
  2. 17
      src/main/java/org/syndicate_lang/actors/Remote.java

54
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);
}

17
src/main/java/org/syndicate_lang/actors/Remote.java

@ -18,18 +18,21 @@ public class Remote<T> {
}
public void async(Consumer<T> f) {
this.async(0, f);
this._actor.execute(() -> f.accept(this._target));
}
public void async(long delayMilliseconds, Consumer<T> f) {
this._actor.later(delayMilliseconds, () -> f.accept(this._target));
}
public Promise<Object> syncVoid(Consumer<T> f) {
return this.syncVoid(0, f);
public Promise<?> syncVoid(Consumer<T> f) {
return this.sync((t) -> {
f.accept(t);
return null;
});
}
public Promise<Object> syncVoid(long delayMilliseconds, Consumer<T> f) {
public Promise<?> syncVoid(long delayMilliseconds, Consumer<T> f) {
return this.sync(delayMilliseconds, (t) -> {
f.accept(t);
return null;
@ -37,7 +40,11 @@ public class Remote<T> {
}
public<R> Promise<R> sync(Function<T, R> f) {
return this.sync(0, f);
Promise<R> p = new Promise<>();
this._actor.execute(
() -> p.resolveWith(f.apply(this._target)),
() -> p.rejectWith(this._actor.getExitReason()));
return p;
}
public<R> Promise<R> sync(long delayMilliseconds, Function<T, R> f) {

Loading…
Cancel
Save