]> git.basschouten.com Git - openhab-addons.git/blob
a73096d1c784c6c1cfe1ad22a36a71c67584f011
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.automation.jsscripting.internal.threading;
14
15 import java.time.Duration;
16 import java.time.Instant;
17 import java.time.ZonedDateTime;
18 import java.time.temporal.Temporal;
19 import java.util.Map;
20 import java.util.concurrent.ConcurrentHashMap;
21 import java.util.concurrent.atomic.AtomicLong;
22 import java.util.concurrent.locks.Lock;
23
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;
30
31 /**
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.
34  *
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}
37  */
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";
46
47     public ThreadsafeTimers(Lock lock, ScriptExecution scriptExecution, Scheduler scheduler) {
48         this.lock = lock;
49         this.scheduler = scheduler;
50         this.scriptExecution = scriptExecution;
51     }
52
53     /**
54      * Set the identifier base string used for naming scheduled jobs.
55      *
56      * @param identifier identifier to use
57      */
58     public void setIdentifier(String identifier) {
59         this.identifier = identifier;
60     }
61
62     /**
63      * Schedules a block of code for later execution.
64      *
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
68      */
69     public Timer createTimer(ZonedDateTime instant, Runnable closure) {
70         return createTimer(identifier, instant, closure);
71     }
72
73     /**
74      * Schedules a block of code for later execution.
75      *
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
80      */
81     public Timer createTimer(@Nullable String identifier, ZonedDateTime instant, Runnable closure) {
82         return scriptExecution.createTimer(identifier, instant, () -> {
83             lock.lock();
84             try {
85                 closure.run();
86             } finally { // Make sure that Lock is unlocked regardless of an exception is thrown or not to avoid
87                         // deadlocks
88                 lock.unlock();
89             }
90         });
91     }
92
93     /**
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.
96      *
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.
101      */
102     public long setTimeout(Runnable callback, Long delay) {
103         long id = lastId.incrementAndGet();
104         ScheduledCompletableFuture<Object> future = scheduler.schedule(() -> {
105             lock.lock();
106             try {
107                 callback.run();
108                 idSchedulerMapping.remove(id);
109             } finally { // Make sure that Lock is unlocked regardless of an exception is thrown or not to avoid
110                         // deadlocks
111                 lock.unlock();
112             }
113         }, identifier + ".timeout." + id, Instant.now().plusMillis(delay));
114         idSchedulerMapping.put(id, future);
115         return id;
116     }
117
118     /**
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>.
121      *
122      * @param timeoutId The identifier of the timeout you want to cancel. This ID was returned by the corresponding call
123      *            to setTimeout().
124      */
125     public void clearTimeout(long timeoutId) {
126         ScheduledCompletableFuture<Object> scheduled = idSchedulerMapping.remove(timeoutId);
127         if (scheduled != null) {
128             scheduled.cancel(true);
129         }
130     }
131
132     /**
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.
135      *
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.
140      */
141     public long setInterval(Runnable callback, Long delay) {
142         long id = lastId.incrementAndGet();
143         ScheduledCompletableFuture<Object> future = scheduler.schedule(() -> {
144             lock.lock();
145             try {
146                 callback.run();
147             } finally { // Make sure that Lock is unlocked regardless of an exception is thrown or not to avoid
148                         // deadlocks
149                 lock.unlock();
150             }
151         }, identifier + ".interval." + id, new LoopingAdjuster(Duration.ofMillis(delay)));
152         idSchedulerMapping.put(id, future);
153         return id;
154     }
155
156     /**
157      * <a href="https://developer.mozilla.org/en-US/docs/Web/API/clearInterval"><code>clearInterval()</code></a>
158      * polyfill.
159      * Cancels a timed, repeating action which was previously established by a call to <code>setInterval()</code>.
160      *
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>.
163      */
164     public void clearInterval(long intervalID) {
165         clearTimeout(intervalID);
166     }
167
168     /**
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
172      * running endless.
173      */
174     public void clearAll() {
175         idSchedulerMapping.forEach((id, future) -> future.cancel(true));
176         idSchedulerMapping.clear();
177     }
178
179     /**
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.
182      *
183      * @author Florian Hotze - Initial contribution
184      */
185     private static class LoopingAdjuster implements SchedulerTemporalAdjuster {
186
187         private Duration delay;
188         private @Nullable Temporal timeDone;
189
190         LoopingAdjuster(Duration delay) {
191             this.delay = delay;
192         }
193
194         @Override
195         public boolean isDone(Temporal temporal) {
196             // Always return false so that a new job will be scheduled
197             return false;
198         }
199
200         @Override
201         public Temporal adjustInto(Temporal temporal) {
202             Temporal localTimeDone = timeDone;
203             Temporal nextTime;
204             if (localTimeDone != null) {
205                 nextTime = localTimeDone.plus(delay);
206             } else {
207                 nextTime = temporal.plus(delay);
208             }
209             timeDone = nextTime;
210             return nextTime;
211         }
212     }
213 }