Skip to content

Commit

Permalink
optimized StepperTaskWorker
Browse files Browse the repository at this point in the history
  • Loading branch information
alex9849 committed Dec 25, 2023
1 parent bf44fa4 commit f75c9f5
Showing 1 changed file with 29 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,18 @@

import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;

public class StepperTaskWorker extends Thread {
private static StepperTaskWorker instance;
private final HashMap<AcceleratingStepper, CompletableFuture<Void>> motorTasks;
private final List<Job> motorTasks;
private final Object nrLockRequestedLock = new Object();
private Integer nrLockRequested = 0;

private StepperTaskWorker() {
this.setPriority(MAX_PRIORITY);
this.setDaemon(true);
motorTasks = new HashMap<>();
motorTasks = new ArrayList<>();
this.start();
}

Expand All @@ -35,7 +33,7 @@ public Future<Void> submitTask(AcceleratingStepper step) {
}
synchronized (motorTasks) {
CompletableFuture<Void> future = new CompletableFuture<>();
motorTasks.put(step, future);
motorTasks.add(new Job(step, future));
synchronized (nrLockRequestedLock) {
nrLockRequested--;
}
Expand All @@ -49,9 +47,13 @@ public void cancelTask(AcceleratingStepper step) {
nrLockRequested++;
}
synchronized (motorTasks) {
CompletableFuture<Void> future = motorTasks.remove(step);
if (future != null) {
future.complete(null);
for(int i = 0; i < motorTasks.size(); i++) {
Job job = motorTasks.get(i);
if(job.step == step) {
job.future.complete(null);
motorTasks.remove(i);
break;
}
}
synchronized (nrLockRequestedLock) {
nrLockRequested--;
Expand All @@ -63,31 +65,37 @@ public void cancelTask(AcceleratingStepper step) {

@Override
public void run() {
Set<AcceleratingStepper> toRemove = new HashSet<>();
try (AffinityLock al = AffinityLock.acquireCore()) {
synchronized (motorTasks) {
while (true) {
while (motorTasks.isEmpty() || nrLockRequested > 0) {
try {
motorTasks.wait();
} catch (InterruptedException ignored) {
}
} catch (InterruptedException ignored) {}
}
for (AcceleratingStepper step : motorTasks.keySet()) {
if (step.distanceToGo() != 0) {
step.run();
for(int i = 0; i < motorTasks.size(); i++) {
Job job = motorTasks.get(i);
if (job.step.distanceToGo() != 0) {
job.step.run();
}
if (step.distanceToGo() == 0) {
toRemove.add(step);
if (job.step.distanceToGo() == 0) {
job.future.complete(null);
motorTasks.remove(i--);
}
}
for (AcceleratingStepper step : toRemove) {
CompletableFuture<Void> future = motorTasks.remove(step);
future.complete(null);
}
toRemove.clear();
}
}
}
}

private static class Job {

public Job(AcceleratingStepper step, CompletableFuture<Void> future) {
this.step = step;
this.future = future;
}

private final AcceleratingStepper step;
private final CompletableFuture<Void> future;
}
}

0 comments on commit f75c9f5

Please sign in to comment.