MzScheme supports multiple threads of control within a program. Threads are implemented for all operating systems, even when the operating system does not provide primitive thread support.
(thread
thunk
)
invokes the procedure thunk
with no
arguments in a new thread of control. The
procedure
returns immediately with a thread descriptor value. When
the invocation of thread
thunk
returns, the thread created to invoke
thunk
terminates.
Example:
(thread
(lambda () (sleep
2) (display
7) (newline
))) ; => a thread descriptors
display
7
after two seconds pass
Each thread has its own parameter settings (see section 7.4), such as the current directory or current exception handler. A newly-created thread inherits the parameter settings of the creating thread, except
the
parameter, which is initialized
to the default error escape handler; anderror-escape-handler
the
parameter, which is
initialized to the value of current-exception-handler
.initial-exception-handler
When a thread is created, it is placed into the management of the current custodian (See section 9.2). A thread that has not terminated can be ``garbage collected'' only if it is unreachable and blocked on an unreachable semaphore.7
(current-thread
)
returns the thread descriptor for the currently
executing thread.
(thread?
v
)
returns #t
if v
is a thread descriptor,
#f
otherwise.
(sleep
[x
])
causes the current thread to sleep for at least
x
seconds, where x
is a non-negative real number. The
x
argument defaults to 0 (allowing other threads to execute
when operating system threads are not used). The value of x
can
be non-integral to request a sleep duration to any precision, but the
precision of the actual sleep time is unspecified.
(thread-running?
thread
)
returns #t
if thread
has not
terminated, #f
otherwise.
(thread-wait
thread
)
blocks execution of the current thread until
thread
has terminated. Note that (thread-wait
(current-thread))
deadlocks the current thread, but a break
can end the deadlock (if breaking is enabled; see
section 6.6).
(kill-thread
thread
)
terminates the specified thread
immediately. Terminating the main thread exits the application. If
thread
is already not running,
does nothing.
Otherwise, if the current custodian (see section 9.2) does
not manage kill-thread
thread
(and none of its subordinates manages
thread
), the exn:misc
exception is raised.
All of the MzScheme (and MrEd) primitives are kill-safe; that is, killing a thread never interferes with the application of primitives in other threads. For example, if a thread is killed while extracting a character from an input port, the character is either completely consumed or not consumed, and other threads can safely use the port.
(break-thread
thread
)
registers a break with the specified
thread. If breaking is disabled in thread
, the break will be
ignored until breaks are re-enabled (see section 6.6).
(call-in-nested-thread
thunk
[custodian
])
creates a nested thread
managed by custodian
to execute thunk
.8 The current
thread blocks until thunk
returns, and the result of the
call is the result returned by
call-in-nested-thread
thunk
. The default value of custodian
is the current
custodian.
The nested thread's exception handler is initialized to a procedure that jumps to the beginning of the thread and transfers the exception to the original thread. The handler thus terminates the nested thread and re-raises the exception in the original thread.
If the thread created by
dies before
call-in-nested-thread
thunk
returns, the exn:thread
exception is raised in the original
thread. If the original thread is killed before thunk
returns,
a break is queued for the nested thread.
If a break is queued for the original thread (with
) while the nested thread is running, the break is
redirected to the nested thread. If a break is already queued on the
original thread when the nested thread is created, the break is moved
to the nested thread. If a break remains queued on the nested thread
when it completes, the break is moved to the original thread.break-thread
A semaphore is a value that is used to synchronize MzScheme
threads. Each semaphore has an internal counter; when this counter is
zero, the semaphore can block a thread's execution (through
) until another thread increments the counter
(using semaphore-wait
). The maximum value for a semaphore's
internal counter is platform-specific, but always at least 10000. A
semaphores's counter is updated in a single-threaded manner, of
course, so tat semaphore can be used for reliable synchronization.semaphore-post
(make-semaphore
[init-k
])
creates and returns a new
semaphore with the counter initially set to init-k
, which
defaults to 0
. If init-k
is larger than a semaphore's
maximum internal counter value, the
exn:application:mismatch
exception is raised.
(semaphore?
v
)
returns #t
if v
is a semaphore
created by
, make-semaphore
#f
otherwise.
(semaphore-post
sema
)
increments the semaphore's internal
counter and returns void. If the semaphore's internal counter has
already reached its maximum value, the exn:misc
exception is raised.
(semaphore-wait
sema
)
blocks until the internal counter for
semaphore sema
is non-zero. When the counter is non-zero, it is
decremented and
returns void.semaphore-wait
(semaphore-try-wait?
sema
)
is like
,
but semaphore-wait
never blocks execution. If
semaphore-try-wait?
sema
's internal counter is zero,
returns
semaphore-try-wait?
#f
immediately without decrementing the counter. If
sema
's counter is positive, it is decremented and #t
is
returned.
(semaphore-wait/enable-break
sema
)
is like
, but breaking is enabled (see
section 6.6) while waiting on semaphore-wait
sema
. If breaking is
disabled when
is called, then
either the semaphore's counter is decremented or the
semaphore-wait/enable-break
exn:break
exception is raised, but not both.
See also
in section 7.3.object-wait-multiple
(object-wait-multiple
timeout v
···1)
blocks as long as all of the
v
s are in a blocking state, as defined below, or until
timeout
seconds have passed. The timeout
argument can be
a real number or #f
; if timeout
is #f
, then
does not return until some object-wait-multiple
v
is
unblocked.
When at least one v
is unblocked, it is returned as the result.
If multiple v
s are unblocked before the call to
object-wait-multple
, the returned v
is the earliest
unblocked v
according to argument order. If multiple v
s
become unblocked later, any one of the unblocked v
s can be
returned (but only one).
If the returned v
is a semaphore, then the semaphore's internal
count is decremented, just as with
. Otherwise, the returned semaphore-wait
v
is
unmodified. Any v
that is not returned by
is unmodified.object-wait-multiple
If
returns because object-wait-multiple
timeout
seconds have passed, the return value is #f
. Each v
is
checked at least once, so a timeout
value of 0 can be used
for polling.
Only certain kinds of values, lised below, are waitable. If any other
kind of value is provided to
, the
object-wait-multiple
exn:application:mismatch
exception is raised.9
semaphore
-- a semaphore is in a blocking state when
(see section 7.2) would block.semaphore-wait
input-port
-- an input port is in a blocking state when
would block.read-char
output-port
-- an output port is in a blocking state if
would block (see section 11.2.2), unless
the port contains buffered characters and
write-string-avail
can flush part of the buffer (although
write-string-avail*
might block).write-string-avail
tcp-listener
-- a TCP listener is in a blocking state
when
(see section 11.4) would block.tcp-accept
thread
-- a thread is in a blocking state when
(see section 7.1) would block.thread-wait
subprocess
-- a subprocess is in a blocking state when
(see section 15.2) would block.subprocess-wait
will-executor
-- a will executor is in a blocking
state when
(see section 13.2) would
block.will-execute
(object-wait-multiple/enable-break
v
···1)
is like
, but breaking is enabled (see
section 6.6) while waiting on the object-wait-multiple
v
s. If breaking is
disabled when
is called,
then either all object-wait-multiple/enable-break
v
s remain unmodified or the exn:break
exception is raised, but not both.
(object-waitable?
v
)
returns #t
if v
is a waitable
object, #f
otherwise. See object-wait-multiple
,
above, for the list of waitable object types.
A parameter is a thread-specific setting, such a the
current output port or the current directory for resolving relative
pathnames. A parameter procedure sets and retrieves the
value of a specific parameter. For example, the
parameter procedure sets and retrieves a
port value that is used by current-output-port
when a specific output port
is not provided. Applying a parameter procedure without an argument
obtains the current value of a parameter in the current thread, and
applying a parameter procedure to a single argument sets the
parameter's value in the current thread (and returns void). For
example, display
(current-output-port)
returns the current default
output port, while (current-output-port
sets the
default output port to p
)p
.
MzScheme's built-in parameter procedures are listed in the following
sections. The
procedure, described in
section 7.4.2, creates a new parameter and returns a
corresponding parameter procedure.make-parameter
(current-directory
[path
])
gets or sets a string path that determines the
current directory. When the parameter procedure is called to set the
current directory, the path argument is expanded and then simplified
using
(see section 11.3.1) and converted to
an immutable string; expansion and simplification raise an exception
if the path is ill-formed. Otherwise, if the given path cannot be
made the current directory (e.g., because the path does not exist),
the simplify-path
exn:i/o:filesystem
exception is raised.
(current-input-port
[input-port
])
gets or sets an input port used by
, read
, read-char
char-ready?
,
,
read-line
, and read-string
when a specific input port
is not provided.
read-string-avail!
(current-output-port
[output-port
])
gets or sets an output port used by
, display
, write
, print
, and
write-char
when a specific output port is not provided.
printf
(current-error-port
[output-port
])
gets or sets an output port used by
the default error display handler.
(global-port-print-handler
[proc
])
gets or sets a procedure that takes an
arbitrary value and an output port. This global port print
handler is called by the default port print handler (see
section 11.2.5) to
values into a port.
print
(read-case-sensitive
[on?
])
gets or sets a boolean value that controls
parsing input symbols. When this parameter's value is #f
,
the reader downcases symbols (e.g., hi
when the input
is any one of hi
, Hi
,
HI
, or hI
). The parameter also affects
the way that
prints symbols containing uppercase
characters; if the parameter's value is write
#f
, then symbols are
printed with uppercase characters quoted by a backslash
(\) or vertical bar (|). The parameter's value is
overridden by backslash and vertical-bar quotes and the #cs
and #ci
prefixes; see section 14.3 for more
information. While a module is loaded, the parameter is set to
#f
(see section 5.8).
(read-square-bracket-as-paren
[on?
])
gets or sets a boolean
value that controls whether square brackets (``['' and
``]'') are treated as parentheses. See section 14.3 for more
information.
(read-curly-brace-as-paren
[on?
])
gets or sets a boolean value
that controls whether curly braces (``{'' and
``}'') are treated as parentheses. See section 14.3 for more
information.
(read-accept-box
[on?
])
gets or sets a boolean value that controls
parsing #\&
input. See section 14.3 for more information.
(read-accept-compiled
[on?
])
gets or sets a boolean value that
controls parsing pre-compiled input. See section 14.3 for more
information.
(read-accept-bar-quote
[on?
])
gets or sets a boolean value that
controls parsing and printing a vertical bar (|) in
symbols. See section 14.3 and section 14.4 for more
information.
(read-accept-graph
[on?
])
gets or sets a boolean value that controls
parsing input S-expressions with sharing. See section 14.5
for more information.
(read-decimal-as-inexact
[on?
])
gets or sets a boolean value that
controls parsing input numbers with a decimal point or exponent (but
no explicit exactness tag). See section 14.5 for more
information.
(read-accept-dot
[on?
])
gets or sets a boolean value that controls
parsing input with a dot, which is normally used for literal cons
cells. See section 14.3 for more information.
(read-accept-quasiquote
[on?
])
gets or sets a boolean value that controls
parsing input with a backquote or comma, which is normally used for
quasiquote
, unquote
, and unquote-splicing
abbreviations. See section 14.3 for more information.
(print-graph
[on?
])
gets or sets a boolean value that controls printing
S-expressions with sharing. See section 14.5 for more
information.
(print-struct
[on?
])
gets or sets a boolean value that controls printing
structure values. See section 14.4 for more information.
(print-box
[on?
])
gets or sets a boolean value that controls printing box
values. See section 14.4 for more information.
(print-vector-length
[on?
])
gets or sets a boolean value that controls
printing vectors. See section 14.4 for more information.
(current-prompt-read
[proc
])
gets or sets a procedure that takes no
arguments, displays a prompt string, and returns an expression to
evaluate. This prompt read handler is called by the read
phase of
(see section 14.1). The
default prompt read handler prints ``> '' and returns the result of
read-eval-print-loop
(
, where read-syntax
name-string)name-string
corresponds to the current input source.
(current-eval
[proc
])
gets or sets a procedure that takes a syntax object
or S-expression and returns its value (or values; see
section 2.2). This evaluation handler is called by
, the default load handler, and
eval
to evaluate an expression (see
section 14.1). The default evaluation handler compiles and
executes the expression in the current namespace (determined by the
read-eval-print-loop
parameter).
current-namespace
(current-namespace
[namespace
])
gets or sets a namespace value (see
section 8) that determines the global variable namespace
used to resolve variable references. The current namespace
is used by the default evaluation handler, the
procedure, and other built-in procedures that operate on global
variables.
compile
(current-print
[proc
])
gets or sets a procedure that takes a value to
print. This print handler is called by
(see section 14.1) to print the
result of an evaluation (and the result is ignored). The default
print handler read-eval-print-loop
s the value to the current output port
(determined by the print
parameter) and then
outputs a newline.
current-output-port
(compile-allow-set!-undefined
[on?
])
gets or sets
a boolean value indicating how to compile a
set!
expression
that mutates a global variable. If the value of this parameter is a
true value, set!
expressions for global variables are compiled
so that the global variable is set even if it was not previously
defined. Otherwise, set!
expressions for global variables are
compiled to raise the exn:variable
exception if the global
variable is not defined at the time the set!
is performed.
Note that this parameter is used when an expression is compiled, not when it is evaluated.
(current-load
[proc
])
gets or sets a procedure that loads a file and
returns the value (or values; see section 2.2) of the last expression
read from the file. This load handler is called by
load
, load-relative
,
load/use-compiled
, and load/cd
.
A load handler procedure takes two arguments: a file path string and
an expected module name. The expected module name is either a symbol
or #f
; see section 5.8 for further information.
The default load handler reads expressions from the file (with compiled expressions enabled and line-counting enabled) and passes each expression to the current evaluation handler. The default load handler also treats a hash mark on the first line of the file as a comment (see section 14.3). The current load directory for loading the file is set before the load handler is called (see section 14.1).
(current-load-extension
[proc
])
gets or sets a procedure loads a dynamic
extension (see section 14.7) and returns the extension's
value(s). This load extension handler is called by
load-extension
, load-relative
, and
load/use-compiled
.
A load extension handler procedure takes two arguments: a file path
string and an expected module name. The expected module name is
either a symbol or #f
; see section 5.8 for
further information.
The default load extension handler loads an extension using operating system primitives.
(current-load/use-compiled
[proc
])
gets or sets a procedure that loads a
file or a compiled version of the file; see section 14.1 for
more information. A load/use-compiled handler procedure
takes the same arguments as a load handler. The handler is expected
to call the load handler or the load-extension handler. Unlike a load
handler or load-extension handler, a load/use-compiled handler is
expected to set the current load-relative
directory.
(current-load-relative-directory
[path
])
gets or sets a complete
directory pathname or #f
. This current
directory is set by load-relative
load
, load-relative
,
load/use-compiled
,
,
load/cd
load-extension
, and load-relative-extension
to
the directory of the file being loaded. This parameter is used by
load-relative
, load/use-compiled
and
load-relative-extension
(see section 14.1). When a
new pathname is provided to the parameter procedure
, it is immediately expanded
(see section 11.3.1) and converted to an immutable string;
the result must be a complete pathname for an existing directory.
current-load-relative-directory
(use-compiled-file-kinds
[kind-symbol
])
gets or sets a symbol, either
'all
or 'none
, indicating whether
load/used-compiled
(and thus require
) should
recognize compiled files. If the value of this parameter is
'all
, then the default handler for
recognizes compiled files as described in section 14.1. If the
value is load/use-compiled
'none
, then the default handler for
ignores compiled files.
load/use-compiled
(current-library-collection-paths
[path-list
])
gets or sets a list of
complete directory pathnames for library collections used by
require
. See Chapter 16 for more information. When
a new list of pathnames is provided to the parameter procedure, it is
converted to an immutable list of immutable strings.
(current-command-line-arguments
[string-vector
])
gets or sets a vector of
strings representing command-line arguments. The stand-alone version
of MzScheme (and MrEd) initializes the parameter to contain extra
command-line arguments. (The same vector is also installed as the
value of the argv
global.)
(current-exception-handler
[proc
])
gets or sets a procedure that is
invoked to handle an exception. See section 6.1 for more
information about exceptions.
(initial-exception-handler
[proc
])
gets or sets a procedure that is
used as the initial current exception handler for a new thread.
(error-escape-handler
[proc
])
gets or sets a procedure that takes no
arguments and escapes from the dynamic context of an exception. The
default error escape handler escapes to the start of the current
thread, but
(see section 14.1) also
sets the escape handler. To report a run-time error, use read-eval-print-loop
(see section 6.1) or raise
(see section 6.2) instead
of calling the error escape procedure directly. If an exception is
raised while the error escape handler is executing, an error message
is printed using a primitive error printer and the primitive error
escape handler is invoked. Unlike all other parameters, the value of
the error
parameter in a new thread is not
inherited from the creating thread; instead, the parameter is always
initialized to the default error escape handler.
error-escape-handler
(error-display-handler
[proc
])
gets or sets a procedure that takes two
arguments: a string to print as an error message, and a value
representing a raised exception. This error display handler
is called by the default exception handler with an error message and
the exception value. The default error display handler
s its first argument to the current error port
(determined by the display
parameter), and
ignores the second argument. To report a run-time error, use
current-error-port
(see section 6.1) or raise
(see
section 6.2) instead of calling the error display procedure
directly. If an exception is raised while the error display handler
is executing, an error message is printed using a primitive error
printer and the primitive error escape handler is invoked.
error
(error-print-width
[k
])
gets or sets an integer greater than 3
.
This value is used as the maximum number of characters used to print
a Scheme value that is embedded in a primitive error message.
(error-value->string-handler
[proc
])
gets or sets a procedure that takes
an arbitrary Scheme value and an integer and returns a string. This
error value conversion handler is used to to print a
Scheme value that is embedded in a primitive error message. The
integer argument to the handler specifies the maximum number of
characters that should be used to represent the value in the
resulting string. The default error value conversion handler
s the value into a string;10
if the printed form is too long, the printed form is truncated and
the last three characters of the return string are set to ``...''.print
If the string returned by an error value conversion handler is longer
than requested, the string is destructively ``truncated'' by setting
the first extra position in the string to the null character. If a
non-string is returned, then the string "..."
is used. If a
primitive error string needs to be generated before the handler has
returned, the default error value conversion handler is used.
(error-print-source-location
[include?
])
gets or sets a boolean that
controls whether read and syntax error messages include source
information, such as the source line and column or the
expression. Only the message field of an exn:read
or
exn:syntax
structure is affected by the parameter. The
default is #t
.
(break-enabled
[enabled?
])
gets or sets a boolean value that controls
whether breaks are allowed. See section 6.6 for more
information.
(current-security-guard
[security-guard
])
gets or sets a security guard
(see section 9.1) that controls access to the filesystem
and network.
(current-custodian
[custodian
])
gets or sets a custodian (see
section 9.2) that assumes responsibility for newly created
threads, ports, and TCP listeners.
(current-inspector
[inspector
])
gets or sets an inspector (see
section 4.5) that controls debugging access to newly created
structure types.
(exit-handler
[proc
])
gets or sets a procedure that takes a single
argument. This exit handler is called by exit
.
The default exit handler takes any argument and shuts down MzScheme;
see section 14.2 for information about exit codes.
(current-pseudo-random-generator
[generator
])
gets or sets a
pseudo-random number generator (see section 3.3) used by
random
and random-seed
.
(current-locale
[string-or-#f
])
gets or sets a string/boolean value that
controls the interpretation of characters for functions such as
, char-locale<?
, and
char-locale-upcase
(see section 3.4 and
section 3.5). When locale sensitivity is disabled by setting the
parameter to string-locale<?
#f
, characters are interpreted in a fully
portable manner, which is the same as the standard procedures;
otherwise, they are interpreted according to a locale setting (in the
sense of the C library's setlocale). The ""
locale is
always a synonym for the current machine's default locale; other
locale names are platform-specific.11
String and character printing with
is affected by the
parameter, because unprintable characters are printed with escapes
(see section 14.4). Case convesion for case-insensitive symbols
and regular expression patterns (see section 10) are not
affected. The parameter's default value is write
""
.
(current-module-name-resolver
[proc
])
gets or sets a procedure used to
resolve module paths. See section 5.4 for more information.
(current-module-name-prefix
[symbol
])
gets or sets a symbol prefixed onto
a module declaration when it is evaluated. This parameter is intended
for use by a module name resolver; see section 5.4 for more
information.
(make-parameter
v
[guard-proc
])
returns a new parameter
procedure. The value of the parameter is initialized to v
in
all threads. If guard-proc
is supplied, it is used as the
parameter's guard procedure. A guard procedure takes one
argument. Whenever the parameter procedure is applied to an argument,
the argument is passed on to the guard procedure. The result returned
by the guard procedure is used as the new parameter value. A guard
procedure can raise an exception to reject a change to the
parameter's value.
(parameter?
v
)
returns #t
if v
is a parameter
procedure, #f
otherwise.
(parameter-procedure=?
a b
)
returns #t
if the parameter
procedures a
and b
always modify the same parameter,
#f
otherwise.
The parameterize
form evaluates an expression with
temporarily values installed for a group of parameters. The syntax of
parameterize
is:
(parameterize ((parameter-expr value-expr) ···) body-expr ···1)
The result of a parameterize
expression is the result of the
last body-expr
. The parameter-expr
s determine the
parameters to set, and the value-expr
s determine the
corresponding values to install before evaluating the
body-expr
s. All of the parameter-expr
s are evaluated
first (checked with
), then all
check-parameter-procedure
value-expr
s are evaluated, and then the parameters are set.
After the body-expr
s are evaluated, each parameter's setting is
restored to its original value in the dynamic context of the
parameterize
expression. More generally, the values specified
by the value-expr
s determine initial ``remembered'' values, and
whenever control jumps into or out of the body-expr
s, the value
of each parameter is swapped with the corresponding ``remembered''
value.
Examples:
(parameterize ([exit-handler
(lambda (x) 'no-exit)]) (exit
)) ; =>'no-exit
(define p1 (make-parameter
1)) (define p2 (make-parameter
2)) (parameterize ([p1 3] [p2 (p1)]) (cons
(p1) (p2))) ; =>'(3 . 1)
(let ([k (let/cc out (parameterize ([p1 2]) (p1 3) (cons
(let/cc k (out k)) (p1))))]) (if (procedure?
k) (k (p1)) k)) ; =>'(1 . 3)
(check-parameter-procedure
v
)
returns v
if it is a
procedure that can take both 0 arguments and 1 argument, and raises
otherwise. The
exn:application:type
procedure is used in the expansion of
check-parameter-procedure
parameterize
.
7 In MrEd, a handler thread for an eventspace is blocked on an internal semaphore when its event queue is empty. Thus, the handler thread is collectable when the eventspace is unreachable and contains no visible windows or running timers.
8 The
nested thread's current custodian is inherited from the creating
thread, independent of the custodian
argument.
9 An extension or embedding application can extend the set of waitable values.
10 Using the current global port print handler; see section 7.4.1.2.
11 The "C"
locale is
also always available; setting the locale to "C"
is the same
as disabling locale sensitivity with #f
only when string and
character operations are restricted to the first 128 characters.