Debian Codegraph @

src/doc/manual/extensions/mp_ref_process.txi

raw
1 @node Processes (native threads)
2 @subsection Processes (native threads)
3
4 Process is a primitive representing native thread.
5
6 @node Processes dictionary
7 @subsection Processes dictionary
8
9 @cppdef mp_all_processes
10 @lspdef mp:all-processes
11
12 @deftypefun cl_object mp_all_processes ()
13 @end deftypefun
14
15 @defun mp:all-processes
16
17 Returns the list of processes associated to running tasks. The list is a
18 fresh new one and can be destructively modified. However, it may happen
19 that the output list is not up to date, because some of the tasks have
20 expired before this copy is returned.
21
22 @end defun
23
24
25 @cppdef mp_exit_process
26 @lspdef mp:exit-process
27
28 @deftypefun cl_object mp_exit_process () ecl_attr_noreturn
29 @end deftypefun
30
31 @defun mp:exit-process
32
33 When called from a running task, this function immediately causes the
34 task to finish. When invoked from the main thread, it is equivalent to
35 invoking @coderef{ext:quit} with exit code 0.
36
37 @end defun
38
39
40 @cppdef mp_interrupt_process
41 @lspdef mp:interrupt-process
42
43 @deftypefun cl_object mp_interrupt_process (cl_object process, cl_object function)
44 @end deftypefun
45
46 @defun mp:interrupt-process process function
47
48 Interrupt a task. This function sends a signal to a running
49 @var{process}. When the task is free to process that signal, it will
50 stop whatever it is doing and execute the given function.
51
52 @strong{WARNING:} Use with care! Interrupts can happen anywhere,
53 except in code regions explicitely protected with
54 @coderef{mp:without-interrupts}. This can lead to dangerous situations
55 when interrupting functions which are not thread safe. In particular,
56 one has to consider:
57 @itemize
58 @item Reentrancy: Functions, which usually are not called recursively can be re-entered during execution of the interrupt.
59 @item Stack unwinding: Non-local jumps like @code{throw} or @code{return-from} in the interrupting code will handle @code{unwind-protect} forms like usual. However, the cleanup forms of an @code{unwind-protect} can still be interrupted. In that case the execution flow will jump to the next @code{unwind-protect}.
60 @end itemize
61 Note also that no guarantees are made that functions from the Common
62 Lisp standard or ECL extensions are interrupt safe (although most of
63 them will be). In particular, the compiler (@code{compile} and
64 @code{compile-file} functions), FFI calls and aquire/release functions
65 for multithreading synchronization objects like mutexes or condition
66 variables should not be interrupted by @coderef{mp:interrupt-process}.
67
68 @exindex Process interruption
69 Example:
70
71 Kill a task that is doing nothing (See @coderef{mp:process-kill}).
72
73 @lisp
74 (flet ((task-to-be-killed ()
75 ;; Infinite loop
76 (loop (sleep 1))))
77 (let ((task (mp:process-run-function 'background #'task-to-be-killed)))
78 (sleep 10)
79 (mp:interrupt-process task 'mp:exit-process)))
80 @end lisp
81
82 @end defun
83
84
85 @cppdef mp_make_process
86 @lspdef mp:make-process
87
88 @deftypefun cl_object mp_make_process (cl_narg narg, ...)
89 @end deftypefun
90
91 @defun mp:make-process &key name initial-bindings
92
93 Create a new thread. This function creates a separate task with a name
94 set to @var{name} and no function to run. See also
95 @coderef{mp:process-run-function}. Returns newly created process.
96
97 If @var{initial-bindings} is false, the new process inherits local
98 bindings to special variables (i.e. binding a special variable with
99 @code{let} or @code{let*}) from the thread that enables it, otherwise
100 the new thread initially possesses no local bindings.
101 @end defun
102
103
104 @cppdef mp_process_active_p
105 @lspdef mp:process-active-p
106
107 @deftypefun cl_object mp_process_active_p (cl_object process)
108 @end deftypefun
109
110 @defun mp:process-active-p process
111
112 Returns @code{t} when @var{process} is active, @code{nil}
113 otherwise. Signals an error if @var{process} doesn't designate a valid
114 process.
115
116 @end defun
117
118
119 @cppdef mp_process_enable
120 @lspdef mp:process-enable
121
122 @deftypefun cl_object mp_process_enable (cl_object process)
123 @end deftypefun
124
125 @defun mp:process-enable process
126
127 The argument to this function should be a process created by
128 @coderef{mp:make-process}, which has a function associated as per
129 @coderef{mp:process-preset} but which is not yet running. After invoking
130 this function a new thread will be created in which the associated
131 function will be executed. Returns @var{process} if the thread
132 creation was successful and @code{nil} otherwise.
133
134 @exindex Possible implementation of @coderef{mp:process-run-function}:
135
136 @lisp
137 (defun process-run-function (process-name process-function &rest args)
138 (let ((process (mp:make-process name)))
139 (apply #'mp:process-preset process function args)
140 (mp:process-enable process)))
141 @end lisp
142 @end defun
143
144
145 @cppdef mp_process_yield
146 @lspdef mp:process-yield
147
148 @deftypefun cl_object mp_process_yield ()
149 @end deftypefun
150
151 @defun mp:process-yield
152 Yield the processor to other threads.
153 @end defun
154
155
156 @cppdef mp_process-join
157 @lspdef mp:process-join
158
159 @deftypefun cl_object mp_process_join (cl_object process)
160 @end deftypefun
161
162 @defun mp:process-join process
163 Suspend current thread until @var{process} exits. Return the result
164 values of the @var{process} function.
165 @end defun
166
167
168 @cppdef mp_process_kill
169 @lspdef mp:process-kill
170
171 @deftypefun cl_object mp_process_kill (cl_object process)
172 @end deftypefun
173
174 @defun mp:process-kill process
175 Try to stop a running task. Killing a process may fail if the task has
176 disabled interrupts.
177
178 @exindex Killing process
179 Example:
180
181 Kill a task that is doing nothing
182 @lisp
183 (flet ((task-to-be-killed ()
184 ;; Infinite loop
185 (loop (sleep 1))))
186 (let ((task (mp:process-run-function 'background #'task-to-be-killed)))
187 (sleep 10)
188 (mp:process-kill task)))
189 @end lisp
190 @end defun
191
192
193 @cppdef mp_process_suspend
194 @lspdef mp:process-suspend
195
196 @deftypefun cl_object mp_process_suspend (cl_object process)
197 @end deftypefun
198
199 @defun mp:process-suspend process
200 Suspend a running @var{process}. May be resumed with
201 @coderef{mp:process-resume}.
202
203 @exindex Suspend and resume process
204 Example:
205
206 @lisp
207 (flet ((ticking-task ()
208 ;; Infinite loop
209 (loop
210 (sleep 1)
211 (print :tick))))
212 (print "Running task (one tick per second)")
213 (let ((task (mp:process-run-function 'background #'ticking-task)))
214 (sleep 5)
215 (print "Suspending task for 5 seconds")
216 (mp:process-suspend task)
217 (sleep 5)
218 (print "Resuming task for 5 seconds")
219 (mp:process-resume task)
220 (sleep 5)
221 (print "Killing task")
222 (mp:process-kill task)))
223 @end lisp
224 @end defun
225
226
227 @cppdef mp_process_resume
228 @lspdef mp:process-resume
229
230 @deftypefun cl_object mp_process_resume (cl_object process)
231 @end deftypefun
232
233 @defun mp:process-resume process
234 Resumes a suspended @var{process}. See example in
235 @coderef{mp:process-suspend}.
236 @end defun
237
238
239 @cppdef mp_process_name
240 @lspdef mp:process-name
241
242 @deftypefun cl_object mp_process_name (cl_object process)
243 @end deftypefun
244
245 @defun mp:process-name process
246 Returns the name of a @var{process} (if any).
247 @end defun
248
249
250 @cppdef mp_process_preset
251 @lspdef mp:process-preset
252
253 @deftypefun cl_object mp_process_preset (cl_narg narg, cl_object process, cl_object function, ...)
254 @end deftypefun
255
256 @defun mp:process-preset process function &rest function-args
257
258 Associates a @var{function} to call with the arguments
259 @var{function-args}, with a stopped @var{process}. The function will
260 be the entry point when the task is enabled in the future.
261
262 See @coderef{mp:process-enable} and @coderef{mp:process-run-function}.
263
264 @end defun
265
266
267 @cppdef mp_process_run_function
268 @lspdef mp:process-run-function
269
270 @deftypefun cl_object mp_process_run_function (cl_narg narg, cl_object name, cl_object function, ...)
271 @end deftypefun
272
273 @defun mp:process-run-function name function &rest function-args
274 Create a new process using @coderef{mp:make-process}, associate a function
275 to it and start it using @coderef{mp:process-preset}.
276
277 @exindex mp:process-run-function usage
278 Example:
279
280 @lisp
281 (flet ((count-numbers (end-number)
282 (dotimes (i end-number)
283 (format t "~%;;; Counting: ~i" i)
284 (terpri)
285 (sleep 1))))
286 (mp:process-run-function 'counter #'count-numbers 10))
287 @end lisp
288 @end defun
289
290
291 @cppdef mp_current_process
292 @lspdef mp:*current-process*
293
294 @deftypefun cl_object mp_current_process ()
295 @end deftypefun
296
297 @defvr Variable {mp:*current-process*}
298 Returns/holds the current process of a caller.
299 @end defvr
300
301
302 @cppdef mp_block_signals
303 @lspdef mp:block-signals
304
305 @deftypefun cl_object mp_block_signals ()
306 @end deftypefun
307
308 @defun mp:block-signals
309 Blocks process for interrupts and returns the previous sigmask.
310
311 See @coderef{mp:interrupt-process}.
312 @end defun
313
314
315 @cppdef mp_restore_signals
316 @lspdef mp:restore-signals
317
318 @deftypefun cl_object mp_restore_signals (cl_object sigmask)
319 @end deftypefun
320
321 @defun mp:restore-signals sigmask
322 Enables the interrupts from @var{sigmask}.
323
324 See @coderef{mp:interrupt-process}.
325 @end defun
326
327
328 @lspdef mp:without-interrupts
329 @lspdef mp:allow-with-interrupts
330 @lspdef mp:with-local-interrupts
331 @lspdef mp:with-restored-interrupts
332
333 @defmac mp:without-interrupts &body body
334
335 Executes @var{body} with all deferrable interrupts disabled. Deferrable
336 interrupts arriving during execution of the @var{body} take effect
337 after @var{body} has been executed.
338
339 Deferrable interrupts include most blockable POSIX signals, and
340 @coderef{mp:interrupt-process}. Does not interfere with garbage
341 collection, and unlike in many traditional Lisps using userspace
342 threads, in ECL @coderef{mp:without-interrupts} does not inhibit
343 scheduling of other threads.
344
345 Binds @coderef{mp:allow-with-interrupts},
346 @coderef{mp:with-local-interrupts} and
347 @coderef{mp:with-restored-interrupts} as a local macros.
348
349 @coderef{mp:with-restored-interrupts} executes the body with interrupts
350 enabled if and only if the @coderef{mp:without-interrupts} was in an
351 environment in which interrupts were allowed.
352
353 @coderef{mp:allow-with-interrupts} allows the
354 @coderef{mp:with-interrupts} to take effect during the dynamic scope of
355 its body, unless there is an outer @coderef{mp:without-interrupts}
356 without a corresponding @coderef{mp:allow-with-interrupts}.
357
358 @coderef{mp:with-local-interrupts} executes its body with interrupts
359 enabled provided that there is an @coderef{mp:allow-with-interrupts} for
360 every @coderef{mp:without-interrupts} surrounding the current one.
361 @coderef{mp:with-local-interrupts} is equivalent to:
362
363 @lisp
364 (mp:allow-with-interrupts (mp:with-interrupts ...))
365 @end lisp
366
367 Care must be taken not to let either @coderef{mp:allow-with-interrupts}
368 or @coderef{mp:with-local-interrupts} appear in a function that escapes
369 from inside the @coderef{mp:without-interrupts} in:
370
371 @lisp
372 (mp:without-interrupts
373 ;; The body of the lambda would be executed with WITH-INTERRUPTS allowed
374 ;; regardless of the interrupt policy in effect when it is called.
375 (lambda () (mp:allow-with-interrupts ...)))
376
377 (mp:without-interrupts
378 ;; The body of the lambda would be executed with interrupts enabled
379 ;; regardless of the interrupt policy in effect when it is called.
380 (lambda () (mp:with-local-interrupts ...)))
381 @end lisp
382 @end defmac
383
384
385 @lspdef mp:with-interrupts
386 @defmac mp:with-interrupts &body body
387 Executes @var{body} with deferrable interrupts conditionally
388 enabled. If there are pending interrupts they take effect prior to
389 executing @var{body}.
390
391 As interrupts are normally allowed @coderef{mp:with-interrupts} only
392 makes sense if there is an outer @coderef{mp:without-interrupts} with a
393 corresponding @coderef{mp:allow-with-interrupts}: interrupts are not
394 enabled if any outer @coderef{mp:without-interrupts} is not accompanied
395 by @coderef{mp:allow-with-interrupts}.
396 @end defmac