Debian Codegraph @

src/doc/old-manual/ref_mp.xmlf

raw
1 <?xml version="1.0" encoding="utf-8"?>
2 <!DOCTYPE book [
3 <!ENTITY % eclent SYSTEM "ecl.ent">
4 %eclent;
5 ]>
6 <book xmlns="http://docbook.org/ns/docbook" version="5.0" xml:lang="en">
7 <chapter>
8 <title>Multithreading</title>
9 <section xml:id="ext.mp.dict"><title>Multithreading Reference</title>
10
11 <!-- ====================================================================== -->
12 <!-- MP:ALL-PROCESSES -->
13 <!-- ====================================================================== -->
14
15 <refentry xml:id="ref.mp.all-processes">
16 <refnamediv>
17 <refname><function>mp:all-processes</function></refname>
18 <refpurpose>Return the list of active processes.</refpurpose>
19 </refnamediv>
20
21 <refsynopsisdiv>
22 <title>Function</title>
23 <funcsynopsis>
24 <funcprototype>
25 <funcdef>mp:all-processes</funcdef>
26 <paramdef></paramdef>
27 </funcprototype>
28 </funcsynopsis>
29 </refsynopsisdiv>
30
31 <refsect1>
32 <title>Description</title>
33 <para>Returns the list of processes associated to running tasks. The list is
34 a fresh new one and can be destructively modified. However, it may happen
35 that the output list is not up to date, because some of the tasks has expired
36 before this copy is returned.</para>
37 </refsect1>
38
39 </refentry>
40
41 <!-- ====================================================================== -->
42 <!-- MP:EXIT-PROCESS -->
43 <!-- ====================================================================== -->
44
45 <refentry xml:id="ref.mp.exit-process">
46 <refnamediv>
47 <refname><function>mp:exit-process</function></refname>
48 <refpurpose>Exit the task from which it is invoked.</refpurpose>
49 </refnamediv>
50
51 <refsynopsisdiv>
52 <title>Function</title>
53 <funcsynopsis>
54 <funcprototype>
55 <funcdef>mp:exit-process</funcdef>
56 <paramdef></paramdef>
57 </funcprototype>
58 </funcsynopsis>
59 </refsynopsisdiv>
60
61 <refsect1>
62 <title>Description</title>
63 <para>When called from a running task, this function immediately causes the
64 task to finish. When invoked from the main thread, it is equivalent to
65 invoking <xref linkend="ref.quit"/> with exit code 0.</para>
66 </refsect1>
67
68 <refsect1>
69 <title>Example</title>
70 <para>See <xref linkend="ref.mp.interrupt-process"/>.</para>
71 </refsect1>
72
73 </refentry>
74
75 <!-- ====================================================================== -->
76 <!-- MP:HOLDING-LOCK-P -->
77 <!-- ====================================================================== -->
78
79 <refentry xml:id="ref.mp.holding-lock-p">
80 <refnamediv>
81 <refname><function>mp:holding-lock-p</function></refname>
82 <refpurpose>Determine whether current process holds the lock.</refpurpose>
83 </refnamediv>
84
85 <refsynopsisdiv>
86 <title>Function</title>
87 <funcsynopsis>
88 <funcprototype>
89 <funcdef>mp:holding-lock-p</funcdef>
90 <paramdef><parameter>lock</parameter></paramdef>
91 </funcprototype>
92 </funcsynopsis>
93 <variablelist>
94 <varlistentry>
95 <term><replaceable>lock</replaceable></term>
96 <listitem><para>An object of type <replaceable>mp:lock</replaceable>.</para></listitem>
97 </varlistentry>
98 </variablelist>
99 </refsynopsisdiv>
100
101 <refsect1>
102 <title>Description</title>
103 <para>Returns true if the current process holds the lock.</para>
104 </refsect1>
105
106 </refentry>
107
108 <!-- ====================================================================== -->
109 <!-- MP:INTERRUPT-PROCESS -->
110 <!-- ====================================================================== -->
111
112 <refentry xml:id="ref.mp.interrupt-process">
113 <refnamediv>
114 <refname><function>mp:interrupt-process</function></refname>
115 <refpurpose>Interrupt a task.</refpurpose>
116 </refnamediv>
117
118 <refsynopsisdiv>
119 <title>Function</title>
120 <funcsynopsis>
121 <funcprototype>
122 <funcdef>mp:interrupt-process</funcdef>
123 <paramdef><parameter>process</parameter></paramdef>
124 <paramdef><parameter>function</parameter></paramdef>
125 </funcprototype>
126 </funcsynopsis>
127 <variablelist>
128 <varlistentry>
129 <term><replaceable>process</replaceable></term>
130 <listitem><para>An object of type
131 <replaceable>mp:process</replaceable>.</para></listitem>
132 </varlistentry>
133 <varlistentry>
134 <term><replaceable>function</replaceable></term>
135 <listitem><para>A function, which is to be executed in the interrupted
136 process.</para></listitem>
137 </varlistentry>
138 </variablelist>
139 </refsynopsisdiv>
140
141 <refsect1>
142 <title>Description</title>
143 <para>This function sends a signal to a running task. When the task is
144 free to process that signal, it will stop whatever it is doing and
145 execute the given <replaceable>function</replaceable>.</para>
146 </refsect1>
147
148 <refsect1>
149 <title>Example</title>
150 <para>Kill a task that is doing nothing (See <xref linkend="ref.mp.process-kill"/>).</para>
151 <programlisting>
152 (flet ((task-to-be-killed ()
153 (loop (sleep 1)) ; Infinite loop
154 ))
155 (let ((task (mp:process-run-function 'background #'task-to-be-killed)))
156 (sleep 10)
157 (mp:interrupt-process task 'mp:exit-process)))
158 </programlisting>
159 </refsect1>
160
161 </refentry>
162
163 <!-- ====================================================================== -->
164 <!-- MP:GET-LOCK -->
165 <!-- ====================================================================== -->
166
167 <refentry xml:id="ref.mp.get-lock">
168 <refnamediv>
169 <refname><function>mp:get-lock</function></refname>
170 <refpurpose>Try to obtain a lock.</refpurpose>
171 </refnamediv>
172
173 <refsynopsisdiv>
174 <title>Function</title>
175 <funcsynopsis>
176 <funcprototype>
177 <funcdef>mp:get-lock</funcdef>
178 <paramdef><parameter>lock</parameter></paramdef>
179 <paramdef><parameter>&optional;</parameter></paramdef>
180 <paramdef><parameter>wait</parameter></paramdef>
181 </funcprototype>
182 </funcsynopsis>
183 <variablelist>
184 <varlistentry>
185 <term><replaceable>lock</replaceable></term>
186 <listitem><para>An object of type
187 <replaceable>mp:lock</replaceable>.</para></listitem>
188 </varlistentry>
189 <varlistentry>
190 <term><replaceable>wait</replaceable></term>
191 <listitem><para>Whether to wait to obtain the lock. Defaults to
192 &nil;.</para></listitem>
193 </varlistentry>
194 </variablelist>
195 </refsynopsisdiv>
196
197 <refsect1>
198 <title>Description</title>
199 <para>This function tries to obtain exclusive use of a lock. If
200 <replaceable>wait</replaceable> is &t;, and another task owns the lock, it
201 may wait until the lock has been released. If <replaceable>wait</replaceable>
202 is &nil; it will return true if and only if it succeeded on acquiring
203 the lock.</para>
204 </refsect1>
205
206 <refsect1>
207 <title>Example</title>
208 <para>See <xref linkend="ref.mp.with-lock"/>.</para>
209 </refsect1>
210
211 </refentry>
212
213 <!-- ====================================================================== -->
214 <!-- MP:GIVEUP-LOCK -->
215 <!-- ====================================================================== -->
216
217 <refentry xml:id="ref.mp.giveup-lock">
218 <refnamediv>
219 <refname><function>mp:giveup-lock</function></refname>
220 <refpurpose>Release a lock we have obtained before.</refpurpose>
221 </refnamediv>
222
223 <refsynopsisdiv>
224 <title>Function</title>
225 <funcsynopsis>
226 <funcprototype>
227 <funcdef>mp:giveup-lock</funcdef>
228 <paramdef><parameter>lock</parameter></paramdef>
229 </funcprototype>
230 </funcsynopsis>
231 <variablelist>
232 <varlistentry>
233 <term><replaceable>lock</replaceable></term>
234 <listitem><para>An object of type
235 <replaceable>mp:lock</replaceable>.</para></listitem>
236 </varlistentry>
237 </variablelist>
238 </refsynopsisdiv>
239
240 <refsect1>
241 <title>Description</title>
242 <para>This function releases a lock which had been previously obtained using
243 <xref linkend="ref.mp.get-lock"/>.</para>
244 </refsect1>
245
246 <refsect1>
247 <title>Example</title>
248 <para>See <xref linkend="ref.mp.with-lock"/>.</para>
249 </refsect1>
250
251 </refentry>
252
253 <!-- ====================================================================== -->
254 <!-- MP:MAKE-LOCK -->
255 <!-- ====================================================================== -->
256
257 <refentry xml:id="ref.mp.make-lock">
258 <refnamediv>
259 <refname><function>mp:make-lock</function></refname>
260 <refpurpose>Create a new lock.</refpurpose>
261 </refnamediv>
262
263 <refsynopsisdiv>
264 <title>Function</title>
265 <funcsynopsis>
266 <funcprototype>
267 <funcdef>mp:make-lock</funcdef>
268 <paramdef><parameter>&key;</parameter></paramdef>
269 <paramdef><parameter>name</parameter></paramdef>
270 </funcprototype>
271 </funcsynopsis>
272 <variablelist>
273 <varlistentry>
274 <term><replaceable>name</replaceable></term>
275 <listitem><para>A symbol to name the lock. Names not be unique. It defaults
276 to &nil;.</para></listitem>
277 </varlistentry>
278 </variablelist>
279 </refsynopsisdiv>
280
281 <refsect1>
282 <title>Description</title>
283 <para>This function creates a lock object that can be use to synchronize
284 different tasks. The own is initally free and not owned by any task. Locks
285 are typically implemented using Posix mutexes. When garbage collected, a lock
286 is automatically freed.</para>
287 </refsect1>
288
289 <refsect1>
290 <title>Example</title>
291
292 <para>See <xref linkend="ref.mp.with-lock"/>.</para>
293 </refsect1>
294 </refentry>
295
296 <!-- ====================================================================== -->
297 <!-- MP:MAKE-PROCESS -->
298 <!-- ====================================================================== -->
299
300 <refentry xml:id="ref.mp.make-process">
301 <refnamediv>
302 <refname><function>mp:make-process</function></refname>
303 <refpurpose>Create a new thread.</refpurpose>
304 </refnamediv>
305
306 <refsynopsisdiv>
307 <title>Function</title>
308 <funcsynopsis>
309 <funcprototype>
310 <funcdef>mp:make-process</funcdef>
311 <paramdef><parameter>&key;</parameter></paramdef>
312 <paramdef><parameter>name</parameter></paramdef>
313 <paramdef><parameter>initial-bindings</parameter></paramdef>
314 </funcprototype>
315 </funcsynopsis>
316 <variablelist>
317 <varlistentry>
318 <term><replaceable>name</replaceable></term>
319 <listitem><para>A symbol to name the process. Processes can be
320 unnamed and names need not be unique.</para></listitem>
321 </varlistentry>
322 <varlistentry>
323 <term><replaceable>initial-bindings</replaceable></term>
324 <listitem><para>The list of special variables which will be local to
325 the new process. It defaults to &t;, which means copying all variables
326 which are local to this process.</para></listitem>
327 </varlistentry>
328 </variablelist>
329 </refsynopsisdiv>
330
331 <refsect1>
332 <title>Description</title>
333 <para>This function creates a separate task, with a name, set of variable
334 bindings and no function to run.</para>
335
336 <para>See also <xref linkend="ref.mp.process-run-function"/>.</para>
337 </refsect1>
338
339 <refsect1>
340 <title>Example</title>
341
342 <para>See <xref linkend="ref.mp.process-enable"/></para>
343 </refsect1>
344 </refentry>
345
346 <!-- ====================================================================== -->
347 <!-- MP:PROCESS-ACTIVE-P -->
348 <!-- ====================================================================== -->
349
350 <refentry xml:id="ref.mp.process-active-p">
351 <refnamediv>
352 <refname><function>mp:process-active-p</function></refname>
353 <refpurpose>Determine whether a task is running.</refpurpose>
354 </refnamediv>
355
356 <refsynopsisdiv>
357 <title>Function</title>
358 <funcsynopsis>
359 <funcprototype>
360 <funcdef>mp:process-active-p</funcdef>
361 <paramdef><parameter>process</parameter></paramdef>
362 </funcprototype>
363 </funcsynopsis>
364 <variablelist>
365 <varlistentry>
366 <term><replaceable>process</replaceable></term>
367 <listitem><para>An object of type <replaceable>mp:process</replaceable>.</para></listitem>
368 </varlistentry>
369 </variablelist>
370 </refsynopsisdiv>
371
372 <refsect1>
373 <title>Description</title>
374 <para>Returns true if the task is currently running.</para>
375 </refsect1>
376
377 </refentry>
378
379 <!-- ====================================================================== -->
380 <!-- MP:PROCESS-ENABLE -->
381 <!-- ====================================================================== -->
382
383 <refentry xml:id="ref.mp.process-enable">
384 <refnamediv>
385 <refname><function>mp:process-enable</function></refname>
386 <refpurpose>Start up a task which was not running.</refpurpose>
387 </refnamediv>
388
389 <refsynopsisdiv>
390 <title>Function</title>
391 <funcsynopsis>
392 <funcprototype>
393 <funcdef>mp:process-enable</funcdef>
394 <paramdef><parameter>process</parameter></paramdef>
395 </funcprototype>
396 </funcsynopsis>
397 <variablelist>
398 <varlistentry>
399 <term><replaceable>process</replaceable></term>
400 <listitem><para>An object of type <replaceable>mp:process</replaceable>.</para></listitem>
401 </varlistentry>
402 </variablelist>
403 </refsynopsisdiv>
404
405 <refsect1>
406 <title>Description</title>
407 <para>The argument to this function should be a process created by <xref
408 linkend="ref.mp.make-process"/>, which has a function associated as per <xref
409 linkend="ref.mp.process-preset"/> but which is not yet running. After
410 invoking this function a new thread will be created in which the associated
411 function will be executed.</para>
412 </refsect1>
413
414 <refsect1>
415 <title>Example</title>
416 <para>The following is a possible implementation of <xref linkend="ref.mp.process-run-function"/>.</para>
417 <programlisting>
418 (defun process-run-function (process-name process-function &amp;rest args)
419 (let ((process (mp:make-process name)))
420 (apply #'mp:process-preset process function args)
421 (mp:process-enable process)))
422 </programlisting>
423 </refsect1>
424
425 </refentry>
426
427 <!-- ====================================================================== -->
428 <!-- MP:PROCESS-KILL -->
429 <!-- ====================================================================== -->
430
431 <refentry xml:id="ref.mp.process-kill">
432 <refnamediv>
433 <refname><function>mp:process-kill</function></refname>
434 <refpurpose>Try to stop a running task.</refpurpose>
435 </refnamediv>
436
437 <refsynopsisdiv>
438 <title>Function</title>
439 <funcsynopsis>
440 <funcprototype>
441 <funcdef>mp:process-kill</funcdef>
442 <paramdef><parameter>process</parameter></paramdef>
443 </funcprototype>
444 </funcsynopsis>
445 <variablelist>
446 <varlistentry>
447 <term><replaceable>process</replaceable></term>
448 <listitem><para>An object of type
449 <replaceable>mp:process</replaceable>.</para></listitem>
450 </varlistentry>
451 </variablelist>
452 </refsynopsisdiv>
453
454 <refsect1>
455 <title>Description</title>
456 <para>This function tries to stop a running task. Killing a process
457 may fail if the task has disabled interrupts.</para>
458 </refsect1>
459
460 <refsect1>
461 <title>Example</title>
462 <para>Kill a task that is doing nothing.</para>
463 <programlisting>
464 (flet ((task-to-be-killed ()
465 (loop (sleep 1)) ; Infinite loop
466 ))
467 (let ((task (mp:process-run-function 'background #'task-to-be-killed)))
468 (sleep 10)
469 (mp:process-kill task)))
470 </programlisting>
471 </refsect1>
472
473 </refentry>
474
475 <!-- ====================================================================== -->
476 <!-- MP:PROCESS-NAME -->
477 <!-- ====================================================================== -->
478
479 <refentry xml:id="ref.mp.process-name">
480 <refnamediv>
481 <refname><function>mp:process-name</function></refname>
482 <refpurpose>Return the name of a task.</refpurpose>
483 </refnamediv>
484
485 <refsynopsisdiv>
486 <title>Function</title>
487 <funcsynopsis>
488 <funcprototype>
489 <funcdef>mp:process-name</funcdef>
490 <paramdef><parameter>process</parameter></paramdef>
491 </funcprototype>
492 </funcsynopsis>
493 <variablelist>
494 <varlistentry>
495 <term><replaceable>process</replaceable></term>
496 <listitem><para>An object of type <replaceable>mp:process</replaceable>.</para></listitem>
497 </varlistentry>
498 </variablelist>
499 </refsynopsisdiv>
500
501 <refsect1>
502 <title>Description</title>
503 <para>Returns the name of the given task, if any.</para>
504 </refsect1>
505
506 </refentry>
507
508 <!-- ====================================================================== -->
509 <!-- MP:PROCESS-PRESET -->
510 <!-- ====================================================================== -->
511
512 <refentry xml:id="ref.mp.process-preset">
513 <refnamediv>
514 <refname><function>mp:process-preset</function></refname>
515 <refpurpose>Associate a function to a process.</refpurpose>
516 </refnamediv>
517
518 <refsynopsisdiv>
519 <title>Function</title>
520 <funcsynopsis>
521 <funcprototype>
522 <funcdef>mp:process-preset</funcdef>
523 <paramdef><parameter>process</parameter></paramdef>
524 <paramdef><parameter>function</parameter></paramdef>
525 <paramdef><parameter>&rest;</parameter></paramdef>
526 <paramdef><parameter>function-args</parameter></paramdef>
527 </funcprototype>
528 </funcsynopsis>
529 <variablelist>
530 <varlistentry>
531 <term><replaceable>process</replaceable></term>
532 <listitem><para>An object of type
533 <replaceable>mp:process</replaceable>.</para></listitem>
534 </varlistentry>
535 <varlistentry>
536 <term><replaceable>function</replaceable></term>
537 <listitem><para>An interpret or compiled function.</para></listitem>
538 </varlistentry>
539 <varlistentry>
540 <term><replaceable>function-args</replaceable></term>
541 <listitem><para>One or more arguments for the function
542 above.</para></listitem>
543 </varlistentry>
544 </variablelist>
545 </refsynopsisdiv>
546
547 <refsect1>
548 <title>Description</title>
549 <para>This function associates a function to a stopped task. This function
550 will be the entry point when the task is enabled in the future.</para>
551
552 <para>See also <xref linkend="ref.mp.process-run-function"/>.</para>
553 </refsect1>
554
555 <refsect1>
556 <title>Example</title>
557
558 <para>See <xref linkend="ref.mp.process-enable"/>.</para>
559 </refsect1>
560 </refentry>
561
562 <!-- ====================================================================== -->
563 <!-- MP:PROCESS-RUN-FUNCTION -->
564 <!-- ====================================================================== -->
565
566 <refentry xml:id="ref.mp.process-run-function">
567 <refnamediv>
568 <refname><function>mp:process-run-function</function></refname>
569 <refpurpose>Create a task and execute a function in it.</refpurpose>
570 </refnamediv>
571
572 <refsynopsisdiv>
573 <title>Function</title>
574 <funcsynopsis>
575 <funcprototype>
576 <funcdef>mp:process-run-function</funcdef>
577 <paramdef><parameter>name</parameter></paramdef>
578 <paramdef><parameter>function</parameter></paramdef>
579 <paramdef><parameter>&rest;</parameter></paramdef>
580 <paramdef><parameter>function-args</parameter></paramdef>
581 </funcprototype>
582 </funcsynopsis>
583 <variablelist>
584 <varlistentry>
585 <term><replaceable>name</replaceable></term>
586 <listitem><para>A symbol to name the process. Processes can be
587 unnamed and names need not be unique.</para></listitem>
588 </varlistentry>
589 <varlistentry>
590 <term><replaceable>function</replaceable></term>
591 <listitem><para>A function object, interpreted or compiled.</para></listitem>
592 </varlistentry>
593 <varlistentry>
594 <term><replaceable>function-args</replaceable></term>
595 <listitem><para>One or more arguments which will be passed to the
596 function.</para></listitem>
597 </varlistentry>
598 </variablelist>
599 </refsynopsisdiv>
600
601 <refsect1>
602 <title>Description</title>
603 <para>This function creates a new process using <xref
604 linkend="ref.mp.make-process"/>, associates a function to it and then
605 starts it using <xref linkend="ref.mp.process-preset"/>.</para>
606 </refsect1>
607
608 <refsect1>
609 <title>Example</title>
610
611 <para>As an example, the following code starts a background task that
612 outputs a list of numbers, one every second:</para>
613 <programlisting>
614 (flet ((count-numbers (end-number)
615 (dotimes (i end-number)
616 (format t "~%;;; Counting: ~i" i)
617 (terpri)
618 (sleep 1))))
619 (mp:process-run-function 'counter #'count-numbers 10))
620 </programlisting>
621 </refsect1>
622 </refentry>
623
624 <!-- ====================================================================== -->
625 <!-- MP:RECURSIVE-LOCK-P -->
626 <!-- ====================================================================== -->
627
628 <refentry xml:id="ref.mp.recursive-lock-p">
629 <refnamediv>
630 <refname><function>mp:recursive-lock-p</function></refname>
631 <refpurpose>Determine whether lock is recursive or not.</refpurpose>
632 </refnamediv>
633
634 <refsynopsisdiv>
635 <title>Function</title>
636 <funcsynopsis>
637 <funcprototype>
638 <funcdef>mp:recursive-lock-p</funcdef>
639 <paramdef><parameter>lock</parameter></paramdef>
640 </funcprototype>
641 </funcsynopsis>
642 <variablelist>
643 <varlistentry>
644 <term><replaceable>lock</replaceable></term>
645 <listitem><para>An object of type <replaceable>mp:lock</replaceable>.</para></listitem>
646 </varlistentry>
647 </variablelist>
648 </refsynopsisdiv>
649
650 <refsect1>
651 <title>Description</title>
652 <para>Returns true if the lock is recursive.</para>
653 </refsect1>
654
655 </refentry>
656
657 <!-- ====================================================================== -->
658 <!-- MP:WITH-LOCK -->
659 <!-- ====================================================================== -->
660
661 <refentry xml:id="ref.mp.with-lock">
662 <refnamediv>
663 <refname><function>mp:with-lock</function></refname>
664 <refpurpose>Synchronize a piece of code between different tasks.</refpurpose>
665 </refnamediv>
666
667 <refsynopsisdiv>
668 <title>Macro</title>
669 <funcsynopsis>
670 <funcprototype>
671 <funcdef>mp::with-lock</funcdef>
672 <paramdef>(<parameter>lock</parameter>)</paramdef>
673 <paramdef><parameter>&body;</parameter></paramdef>
674 <paramdef><parameter>body</parameter></paramdef>
675 </funcprototype>
676 </funcsynopsis>
677 </refsynopsisdiv>
678
679 <refsect1>
680 <title>Description</title>
681
682 <para>It grabs a lock, executes a piece of lisp code and releases the lock at
683 the end. The inner forms are protected so that when any condition is
684 signalled the lock is released.</para>
685 </refsect1>
686
687 <refsect1>
688 <title>Example</title>
689
690 <para>Ensure each task increments the counter properly. The lock is
691 required because INCF is not an atomic operation.</para>
692 <programlisting>
693 (defvar *counter* 0)
694 (defvar *counter-lock* (mp:make-lock :name 'counter))
695
696 (flet ((task (name)
697 (loop while (&lt;= *counter* 10)
698 do (progn
699 (sleep 1)
700 (with-lock (*counter-lock*)
701 (format t "~%;;; ~A counts ~D" name *counter*)
702 (terpri)
703 (incf *counter*))))))
704 (mp:process-run-function 'ana #'task 'ana)
705 (mp:process-run-function 'jose #'task 'jose))
706 </programlisting>
707 </refsect1>
708 </refentry>
709 </section>
710 </chapter>
711 </book>
712 <!-- Keep this comment at the end of the file
713 Local variables:
714 mode: nxml
715 sgml-parent-document: "ecl.xml"
716 sgml-indent-step: 1
717 nxml-child-indent: 1
718 nxml-outline-child-indent: 1
719 fill-column: 79
720 End:
721 -->