Skip to content

Commit 428bec3

Browse files
clalancetteivanpauno
authored andcommitted
Add an implementation of the spinAll method to the executors (#118)
* Add an implementation of spinAll(). This differs from spinSome() in how it looks at ready executables. spinSome() collects a list of ready executables once at the beginning of execution, and then executes each one until a timeout. spinAll() will continually collect new executables, executing each one until a timeout. This is basically a direct port from the rclcpp version of the method with the same name. Signed-off-by: Chris Lalancette <[email protected]> * Add in SpinTest.java. Signed-off-by: Chris Lalancette <[email protected]> * Remove rogue comment. Signed-off-by: Chris Lalancette <[email protected]>
1 parent e291adc commit 428bec3

File tree

6 files changed

+395
-13
lines changed

6 files changed

+395
-13
lines changed

rcljava/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ if(BUILD_TESTING)
221221

222222
set(${PROJECT_NAME}_test_sources
223223
"src/test/java/org/ros2/rcljava/RCLJavaTest.java"
224+
"src/test/java/org/ros2/rcljava/SpinTest.java"
224225
"src/test/java/org/ros2/rcljava/TimeTest.java"
225226
"src/test/java/org/ros2/rcljava/client/ClientTest.java"
226227
"src/test/java/org/ros2/rcljava/node/NodeTest.java"
@@ -233,6 +234,7 @@ if(BUILD_TESTING)
233234

234235
set(${PROJECT_NAME}_testsuites
235236
"org.ros2.rcljava.RCLJavaTest"
237+
"org.ros2.rcljava.SpinTest"
236238
"org.ros2.rcljava.TimeTest"
237239
"org.ros2.rcljava.client.ClientTest"
238240
"org.ros2.rcljava.node.NodeTest"

rcljava/src/main/java/org/ros2/rcljava/executors/BaseExecutor.java

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -328,19 +328,46 @@ protected AnyExecutable getNextExecutable() {
328328
return null;
329329
}
330330

331-
protected void spinSome() {
332-
AnyExecutable anyExecutable = getNextExecutable();
333-
if (anyExecutable == null) {
334-
waitForWork(0);
335-
anyExecutable = getNextExecutable();
331+
private boolean maxDurationNotElapsed(long maxDurationNs, long startNs) {
332+
long nowNs = System.nanoTime();
333+
if (maxDurationNs == 0) {
334+
// told to spin forever if need be
335+
return true;
336+
} else if (nowNs - startNs < maxDurationNs) {
337+
// told to spin only for some maximum amount of time
338+
return true;
336339
}
340+
// spun too long
341+
return false;
342+
}
337343

338-
while (anyExecutable != null) {
339-
executeAnyExecutable(anyExecutable);
340-
anyExecutable = getNextExecutable();
344+
private void spinSomeImpl(long maxDurationNs, boolean exhaustive) {
345+
long startNs = System.nanoTime();
346+
boolean workAvailable = false;
347+
while (RCLJava.ok() && maxDurationNotElapsed(maxDurationNs, startNs)) {
348+
if (!workAvailable) {
349+
waitForWork(0);
350+
}
351+
AnyExecutable anyExecutable = getNextExecutable();
352+
if (anyExecutable != null) {
353+
executeAnyExecutable(anyExecutable);
354+
} else {
355+
if (!workAvailable || !exhaustive) {
356+
break;
357+
}
358+
workAvailable = false;
359+
}
341360
}
342361
}
343362

363+
protected void spinSome(long maxDurationNs) {
364+
spinSomeImpl(maxDurationNs, false);
365+
}
366+
367+
protected void spinAll(long maxDurationNs) {
368+
spinSomeImpl(maxDurationNs, true);
369+
}
370+
344371
protected void spinOnce(long timeout) {
345372
AnyExecutable anyExecutable = getNextExecutable();
346373
if (anyExecutable == null) {

rcljava/src/main/java/org/ros2/rcljava/executors/Executor.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,19 @@
1818
import org.ros2.rcljava.node.ComposableNode;
1919

2020
public interface Executor {
21-
public void spin();
21+
public void addNode(ComposableNode node);
22+
23+
public void removeNode(ComposableNode node);
2224

2325
public void spinOnce();
2426

2527
public void spinOnce(long timeout);
2628

2729
public void spinSome();
2830

29-
public void addNode(ComposableNode node);
31+
public void spinSome(long maxDurationNs);
3032

31-
public void removeNode(ComposableNode node);
33+
public void spinAll(long maxDurationNs);
34+
35+
public void spin();
3236
}

rcljava/src/main/java/org/ros2/rcljava/executors/MultiThreadedExecutor.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,15 @@ public void spinOnce(long timeout) {
5656
}
5757

5858
public void spinSome() {
59-
this.baseExecutor.spinSome();
59+
this.spinSome(0);
60+
}
61+
62+
public void spinSome(long maxDurationNs) {
63+
this.baseExecutor.spinSome(maxDurationNs);
64+
}
65+
66+
public void spinAll(long maxDurationNs) {
67+
this.baseExecutor.spinAll(maxDurationNs);
6068
}
6169

6270
public void spin() {

rcljava/src/main/java/org/ros2/rcljava/executors/SingleThreadedExecutor.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,15 @@ public void spinOnce(long timeout) {
3939
}
4040

4141
public void spinSome() {
42-
this.baseExecutor.spinSome();
42+
this.spinSome(0);
43+
}
44+
45+
public void spinSome(long maxDurationNs) {
46+
this.baseExecutor.spinSome(maxDurationNs);
47+
}
48+
49+
public void spinAll(long maxDurationNs) {
50+
this.baseExecutor.spinAll(maxDurationNs);
4351
}
4452

4553
public void spin() {

0 commit comments

Comments
 (0)