Skip to content

Commit

Permalink
[core] Ensure that all functions in Scheules BIC use the Time BIC.
Browse files Browse the repository at this point in the history
Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Oct 6, 2018
1 parent efba2bd commit f665af1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
14 changes: 13 additions & 1 deletion main/apiplugins/io.sarl.core/src/io/sarl/core/bic.sarl
Expand Up @@ -509,6 +509,10 @@ capacity Schedules {
*
* <p>The given procedure takes one parameter: the agent associated to the task. It is name <code>it</code> by default.
*
* <p>The given delay is expressed in milliseconds according to the time scale of the SRE.
* It means that the given number of milliseconds may be not real milliseconds, depending
* on the definition of the builtin capacity {@link Time}.
*
* <p>If this function is invoked from a {@code Behavior} and there is no provided task,
* the created task will be associated to the behavior instance. It means that the task will
* be automatically canceled when the behavior instance is unregistered from the the owning agent.
Expand Down Expand Up @@ -612,6 +616,10 @@ capacity Schedules {
*
* <p>The given procedure takes one parameter: the agent associated to the task. It is name <code>it</code> by default.
*
* <p>The given period is expressed in milliseconds according to the time scale of the SRE.
* It means that the given number of milliseconds may be not real milliseconds, depending
* on the definition of the builtin capacity {@link Time}.
*
* <p>If this function is invoked from a {@code Behavior} and there is no provided task,
* the created task will be associated to the behavior instance. It means that the task will
* be automatically canceled when the behavior instance is unregistered from the the owning agent.
Expand Down Expand Up @@ -652,7 +660,11 @@ capacity Schedules {
*
* <p>The given procedure takes one parameter: the agent associated to the task. It is name <code>it</code> by default.
*
* <p>It is recommended that the SRAL run-time environment, which is providing the implementation
* <p>The given delay is expressed in milliseconds according to the time scale of the SRE.
* It means that the given number of milliseconds may be not real milliseconds, depending
* on the definition of the builtin capacity {@link Time}.
*
* <p>It is recommended that the SARL run-time environment, which is providing the implementation
* of this function to provide an efficient implementation in the case the argument
* {@code delay} is equal to zero. Indeed, if the {@code delay} is equal to zero, the task
* should be run in an infinite loop until it is canceled, or the owning agent is killed.
Expand Down
Expand Up @@ -236,10 +236,11 @@ public AgentTask in(AgentTask task, long delay, Procedure1<? super Agent> proced
synchronized (getTaskListMutex()) {
pair = preRunTask(task, procedure);
}
final long osDelay = Math.round(getTimeSkill().getOSTimeFactor() * delay);
final AgentTask runnableTask = pair != null ? pair.getTask() : task;
final ScheduledFuture<?> sf = this.executorService.schedule(
new AgentTaskRunner(runnableTask, false),
delay, TimeUnit.MILLISECONDS);
osDelay, TimeUnit.MILLISECONDS);
synchronized (getTaskListMutex()) {
pair = postRunTask(pair, task, sf);
}
Expand Down Expand Up @@ -413,10 +414,11 @@ public AgentTask every(AgentTask task, long period, Procedure1<? super Agent> pr
synchronized (getTaskListMutex()) {
description = preRunTask(task, procedure);
}
final long osPeriod = Math.round(getTimeSkill().getOSTimeFactor() * period);
final AgentTask runnableTask = description != null ? description.getTask() : task;
final ScheduledFuture<?> sf = this.executorService.scheduleAtFixedRate(
new AgentTaskRunner(runnableTask, true),
0, period, TimeUnit.MILLISECONDS);
0, osPeriod, TimeUnit.MILLISECONDS);
synchronized (getTaskListMutex()) {
description = postRunTask(description, task, sf);
}
Expand All @@ -436,12 +438,13 @@ public AgentTask atFixedDelay(AgentTask task, long delay, Procedure1<? super Age
}
final AgentTask runnableTask = description != null ? description.getTask() : task;
final Future<?> future;
if (delay <= 0) {
final long osDelay = Math.round(getTimeSkill().getOSTimeFactor() * delay);
if (osDelay <= 0) {
future = this.executorService.submit(new AgentInfiniteLoopTask(runnableTask));
} else {
future = this.executorService.scheduleWithFixedDelay(
new AgentTaskRunner(runnableTask, true),
0, delay, TimeUnit.MILLISECONDS);
0, osDelay, TimeUnit.MILLISECONDS);
}
synchronized (getTaskListMutex()) {
description = postRunTask(description, task, future);
Expand Down

0 comments on commit f665af1

Please sign in to comment.