*** empty log message ***
[bpt/guile.git] / doc / ref / api-scheduling.texi
CommitLineData
07d83abe
MV
1@c -*-texinfo-*-
2@c This is part of the GNU Guile Reference Manual.
3@c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004
4@c Free Software Foundation, Inc.
5@c See the file guile.texi for copying conditions.
6
7@page
8@node Scheduling
9@section Threads, Mutexes, Asyncs and Dynamic Roots
10
11[FIXME: This is pasted in from Tom Lord's original guile.texi chapter
12plus the Cygnus programmer's manual; it should be *very* carefully
13reviewed and largely reorganized.]
14
15@menu
16* Arbiters:: Synchronization primitives.
17* Asyncs:: Asynchronous procedure invocation.
b4fddbbe 18* Continuation Barriers:: Protection from non-local control flow.
07d83abe 19* Threads:: Multiple threads of execution.
2567692a 20* Mutexes and Condition Variables:: Synchronization primitives.
b4fddbbe 21* Blocking:: How to block properly in guile mode.
2567692a 22* Critical Sections:: Avoiding concurrency and reentries.
b4fddbbe 23* Fluids and Dynamic States:: Thread-local variables, etc.
07d83abe
MV
24* Futures:: Delayed execution in new threads.
25* Parallel Forms:: Parallel execution of forms.
26@end menu
27
28
29@node Arbiters
30@subsection Arbiters
07d83abe
MV
31@cindex arbiters
32
e136aab0
KR
33Arbiters are synchronization objects, they can be used by threads to
34control access to a shared resource. An arbiter can be locked to
35indicate a resource is in use, and unlocked when done.
07d83abe 36
b4fddbbe
MV
37An arbiter is like a light-weight mutex (@pxref{Mutexes and Condition
38Variables}). It uses less memory and may be faster, but there's no
39way for a thread to block waiting on an arbiter, it can only test and
40get the status returned.
07d83abe
MV
41
42@deffn {Scheme Procedure} make-arbiter name
43@deffnx {C Function} scm_make_arbiter (name)
cdf1ad3b
MV
44Return an object of type arbiter and name @var{name}. Its
45state is initially unlocked. Arbiters are a way to achieve
46process synchronization.
07d83abe
MV
47@end deffn
48
49@deffn {Scheme Procedure} try-arbiter arb
50@deffnx {C Function} scm_try_arbiter (arb)
cdf1ad3b
MV
51@deffnx {C Function} scm_try_arbiter (arb)
52If @var{arb} is unlocked, then lock it and return @code{#t}.
53If @var{arb} is already locked, then do nothing and return
54@code{#f}.
07d83abe
MV
55@end deffn
56
57@deffn {Scheme Procedure} release-arbiter arb
58@deffnx {C Function} scm_release_arbiter (arb)
e136aab0
KR
59If @var{arb} is locked, then unlock it and return @code{#t}. If
60@var{arb} is already unlocked, then do nothing and return @code{#f}.
61
62Typical usage is for the thread which locked an arbiter to later
63release it, but that's not required, any thread can release it.
07d83abe
MV
64@end deffn
65
66
67@node Asyncs
68@subsection Asyncs
69
70@cindex asyncs
71@cindex user asyncs
72@cindex system asyncs
73
74Asyncs are a means of deferring the excution of Scheme code until it is
75safe to do so.
76
77Guile provides two kinds of asyncs that share the basic concept but are
78otherwise quite different: system asyncs and user asyncs. System asyncs
79are integrated into the core of Guile and are executed automatically
80when the system is in a state to allow the execution of Scheme code.
81For example, it is not possible to execute Scheme code in a POSIX signal
82handler, but such a signal handler can queue a system async to be
83executed in the near future, when it is safe to do so.
84
85System asyncs can also be queued for threads other than the current one.
86This way, you can cause threads to asynchronously execute arbitrary
87code.
88
89User asyncs offer a convenient means of queueing procedures for future
90execution and triggering this execution. They will not be executed
91automatically.
92
93@menu
94* System asyncs::
95* User asyncs::
96@end menu
97
98@node System asyncs
99@subsubsection System asyncs
100
101To cause the future asynchronous execution of a procedure in a given
102thread, use @code{system-async-mark}.
103
104Automatic invocation of system asyncs can be temporarily disabled by
105calling @code{call-with-blocked-asyncs}. This function works by
106temporarily increasing the @emph{async blocking level} of the current
107thread while a given procedure is running. The blocking level starts
108out at zero, and whenever a safe point is reached, a blocking level
109greater than zero will prevent the execution of queued asyncs.
110
111Analogously, the procedure @code{call-with-unblocked-asyncs} will
112temporarily decrease the blocking level of the current thread. You
113can use it when you want to disable asyncs by default and only allow
114them temporarily.
115
116In addition to the C versions of @code{call-with-blocked-asyncs} and
117@code{call-with-unblocked-asyncs}, C code can use
b4fddbbe 118@code{scm_frame_block_asyncs} and @code{scm_frame_unblock_asyncs}
07d83abe
MV
119inside a @dfn{frame} (@pxref{Frames}) to block or unblock system asyncs
120temporarily.
121
122@deffn {Scheme Procedure} system-async-mark proc [thread]
123@deffnx {C Function} scm_system_async_mark (proc)
124@deffnx {C Function} scm_system_async_mark_for_thread (proc, thread)
125Mark @var{proc} (a procedure with zero arguments) for future execution
126in @var{thread}. When @var{proc} has already been marked for
127@var{thread} but has not been executed yet, this call has no effect.
128When @var{thread} is omitted, the thread that called
129@code{system-async-mark} is used.
130
131This procedure is not safe to be called from signal handlers. Use
132@code{scm_sigaction} or @code{scm_sigaction_for_thread} to install
133signal handlers.
134@end deffn
135
136@c FIXME: The use of @deffnx for scm_c_call_with_blocked_asyncs and
137@c scm_c_call_with_unblocked_asyncs puts "void" into the function
138@c index. Would prefer to use @deftypefnx if makeinfo allowed that,
139@c or a @deftypefn with an empty return type argument if it didn't
140@c introduce an extra space.
141
142@deffn {Scheme Procedure} call-with-blocked-asyncs proc
143@deffnx {C Function} scm_call_with_blocked_asyncs (proc)
144@deffnx {C Function} void *scm_c_call_with_blocked_asyncs (void * (*proc) (void *data), void *data)
145@findex scm_c_call_with_blocked_asyncs
146Call @var{proc} and block the execution of system asyncs by one level
147for the current thread while it is running. Return the value returned
148by @var{proc}. For the first two variants, call @var{proc} with no
149arguments; for the third, call it with @var{data}.
150@end deffn
151
152@deffn {Scheme Procedure} call-with-unblocked-asyncs proc
153@deffnx {C Function} scm_call_with_unblocked_asyncs (proc)
154@deffnx {C Function} void *scm_c_call_with_unblocked_asyncs (void *(*p) (void *d), void *d)
155@findex scm_c_call_with_unblocked_asyncs
156Call @var{proc} and unblock the execution of system asyncs by one
157level for the current thread while it is running. Return the value
158returned by @var{proc}. For the first two variants, call @var{proc}
159with no arguments; for the third, call it with @var{data}.
160@end deffn
161
162@deftypefn {C Function} void scm_frame_block_asyncs ()
163This function must be used inside a pair of calls to
164@code{scm_frame_begin} and @code{scm_frame_end} (@pxref{Frames}).
165During the dynamic extent of the frame, asyncs are blocked by one level.
166@end deftypefn
167
168@deftypefn {C Function} void scm_frame_unblock_asyncs ()
169This function must be used inside a pair of calls to
170@code{scm_frame_begin} and @code{scm_frame_end} (@pxref{Frames}).
171During the dynamic extent of the frame, asyncs are unblocked by one
172level.
173@end deftypefn
174
175@node User asyncs
176@subsubsection User asyncs
177
178A user async is a pair of a thunk (a parameterless procedure) and a
179mark. Setting the mark on a user async will cause the thunk to be
180executed when the user async is passed to @code{run-asyncs}. Setting
181the mark more than once is satisfied by one execution of the thunk.
182
183User asyncs are created with @code{async}. They are marked with
184@code{async-mark}.
185
186@deffn {Scheme Procedure} async thunk
187@deffnx {C Function} scm_async (thunk)
188Create a new user async for the procedure @var{thunk}.
189@end deffn
190
191@deffn {Scheme Procedure} async-mark a
192@deffnx {C Function} scm_async_mark (a)
193Mark the user async @var{a} for future execution.
194@end deffn
195
196@deffn {Scheme Procedure} run-asyncs list_of_a
197@deffnx {C Function} scm_run_asyncs (list_of_a)
198Execute all thunks from the marked asyncs of the list @var{list_of_a}.
199@end deffn
200
b4fddbbe
MV
201@node Continuation Barriers
202@subsection Continuation Barriers
07d83abe 203
b4fddbbe
MV
204The non-local flow of control caused by continuations might sometimes
205not be wanted. You can use @code{with-continuation-barrier} etc to
206errect fences that continuations can not pass.
07d83abe 207
673ba2da 208@deffn {Scheme Procedure} with-continuation-barrier proc
b4fddbbe
MV
209@deffnx {C Function} scm_with_continuation_barrier (proc)
210Call @var{proc} and return its result. Do not allow the invocation of
211continuations that would leave or enter the dynamic extent of the call
212to @code{with-continuation-barrier}. Such an attempt causes an error
213to be signaled.
07d83abe 214
b4fddbbe
MV
215Throws (such as errors) that are not caught from within @var{proc} are
216caught by @code{with-continuation-barrier}. In that case, a short
217message is printed to the current error port and @code{#f} is returned.
07d83abe 218
b4fddbbe 219Thus, @code{with-continuation-barrier} returns exactly once.
07d83abe
MV
220@end deffn
221
c2110081 222@deftypefn {C Function} {void *} scm_c_with_continuation_barrier (void *(*func) (void *), void *data)
b4fddbbe
MV
223Like @code{scm_with_continuation_barrier} but call @var{func} on
224@var{data}. When an error is caught, @code{NULL} is returned.
225@end deftypefn
07d83abe
MV
226
227@node Threads
228@subsection Threads
229@cindex threads
230@cindex Guile threads
231@cindex POSIX threads
232
cdf1ad3b
MV
233@deffn {Scheme Procedure} all-threads
234@deffnx {C Function} scm_all_threads ()
235Return a list of all threads.
236@end deffn
237
238@deffn {Scheme Procedure} current-thread
239@deffnx {C Function} scm_current_thread ()
240Return the thread that called this function.
241@end deffn
07d83abe
MV
242
243@c begin (texi-doc-string "guile" "call-with-new-thread")
b4fddbbe
MV
244@deffn {Scheme Procedure} call-with-new-thread thunk handler
245Call @code{thunk} in a new thread and with a new dynamic state,
246returning the new thread. The procedure @var{thunk} is called via
247@code{with-continuation-barrier}.
07d83abe 248
b4fddbbe
MV
249When @var{handler} is specified, then @var{thunk} is called from
250within a @code{catch} with tag @code{#t} that has @var{handler} as its
251handler. This catch is established inside the continuation barrier.
07d83abe 252
b4fddbbe
MV
253Once @var{thunk} or @var{handler} returns, the return value is made
254the @emph{exit value} of the thread and the thread is terminated.
07d83abe
MV
255@end deffn
256
b4fddbbe
MV
257@deftypefn {C Function} SCM scm_spawn_thread (scm_t_catch_body body, void *body_data, scm_t_catch_handler handler, void *handler_data)
258Call @var{body} in a new thread, passing it @var{body_data}, returning
259the new thread. The function @var{body} is called via
260@code{scm_c_with_continuation_barrier}.
261
262When @var{handler} is non-@code{NULL}, @var{body} is called via
263@code{scm_internal_catch} with tag @code{SCM_BOOL_T} that has
264@var{handler} and @var{handler_data} as the handler and its data. This
265catch is established inside the continuation barrier.
266
267Once @var{body} or @var{handler} returns, the return value is made the
268@emph{exit value} of the thread and the thread is terminated.
269@end deftypefn
270
07d83abe
MV
271@c begin (texi-doc-string "guile" "join-thread")
272@deffn {Scheme Procedure} join-thread thread
b4fddbbe
MV
273Wait for @var{thread} to terminate and return its exit value. Threads
274that have not been created with @code{call-with-new-thread} or
275@code{scm_spawn_thread} have an exit value of @code{#f}.
07d83abe
MV
276@end deffn
277
cdf1ad3b
MV
278@deffn {Scheme Procedure} thread-exited? thread
279@deffnx {C Function} scm_thread_exited_p (thread)
280Return @code{#t} iff @var{thread} has exited.
281@end deffn
282
07d83abe
MV
283@c begin (texi-doc-string "guile" "yield")
284@deffn {Scheme Procedure} yield
285If one or more threads are waiting to execute, calling yield forces an
286immediate context switch to one of them. Otherwise, yield has no effect.
287@end deffn
288
07d83abe
MV
289Higher level thread procedures are available by loading the
290@code{(ice-9 threads)} module. These provide standardized
3cf066df 291thread creation.
07d83abe
MV
292
293@deffn macro make-thread proc [args@dots{}]
294Apply @var{proc} to @var{args} in a new thread formed by
295@code{call-with-new-thread} using a default error handler that display
b4fddbbe
MV
296the error to the current error port. The @var{args@dots{}}
297expressions are evaluated in the new thread.
07d83abe
MV
298@end deffn
299
300@deffn macro begin-thread first [rest@dots{}]
301Evaluate forms @var{first} and @var{rest} in a new thread formed by
302@code{call-with-new-thread} using a default error handler that display
303the error to the current error port.
304@end deffn
305
2567692a
MV
306@node Mutexes and Condition Variables
307@subsection Mutexes and Condition Variables
308@cindex mutex
309@cindex condition variable
310
311A mutex is a thread synchronization object, it can be used by threads
312to control access to a shared resource. A mutex can be locked to
313indicate a resource is in use, and other threads can then block on the
314mutex to wait for the resource (or can just test and do something else
315if not available). ``Mutex'' is short for ``mutual exclusion''.
316
317There are two types of mutexes in Guile, ``standard'' and
318``recursive''. They're created by @code{make-mutex} and
319@code{make-recursive-mutex} respectively, the operation functions are
320then common to both.
321
322Note that for both types of mutex there's no protection against a
323``deadly embrace''. For instance if one thread has locked mutex A and
324is waiting on mutex B, but another thread owns B and is waiting on A,
325then an endless wait will occur (in the current implementation).
326Acquiring requisite mutexes in a fixed order (like always A before B)
327in all threads is one way to avoid such problems.
328
329@sp 1
330@deffn {Scheme Procedure} make-mutex
331@deffnx {C Function} scm_make_mutex ()
332Return a new standard mutex. It is initially unlocked.
333@end deffn
334
335@deffn {Scheme Procedure} make-recursive-mutex
336@deffnx {C Function} scm_make_recursive_mutex ()
337Create a new recursive mutex. It is initialloy unlocked.
338@end deffn
339
340@deffn {Scheme Procedure} lock-mutex mutex
341@deffnx {C Function} scm_lock_mutex (mutex)
342Lock @var{mutex}. If the mutex is already locked by another thread
343then block and return only when @var{mutex} has been acquired.
344
345For standard mutexes (@code{make-mutex}), and error is signalled if
346the thread has itself already locked @var{mutex}.
347
348For a recursive mutex (@code{make-recursive-mutex}), if the thread has
349itself already locked @var{mutex}, then a further @code{lock-mutex}
350call increments the lock count. An additional @code{unlock-mutex}
351will be required to finally release.
352
353When a system async (@pxref{System asyncs}) is activated for a thread
354blocked in @code{lock-mutex}, the wait is interrupted and the async is
355executed. When the async returns, the wait resumes.
356@end deffn
357
358@deftypefn {C Function} void scm_frame_lock_mutex (SCM mutex)
359Arrange for @var{mutex} to be locked whenever the current frame is
360entered and to be unlocked when it is exited.
361@end deftypefn
362
363@deffn {Scheme Procedure} try-mutex mx
364@deffnx {C Function} scm_try_mutex (mx)
365Try to lock @var{mutex} as per @code{lock-mutex}. If @var{mutex} can
366be acquired immediately then this is done and the return is @code{#t}.
367If @var{mutex} is locked by some other thread then nothing is done and
368the return is @code{#f}.
369@end deffn
370
371@deffn {Scheme Procedure} unlock-mutex mutex
372@deffnx {C Function} scm_unlock_mutex (mutex)
373Unlock @var{mutex}. An error is signalled if @var{mutex} is not
374locked by the calling thread.
375@end deffn
376
377@deffn {Scheme Procedure} make-condition-variable
378@deffnx {C Function} scm_make_condition_variable ()
379Return a new condition variable.
380@end deffn
381
382@deffn {Scheme Procedure} wait-condition-variable condvar mutex [time]
383@deffnx {C Function} scm_wait_condition_variable (condvar, mutex, time)
384Wait until @var{condvar} has been signalled. While waiting,
385@var{mutex} is atomically unlocked (as with @code{unlock-mutex}) and
386is locked again when this function returns. When @var{time} is given,
387it specifies a point in time where the waiting should be aborted. It
388can be either a integer as returned by @code{current-time} or a pair
389as returned by @code{gettimeofday}. When the waiting is aborted,
390@code{#f} is returned. When the condition variable has in fact been
391signalled, @code{#t} is returned. The mutex is re-locked in any case
392before @code{wait-condition-variable} returns.
393
394When a system async is activated for a thread that is blocked in a
395call to @code{wait-condition-variable}, the waiting is interrupted,
396the mutex is locked, and the async is executed. When the async
397returns, the mutex is unlocked again and the waiting is resumed. When
398the thread block while re-acquiring the mutex, execution of asyncs is
399blocked.
400@end deffn
401
402@deffn {Scheme Procedure} signal-condition-variable condvar
403@deffnx {C Function} scm_signal_condition_variable (condvar)
404Wake up one thread that is waiting for @var{condvar}.
405@end deffn
406
407@deffn {Scheme Procedure} broadcast-condition-variable condvar
408@deffnx {C Function} scm_broadcast_condition_variable (condvar)
409Wake up all threads that are waiting for @var{condvar}.
410@end deffn
411
412@sp 1
413The following are higher level operations on mutexes. These are
414available from
415
416@example
417(use-modules (ice-9 threads))
418@end example
419
420@deffn macro with-mutex mutex [body@dots{}]
421Lock @var{mutex}, evaluate the @var{body} forms, then unlock
422@var{mutex}. The return value is the return from the last @var{body}
423form.
424
425The lock, body and unlock form the branches of a @code{dynamic-wind}
426(@pxref{Dynamic Wind}), so @var{mutex} is automatically unlocked if an
427error or new continuation exits @var{body}, and is re-locked if
428@var{body} is re-entered by a captured continuation.
429@end deffn
430
431@deffn macro monitor body@dots{}
432Evaluate the @var{body} forms, with a mutex locked so only one thread
433can execute that code at any one time. The return value is the return
434from the last @var{body} form.
435
436Each @code{monitor} form has its own private mutex and the locking and
437evaluation is as per @code{with-mutex} above. A standard mutex
438(@code{make-mutex}) is used, which means @var{body} must not
439recursively re-enter the @code{monitor} form.
440
441The term ``monitor'' comes from operating system theory, where it
442means a particular bit of code managing access to some resource and
443which only ever executes on behalf of one process at any one time.
444@end deffn
445
446
b4fddbbe
MV
447@node Blocking
448@subsection Blocking in Guile Mode
07d83abe 449
b4fddbbe
MV
450A thread must not block outside of a libguile function while it is in
451guile mode. The following functions can be used to temporily leave
452guile mode or to perform some common blocking operations in a supported
453way.
07d83abe 454
9ba2fab3
MV
455@deftypefn {C Function} scm_t_guile_ticket scm_leave_guile ()
456@deftypefnx {C Function} void scm_enter_guile (scm_t_guile_ticket ticket)
457These two functions must be called as a pair and can be used to leave
458guile mode temporarily. The call to @code{scm_leave_guile} returns a
459ticket that must be used with @code{scm_enter_guile} to match up the two
460calls.
07d83abe 461
b4fddbbe 462While a thread has left guile mode, it must not call any libguile
9ba2fab3
MV
463functions except @code{scm_enter_guile}, @code{scm_with_guile},
464@code{scm_without_guile} or @code{scm_leave_guile} and must not use any
465libguile macros. Also, local variables of type @code{SCM} that are
466allocated while not in guile mode are not protected from the garbage
467collector.
468
469When used from non-guile mode, a pair of calls to @code{scm_leave_guile}
470and @code{scm_enter_guile} do nothing. In that way, you can leave guile
471mode without having to know whether the current thread is in guile mode
472or not.
07d83abe
MV
473@end deftypefn
474
c2110081 475@deftypefn {C Function} {void *} scm_without_guile (void *(*func) (void *), void *data)
9ba2fab3
MV
476Leave guile mode as with @code{scm_leave_guile}, call @var{func} on
477@var{data}, enter guile mode as with @code{scm_enter_guile} and return
478the result of calling @var{func}.
07d83abe
MV
479@end deftypefn
480
b4fddbbe
MV
481@deftypefn {C Function} int scm_pthread_mutex_lock (pthread_mutex_t *mutex)
482Like @code{pthread_mutex_lock}, but leaves guile mode while waiting for
483the mutex.
07d83abe
MV
484@end deftypefn
485
b4fddbbe
MV
486@deftypefn {C Function} int scm_pthread_cond_wait (pthread_cond_t *cond, pthread_mutex_t *mutex)
487@deftypefnx {C Function} int scm_pthread_cond_timedwait (pthread_cond_t *cond, pthread_mutex_t *mutex, struct timespec *abstime)
488Like @code{pthread_cond_wait} and @code{pthread_cond_timedwait}, but
489leaves guile mode while waiting for the condition variable.
07d83abe
MV
490@end deftypefn
491
b4fddbbe
MV
492@deftypefn {C Function} int scm_std_select (int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
493Like @code{select} but leaves guile mode while waiting. Also, the
494delivery of a system async causes this function to be interrupted with
495error code @code{EINTR}.
07d83abe
MV
496@end deftypefn
497
b4fddbbe
MV
498@deftypefn {C Function} {unsigned int} scm_std_sleep ({unsigned int} seconds)
499Like @code{sleep}, but leaves guile mode while sleeping. Also, the
500delivery of a system async causes this function to be interrupted.
07d83abe
MV
501@end deftypefn
502
b4fddbbe
MV
503@deftypefn {C Function} {unsigned long} scm_std_usleep ({unsigned long} usecs)
504Like @code{usleep}, but leaves guile mode while sleeping. Also, the
505delivery of a system async causes this function to be interrupted.
07d83abe
MV
506@end deftypefn
507
07d83abe 508
2567692a
MV
509@node Critical Sections
510@subsection Critical Sections
511
512@deffn {C Macro} SCM_CRITICAL_SECTION_START
513@deffnx {C Macro} SCM_CRITICAL_SECTION_END
514These two macros can be used to delimit a critical section.
515Syntactically, they are both statements and need to be followed
516immediately by a semicolon.
517
518Executing @code{SCM_CRITICAL_SECTION_START} will lock a recursive
519mutex and block the executing of system asyncs. Executing
520@code{SCM_CRITICAL_SECTION_END} will unblock the execution of system
521asyncs and unlock the mutex. Thus, the code that executes between
522these two macros can only be executed in one thread at any one time
523and no system asyncs will run. However, because the mutex is a
524recursive one, the code might still be reentered by the same thread.
525You must either allow for this or avoid it, both by careful coding.
526
527On the other hand, critical sections delimited with these macros can
528be nested since the mutex is recursive.
529
530You must make sure that for each @code{SCM_CRITICAL_SECTION_START},
531the corresponding @code{SCM_CRITICAL_SECTION_END} is always executed.
532This means that no non-local exit (such as a signalled error) might
533happen, for example.
534@end deffn
535
536@deftypefn {C Function} void scm_frame_critical_section (SCM mutex)
537Call @code{scm_frame_lock_mutex} on @var{mutex} and call
538@code{scm_frame_block_asyncs}. When @var{mutex} is false, a recursive
539mutex provided by Guile is used instead.
540
541The effect of a call to @code{scm_frame_critical_section} is that the
542current frame (@pxref{Frames}) turns into a critical section. Because
543of the locked mutex, no second thread can enter it concurrently and
544because of the blocked asyncs, no system async can reenter it from the
545current thread.
546
547When the current thread reenters the critical section anyway, the kind
548of @var{mutex} determines what happens: When @var{mutex} is recursive,
549the reentry is allowed. When it is a normal mutex, an error is
550signalled.
551@end deftypefn
552
553
b4fddbbe
MV
554@node Fluids and Dynamic States
555@subsection Fluids and Dynamic States
07d83abe
MV
556
557@cindex fluids
558
b4fddbbe
MV
559A @emph{fluid} is an object that can store one value per @emph{dynamic
560state}. Each thread has a current dynamic state, and when accessing a
561fluid, this current dynamic state is used to provide the actual value.
562In this way, fluids can be used for thread local storage, but they are
563in fact more flexible: dynamic states are objects of their own and can
564be made current for more than one thread at the same time, or only be
565made current temporarily, for example.
566
567Fluids can also be used to simulate the desirable effects of
568dynamically scoped variables. Dynamically scoped variables are useful
569when you want to set a variable to a value during some dynamic extent
570in the execution of your program and have them revert to their
571original value when the control flow is outside of this dynamic
572extent. See the description of @code{with-fluids} below for details.
07d83abe
MV
573
574New fluids are created with @code{make-fluid} and @code{fluid?} is
575used for testing whether an object is actually a fluid. The values
576stored in a fluid can be accessed with @code{fluid-ref} and
577@code{fluid-set!}.
578
579@deffn {Scheme Procedure} make-fluid
580@deffnx {C Function} scm_make_fluid ()
581Return a newly created fluid.
b4fddbbe
MV
582Fluids are objects that can hold one
583value per dynamic state. That is, modifications to this value are
584only visible to code that executes with the same dynamic state as
585the modifying code. When a new dynamic state is constructed, it
586inherits the values from its parent. Because each thread normally executes
587with its own dynamic state, you can use fluids for thread local storage.
07d83abe
MV
588@end deffn
589
590@deffn {Scheme Procedure} fluid? obj
591@deffnx {C Function} scm_fluid_p (obj)
592Return @code{#t} iff @var{obj} is a fluid; otherwise, return
593@code{#f}.
594@end deffn
595
596@deffn {Scheme Procedure} fluid-ref fluid
597@deffnx {C Function} scm_fluid_ref (fluid)
598Return the value associated with @var{fluid} in the current
599dynamic root. If @var{fluid} has not been set, then return
600@code{#f}.
601@end deffn
602
603@deffn {Scheme Procedure} fluid-set! fluid value
604@deffnx {C Function} scm_fluid_set_x (fluid, value)
605Set the value associated with @var{fluid} in the current dynamic root.
606@end deffn
607
608@code{with-fluids*} temporarily changes the values of one or more fluids,
609so that the given procedure and each procedure called by it access the
610given values. After the procedure returns, the old values are restored.
611
cdf1ad3b
MV
612@deffn {Scheme Procedure} with-fluid* fluid value thunk
613@deffnx {C Function} scm_with_fluid (fluid, value, thunk)
614Set @var{fluid} to @var{value} temporarily, and call @var{thunk}.
615@var{thunk} must be a procedure with no argument.
616@end deffn
617
07d83abe
MV
618@deffn {Scheme Procedure} with-fluids* fluids values thunk
619@deffnx {C Function} scm_with_fluids (fluids, values, thunk)
620Set @var{fluids} to @var{values} temporary, and call @var{thunk}.
621@var{fluids} must be a list of fluids and @var{values} must be the
622same number of their values to be applied. Each substitution is done
623in the order given. @var{thunk} must be a procedure with no argument.
624it is called inside a @code{dynamic-wind} and the fluids are
625set/restored when control enter or leaves the established dynamic
626extent.
627@end deffn
628
629@deffn {Scheme Macro} with-fluids ((fluid value) ...) body...
630Execute @var{body...} while each @var{fluid} is set to the
631corresponding @var{value}. Both @var{fluid} and @var{value} are
632evaluated and @var{fluid} must yield a fluid. @var{body...} is
633executed inside a @code{dynamic-wind} and the fluids are set/restored
634when control enter or leaves the established dynamic extent.
635@end deffn
636
637@deftypefn {C Function} SCM scm_c_with_fluids (SCM fluids, SCM vals, SCM (*cproc)(void *), void *data)
638@deftypefnx {C Function} SCM scm_c_with_fluid (SCM fluid, SCM val, SCM (*cproc)(void *), void *data)
639The function @code{scm_c_with_fluids} is like @code{scm_with_fluids}
640except that it takes a C function to call instead of a Scheme thunk.
641
642The function @code{scm_c_with_fluid} is similar but only allows one
643fluid to be set instead of a list.
644@end deftypefn
645
646@deftypefn {C Function} void scm_frame_fluid (SCM fluid, SCM val)
647This function must be used inside a pair of calls to
648@code{scm_frame_begin} and @code{scm_frame_end} (@pxref{Frames}).
649During the dynamic extent of the frame, the fluid @var{fluid} is set
650to @var{val}.
651
652More precisely, the value of the fluid is swapped with a `backup'
653value whenever the frame is entered or left. The backup value is
654initialized with the @var{val} argument.
655@end deftypefn
656
b4fddbbe
MV
657@deffn {Scheme Procedure} make-dynamic-state [parent]
658@deffnx {C Function} scm_make_dynamic_state (parent)
659Return a copy of the dynamic state object @var{parent}
660or of the current dynamic state when @var{parent} is omitted.
661@end deffn
662
663@deffn {Scheme Procedure} dynamic-state? obj
664@deffnx {C Function} scm_dynamic_state_p (obj)
665Return @code{#t} if @var{obj} is a dynamic state object;
666return @code{#f} otherwise.
667@end deffn
668
669@deftypefn {C Procedure} int scm_is_dynamic_state (SCM obj)
670Return non-zero if @var{obj} is a dynamic state object;
671return zero otherwise.
672@end deftypefn
673
674@deffn {Scheme Procedure} current-dynamic-state
675@deffnx {C Function} scm_current_dynamic_state ()
676Return the current dynamic state object.
677@end deffn
678
679@deffn {Scheme Procedure} set-current-dynamic-state state
680@deffnx {C Function} scm_set_current_dynamic_state (state)
681Set the current dynamic state object to @var{state}
682and return the previous current dynamic state object.
683@end deffn
684
685@deffn {Scheme Procedure} with-dynamic-state state proc
686@deffnx {C Function} scm_with_dynamic_state (state, proc)
687Call @var{proc} while @var{state} is the current dynamic
688state object.
689@end deffn
690
691@deftypefn {C Procedure} void scm_frame_current_dynamic_state (SCM state)
692Set the current dynamic state to @var{state} for the dynamic extent of
693the current frame.
694@end deftypefn
695
c2110081 696@deftypefn {C Procedure} {void *} scm_c_with_dynamic_state (SCM state, void *(*func)(void *), void *data)
b4fddbbe
MV
697Like @code{scm_with_dynamic_state}, but call @var{func} with
698@var{data}.
699@end deftypefn
700
07d83abe
MV
701@node Futures
702@subsection Futures
703@cindex futures
704
705Futures are a convenient way to run a calculation in a new thread, and
706only wait for the result when it's actually needed.
707
708Futures are similar to promises (@pxref{Delayed Evaluation}), in that
709they allow mainline code to continue immediately. But @code{delay}
710doesn't evaluate at all until forced, whereas @code{future} starts
711immediately in a new thread.
712
713@deffn {syntax} future expr
714Begin evaluating @var{expr} in a new thread, and return a ``future''
715object representing the calculation.
716@end deffn
717
718@deffn {Scheme Procedure} make-future thunk
719@deffnx {C Function} scm_make_future (thunk)
720Begin evaluating the call @code{(@var{thunk})} in a new thread, and
721return a ``future'' object representing the calculation.
722@end deffn
723
724@deffn {Scheme Procedure} future-ref f
725@deffnx {C Function} scm_future_ref (f)
726Return the value computed by the future @var{f}. If @var{f} has not
727yet finished executing then wait for it to do so.
728@end deffn
729
730
731@node Parallel Forms
732@subsection Parallel forms
733@cindex parallel forms
734
735The functions described in this section are available from
736
737@example
738(use-modules (ice-9 threads))
739@end example
740
741@deffn syntax parallel expr1 @dots{} exprN
af1323c5 742Evaluate each @var{expr} expression in parallel, each in its own thread.
07d83abe
MV
743Return the results as a set of @var{N} multiple values
744(@pxref{Multiple Values}).
745@end deffn
746
747@deffn syntax letpar ((var1 expr1) @dots{} (varN exprN)) body@dots{}
af1323c5 748Evaluate each @var{expr} in parallel, each in its own thread, then bind
07d83abe
MV
749the results to the corresponding @var{var} variables and evaluate
750@var{body}.
751
752@code{letpar} is like @code{let} (@pxref{Local Bindings}), but all the
753expressions for the bindings are evaluated in parallel.
754@end deffn
755
756@deffn {Scheme Procedure} par-map proc lst1 @dots{} lstN
757@deffnx {Scheme Procedure} par-for-each proc lst1 @dots{} lstN
758Call @var{proc} on the elements of the given lists. @code{par-map}
759returns a list comprising the return values from @var{proc}.
760@code{par-for-each} returns an unspecified value, but waits for all
761calls to complete.
762
763The @var{proc} calls are @code{(@var{proc} @var{elem1} @dots{}
764@var{elemN})}, where each @var{elem} is from the corresponding
765@var{lst}. Each @var{lst} must be the same length. The calls are
af1323c5 766made in parallel, each in its own thread.
07d83abe
MV
767
768These functions are like @code{map} and @code{for-each} (@pxref{List
769Mapping}), but make their @var{proc} calls in parallel.
770@end deffn
771
772@deffn {Scheme Procedure} n-par-map n proc lst1 @dots{} lstN
773@deffnx {Scheme Procedure} n-par-for-each n proc lst1 @dots{} lstN
774Call @var{proc} on the elements of the given lists, in the same way as
775@code{par-map} and @code{par-for-each} above, but use no more than
af1323c5 776@var{n} threads at any one time. The order in which calls are
07d83abe
MV
777initiated within that threads limit is unspecified.
778
779These functions are good for controlling resource consumption if
780@var{proc} calls might be costly, or if there are many to be made. On
781a dual-CPU system for instance @math{@var{n}=4} might be enough to
782keep the CPUs utilized, and not consume too much memory.
783@end deffn
784
785@deffn {Scheme Procedure} n-for-each-par-map n sproc pproc lst1 @dots{} lstN
786Apply @var{pproc} to the elements of the given lists, and apply
787@var{sproc} to each result returned by @var{pproc}. The final return
788value is unspecified, but all calls will have been completed before
789returning.
790
791The calls made are @code{(@var{sproc} (@var{pproc} @var{elem1} @dots{}
792@var{elemN}))}, where each @var{elem} is from the corresponding
793@var{lst}. Each @var{lst} must have the same number of elements.
794
af1323c5
KR
795The @var{pproc} calls are made in parallel, in separate threads. No more
796than @var{n} threads are used at any one time. The order in which
07d83abe
MV
797@var{pproc} calls are initiated within that limit is unspecified.
798
799The @var{sproc} calls are made serially, in list element order, one at
800a time. @var{pproc} calls on later elements may execute in parallel
801with the @var{sproc} calls. Exactly which thread makes each
802@var{sproc} call is unspecified.
803
804This function is designed for individual calculations that can be done
805in parallel, but with results needing to be handled serially, for
806instance to write them to a file. The @var{n} limit on threads
807controls system resource usage when there are many calculations or
808when they might be costly.
809
810It will be seen that @code{n-for-each-par-map} is like a combination
811of @code{n-par-map} and @code{for-each},
812
813@example
af1323c5 814(for-each sproc (n-par-map n pproc lst1 ... lstN))
07d83abe
MV
815@end example
816
817@noindent
818But the actual implementation is more efficient since each @var{sproc}
819call, in turn, can be initiated once the relevant @var{pproc} call has
820completed, it doesn't need to wait for all to finish.
821@end deffn
822
823
3cf066df 824
07d83abe
MV
825@c Local Variables:
826@c TeX-master: "guile.texi"
827@c End: