143
142
<screen>&sh-prompt; clisp -i myfile -x '(&savemem;)'</screen>
144
143
will produce an image without any global handlers inherited
145
144
from the batch mode of the above command.</para></listitem></varlistentry>
146
<varlistentry><term><constant>:EXECUTABLE</constant></term>
147
<listitem><simpara>When non-&nil;, the saved file will be an
145
<varlistentry id="image-exec"><term><constant>:EXECUTABLE</constant></term>
146
<listitem><simpara>When non-&nil;, the saved file will be a
148
147
standalone executable.
149
148
In this case, the &mem-file; extension is not added.
150
149
On &win32; and &cygwin; the extension <filename>#P".exe"</filename>
151
is added instead.</simpara></listitem></varlistentry>
150
is added instead.</simpara>
151
<para>Additionally, if this argument is &zero;, the standard
152
&clisp; command line options will ¬-e; be processed by the
153
executable but will be placed into &args; instead.
154
This is convenient for application delivery, so that your
155
&clisp;-based application can accept, e.g., &opt-x;.
156
To override this feature of the image, you have to prefix the
157
options with <literal>"--clisp"</literal>, e.g.,
158
use <option>--clisp-x</option> instead of &opt-x;.
159
This, given such a &clisp;-based application, you can get to an
160
ordinary &clisp; &repl; by doing <screen>
161
&sh-prompt; application --clisp-x '(&savemem; "myclisp" :executable t :init-function nil)'
162
&sh-prompt; ./myclisp
163
[1]> (<link linkend="factorial">!</link> 20)
164
2432902008176640000</screen>
165
These instructions are also printed by
166
<option>--clisp--help</option>.</para>
167
<simpara>Of course, this feature opens a <emphasis>security hole</emphasis>
168
if the application is running <function role="unix">setuid</function> root,
169
therefore &clisp; resets the effective group and user IDs to the real
170
ones if it sees a <literal>"--clisp-*"</literal> option.
171
</simpara></listitem></varlistentry></variablelist>
153
172
You can use this memory image with the &opt-M; option.
154
173
On &unix; systems, you may compress it with &gnu; &gzip; to save disk
157
<warning id="image-portability"><title>Image Portability</title>
176
<section id="image-portability"><title>Image Portability</title>
158
177
<para>Memory images are ¬-e; portable across different platforms
159
178
(in contrast with platform-independent &fasl-file; files).
160
179
They are ¬-e; even portable across &linkset;s: image saved using
161
the &full; &linkset; cannot be used with the &base; runtime:<screen>
180
the &full; &linkset; cannot be used with the &base; &rt;:<screen>
162
181
&sh-prompt; clisp -K full -x '(&savemem;)'
163
182
&sh-prompt; clisp -K base -M lispinit.mem
164
base/lisp.run: initialization file `lispinit.mem' was not created by this version of CLISP runtime
165
</screen></para></warning>
183
base/lisp.run: initialization file `lispinit.mem' was not created by this version of CLISP runtime</screen>
184
See also <ulink role="sfmail" url="BF6EFF38DF3FA647BBD932720D8BED650BAA11%40parmbx02.ilog.biz"/>/<ulink role="gmane" url="devel/17757"/>.</para></section>
169
188
<section id="quit"><title>Quitting &clisp;</title>
171
190
<para>The functions <simplelist>
172
<member><code>(<function>EXT:EXIT</function>
173
&optional-amp; &status-r;)</code></member>
174
<member><code>(<function>EXT:QUIT</function>
175
&optional-amp; &status-r;)</code></member>
176
<member><code>(<function>EXT:BYE</function>
177
&optional-amp; &status-r;)</code></member></simplelist>
191
<member><code>(&exit; &optional-amp; &status-r;)</code></member>
192
<member><code>(EXT:QUIT &optional-amp; &status-r;)</code></member>
193
<member><code>(EXT:BYE &optional-amp; &status-r;)</code></member></simplelist>
178
194
- all synonymous - terminate &clisp;. If &status-r; is non-&nil;,
179
195
&clisp; aborts with the supplied numeric error &status-r;, i.e.,
180
196
the OS environment is informed that the &clisp; session did not
487
502
<listitem id="charset-CP852-IBM"><simpara><constant>CP852-IBM</constant>,
488
503
an IBM variant of <constant>CP852</constant>.</simpara></listitem>
489
504
<listitem id="charset-CP855"><simpara><constant>CP855</constant>, a DOS oldie,
490
&ascii-pl-ext;, meant to be suitable for the Russian language.
491
</simpara></listitem>
505
&ascii-pl-ext;, &good-for; the Russian language.</simpara></listitem>
492
506
<listitem id="charset-CP857"><simpara><constant>CP857</constant>, a DOS oldie,
493
&ascii-pl-ext;, meant to be suitable for the Turkish language.
494
</simpara></listitem>
507
&ascii-pl-ext;, &good-for; the Turkish language.</simpara></listitem>
495
508
<listitem id="charset-CP860"><simpara><constant>CP860</constant>, a DOS oldie,
496
&ascii-pl-ext;, meant to be suitable for the Portuguese language.
497
</simpara></listitem>
509
&ascii-pl-ext;, &good-for; the Portuguese language.</simpara></listitem>
498
510
<listitem id="charset-CP860-IBM"><simpara><constant>CP860-IBM</constant>,
499
an IBM variant of <constant>CP860</constant>.
500
</simpara></listitem>
511
an IBM variant of <constant>CP860</constant>.</simpara></listitem>
501
512
<listitem id="charset-CP861"><simpara><constant>CP861</constant>, a DOS oldie,
502
&ascii-pl-ext;, meant to be suitable for the Icelandic language.
503
</simpara></listitem>
513
&ascii-pl-ext;, &good-for; the Icelandic language.</simpara></listitem>
504
514
<listitem id="charset-CP861-IBM"><simpara><constant>CP861-IBM</constant>,
505
an IBM variant of <constant>CP861</constant>.
506
</simpara></listitem>
515
an IBM variant of <constant>CP861</constant>.</simpara></listitem>
507
516
<listitem id="charset-CP862"><simpara><constant>CP862</constant>, a DOS oldie,
508
&ascii-pl-ext;, meant to be suitable for the Hebrew language.
509
</simpara></listitem>
517
&ascii-pl-ext;, &good-for; the Hebrew language.</simpara></listitem>
510
518
<listitem id="charset-CP862-IBM"><simpara><constant>CP862-IBM</constant>,
511
an IBM variant of <constant>CP862</constant>.
512
</simpara></listitem>
519
an IBM variant of <constant>CP862</constant>.</simpara></listitem>
513
520
<listitem id="charset-CP863"><simpara><constant>CP863</constant>, a DOS oldie,
514
521
&ascii-pl-ext;.</simpara></listitem>
515
522
<listitem id="charset-CP863-IBM"><simpara><constant>CP863-IBM</constant>,
516
an IBM variant of <constant>CP863</constant>.
517
</simpara></listitem>
523
an IBM variant of <constant>CP863</constant>.</simpara></listitem>
518
524
<listitem id="charset-CP864"><simpara><constant>CP864</constant>, a DOS oldie,
519
meant to be suitable for the Arabic language.
520
</simpara></listitem>
525
&good-for; the Arabic language.</simpara></listitem>
521
526
<listitem id="charset-CP864-IBM"><simpara><constant>CP864-IBM</constant>,
522
527
an IBM variant of <constant>CP864</constant>.
523
528
</simpara></listitem>
524
529
<listitem id="charset-CP865"><simpara><constant>CP865</constant>, a DOS oldie,
525
&ascii-pl-ext;, meant to be suitable for some Nordic languages.
526
</simpara></listitem>
530
&ascii-pl-ext;, &good-for; some Nordic languages.</simpara></listitem>
527
531
<listitem id="charset-CP865-IBM"><simpara><constant>CP865-IBM</constant>,
528
532
an IBM variant of <constant>CP865</constant>.
529
533
</simpara></listitem>
530
534
<listitem id="charset-CP866"><simpara><constant>CP866</constant>, a DOS oldie,
531
&ascii-pl-ext;, meant to be suitable for the Russian language.
532
</simpara></listitem>
535
&ascii-pl-ext;, &good-for; the Russian language.</simpara></listitem>
533
536
<listitem id="charset-CP869"><simpara><constant>CP869</constant>, a DOS oldie,
534
&ascii-pl-ext;, meant to be suitable for the Greek language.
535
</simpara></listitem>
537
&ascii-pl-ext;, &good-for; the Greek language.</simpara></listitem>
536
538
<listitem id="charset-CP869-IBM"><simpara><constant>CP869-IBM</constant>,
537
539
an IBM variant of <constant>CP869</constant>.
538
540
</simpara></listitem>
539
541
<listitem id="charset-CP874"><simpara><constant>CP874</constant>, a DOS oldie,
540
&ascii-pl-ext;, meant to be suitable for the Thai language.
541
</simpara></listitem>
542
&ascii-pl-ext;, &good-for; the Thai language.</simpara></listitem>
542
543
<listitem id="charset-CP874-IBM"><simpara><constant>CP874-IBM</constant>,
543
544
an IBM variant of <constant>CP874</constant>.
544
545
</simpara></listitem>
1969
2040
extension <filename>exe</filename>.</para>
1970
2041
<para>For historical reasons, all examples appear to assume &unix; and
1971
2042
use the <filename>run</filename> file type (<quote>extension</quote>)
1972
for the &clisp; run-time.
2043
for the &clisp; &rt;.
1973
2044
This does ¬-e; mean that they will not work on &win32;.</para>
1976
2047
<section id="mod-overview"><title>Overview</title>
1978
<para>&clisp; has a facility for adding external modules (written in
1979
&c-lang;, for example). It is invoked through &clisp-link;.</para>
2049
<para><emphasis>External modules</emphasis> are a mechanism to add
2050
extensions (written in &c-lang;, for example) to &clisp;.
2051
Extending &clisp; using an external module requires creating a &modset;
2052
and adding it to an existing &linkset; using &clisp-link; to prodice a
2053
new &linkset; which contains the extension. </para>
2055
<simplesect id="modset"><title>Module Set</title>
1981
2056
<para>A <firstterm>module<indexterm id="module" significance="preferred">
1982
<primary id="module-i">module</primary></indexterm></firstterm> is a
1983
piece of external code which defines extra Lisp objects, symbols and
1984
functions. A module <firstterm>name</firstterm> must consist of the
1985
characters <filename>A</filename>-<filename>Z</filename>,
2057
<primary>module</primary></indexterm></firstterm> is a piece of code
2058
(&c-lang; or Lisp) which defines extra (non-core) Lisp objects, symbols
2059
and functions. Together with &link-sh;, which describes how to add the
2060
module to an existing &clisp;, it comprises a &modset;.</para>
2061
<para>More formally,
2062
<firstterm>&modset;<indexterm id="modseti" significance="preferred">
2063
<primary>module set</primary></indexterm></firstterm>
2064
is a directory containing:
2065
<variablelist><varlistentry><term>&link-sh;</term>
2066
<listitem><simpara>some &sh; commands, which prepare the directory
2067
for linking, and set some &env-var;s, see <xref linkend="mod-vars"/>
2068
</simpara></listitem></varlistentry>
2069
<varlistentry><term>all other files that define the module functionality</term>
2070
<listitem><simpara>needed by &link-sh;</simpara></listitem></varlistentry>
2071
</variablelist></para>
2072
<para>In &link-sh; the &modset; directory is referred to
2073
as <varname>$modulename/</varname>.</para>
2074
<para>A module <firstterm>name<indexterm id="module-name">
2075
<primary>module</primary><secondary>name</secondary></indexterm></firstterm>
2076
must consist of the characters <filename>A</filename>-<filename>Z</filename>,
1986
2077
<filename>a</filename>-<filename>z</filename>, <filename>_</filename>,
1987
<filename>0</filename>-<filename>9</filename>.
1988
The module name <quote>clisp</quote> is reserved.
1989
Normally a module name is derived from the corresponding file name.</para>
1991
<para>&clisp-link; needs a directory containing:
1993
<listitem><simpara><filename>"modules.c"</filename></simpara></listitem>
1994
<listitem><simpara>&clisp-h;</simpara></listitem>
1996
&clisp-link; expects to find these files in a
1997
subdirectory <filename>linkkit/</filename> of the current directory.
1998
This can be overridden by the &env-var; <envar>CLISP_LINKKIT</envar>.</para>
2000
<para>&clisp-link; operates on &clisp; &linkset;s and on &modset;s.</para>
2002
<simplesect id="linkset"><title>&linkset;</title>
2003
<para>A <firstterm>&linkset;<indexterm id="linkseti" significance="preferred">
2004
<primary id="linkset-i">linking set</primary></indexterm></firstterm>
2005
is a directory containing:
2007
<varlistentry><term><filename>makevars</filename></term>
2008
<listitem><para>some &sh; commands, setting the variables
2009
<variablelist>&varlist-table;<varlistentry><term><envar>CC</envar></term>
2010
<listitem><simpara>the &c-lang; compiler
2011
</simpara></listitem></varlistentry>
2012
<varlistentry><term><envar>CPPFLAGS</envar></term>
2013
<listitem><simpara>flags for the &c-lang; compiler, when preprocessing
2014
or compiling</simpara></listitem></varlistentry>
2015
<varlistentry><term><envar>CFLAGS</envar></term>
2016
<listitem><simpara>flags for the &c-lang; compiler, when compiling or
2017
linking</simpara></listitem></varlistentry>
2018
<varlistentry><term><envar>CLFLAGS</envar></term>
2019
<listitem><simpara>flags for the &c-lang; compiler, when linking
2020
</simpara></listitem></varlistentry>
2021
<varlistentry><term><envar>LIBS</envar></term>
2022
<listitem><simpara>libraries to use when linking (either present in
2023
the &linkset; directory, or system-wide)
2024
</simpara></listitem></varlistentry>
2025
<varlistentry><term><envar>X_LIBS</envar></term>
2026
<listitem><simpara>additional &X; libraries to use
2027
</simpara></listitem></varlistentry>
2028
<varlistentry><term><envar>RANLIB</envar></term>
2029
<listitem><simpara>the ranlib command
2030
</simpara></listitem></varlistentry>
2031
<varlistentry><term><envar>FILES</envar></term>
2032
<listitem><simpara>the list of files needed when linking
2033
</simpara></listitem></varlistentry>
2034
</variablelist></para></listitem></varlistentry>
2078
<filename>0</filename>-<filename>9</filename>.</para>
2079
<para>The module name <quote>clisp</quote> is reserved.</para>
2082
<simplesect id="linkset"><title>Linking Set</title>
2083
<para>A <firstterm>&linkset;<indexterm id="linkseti" significance="preferred">
2084
<primary>linking set</primary></indexterm></firstterm>
2085
is a collection of files (&rt;, &mem-image; &c) which allows
2086
performing two major tasks:
2087
<orderedlist><listitem><para>Running &clisp;: to run a &clisp;
2088
contained in some &linkset; &dir-r;, call<screen>
2089
&sh-prompt; &dir-r;/lisp.run &opt-M; &dir-r;/lispinit.mem</screen>
2090
or <screen>&sh-prompt; clisp &opt-K; &dir-r;</screen>
2091
(recommended, since it also passes
2092
<option><olink targetdoc="man" targetptr="opt-libdir">-B</olink></option>
2093
to the &rt;).</para></listitem>
2094
<listitem><simpara>Adding a &modset; to create a new &linkset; which
2095
will contain the module functionality.</simpara></listitem></orderedlist>
2096
The &clisp; build directory contains three &linkset;s in
2097
directories <command>boot</command>, &base;, and &full;, and a &clisp;
2098
installation normally contains two &linkset;s: &base;, and &full;</para>
2099
<para>More formally, a &linkset; is a directory containing at least
2100
these files:<variablelist>
2101
<varlistentry><term>&lisp-run;</term>
2102
<listitem><simpara>the executable &rt;</simpara></listitem></varlistentry>
2103
<varlistentry><term>&lispinit;</term>
2104
<listitem><simpara>the &mem-image;</simpara></listitem></varlistentry>
2035
2105
<varlistentry><term><filename>modules.h</filename></term>
2036
2106
<listitem><simpara>the list of modules contained in this &linkset;
2037
2107
</simpara></listitem></varlistentry>
2038
2108
<varlistentry><term><filename>modules.o</filename></term>
2039
2109
<listitem><simpara>the compiled list of modules contained in this &linkset;
2040
2110
</simpara></listitem></varlistentry>
2041
<varlistentry><term>all the <filename>FILES</filename></term>
2111
<varlistentry><term><filename>makevars</filename></term>
2112
<listitem><para>some &sh; commands, setting the variables
2113
<variablelist>&varlist-table;<varlistentry><term><envar>CC</envar></term>
2114
<listitem><simpara>the &c-lang; compiler
2115
</simpara></listitem></varlistentry>
2116
<varlistentry><term><envar>CPPFLAGS</envar></term>
2117
<listitem><simpara>flags for the &c-lang; compiler, when preprocessing
2118
or compiling</simpara></listitem></varlistentry>
2119
<varlistentry><term><envar>CFLAGS</envar></term>
2120
<listitem><simpara>flags for the &c-lang; compiler, when compiling or
2121
linking</simpara></listitem></varlistentry>
2122
<varlistentry><term><envar>CLFLAGS</envar></term>
2123
<listitem><simpara>flags for the &c-lang; compiler, when linking
2124
</simpara></listitem></varlistentry>
2125
<varlistentry><term><envar>LIBS</envar></term>
2126
<listitem><simpara>libraries to use when linking (either present in
2127
the &linkset; directory, or system-wide)
2128
</simpara></listitem></varlistentry>
2129
<varlistentry><term><envar>X_LIBS</envar></term>
2130
<listitem><simpara>additional &X; libraries to use
2131
</simpara></listitem></varlistentry>
2132
<varlistentry><term><envar>RANLIB</envar></term>
2133
<listitem><simpara>the ranlib command
2134
</simpara></listitem></varlistentry>
2135
<varlistentry><term><envar>FILES</envar></term>
2136
<listitem><simpara>the list of files needed when linking
2137
</simpara></listitem></varlistentry>
2138
</variablelist></para></listitem></varlistentry>
2139
<varlistentry><term>all the <envar>FILES</envar></term>
2042
2140
<listitem><simpara>listed in <filename>makevars</filename>
2043
2141
</simpara></listitem></varlistentry>
2044
<varlistentry><term>&lisp-run;</term>
2045
<listitem><simpara>the executable</simpara></listitem></varlistentry>
2046
<varlistentry><term>&lispinit;</term>
2047
<listitem><simpara>the &mem-image;</simpara></listitem></varlistentry>
2048
</variablelist></para>
2050
<para>To run a &clisp; contained in some &linkset; &directory-r;, call<screen>
2051
&sh-prompt; &directory-r;/lisp.run &opt-M; &directory-r;/lispinit.mem</screen>
2052
or <screen>&sh-prompt; clisp &opt-K; &directory-r;</screen>
2053
(recommended, since it also passes
2054
<option><olink targetdoc="man" targetptr="opt-libdir">-B</olink></option>
2055
to the run-time).</para></simplesect>
2057
<simplesect id="modset"><title>&modset;</title>
2058
<para>A <firstterm>&modset;<indexterm id="modseti" significance="preferred">
2059
<primary id="modset-i">module set</primary></indexterm></firstterm>
2060
is a directory containing:
2062
<varlistentry><term>&link-sh;</term>
2063
<listitem><simpara>some &sh; commands, which prepare the directory
2064
before linking, and set the variables <envar>NEW_FILES</envar>,
2065
<envar>NEW_LIBS</envar>, <envar>NEW_MODULES</envar>, &mod-load;
2066
and optionally &mod-preload;</simpara></listitem></varlistentry>
2067
<varlistentry><term>and any other files</term>
2068
<listitem><simpara>needed by &link-sh;
2069
</simpara></listitem></varlistentry>
2070
</variablelist></para>
2072
<para>In &link-sh; the &modset; directory is referred to
2073
as <varname>$modulename/</varname>.</para></simplesect>
2142
</variablelist></para>
2145
<simplesect id="clisp-link"><title>Operating on &modset;s and &linkset;s</title>
2147
<para>Adding one or several &modset;s to a &linkset;s produces a new
2148
&linkset; which contains the functionality of those modules in addition
2149
to all the functionality present in the original &linkset;.
2150
This is done by the &clisp-link; shell script, installed in &libdir;,
2151
so that you can invoke it with
2152
<screen>&sh-prompt; `&clisp-cmd; &opt-b;`/&clisp-link;</screen></para>
2154
<variablelist><title>&clisp-link; commands</title>
2155
<varlistentry><term>create</term>
2156
<listitem><para>The command
2157
<screen>&sh-prompt; &clisp-link; create &mod-r; &file-r; ...</screen>
2158
creates a &modset; in &mod-r; directory which refers
2159
(via symbolic links) to files &file-r;...
2160
The files are expected to be modules of their own.
2161
</para></listitem></varlistentry>
2162
<varlistentry><term>add</term>
2163
<listitem><para>The command
2164
<screen>&sh-prompt; &clisp-link; add &source-r; &dest-r; &mod-r; ...</screen>
2165
combines the &linkset; in directory &source-r; and the &module;s in
2166
directories &mod-r;... to a new &linkset;, in the directory &dest-r;
2167
which is newly created.</para></listitem></varlistentry>
2168
<varlistentry><term>run</term>
2169
<listitem><para>The command
2170
<screen>&sh-prompt; &clisp-link; run &source-r; &mod-r; ...</screen>
2171
runs the &linkset; in directory &source-r;, with the &module;s
2172
in directories &mod-r;...
2173
If &clisp; has been built with the configuration option &with-dynmod;,
2174
the loading will be performed <link linkend="mod-dynload">dynamically</link>.
2175
Otherwise - this is much slower - a temporary &linkset; will be created
2176
and deleted afterwards.</para></listitem></varlistentry></variablelist>
2178
<formalpara id="clisp-link-kit"><title>Requirements</title>
2179
<para>&clisp-link; needs a <quote>link kit</quote> directory containing:
2181
<listitem><simpara><filename>"modules.c"</filename></simpara></listitem>
2182
<listitem><simpara>&clisp-h;</simpara></listitem>
2183
</itemizedlist> &clisp-link; expects to find these files in a
2184
subdirectory <filename>linkkit/</filename> of the current directory.
2185
or in its own installation directory (i.e., &libdir;).
2186
This can be overridden by the &env-var; <envar>CLISP_LINKKIT</envar>.
2187
</para></formalpara>
2189
<para>See also <xref linkend="mod-set-example"/>.</para>
2075
2192
<simplesect id="mod-vars"><title>Module set variables</title>
2076
2193
<para>The following variables should be defined in &link-sh;.</para>
2078
2195
<varlistentry><term><envar>NEW_FILES</envar></term>
2079
<listitem><simpara>the space-separated list of files that
2080
belong to the &modset; and will belong to every new &linkset;.
2081
</simpara></listitem></varlistentry>
2196
<listitem><simpara>the space-separated list of object files that
2197
belong to the &modset; and will belong to every new &linkset; linked
2198
with this &modset;.</simpara></listitem></varlistentry>
2082
2199
<varlistentry><term><envar>NEW_LIBS</envar></term>
2083
<listitem><simpara>the space-separated list of files or
2200
<listitem><simpara>the space-separated list of object files, libraries or
2084
2201
&c-lang; compiler switches that need to be passed to the &c-lang;
2085
2202
compiler when linking the &lisp-run; belonging to a new &linkset;.
2086
2203
</simpara></listitem></varlistentry>
2087
2204
<varlistentry><term><envar>NEW_MODULES</envar></term>
2088
2205
<listitem><simpara>the space-separated list of the module names
2089
2206
belonging to the &modset;. Normally, every &c-file; file in the
2090
&modset; defines a module of its own. The module name is derived
2091
from the file name.</simpara></listitem></varlistentry>
2207
&modset; defines a module of its own. The module name is usually
2208
derived from the file name.</simpara></listitem></varlistentry>
2092
2209
<varlistentry id="mod-load"><term>&mod-load;</term>
2093
2210
<listitem><simpara>the space-separated list of Lisp files to load
2094
2211
before building the &lispinit; belonging to a new &linkset;.
3358
3478
a string, without the trailing zero character.
3359
3479
</simpara></listitem></varlistentry>
3361
<varlistentry id="c-struct"><term><literal role="type">(&c-struct;
3362
&class-r; (<replaceable>ident&sub-1;</replaceable>
3363
<replaceable>c-type&sub-1;</replaceable>) ...
3364
(<replaceable>ident&sub-n;</replaceable>
3365
<replaceable>c-type&sub-n;</replaceable>))</literal></term>
3481
<varlistentry id="c-struct"><term><literal role="type">(&c-struct; &class-r;
3482
(&ident1-r; &ctype1-r;) ... (&identn-r; &ctypen-r;))</literal></term>
3366
3483
<listitem><simpara>This type is equivalent to what &c-lang; calls
3367
<type>struct { <replaceable>c-type&sub-1;</replaceable>
3368
<replaceable>ident&sub-1;</replaceable>; ...;
3369
<replaceable>c-type&sub-n;</replaceable>
3370
<replaceable>ident&sub-n;</replaceable>; }</type>.
3371
Its Lisp equivalent is: if &class-r; is &vector-t;, a
3372
&simple-vector-t;; if &class-r; is &list-t;, a &proper-list-glo;;
3373
if &class-r; is a symbol naming a structure or &clos; class, an
3374
instance of this class, with slots of names
3375
<replaceable>ident&sub-1;</replaceable>, ...,
3376
<replaceable>ident&sub-n;</replaceable>.</simpara><simpara>
3377
&class-r; may also be a &cons-t; of a &symbol-t; (as above) and
3378
a &list-t; of &def-c-struct; options.
3379
</simpara></listitem></varlistentry>
3484
<type>struct { &ctype1-r; &ident1-r;; ...; &ctypen-r; &identn-r;; }</type>.
3485
Its Lisp equivalent is: if &class-r; is &vector-t;, a
3486
&simple-vector-t;; if &class-r; is &list-t;, a &proper-list-glo;;
3487
if &class-r; is a symbol naming a structure or &clos; class, an
3488
instance of this class, with slots of names
3489
&ident1-r;, ..., &identn-r;.</simpara><simpara>
3490
&class-r; may also be a &cons-t; of a &symbol-t; (as above) and
3491
a &list-t; of &def-c-struct; options.
3492
</simpara></listitem></varlistentry>
3381
3494
<varlistentry id="c-union"><term><literal role="type">(&c-union;
3382
(<replaceable>ident&sub-1;</replaceable>
3383
<replaceable>c-type&sub-1;</replaceable>) ...
3384
(<replaceable>ident&sub-n;</replaceable>
3385
<replaceable>c-type&sub-n;</replaceable>))</literal></term>
3495
(&ident1-r; &ctype1-r;) ... (&identn-r; &ctypen-r;))</literal></term>
3386
3496
<listitem><simpara>This type is equivalent to what &c-lang; calls
3387
<type>union { <replaceable>c-type&sub-1;</replaceable>
3388
<replaceable>ident&sub-1;</replaceable>; ...;
3389
<replaceable>c-type&sub-n;</replaceable>
3390
<replaceable>ident&sub-n;</replaceable>;
3392
Conversion to and from Lisp assumes that a value is to be viewed as
3393
being of <replaceable>c-type&sub-1;</replaceable>.
3394
</simpara></listitem></varlistentry>
3497
<type>union { &ctype1-r; &ident1-r;; ...; &ctypen-r; &identn-r;; }</type>.
3498
Conversion to and from Lisp assumes that a value is to be viewed as
3499
being of &ctype1-r;.
3500
</simpara></listitem></varlistentry>
3396
3502
<varlistentry id="c-array"><term><literal role="type">(&c-array;
3397
&ctype-r; <replaceable>dim&sub-1;</replaceable>)</literal></term>
3398
<term><literal role="type">(&c-array;
3399
&ctype-r; (<replaceable>dim&sub-1;</replaceable> ...
3400
<replaceable>dim&sub-n;</replaceable>))</literal></term>
3503
&ctype-r; &dim1-r;)</literal></term>
3504
<term><literal role="type">(&c-array; &ctype-r; (&dim1-r;
3505
... &dimn-r;))</literal></term>
3401
3506
<listitem><simpara>This type is equivalent to what &c-lang; calls
3402
<type>&ctype-r; [<replaceable>dim&sub-1;</replaceable>]
3403
... [<replaceable>dim&sub-n;</replaceable>]</type>.
3404
Note that when an array is passed as an argument to a function in
3405
&c-lang;, it is actually passed as a pointer; you therefore have to
3406
write <literal role="type">(&c-ptr; (&c-array; ...))</literal> for this
3407
argument's type.</simpara></listitem></varlistentry>
3507
<type>&ctype-r; [&dim1-r;] ... [&dimn-r;]</type>.
3508
Note that when an array is passed as an argument to a function in
3509
&c-lang;, it is actually passed as a pointer; you therefore have to
3510
write <literal role="type">(&c-ptr; (&c-array; ...))</literal> for this
3511
argument's type.</simpara></listitem></varlistentry>
3409
3513
<varlistentry id="c-array-max"><term><literal role="type">(&c-array-max;
3410
&ctype-r; &maxdim-r;)</literal></term>
3514
&ctype-r; &maxdim-r;)</literal></term>
3411
3515
<listitem><simpara>This type is equivalent to what &c-lang; calls
3412
<type>&ctype-r; [&maxdim-r;]</type>, an array containing up to
3413
&maxdim-r; elements.
3414
The array is zero-terminated if it contains less than &maxdim-r; elements.
3415
Conversion from Lisp of an array with more than &maxdim-r; elements
3416
silently ignores the superfluous elements.
3417
</simpara></listitem></varlistentry>
3516
<type>&ctype-r; [&maxdim-r;]</type>, an array containing up to
3517
&maxdim-r; elements.
3518
The array is zero-terminated if it contains less than &maxdim-r; elements.
3519
Conversion from Lisp of an array with more than &maxdim-r; elements
3520
silently ignores the extra elements.
3521
</simpara></listitem></varlistentry>
3419
3523
<varlistentry id="c-function"><term><literal
3420
3524
role="type">(&c-function; (&arguments-k;
4021
4138
<example id="dffi-gethostname"><title>Variable size arguments:
4022
4139
calling <function role="unix">gethostname</function> from &clisp;</title>
4024
<para><funcsynopsis id="gethostname-synopsis"><funcprototype>
4141
<para>The standard &unix; function
4142
<funcsynopsis id="gethostname-synopsis"><funcprototype>
4025
4143
<funcdef><type>int</type> <function>gethostname</function></funcdef>
4026
4144
<paramdef><type>char*</type> <parameter>name</parameter></paramdef>
4027
<paramdef><type>size_t</type> <parameter>namelen</parameter></paramdef>
4145
<paramdef><type>size_t</type> <parameter>length</parameter></paramdef>
4028
4146
</funcprototype></funcsynopsis>
4029
follows a typical pattern of &c-lang; "out"-parameter convention - it
4030
expects a pointer to a buffer it is going to fill.
4147
follows a typical pattern of &c-lang; <quote>out</quote>-parameter
4148
convention: it expects a pointer to a buffer it is going to fill.
4031
4149
So you must view this parameter as either &out-k; or &in-out-k;.
4032
4150
Additionally, one must tell the function the size of the buffer.
4033
Here <replaceable>namelen</replaceable> is just an &in-k; parameter.
4151
Here &len-r; is just an &in-k; parameter.
4034
4152
Sometimes this will be an &in-out-k; parameter, returning the
4035
4153
number of bytes actually filled in.</para>
4037
<para>So &name-r; is actually a pointer to an array of up to
4038
<replaceable>namelen</replaceable> characters, regardless of what the
4039
poor <type>char*</type> &c-lang; prototype says, to be used like a
4040
&c-lang; <type>string</type> (&c-NULL;-termination). &unix; specifies
4041
that <quote>host names are limited to
4042
<constant>HOST_NAME_MAX</constant> bytes</quote>, which is, of course,
4043
system dependent, but it appears that 256 is sufficient.</para>
4155
<para>So &name-r; is actually a pointer to an array of up to &len-r;
4156
characters, regardless of what the poor <type>char*</type> &c-lang;
4157
prototype says, to be used like a &c-lang; <type>string</type>
4158
(&c-NULL;-termination). &unix; specifies that <quote>host names are
4159
limited to <constant>HOST_NAME_MAX</constant> bytes</quote>, which is,
4160
of course, system dependent, but it appears that 256 is sufficient.</para>
4045
4162
<para>In the present example, you can use allocation &alloca-k;, like
4046
you would do in &c-lang;: stack-allocate a temporary.</para>
4163
you would do in &c-lang;: stack-allocate a temporary:
4048
4164
<programlisting language="lisp">
4049
4165
(&def-call-out; gethostname
4050
(:arguments (name (&c-ptr; (&c-array-max; ffi:char 256))
4166
(&arguments-k; (name (&c-ptr; (&c-array-max; ffi:char 256))
4051
4167
&out-k; &alloca-k;)
4054
(:return-type ffi:int))
4169
(&lang-k; :stdc) (&library-k; :default)
4170
(&ret-type-k; ffi:int))
4171
<computeroutput>GETHOSTNAME</computeroutput>
4056
4172
(defun myhostname ()
4057
(multiple-value-bind (success name)
4058
;; &out-k; and &in-out-k; parameters are returned as &mul-val;
4173
(multiple-value-bind (success name)<lineannotation>&out-k; and &in-out-k; parameters are returned as &mul-val;</lineannotation>
4059
4174
(gethostname 256)
4060
4175
(if (zerop success) name
4061
(error ...)))) ;; <function role="unix">strerror</function>(<varname role="unix">errno</varname>)
4063
(defvar hostname (myhostname))
4176
(error "~S: ~S: ~S" 'myhostname (os:errno) (os:strerror)))))<lineannotation>See <xref linkend="errno"/></lineannotation>
4177
<computeroutput>MYHOSTNAME</computeroutput>
4179
<computeroutput>#(97 98 97 122 111 110 107)</computeroutput></programlisting>
4180
It is a &simple-vector-t;, not a &string-t;, because the &name-r;
4181
argument is an array of <type>char</type> (an &integer-t; type, see
4182
<xref linkend="dffi-types"/>), not <type>character</type>.
4183
<programlisting language="lisp">
4184
(&def-call-out; gethostname
4185
(&arguments-k; (name (&c-ptr; (&c-array-max; character 256))
4188
(&lang-k; :stdc) (&library-k; :default)
4189
(&ret-type-k; ffi:int))
4190
<computeroutput>GETHOSTNAME</computeroutput>
4192
<computeroutput>"abazonk"</computeroutput></programlisting>
4193
Now we have a different problem:
4194
if <function role="unix">gethostname</function> fails, then the buffer
4195
allocated for &name-r; will be filled with garbage, but it will still go
4196
through the string conversion &before-e; we can check
4197
the <replaceable>success</replaceable> status.
4198
If &foreign-enc; is &iso-8859-1;, this is not a problem since no real
4199
conversion is happening, but with &utf-8; an &error-t; may be &signal;ed.
4200
A safe approach is to pass to the foreign function our own
4201
stack-allocated buffer, and only convert the buffer to a string when the
4202
foreign function succeeds: <programlisting language="lisp">
4203
(&def-call-out; gethostname
4204
(&arguments-k; (name &c-pointer;)
4206
(&lang-k; :stdc) (&library-k; :default)
4207
(&ret-type-k; ffi:int))
4208
<computeroutput>GETHOSTNAME</computeroutput>
4209
(defun myhostname ()
4210
(&with-foreign-object; (name '(&c-array-max; character 256))
4211
(let ((success (gethostname name 256)))
4212
(if (zerop success) (&foreign-value; name)
4213
(error "~S: ~S: ~S" 'myhostname (os:errno) (os:strerror))))))
4214
<computeroutput>MYHOSTNAME</computeroutput>
4216
<computeroutput>"abazonk"</computeroutput></programlisting>
4217
Note that the &type-r; argument of &with-foreign-object; is evaluated,
4218
so we do not have to make any assumptions
4219
about <constant>HOST_NAME_MAX</constant>: <programlisting language="lisp">
4220
(defun myhostname ()
4221
(let ((host-name-max (<link linkend="sysinfo">os:sysconf</link> :host-name-max)))
4222
(&with-foreign-object; (name `(&c-array-max; character ,host-name-max))
4223
(let ((success (gethostname name host-name-max)))
4224
(if (zerop success) (&foreign-value; name)
4225
(error "~S: ~S: ~S" 'myhostname (os:errno) (os:strerror)))))))
4226
<computeroutput>MYHOSTNAME</computeroutput>
4228
<computeroutput>"abazonk"</computeroutput></programlisting>
4067
4231
<example id="dffi-dll-var">
4068
4232
<title>Accessing variables in shared libraries</title>
4520
4684
<!-- #endif -->
4687
<section id="mt"><title>Multiple Threads of Execution</title>
4688
<subtitle><emphasis role="plat-dep">Only in &clisp; built &with;
4689
compile-time flag <option>--with-threads</option>.</emphasis></subtitle>
4691
<warning><simpara>This functionality is
4692
<emphasis role="strong">experimental</emphasis>.</simpara>
4693
<simpara>Use it at your own risk.</simpara>
4694
<simpara>Discuss it on <ulink url="ml">clisp-devel</ulink>.</simpara></warning>
4696
<section id="mt-intro"><title>Introduction</title>
4697
<para>&clisp; uses the OS threads to implement multiple threads of execution.
4698
Two flavors are supported: &posix; and &win32;. Both are preemptive.</para>
4699
<para>All symbols are exported from the package &mt-pac;, which has nicknames
4700
<quote role="package">MT</quote> (for <emphasis>MultiThreading</emphasis>) and
4701
<quote role="package">MP</quote> (for <emphasis>MultiProcessing</emphasis>).
4702
When this functionality is present, &features-my;
4703
contains the symbol <constant>:MT</constant>.</para>
4704
<para>See also <xref linkend="gc-mt"/>.</para></section>
4706
<section id="mt-general"><title>General principles</title>
4708
<section id="mt-parallelizability"><title>Parallelizability</title>
4710
<para>A program developed for a single-threaded world which shares no
4711
application objects with programs running in other threads must run
4712
fine, without problems.</para>
4715
if, in a single-threaded world, execution of program A before program B
4716
produces semantically the same results as execution of program B before
4717
program A, then in a multithreaded world, it is possible to run A and B
4718
simultaneously in different threads, and the result will be the same as
4719
in the two single-threaded cases (A before B, or B before A).
4720
<itemizedlist><title>Summary</title>
4721
<listitem><simpara>If A and B have no common objects, then the
4722
implementation ensures that the principle is fulfilled.</simpara></listitem>
4723
<listitem><simpara>If A and B share some objects, the implementation
4724
allows the programs to satisfy the principle with little effort.
4725
</simpara></listitem></itemizedlist></para></section>
4727
<section id="mt-symvalue"><title>Special Variable Values</title>
4728
<para>Every &dyn-var; has a global value that can be shared across all
4730
<para>Bindings of &dyn-var;s (via &let;/&let-star;/&multiple-value-bind;)
4731
are local to &thread;s, i.e. every &symbol-t; has a different value cell in
4732
each &thread;. &symbol-value-thread; can be used to inspect and modify
4733
these thread local bindings.</para>
4734
<para>Threads do not inherit dynamic bindings from the parent thread.</para>
4736
<para>Example:<programlisting language="lisp">
4737
(defvar *global* 1)<lineannotation>create a &glob-var;</lineannotation>
4739
<lineannotation>here *global* and (&symbol-value; *global*) will be 1 not 2!</lineannotation>
4740
(setq *global* 5)<lineannotation>change the &glob-var; value</lineannotation>
4741
(let ((*global* 10))<lineannotation>&thr-var; value is initialized</lineannotation>
4742
(setq *global* 20)<lineannotation>&thr-var; value is changed</lineannotation>
4743
<lineannotation>&glob-var; value is not accessible here (only via &symbol-value-thread;)</lineannotation>
4745
(setq *global* 30))<lineannotation>&glob-var; value is modified again</lineannotation>
4746
(let ((*global* 2))<lineannotation>&thr-var; value is initialized</lineannotation>
4747
(&make-thread; #'thread-1))
4748
</programlisting></para>
4752
<section id="mt-package"><title>Packages</title>
4753
<subtitle>This information is likely to change in the near future</subtitle>
4754
<para>&package-t; objects have an internal mutex and are locked by
4755
&intern; before adding a symbol (if &find-symbol; fails). All
4756
modifications of internal package data are guarded by this mutex.</para>
4757
<warning><para>Currently there is one exception: &use-package; is not
4758
protected and thus not thread safe.</para></warning>
4759
<warning><para>Symbol look-up within package currently is not guarded!
4760
This means that in certain cases &find-symbol; may return &nil; even
4761
though the symbol is accessible (this may happen when internal package
4762
hash tables are re-hashed at the time &find-symbol; performs look-up).
4764
<para>One crucial implication is that one <emphasis>cannot</emphasis>
4765
call &load; from different threads at the same time.</para>
4766
<para>See also <xref linkend="mt-clos"/>.</para></section>
4768
<section id="mt-clos"><title>&clos;</title>
4769
<subtitle>This information is likely to change in the near future</subtitle>
4770
<para>&clos; is ¬-e; thread-safe. &defclass;, &defgeneric;,
4771
&defmethod;, &defstruct; modify &clos; without any locking and may
4772
interfere with each other.</para>
4773
<para>It is recommended that all code is &load;ed &before-e;
4774
any &thread;s are spawned.</para>
4775
<para>See also <xref linkend="mt-package"/>.</para></section>
4777
<section id="mt-mutable"><title>Hash Tables, Sequences, and other
4778
mutable objects</title>
4779
<subtitle>If you want to shoot yourself, it is <emphasis>your</emphasis>
4780
responsibility to wear armor.</subtitle>
4782
<para>Nothing is ever locked automatically (automatic locking will
4783
impose an unjustifiable penalty on &hash-table-t;s and &sequence-t;s
4784
local to threads), so the user must use locks when sharing
4785
&hash-table-t;s, &sequence-t;s and user-defined mutable objects between
4787
<para>This approach is consistent with the usual &cl; approach:
4788
<blockquote><attribution><ulink role="clhs" url="sec_3-6"/></attribution>
4789
<para>The consequences are undefined when code executed during an
4790
object-traversing operation destructively modifies the object in a
4791
way that might affect the ongoing traversal operation...
4792
</para></blockquote>
4793
<blockquote><attribution><ulink role="clhs" url="sec_18-1-2"/></attribution>
4794
<para>If an object O1 is used as a key in a hash table H and is then
4795
visibly modified with regard to the equivalence test of H, then the
4796
consequences are unspecified if O1 is used as a key in further
4797
operations on H...</para></blockquote></para>
4800
<section id="mt-unsafe"><title>Examples of thread-unsafe code</title>
4801
<para>Here are some forms whose results are undefined if two threads
4802
evaluate them without locking:<programlisting language="lisp">
4803
(&incf; (&gethash; x global-ht 0))<lineannotation>see <xref linkend="mt-mutable"/></lineannotation>
4804
(&setf; (&aref; global-array ...) ...)<lineannotation>ditto</lineannotation>
4805
(&defmethod; &gf-r; (...) ...)<lineannotation>see <xref linkend="mt-clos"/></lineannotation>
4806
(&load; &filename-r;)<lineannotation>see <xref linkend="mt-package"/></lineannotation>
4807
(&read-from-string; &string-r;)<lineannotation>ditto</lineannotation>
4808
</programlisting></para></section>
4812
<section id="mt-api"><title>Thread API reference</title>
4814
<varlistentry id="thread"><term>&thread;</term>
4815
<listitem><simpara>The type of the object returned by &make-thread;.</simpara>
4816
<simpara>Each &thread; represent a separate computation, executed in
4817
parallel to each other.</simpara></listitem></varlistentry>
4818
<varlistentry id="make-thread"><term><code>(&make-thread; &func-r;
4819
&key-amp; &name-k; :INITIAL-BINDINGS :CSTACK-SIZE :VSTACK-SIZE)</code></term>
4820
<listitem><simpara>Start a new named &thread; running &func-r;.</simpara>
4822
<varlistentry><term><constant>:INITIAL-BINDINGS</constant></term>
4823
<listitem><simpara>an &alist; of <literal role="data">(&symbol-r;
4824
. &form-r;)</literal>.
4825
The &form-r;s are &eval;uated in the context of the new thread
4826
and &symbol-r;s are bound to the result in the thread before &func-r; is
4827
called. The default value is &default-special-bindings;.
4828
</simpara></listitem></varlistentry>
4829
<varlistentry><term><constant>:CSTACK-SIZE</constant></term>
4830
<listitem><simpara>the size in bytes of the control (&c-lang;) stack.
4831
If 0, the value is decided by the OS.</simpara></listitem></varlistentry>
4832
<varlistentry><term><constant>:VSTACK-SIZE</constant></term>
4833
<listitem><simpara>the size of the &clisp; &STACK; in &object-t;s.
4834
The default value is calculated based on the &opt-m; option
4835
used when &clisp; was started.
4836
If 0, the value will be the same as that of the calling thread.
4837
</simpara></listitem></varlistentry></variablelist>
4838
<simpara>If &thread; creation fails (e.g., due to a lack of system
4839
memory), a &control-error-t; is &signal;ed.</simpara>
4840
<simpara>Cf. <function role="unix">pthread_create</function>.</simpara>
4841
</listitem></varlistentry>
4842
<varlistentry id="threadp"><term><code>(&threadp; &object-r;)</code></term>
4843
<listitem><simpara>Check whether the object is of type &thread;.
4844
</simpara></listitem></varlistentry>
4845
<varlistentry id="thread-yield"><term><code>(&thread-yield; &thr;)</code></term>
4846
<listitem><simpara>Relinquish the CPU. The &thr; is placed at the end
4847
of the run queue and another thread is scheduled to run.</simpara>
4848
<simpara>Cf. <function role="unix">sched_yield</function>.</simpara>
4849
</listitem></varlistentry>
4850
<varlistentry id="thread-interrupt"><term><code>(&thread-interrupt; &thr;
4851
&key-amp; &function-k; &override-k; &arguments-k;)</code></term>
4852
<listitem><simpara>Interrupt the normal execution flow in &thr; and ask
4853
it to &apply; &func-r; to &args-r;.</simpara>
4854
<simpara>Use <code>(&thread-interrupt; &thr; &function-k; &nil;)</code>
4855
to debug &thr; and <code>(&thread-interrupt; &thr; &function-k;
4856
&t;)</code> to terminate &thr;.</simpara>
4857
<simpara>The &override-k; argument overrides &with-deferred-interrupts;
4858
and should be used with extreme care.</simpara>
4859
<simpara>Threads can only be interrupted at a point where the &gc;ion
4860
can run, see <xref linkend="gc-mt"/>.</simpara>
4861
<simpara>Currently on &win32; blocking I/O cannot be interrupted.
4862
The interrupt will be handled after the call returns.</simpara>
4863
<warning><para>Thread may be interrupted inside &unwind-protect;'s
4864
cleanup forms and on non-local exit from &func-r; -
4865
they may not execute entirely. In order to prevent this,
4866
&with-deferred-interrupts; is provided.</para></warning>
4867
</listitem></varlistentry>
4868
<varlistentry id="thread-name"><term><code>(&thread-name; &thr;)</code></term>
4869
<listitem><simpara>Return the &name-r; of the &thr;.
4870
</simpara></listitem></varlistentry>
4871
<varlistentry id="thread-active-p"><term><code>(&thread-active-p;
4872
&thr;)</code></term>
4873
<listitem><simpara>Return &nil; if the thread has already terminated
4874
and &t; otherwise.</simpara>
4875
<warning><para>By the time this function returns &t;, &thr; may have
4876
already terminated anyway.</para></warning></listitem></varlistentry>
4877
<varlistentry id="current-thread"><term><code>(¤t-thread;)</code></term>
4878
<listitem><simpara>Return the &thread; object encapsulating the caller.
4879
</simpara></listitem></varlistentry>
4880
<varlistentry id="list-threads"><term><code>(&list-threads;)</code></term>
4881
<listitem><simpara>Return the &list-t; of all currently running &thread;s.
4882
</simpara><warning><para>By the time this function returns, the set of
4883
actually running threads may have a single intersection with the
4884
return value - the ¤t-thread;.
4885
</para></warning></listitem></varlistentry>
4886
<varlistentry id="mutex"><term>&mutex;</term>
4887
<listitem><simpara>The type of the object return by &make-mutex;.</simpara>
4888
<simpara>This represents a <emphasis>lock</emphasis>, i.e., a way to
4889
prevent different threads from doing something at the same time,
4890
e.g., modifying the same object.</simpara></listitem></varlistentry>
4891
<varlistentry id="mutexp"><term><code>(&mutexp; &object-r;)</code></term>
4892
<listitem><simpara>Check whether the object is of type &mutex;.
4893
</simpara></listitem></varlistentry>
4894
<varlistentry id="make-mutex"><term><code>(&make-mutex; &key-amp;
4895
&name-k; <constant>:RECURSIVE-P</constant>)</code></term>
4896
<listitem><simpara>Create new &mutex; object - not locked by any thread.
4897
&name-k; should be a &string-t; describing the mutex (this really helps
4898
debugging deadlocks). When <replaceable>RECURSIVE-P</replaceable> is
4899
non-&nil;, a recursive &mutex; is created i.e., a thread can acquire
4900
the mutex repeatedly (and should, of course, release it for each
4901
successful acquisition).</simpara>
4902
<simpara>Cf. <function role="unix">pthread_mutex_init</function>.</simpara>
4903
</listitem></varlistentry>
4904
<varlistentry id="mutex-name"><term><code>(&mutex-name; &thr;)</code></term>
4905
<listitem><simpara>Return the &name-r; of the &mutex;.
4906
</simpara></listitem></varlistentry>
4907
<varlistentry id="mutex-lock"><term><code>(&mutex-lock; &mutex-r;
4908
&key-amp; &timeout-k;)</code></term>
4909
<listitem><simpara>Acquire the &mutex-r;. If &mutex-r; is locked by
4910
another thread, the call blocks and waits up to &timeout-k; &seconds;.
4911
If &timeout-k; is not specified, waits forever.</simpara>
4912
<simpara>Return &t; on a successful locking of &mutex-r;, and &nil; on
4914
<para>If the calling thread has already acquired &mutex-r;, then
4915
<itemizedlist><listitem><simpara>if &mutex-r; is recursive, &t; is
4916
returned (for each recursive &mutex-lock; there should be a
4917
separate &mutex-unlock;);</simpara></listitem>
4918
<listitem><simpara>If &mutex-r; is non-recursive an &err-sig; to
4919
avoid a deadlock.</simpara></listitem></itemizedlist></para>
4920
<simpara>Cf. <function role="unix">pthread_mutex_lock</function>.</simpara>
4921
</listitem></varlistentry>
4922
<varlistentry id="mutex-unlock"><term><code>(&mutex-unlock;
4923
&mutex-r;)</code></term>
4924
<listitem><simpara>Release (unlock) &mutex-r;. If the calling thread is
4925
not locking &mutex-r;, an &err-sig;.</simpara>
4926
<simpara>Cf. <function role="unix">pthread_mutex_unlock</function>.</simpara>
4927
</listitem></varlistentry>
4928
<varlistentry id="mutex-owner"><term><code>(&mutex-owner;
4929
&mutex-r;)</code></term>
4930
<listitem><simpara>Return the &thread; that owns (locks) &mutex-r;,
4931
or &nil; if &mutex-r; is not locked.</simpara>
4932
<warning><para>By the time this function returns the &mutex-r;
4933
ownership may have changed (unless the owner is the
4934
¤t-thread;). The function is mostly useful for debugging
4935
deadlocks.</para></warning></listitem></varlistentry>
4936
<varlistentry id="mutex-recursive-p"><term><code>(&mutex-recursive-p;
4937
&mutex-r;)</code></term>
4938
<listitem><simpara>Return a indicator whether &mutex-r; is recursive.
4939
</simpara></listitem></varlistentry>
4940
<varlistentry id="with-lock"><term><code>(&with-lock; (&mutex-r;)
4941
&body-amp; &body-r;)</code></term>
4942
<listitem><simpara>Execute &body-r; with &mutex-r; locked.
4943
Upon exit &mutex-r; is released. Return whatever &body-r; returns.
4944
</simpara></listitem></varlistentry>
4945
<varlistentry id="exemption"><term>&exemption;</term>
4946
<listitem><simpara>The type of the object returned by &make-exemption;.
4947
These correspond to the &posix; condition variables,
4948
see <filename role="unix">pthread.h</filename>.</simpara>
4949
<simpara>These objects allow broadcasting state from one &thread; to
4950
the others.</simpara></listitem></varlistentry>
4951
<varlistentry id="exemptionp"><term><code>(&exemptionp;
4952
&object-r;)</code></term>
4953
<listitem><simpara>Check whether the object is of type &exemption;.
4954
</simpara></listitem></varlistentry>
4955
<varlistentry id="make-exemption"><term><code>(&make-exemption;
4956
&key-amp; &name-k;)</code></term>
4957
<listitem><simpara>Create a new &exemption; object.
4958
&name-k; should be a &string-t; describing the exemption (this really
4959
helps debugging deadlocks).</simpara>
4960
<simpara>Cf. <function role="unix">pthread_cond_init</function>.</simpara>
4961
</listitem></varlistentry>
4962
<varlistentry id="exemption-name"><term><code>(&exemption-name;
4963
&thr;)</code></term>
4964
<listitem><simpara>Return the &name-r; of the &exemp-r;.
4965
</simpara></listitem></varlistentry>
4966
<varlistentry id="exemption-signal"><term><code>(&exemption-signal;
4967
&exemp-r;)</code></term>
4968
<listitem><simpara>Signal &exemp-r; object, i.e. wake up a thread blocked
4969
on waiting for &exemp-r;.</simpara>
4970
<simpara>Cf. <function role="unix">pthread_cond_signal</function>.</simpara>
4971
</listitem></varlistentry>
4972
<varlistentry id="exemption-wait"><term><code>(&exemption-wait;
4973
&exemp-r; &mutex-r; &key-amp; &timeout-k;)</code></term>
4974
<listitem><simpara>Wait for another &thread; to call &exemption-signal;
4975
or &exemption-broadcast; on &exemp-r;.
4976
&mutex-r; should be locked by the caller; otherwise an &err-sig;.
4977
The function releases the &mutex-r; and waits for &exemp-r;.
4978
On return &mutex-r; is acquired again.</simpara>
4979
<simpara>The function waits up to &timeout-k; &seconds;.
4980
If timeout is not specified, waits forever.</simpara><simpara>
4981
Returns &t; if &exemp-r; was signaled and &nil; on timeout.</simpara>
4982
<para>On &posix; it is possible to have
4983
<ulink url="http://en.wikipedia.org/wiki/Spurious_wakeup">spurious
4984
wakeup</ulink>s, i.e., this function may return &t; even though no
4985
thread called &exemption-broadcast; or &exemption-signal;.
4986
Therefore, a common idiom for using this function is <code>(&loop;
4987
:while (some-condition-is-satisfied) :do (&exemption-wait; &exemp-r;
4988
&mutex-r;))</code>.</para>
4989
<simpara>Cf. <function role="unix">pthread_cond_wait</function>.</simpara>
4990
</listitem></varlistentry>
4991
<varlistentry id="exemption-broadcast"><term><code>(&exemption-broadcast;
4992
&exemp-r;)</code></term>
4993
<listitem><simpara>Signal &exemp-r; to all threads waiting for it.</simpara>
4994
<simpara>Cf. <function role="unix">pthread_cond_broadcast</function>.
4995
</simpara></listitem></varlistentry>
4996
<varlistentry id="y-or-n-p-timeout"><term><code>(&y-or-n-p-timeout;
4997
seconds default &rest-amp; &args-r;)</code></term>
4998
<term><code>(&yes-or-no-p-timeout; seconds default &rest-amp;
4999
&args-r;)</code></term>
5000
<listitem><simpara>Similar to &y-or-n-p; and &yes-or-no-p;, but use
5001
&with-timeout; to return <replaceable>DEFAULT</replaceable> when no
5002
reply is given within timeout &seconds;.</simpara></listitem></varlistentry>
5003
<varlistentry id="with-timeout"><term><code>(&with-timeout;
5004
(seconds &body-amp; timeout-forms) &body-amp; &body-r;)</code></term>
5005
<listitem><simpara>Execute &body-r;. If it does not finish for up to
5006
&seconds;, it is interrupted and <replaceable>timeout-forms</replaceable>
5007
are executed.</simpara>
5008
<simpara>Return the values of the last evaluated form in either
5009
&body-r; or <replaceable>timeout-forms</replaceable>.</simpara>
5010
<warning><para>Since on timeout the current thread is interrupted,
5011
special care may be needed for ensuring proper cleanup in &body-r;.
5012
See &thread-interrupt; and &with-deferred-interrupts;.</para></warning>
5013
</listitem></varlistentry>
5014
<varlistentry id="symbol-value-thread"><term><code>(&setf;
5015
(&symbol-value-thread; &symbol-r; &thr;) &value-r;)</code></term>
5016
<term><code>(&symbol-value-thread; &symbol-r; &thr;)</code></term>
5017
<listitem><simpara>Access or set the &thr-var; value.
5018
When &thr; is &t;, use the ¤t-thread;; if it is &nil;, use the
5020
Returns two values: the &symbol-r; binding and an indicator: &nil; if
5021
not bound in the &thr;, &t; if bound, and &symbol-t; &makunbound; if
5022
the &thr-var; binding was removed with &makunbound;.
5023
</simpara></listitem></varlistentry>
5024
<varlistentry id="default-special-bindings">
5025
<term>&default-special-bindings;</term>
5026
<listitem><simpara>The default <constant>:INITIAL-BINDINGS</constant>
5027
argument of &make-thread;.</simpara></listitem></varlistentry>
5028
<varlistentry id="with-deferred-interrupts"><term>
5029
<code>(&with-deferred-interrupts; &body-amp; &body-r;)</code></term>
5030
<listitem><simpara>Defer thread interrupts (but ¬-e; thread
5031
preemption) while &body-r; is executed. If there is an interrupt
5032
while &body-r; is run, it is queued and will be executed after
5033
&body-r; finishes.</simpara>
5034
<warning><para>Care is needed if waiting or blocking in &body-r;,
5035
since there is no way to interrupt it (in case of a deadlock).
5036
The macro was added to avoid partial &unwind-protect;'s cleanup
5037
forms evaluation in case they are interrupted with a non-local exit.
5038
</para></warning></listitem></varlistentry>
5039
</variablelist></section>
4523
5042
<section id="quickstart"><title>Quickstarting delivery with &clisp;</title>
4525
5044
<para>This section describes three ways to turn &clisp; programs into