-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
74 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
.../us/ihmc/concurrent/runtime/barrierScheduler/implicitContext/SingleThreadedScheduler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |