Skip to content

Commit

Permalink
Finish 1.2.5
Browse files Browse the repository at this point in the history
  • Loading branch information
Georg Wiedebach committed May 8, 2019
2 parents 1c9921f + a79d815 commit d32e298
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 3 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {

ihmc {
group = "us.ihmc"
version = "1.2.4"
version = "1.2.5"
vcsUrl = "https://github.com/ihmcrobotics/ihmc-realtime"
openSource = true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ private boolean allTasksOnSchedule()
}

/**
* Requests shutdown on all tasks.
* This is a blocking call.
* Requests shutdown on all tasks. This is a blocking call. After calling this do not call the
* {@link #run()} method anymore.
*/
public void shutdown()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package us.ihmc.concurrent.runtime.barrierScheduler.implicitContext;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
* A single threaded version of the {@link BarrierScheduler}. This allows running a set of tasks in
* a single thread sequentially using the same {@link Task} API as the multi-threaded version. The
* {@link #run()} method needs to be invoked every scheduler tick but no task threads need to be
* started. When a task is executed the {@link #run()} method blocks until completion of the task.
*
* @param <C> the context type
*/
public class SingleThreadedScheduler<C> implements Runnable
{
private final List<? extends Task<C>> tasks;

private final C masterContext;

private final boolean[] tasksInitialized;

private long tick;

public SingleThreadedScheduler(Collection<? extends Task<C>> tasks, C masterContext)
{
this.tasks = new ArrayList<>(tasks);
this.masterContext = masterContext;
this.tasksInitialized = new boolean[tasks.size()];
}

@Override
public void run()
{
for (int i = 0; i < tasks.size(); i++)
{
Task<C> task = tasks.get(i);
if (task.isPending(tick))
task.updateMasterContext(masterContext);
}

for (int i = 0; i < tasks.size(); i++)
{
Task<C> task = tasks.get(i);
if (task.isPending(tick))
task.updateLocalContext(masterContext);
}

for (int i = 0; i < tasks.size(); i++)
{
Task<C> task = tasks.get(i);
if (task.isPending(tick))
{
if (!tasksInitialized[i])
tasksInitialized[i] = task.initialize();
if (tasksInitialized[i])
task.execute();
}
}

tick++;
}

public void shutdown()
{
for (int i = 0; i < tasks.size(); i++)
{
tasks.get(i).cleanup();
}
}
}

0 comments on commit d32e298

Please sign in to comment.