-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathsynchronize.R
220 lines (191 loc) · 5.99 KB
/
synchronize.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
promise_globals <- new.env(parent = emptyenv())
promise_globals$interrupt_domains <- list()
push_interrupt_domain <- function(domain) {
n_domains <- length(promise_globals$interrupt_domains)
promise_globals$interrupt_domains[[n_domains + 1]] <- domain
}
pop_interrupt_domain <- function() {
n_domains <- length(promise_globals$interrupt_domains)
if (length(n_domains) == 0)
return(NULL)
domain <- promise_globals$interrupt_domains[[n_domains]]
promise_globals$interrupt_domains[[n_domains]] <- NULL
domain
}
current_interrupt_domain <- function() {
promise_globals$interrupt_domains[[length(promise_globals$interrupt_domains)]]
}
create_interrupt_domain <- function() {
domain <- new_promise_domain(
wrapOnFulfilled = function(onFulfilled) {
function(...) {
push_interrupt_domain(domain)
on.exit(pop_interrupt_domain(), add = TRUE)
if (domain$interrupted) {
stop("Operation was interrupted 1")
}
tryCatch(
{
onFulfilled(...)
},
interrupt = function(e) {
# message("wrapOnFulfilled inner caught interrupt")
# Call function here that returns current interrupt
domain$interrupted <- TRUE
stop("Operation was interrupted 2")
}
)
}
},
wrapOnRejected = function(onRejected) {
function(...) {
push_interrupt_domain(domain)
on.exit(pop_interrupt_domain(), add = TRUE)
if (domain$interrupted) {
stop("Operation was interrupted 3")
}
tryCatch(
onRejected(...),
interrupt = function(e) {
domain$interrupted <- TRUE
stop("Operation was interrupted 4")
}
)
}
},
wrapOnFinally = function(onFinally) {
function(...) {
push_interrupt_domain(domain)
on.exit(pop_interrupt_domain(), add = TRUE)
tryCatch(
onFinally(...),
interrupt = function(e) {
domain$interrupted <- TRUE
stop("Operation was interrupted 5")
}
)
}
},
wrapSync = function(expr) {
push_interrupt_domain(domain)
on.exit(pop_interrupt_domain(), add = TRUE)
# Counting is currently not used
if (is.null(promise_globals$synchronized)) {
promise_globals$synchronized <- 0L
}
promise_globals$synchronized <- promise_globals$synchronized + 1L
on.exit(promise_globals$synchronized <- promise_globals$synchronized - 1L)
force(expr)
},
interrupted = FALSE
)
domain
}
# This function takes a promise and blocks until it is resolved. It runs the
# promise's callbacks in the provided event loop. If the promise is
# interrupted then this function tries to catch the interrupt, then runs the
# loop until it's empty; then it throws a new interrupt. If the promise throws
# an error, then also throws an error.
synchronize <- function(expr, loop) {
domain <- create_interrupt_domain()
with_promise_domain(domain, {
tryCatch(
{
result <- force(expr)
if (is.promising(result)) {
value <- NULL
type <- NULL
result$
then(function(val) {
value <<- val
type <<- "success"
})$
catch(function(reason) {
value <<- reason
type <<- "error"
})
while (is.null(type) && !domain$interrupted) {
run_now(loop = loop)
}
if (is.null(type)) {
generateInterrupt()
} else if (type == "success") {
value
} else if (type == "error") {
stop(value)
}
}
},
interrupt = function(e) {
domain$interrupted <<- TRUE
message("Attempting to interrupt gracefully; press Esc/Ctrl+C to force interrupt")
while (!loop_empty(loop = loop)) {
run_now(loop = loop)
}
generateInterrupt()
}
)
})
}
# A wrapper for later() which polls for interrupts. If an interrupt has
# occurred either while running another callback, or when run_now() is
# waiting, the interrupt will be detected and (1) the scheduled `func` will be
# cancelled, and (2) the `on_interrupt` callback will be invoked.
later_with_interrupt <- function(
func,
delay = 0,
loop = current_loop(),
on_interrupt = function() {},
interrupt_domain = current_interrupt_domain(),
poll_interval = 0.1
) {
force(func)
force(loop)
force(interrupt_domain)
force(on_interrupt)
force(poll_interval)
if (is.null(interrupt_domain)) {
return(later(func, delay, loop))
}
end_time <- as.numeric(Sys.time()) + delay
cancel <- NULL
nextTurn <- function(init = FALSE) {
if (isTRUE(interrupt_domain$interrupted)) {
on_interrupt()
return()
}
this_delay <- min(poll_interval, end_time - as.numeric(Sys.time()))
if (this_delay <= 0) {
# Time has expired. If we've never progressed to the next tick (i.e.
# init == TRUE) then don't invoke the callback yet, wait until the next
# tick. Otherwise, do invoke the callback.
if (!init) {
func()
return()
}
this_delay <- 0
}
cancel <<- later::later(nextTurn, this_delay, loop)
}
nextTurn(init = TRUE)
function() {
cancel()
}
}
# TODO
#
# The notion of cancellability should go into later package. Instead of this
# function taking `interrupt_domain`, which is a promise-level object, we could
# do something like the following:
#
# Add on_interrupt option to later(); if FALSE/NULL (the default) then interrupts
# don't affect scheduled callback. If TRUE, then interrupt cancels the later().
# If a function, then interrupt cancels the later() and calls the on_interrupt
# function.
# Add later::interrupt() function, so that later can know that an interrupt
# happened.
# Add option to later() to make the callback uninterruptable.
generateInterrupt <- function() {
tools::pskill(Sys.getpid(), tools::SIGINT)
Sys.sleep(1)
}