Debian Codegraph @

R/sync.R

raw
1 # nanonext - Synchronisation Primitives ----------------------------------------
2
3 #' Condition Variables
4 #'
5 #' `cv` creates a new condition variable (protected by a mutex internal to the
6 #' object).
7 #'
8 #' Pass the 'conditionVariable' to the asynchronous receive functions
9 #' [recv_aio()] or [request()]. Alternatively, to be notified of a pipe event,
10 #' pass it to [pipe_notify()].
11 #'
12 #' Completion of the receive or pipe event, which happens asynchronously and
13 #' independently of the main R thread, will signal the condition variable by
14 #' incrementing it by 1.
15 #'
16 #' This will cause the R execution thread waiting on the condition variable
17 #' using `wait()` or `until()` to wake and continue.
18 #'
19 #' For argument `msec`, non-integer values will be coerced to integer.
20 #' Non-numeric input will be ignored and return immediately.
21 #'
22 #' @return For **cv**: a 'conditionVariable' object.
23 #'
24 #' For **wait**: (invisibly) logical TRUE, or else FALSE if a flag has been
25 #' set.
26 #'
27 #' For **until**: (invisibly) logical TRUE if signalled, or else FALSE if the
28 #' timeout was reached.
29 #'
30 #' For **cv_value**: integer value of the condition variable.
31 #'
32 #' For **cv_reset** and **cv_signal**: zero (invisibly).
33 #'
34 #' @section Condition:
35 #'
36 #' The condition internal to this 'conditionVariable' maintains a state (value).
37 #' Each signal increments the value by 1. Each time `wait()` or `until()`
38 #' returns (apart from due to timeout), the value is decremented by 1.
39 #'
40 #' The internal condition may be inspected at any time using `cv_value()` and
41 #' reset using `cv_reset()`. This affords a high degree of flexibility in
42 #' designing complex concurrent applications.
43 #'
44 #' @section Flag:
45 #'
46 #' The condition variable also contains a flag that certain signalling functions
47 #' such as [pipe_notify()] can set. When this flag has been set, all subsequent
48 #' `wait()` calls will return logical FALSE instead of TRUE.
49 #'
50 #' Note that the flag is not automatically reset, but may be reset manually
51 #' using `cv_reset()`.
52 #'
53 #' @examples
54 #' cv <- cv()
55 #'
56 #' @export
57 #'
58 cv <- function() .Call(rnng_cv_alloc)
59
60 #' Condition Variables - Wait
61 #'
62 #' `wait` waits on a condition being signalled by completion of an
63 #' asynchronous receive or pipe event. \cr `wait_` is a variant that allows
64 #' user interrupts, suitable for interactive use.
65 #'
66 #' @param cv a 'conditionVariable' object.
67 #'
68 #' @examples
69 #' \dontrun{
70 #' wait(cv) # would block until the cv is signalled
71 #' wait_(cv) # would block until the cv is signalled or interrupted
72 #' }
73 #'
74 #' @rdname cv
75 #' @export
76 #'
77 wait <- function(cv) invisible(.Call(rnng_cv_wait, cv))
78
79 #' @rdname cv
80 #' @export
81 #'
82 wait_ <- function(cv) invisible(.Call(rnng_cv_wait_safe, cv))
83
84 #' Condition Variables - Until
85 #'
86 #' `until` waits until a future time on a condition being signalled by
87 #' completion of an asynchronous receive or pipe event. \cr `until_` is a
88 #' variant that allows user interrupts, suitable for interactive use.
89 #'
90 #' @param msec maximum time in milliseconds to wait for the condition variable
91 #' to be signalled.
92 #'
93 #' @examples
94 #' until(cv, 10L)
95 #' until_(cv, 10L)
96 #'
97 #' @rdname cv
98 #' @export
99 #'
100 until <- function(cv, msec) invisible(.Call(rnng_cv_until, cv, msec))
101
102 #' @rdname cv
103 #' @export
104 #'
105 until_ <- function(cv, msec) invisible(.Call(rnng_cv_until_safe, cv, msec))
106
107 #' Condition Variables - Value
108 #'
109 #' `cv_value` inspects the internal value of a condition variable.
110 #'
111 #' @examples
112 #' cv_value(cv)
113 #'
114 #' @rdname cv
115 #' @export
116 #'
117 cv_value <- function(cv) .Call(rnng_cv_value, cv)
118
119 #' Condition Variables - Reset
120 #'
121 #' `cv_reset` resets the internal value and flag of a condition variable.
122 #'
123 #' @examples
124 #' cv_reset(cv)
125 #'
126 #' @rdname cv
127 #' @export
128 #'
129 cv_reset <- function(cv) invisible(.Call(rnng_cv_reset, cv))
130
131 #' Condition Variables - Signal
132 #'
133 #' `cv_signal` signals a condition variable.
134 #'
135 #' @examples
136 #' cv_value(cv)
137 #' cv_signal(cv)
138 #' cv_value(cv)
139 #'
140 #' @rdname cv
141 #' @export
142 #'
143 cv_signal <- function(cv) invisible(.Call(rnng_cv_signal, cv))
144
145 #' Pipe Notify
146 #'
147 #' Signals a 'conditionVariable' whenever pipes (individual connections) are
148 #' added or removed at a socket.
149 #'
150 #' For add: this event occurs after the pipe is fully added to the socket. Prior
151 #' to this time, it is not possible to communicate over the pipe with the
152 #' socket.
153 #'
154 #' For remove: this event occurs after the pipe has been removed from the
155 #' socket. The underlying transport may be closed at this point, and it is not
156 #' possible to communicate using this pipe.
157 #'
158 #' @param socket a Socket.
159 #' @param cv a 'conditionVariable' to signal, or NULL to cancel a previously set
160 #' signal.
161 #' @param add \[default FALSE\] logical value whether to signal (or cancel
162 #' signal) when a pipe is added.
163 #' @param remove \[default FALSE\] logical value whether to signal (or cancel
164 #' signal) when a pipe is removed.
165 #' @param flag \[default FALSE\] logical value whether to also set a flag in the
166 #' 'conditionVariable'. This can help distinguish between different types of
167 #' signal, and causes any subsequent [wait()] to return FALSE instead of TRUE.
168 #' If a signal from the \pkg{tools} package, e.g. `tools::SIGINT`, or an
169 #' equivalent integer value is supplied, this sets a flag and additionally
170 #' raises this signal upon the flag being set. For `tools::SIGTERM`, the
171 #' signal is raised with a 200ms grace period to allow a process to exit
172 #' normally.
173 #'
174 #' @return Invisibly, zero on success (will otherwise error).
175 #'
176 #' @examples
177 #' s <- socket(listen = "inproc://nanopipe")
178 #' cv <- cv()
179 #'
180 #' pipe_notify(s, cv, add = TRUE, remove = TRUE, flag = TRUE)
181 #' cv_value(cv)
182 #'
183 #' s1 <- socket(dial = "inproc://nanopipe")
184 #' cv_value(cv)
185 #' reap(s1)
186 #' cv_value(cv)
187 #'
188 #' pipe_notify(s, NULL, add = TRUE, remove = TRUE)
189 #' s1 <- socket(dial = "inproc://nanopipe")
190 #' cv_value(cv)
191 #' reap(s1)
192 #'
193 #' (wait(cv))
194 #'
195 #' close(s)
196 #'
197 #' @export
198 #'
199 pipe_notify <- function(socket, cv, add = FALSE, remove = FALSE, flag = FALSE)
200 invisible(.Call(rnng_pipe_notify, socket, cv, add, remove, flag))
201
202 #' Signal Forwarder
203 #'
204 #' Forwards signals from one 'conditionVariable' to another.
205 #'
206 #' The condition value of `cv` is initially reset to zero when this operator
207 #' returns. Only one forwarder can be active on a `cv` at any given time, and
208 #' assigning a new forwarding target cancels any currently existing forwarding.
209 #'
210 #' Changes in the condition value of `cv` are forwarded to `cv2`, but only on
211 #' each occassion `cv` is signalled. This means that waiting on `cv` will cause
212 #' a temporary divergence between the actual condition value of `cv` and that
213 #' recorded at `cv2`, until the next time `cv` is signalled.
214 #'
215 #' @param cv a 'conditionVariable' object, from which to forward the signal.
216 #' @param cv2 a 'conditionVariable' object, to which the signal is forwarded.
217 #'
218 #' @return Invisibly, `cv2`.
219 #'
220 #' @examples
221 #' cva <- cv(); cvb <- cv(); cv1 <- cv(); cv2 <- cv()
222 #'
223 #' cva %~>% cv1 %~>% cv2
224 #' cvb %~>% cv2
225 #'
226 #' cv_signal(cva)
227 #' cv_signal(cvb)
228 #' cv_value(cv1)
229 #' cv_value(cv2)
230 #'
231 #' @export
232 #'
233 `%~>%` <- function(cv, cv2) invisible(.Call(rnng_signal_thread_create, cv, cv2))