]> git.basschouten.com Git - openhab-addons.git/blob
9583ca57c12b01c357bdb607bbe30b9cf2c4b79c
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.ZonedDateTime;
16 import java.util.concurrent.TimeUnit;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.core.model.script.ScriptServiceUtil;
21 import org.openhab.core.model.script.actions.Timer;
22 import org.openhab.core.scheduler.ScheduledCompletableFuture;
23 import org.openhab.core.scheduler.Scheduler;
24 import org.openhab.core.scheduler.SchedulerRunnable;
25
26 /**
27  * A replacement for the timer functionality of {@link org.openhab.core.model.script.actions.ScriptExecution
28  * ScriptExecution} which controls multithreaded execution access to the single-threaded GraalJS contexts.
29  *
30  * @author Florian Hotze - Initial contribution
31  */
32 public class ThreadsafeTimers {
33     private final Object lock;
34
35     public ThreadsafeTimers(Object lock) {
36         this.lock = lock;
37     }
38
39     public Timer createTimer(ZonedDateTime instant, Runnable callable) {
40         return createTimer(null, instant, callable);
41     }
42
43     public Timer createTimer(@Nullable String identifier, ZonedDateTime instant, Runnable callable) {
44         Scheduler scheduler = ScriptServiceUtil.getScheduler();
45
46         return new TimerImpl(scheduler, instant, () -> {
47             synchronized (lock) {
48                 callable.run();
49             }
50
51         }, identifier);
52     }
53
54     public Timer createTimerWithArgument(ZonedDateTime instant, Object arg1, Runnable callable) {
55         return createTimerWithArgument(null, instant, arg1, callable);
56     }
57
58     public Timer createTimerWithArgument(@Nullable String identifier, ZonedDateTime instant, Object arg1,
59             Runnable callable) {
60         Scheduler scheduler = ScriptServiceUtil.getScheduler();
61         return new TimerImpl(scheduler, instant, () -> {
62             synchronized (lock) {
63                 callable.run();
64             }
65
66         }, identifier);
67     }
68
69     /**
70      * This is an implementation of the {@link Timer} interface.
71      * Copy of {@link org.openhab.core.model.script.internal.actions.TimerImpl} as this is not accessible from outside
72      * the
73      * package.
74      *
75      * @author Kai Kreuzer - Initial contribution
76      */
77     @NonNullByDefault
78     public static class TimerImpl implements Timer {
79
80         private final Scheduler scheduler;
81         private final ZonedDateTime startTime;
82         private final SchedulerRunnable runnable;
83         private final @Nullable String identifier;
84         private ScheduledCompletableFuture<?> future;
85
86         public TimerImpl(Scheduler scheduler, ZonedDateTime startTime, SchedulerRunnable runnable) {
87             this(scheduler, startTime, runnable, null);
88         }
89
90         public TimerImpl(Scheduler scheduler, ZonedDateTime startTime, SchedulerRunnable runnable,
91                 @Nullable String identifier) {
92             this.scheduler = scheduler;
93             this.startTime = startTime;
94             this.runnable = runnable;
95             this.identifier = identifier;
96
97             future = scheduler.schedule(runnable, identifier, startTime.toInstant());
98         }
99
100         @Override
101         public boolean cancel() {
102             return future.cancel(true);
103         }
104
105         @Override
106         public synchronized boolean reschedule(ZonedDateTime newTime) {
107             future.cancel(false);
108             future = scheduler.schedule(runnable, identifier, newTime.toInstant());
109             return true;
110         }
111
112         @Override
113         public @Nullable ZonedDateTime getExecutionTime() {
114             return future.isCancelled() ? null : ZonedDateTime.now().plusNanos(future.getDelay(TimeUnit.NANOSECONDS));
115         }
116
117         @Override
118         public boolean isActive() {
119             return !future.isDone();
120         }
121
122         @Override
123         public boolean isCancelled() {
124             return future.isCancelled();
125         }
126
127         @Override
128         public boolean isRunning() {
129             return isActive() && ZonedDateTime.now().isAfter(startTime);
130         }
131
132         @Override
133         public boolean hasTerminated() {
134             return future.isDone();
135         }
136     }
137 }