2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.automation.jsscripting.internal.threading;
15 import java.time.Duration;
16 import java.time.Instant;
17 import java.time.ZonedDateTime;
18 import java.time.temporal.Temporal;
20 import java.util.concurrent.ConcurrentHashMap;
21 import java.util.concurrent.atomic.AtomicLong;
22 import java.util.concurrent.locks.Lock;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.core.automation.module.script.action.ScriptExecution;
26 import org.openhab.core.automation.module.script.action.Timer;
27 import org.openhab.core.scheduler.ScheduledCompletableFuture;
28 import org.openhab.core.scheduler.Scheduler;
29 import org.openhab.core.scheduler.SchedulerTemporalAdjuster;
32 * A polyfill implementation of NodeJS timer functionality (<code>setTimeout()</code>, <code>setInterval()</code> and
33 * the cancel methods) which controls multithreaded execution access to the single-threaded GraalJS contexts.
35 * @author Florian Hotze - Initial contribution; Reimplementation to conform standard JS setTimeout and setInterval;
36 * Threadsafe reimplementation of the timer creation methods of {@link ScriptExecution}
38 public class ThreadsafeTimers {
39 private final Lock lock;
40 private final Scheduler scheduler;
41 private final ScriptExecution scriptExecution;
42 // Mapping of positive, non-zero integer values (used as timeoutID or intervalID) and the Scheduler
43 private final Map<Long, ScheduledCompletableFuture<Object>> idSchedulerMapping = new ConcurrentHashMap<>();
44 private AtomicLong lastId = new AtomicLong();
45 private String identifier = "noIdentifier";
47 public ThreadsafeTimers(Lock lock, ScriptExecution scriptExecution, Scheduler scheduler) {
49 this.scheduler = scheduler;
50 this.scriptExecution = scriptExecution;
54 * Set the identifier base string used for naming scheduled jobs.
56 * @param identifier identifier to use
58 public void setIdentifier(String identifier) {
59 this.identifier = identifier;
63 * Schedules a block of code for later execution.
65 * @param instant the point in time when the code should be executed
66 * @param closure the code block to execute
67 * @return a handle to the created timer, so that it can be canceled or rescheduled
69 public Timer createTimer(ZonedDateTime instant, Runnable closure) {
70 return createTimer(identifier, instant, closure);
74 * Schedules a block of code for later execution.
76 * @param identifier an optional identifier
77 * @param instant the point in time when the code should be executed
78 * @param closure the code block to execute
79 * @return a handle to the created timer, so that it can be canceled or rescheduled
81 public Timer createTimer(@Nullable String identifier, ZonedDateTime instant, Runnable closure) {
82 return scriptExecution.createTimer(identifier, instant, () -> {
86 } finally { // Make sure that Lock is unlocked regardless of an exception is thrown or not to avoid
94 * <a href="https://developer.mozilla.org/en-US/docs/Web/API/setTimeout"><code>setTimeout()</code></a> polyfill.
95 * Sets a timer which executes a given function once the timer expires.
97 * @param callback function to run after the given delay
98 * @param delay time in milliseconds that the timer should wait before the callback is executed
99 * @return Positive integer value which identifies the timer created; this value can be passed to
100 * <code>clearTimeout()</code> to cancel the timeout.
102 public long setTimeout(Runnable callback, Long delay) {
103 long id = lastId.incrementAndGet();
104 ScheduledCompletableFuture<Object> future = scheduler.schedule(() -> {
108 idSchedulerMapping.remove(id);
109 } finally { // Make sure that Lock is unlocked regardless of an exception is thrown or not to avoid
113 }, identifier + ".timeout." + id, Instant.now().plusMillis(delay));
114 idSchedulerMapping.put(id, future);
119 * <a href="https://developer.mozilla.org/en-US/docs/Web/API/clearTimeout"><code>clearTimeout()</code></a> polyfill.
120 * Cancels a timeout previously created by <code>setTimeout()</code>.
122 * @param timeoutId The identifier of the timeout you want to cancel. This ID was returned by the corresponding call
125 public void clearTimeout(long timeoutId) {
126 ScheduledCompletableFuture<Object> scheduled = idSchedulerMapping.remove(timeoutId);
127 if (scheduled != null) {
128 scheduled.cancel(true);
133 * <a href="https://developer.mozilla.org/en-US/docs/Web/API/setInterval"><code>setInterval()</code></a> polyfill.
134 * Repeatedly calls a function with a fixed time delay between each call.
136 * @param callback function to run
137 * @param delay time in milliseconds that the timer should delay in between executions of the callback
138 * @return Numeric, non-zero value which identifies the timer created; this value can be passed to
139 * <code>clearInterval()</code> to cancel the interval.
141 public long setInterval(Runnable callback, Long delay) {
142 long id = lastId.incrementAndGet();
143 ScheduledCompletableFuture<Object> future = scheduler.schedule(() -> {
147 } finally { // Make sure that Lock is unlocked regardless of an exception is thrown or not to avoid
151 }, identifier + ".interval." + id, new LoopingAdjuster(Duration.ofMillis(delay)));
152 idSchedulerMapping.put(id, future);
157 * <a href="https://developer.mozilla.org/en-US/docs/Web/API/clearInterval"><code>clearInterval()</code></a>
159 * Cancels a timed, repeating action which was previously established by a call to <code>setInterval()</code>.
161 * @param intervalID The identifier of the repeated action you want to cancel. This ID was returned by the
162 * corresponding call to <code>setInterval()</code>.
164 public void clearInterval(long intervalID) {
165 clearTimeout(intervalID);
169 * Cancels all timed actions (i.e. timeouts and intervals) that were created with this instance of
170 * {@link ThreadsafeTimers}.
171 * Should be called in a de-initialization/unload hook of the script engine to avoid having scheduled jobs that are
174 public void clearAll() {
175 idSchedulerMapping.forEach((id, future) -> future.cancel(true));
176 idSchedulerMapping.clear();
180 * This is a temporal adjuster that takes a single delay.
181 * This adjuster makes the scheduler run as a fixed rate scheduler from the first time adjustInto was called.
183 * @author Florian Hotze - Initial contribution
185 private static class LoopingAdjuster implements SchedulerTemporalAdjuster {
187 private Duration delay;
188 private @Nullable Temporal timeDone;
190 LoopingAdjuster(Duration delay) {
195 public boolean isDone(Temporal temporal) {
196 // Always return false so that a new job will be scheduled
201 public Temporal adjustInto(Temporal temporal) {
202 Temporal localTimeDone = timeDone;
204 if (localTimeDone != null) {
205 nextTime = localTimeDone.plus(delay);
207 nextTime = temporal.plus(delay);