*** empty log message ***
[bpt/guile.git] / libguile / async.c
CommitLineData
2d3179db 1/* Copyright (C) 1995,1996,1997,1998,2000,2001, 2002 Free Software Foundation, Inc.
843e4e9d 2 *
0f2d19dd
JB
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
6 * any later version.
843e4e9d 7 *
0f2d19dd
JB
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
843e4e9d 12 *
0f2d19dd
JB
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
82892bed
JB
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
0f2d19dd
JB
17 *
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
20 *
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
26 *
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
29 *
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
37 *
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
82892bed 40 * If you do not wish that, delete this exception notice. */
1bbd0b84 41
1bbd0b84 42
0f2d19dd
JB
43\f
44
0f2d19dd 45#include <signal.h>
a0599745
MD
46#include "libguile/_scm.h"
47#include "libguile/eval.h"
48#include "libguile/throw.h"
49#include "libguile/root.h"
50#include "libguile/smob.h"
c96d76b8 51#include "libguile/lang.h"
e292f7aa 52#include "libguile/dynwind.h"
2d3179db 53#include "libguile/deprecation.h"
20e6290e 54
a0599745
MD
55#include "libguile/validate.h"
56#include "libguile/async.h"
0f2d19dd 57
95b88819
GH
58#ifdef HAVE_STRING_H
59#include <string.h>
60#endif
0f2d19dd
JB
61#ifdef HAVE_UNISTD_H
62#include <unistd.h>
63#endif
64
e292f7aa
MV
65/* This is not used for anything except checking that DEFER_INTS and
66 ALLOW_INTS are used properly.
67 */
68int scm_ints_disabled = 1;
0f2d19dd
JB
69
70\f
71/* {Asynchronous Events}
72 *
2d3179db
MV
73 * There are two kinds of asyncs: system asyncs and user asyncs. The
74 * two kinds have some concepts in commen but work slightly
75 * differently and are not interchangeable.
0f2d19dd 76 *
2d3179db
MV
77 * System asyncs are used to run arbitrary code at the next safe point
78 * in a specified thread. You can use them to trigger execution of
79 * Scheme code from signal handlers or to interrupt a thread, for
80 * example.
0f2d19dd 81 *
2d3179db
MV
82 * Each thread has a list of 'activated asyncs', which is a normal
83 * Scheme list of procedures with zero arguments. When a thread
84 * executes a SCM_ASYNC_TICK statement (which is included in
85 * SCM_TICK), it will call all procedures on this list.
843e4e9d 86 *
2d3179db
MV
87 * Also, a thread will wake up when a procedure is added to its list
88 * of active asyncs and call them. After that, it will go to sleep
89 * again. (Not implemented yet.)
0f2d19dd 90 *
0f2d19dd 91 *
2d3179db
MV
92 * User asyncs are a little data structure that consists of a
93 * procedure of zero arguments and a mark. There are functions for
94 * setting the mark of a user async and for calling all procedures of
95 * marked asyncs in a given list. Nothing you couldn't quickly
96 * implement yourself.
0f2d19dd
JB
97 */
98
0f2d19dd 99
2d3179db 100\f
0f2d19dd 101
2d3179db 102/* User asyncs. */
e94e3f21 103
2d3179db 104static scm_t_bits tc16_async;
e94e3f21
ML
105
106/* cmm: this has SCM_ prefix because SCM_MAKE_VALIDATE expects it.
107 this is ugly. */
e841c3e0 108#define SCM_ASYNCP(X) SCM_TYP16_PREDICATE (tc16_async, X)
34d19ef6 109#define VALIDATE_ASYNC(pos, a) SCM_MAKE_VALIDATE(pos, a, ASYNCP)
e94e3f21
ML
110
111#define ASYNC_GOT_IT(X) (SCM_CELL_WORD_0 (X) >> 16)
d1ca2c64 112#define SET_ASYNC_GOT_IT(X, V) (SCM_SET_CELL_WORD_0 ((X), SCM_TYP16 (X) | ((V) << 16)))
e94e3f21 113#define ASYNC_THUNK(X) SCM_CELL_OBJECT_1 (X)
0f2d19dd 114
0f2d19dd 115static SCM
2d3179db 116async_gc_mark (SCM obj)
0f2d19dd 117{
e94e3f21 118 return ASYNC_THUNK (obj);
0f2d19dd
JB
119}
120
843e4e9d 121SCM_DEFINE (scm_async, "async", 1, 0, 0,
811cf846
MG
122 (SCM thunk),
123 "Create a new async for the procedure @var{thunk}.")
1bbd0b84 124#define FUNC_NAME s_scm_async
0f2d19dd 125{
e94e3f21 126 SCM_RETURN_NEWSMOB (tc16_async, SCM_UNPACK (thunk));
0f2d19dd 127}
1bbd0b84 128#undef FUNC_NAME
0f2d19dd 129
843e4e9d 130SCM_DEFINE (scm_async_mark, "async-mark", 1, 0, 0,
1bbd0b84 131 (SCM a),
811cf846 132 "Mark the async @var{a} for future execution.")
1bbd0b84 133#define FUNC_NAME s_scm_async_mark
0f2d19dd 134{
e94e3f21 135 VALIDATE_ASYNC (1, a);
e94e3f21 136 SET_ASYNC_GOT_IT (a, 1);
0f2d19dd
JB
137 return SCM_UNSPECIFIED;
138}
1bbd0b84 139#undef FUNC_NAME
0f2d19dd 140
843e4e9d 141SCM_DEFINE (scm_run_asyncs, "run-asyncs", 1, 0, 0,
811cf846
MG
142 (SCM list_of_a),
143 "Execute all thunks from the asyncs of the list @var{list_of_a}.")
1bbd0b84 144#define FUNC_NAME s_scm_run_asyncs
0f2d19dd 145{
c96d76b8 146 while (! SCM_NULL_OR_NIL_P (list_of_a))
0f2d19dd
JB
147 {
148 SCM a;
9f0e55a6 149 SCM_VALIDATE_CONS (1, list_of_a);
1bbd0b84 150 a = SCM_CAR (list_of_a);
e94e3f21 151 VALIDATE_ASYNC (SCM_ARG1, a);
e94e3f21 152 if (ASYNC_GOT_IT (a))
0f2d19dd 153 {
e94e3f21 154 SET_ASYNC_GOT_IT (a, 0);
fdc28395 155 scm_call_0 (ASYNC_THUNK (a));
0f2d19dd 156 }
1bbd0b84 157 list_of_a = SCM_CDR (list_of_a);
0f2d19dd
JB
158 }
159 return SCM_BOOL_T;
160}
1bbd0b84 161#undef FUNC_NAME
0f2d19dd
JB
162
163\f
164
2d3179db 165/* System asyncs. */
0f2d19dd 166
2d3179db
MV
167void
168scm_async_click ()
0f2d19dd 169{
2d3179db
MV
170 SCM asyncs;
171
e292f7aa 172 if (scm_root->block_asyncs == 0)
2d3179db 173 {
e292f7aa 174 while (!SCM_NULLP(asyncs = scm_root->active_asyncs))
2d3179db 175 {
e292f7aa 176 scm_root->active_asyncs = SCM_EOL;
2d3179db
MV
177 do
178 {
179 SCM c = SCM_CDR (asyncs);
f6b44bd9 180 SCM_SETCDR (asyncs, SCM_BOOL_F);
2d3179db
MV
181 scm_call_0 (SCM_CAR (asyncs));
182 asyncs = c;
183 }
184 while (!SCM_NULLP(asyncs));
185 }
186 }
0f2d19dd
JB
187}
188
2d3179db
MV
189SCM_DEFINE (scm_system_async, "system-async", 1, 0, 0,
190 (SCM thunk),
191 "This function is deprecated. You can use @var{thunk} directly\n"
192 "instead of explicitely creating a asnc object.\n")
193#define FUNC_NAME s_scm_system_async
194{
195 scm_c_issue_deprecation_warning
196 ("'system-async' is deprecated. "
197 "Use the procedure directly with 'system-async-mark'.");
198 return thunk;
199}
200#undef FUNC_NAME
0f2d19dd 201
2d3179db
MV
202void
203scm_i_queue_async_cell (SCM c, scm_root_state *root)
204{
f6b44bd9 205 if (SCM_CDR (c) == SCM_BOOL_F)
2d3179db 206 {
f6b44bd9
MV
207 SCM p = root->active_asyncs;
208 SCM_SETCDR (c, SCM_EOL);
209 if (p == SCM_EOL)
210 root->active_asyncs = c;
211 else
212 {
213 SCM pp;
214 while ((pp = SCM_CDR(p)) != SCM_EOL)
215 {
216 if (SCM_CAR (p) == SCM_CAR (c))
217 return;
218 p = pp;
219 }
220 SCM_SETCDR (p, c);
221 }
2d3179db
MV
222 }
223}
0f2d19dd 224
2d3179db
MV
225SCM_DEFINE (scm_system_async_mark_for_thread, "system-async-mark", 1, 1, 0,
226 (SCM proc, SCM thread),
227 "Register the procedure @var{proc} for future execution\n"
228 "in @var{thread}. When @var{thread} is not specified,\n"
229 "use the current thread.")
230#define FUNC_NAME s_scm_system_async_mark_for_thread
231{
f6b44bd9 232 scm_i_queue_async_cell (scm_cons (proc, SCM_BOOL_F),
2d3179db
MV
233 (SCM_UNBNDP (thread)
234 ? scm_root
235 : scm_i_thread_root (thread)));
236 return SCM_UNSPECIFIED;
237}
238#undef FUNC_NAME
9f0e55a6 239
2d3179db
MV
240SCM
241scm_system_async_mark (SCM proc)
242#define FUNC_NAME s_scm_system_async_mark_for_thread
0f2d19dd 243{
2d3179db 244 return scm_system_async_mark_for_thread (proc, SCM_UNDEFINED);
0f2d19dd 245}
1bbd0b84 246#undef FUNC_NAME
0f2d19dd
JB
247
248\f
249
250
2d3179db
MV
251SCM_DEFINE (scm_noop, "noop", 0, 0, 1,
252 (SCM args),
253 "Do nothing. When called without arguments, return @code{#f},\n"
254 "otherwise return the first argument.")
255#define FUNC_NAME s_scm_noop
0f2d19dd 256{
2d3179db
MV
257 SCM_VALIDATE_REST_ARGUMENT (args);
258 return (SCM_NULL_OR_NIL_P (args) ? SCM_BOOL_F : SCM_CAR (args));
0f2d19dd 259}
1bbd0b84 260#undef FUNC_NAME
0f2d19dd 261
0f2d19dd 262
0f2d19dd
JB
263\f
264
e292f7aa
MV
265#if (SCM_DEBUG_DEPRECATED == 0)
266
843e4e9d 267SCM_DEFINE (scm_unmask_signals, "unmask-signals", 0, 0, 0,
811cf846
MG
268 (),
269 "Unmask signals. The returned value is not specified.")
1bbd0b84 270#define FUNC_NAME s_scm_unmask_signals
0f2d19dd 271{
e292f7aa
MV
272 scm_c_issue_deprecation_warning
273 ("'unmask-signals' is deprecated. "
274 "Use 'call-with-blocked-asyncs' instead.");
275
276 if (scm_root->block_asyncs == 0)
277 SCM_MISC_ERROR ("signals already unmasked", SCM_EOL);
278 scm_root->block_asyncs = 0;
0f2d19dd
JB
279 return SCM_UNSPECIFIED;
280}
1bbd0b84 281#undef FUNC_NAME
0f2d19dd
JB
282
283
843e4e9d 284SCM_DEFINE (scm_mask_signals, "mask-signals", 0, 0, 0,
811cf846
MG
285 (),
286 "Mask signals. The returned value is not specified.")
1bbd0b84 287#define FUNC_NAME s_scm_mask_signals
0f2d19dd 288{
e292f7aa
MV
289 scm_c_issue_deprecation_warning
290 ("'mask-signals' is deprecated. Use 'call-with-blocked-asyncs' instead.");
291
292 if (scm_root->block_asyncs > 0)
293 SCM_MISC_ERROR ("signals already masked", SCM_EOL);
294 scm_root->block_asyncs = 1;
0f2d19dd
JB
295 return SCM_UNSPECIFIED;
296}
1bbd0b84 297#undef FUNC_NAME
0f2d19dd 298
e292f7aa
MV
299#endif /* SCM_DEBUG_DEPRECATED == 0 */
300
301static void
302increase_block (void *unused)
303{
304 scm_root->block_asyncs++;
305}
306
307static void
308decrease_block (void *unused)
309{
310 scm_root->block_asyncs--;
311}
312
313SCM_DEFINE (scm_call_with_blocked_asyncs, "call-with-blocked-asyncs", 1, 0, 0,
314 (SCM proc),
315 "Call @var{proc} with no arguments and block the execution\n"
316 "of system asyncs by one level for the current thread while\n"
317 "it is running. Return the value returned by @var{proc}.\n")
318#define FUNC_NAME s_scm_call_with_blocked_asyncs
319{
320 return scm_internal_dynamic_wind (increase_block,
321 (scm_t_inner) scm_call_0,
322 decrease_block,
323 proc, NULL);
324}
325#undef FUNC_NAME
326
327void *
328scm_c_call_with_blocked_asyncs (void *(*proc) (void *data), void *data)
329{
330 return scm_internal_dynamic_wind (increase_block,
331 (scm_t_inner) proc,
332 decrease_block,
333 data, NULL);
334}
335
336
337SCM_DEFINE (scm_call_with_unblocked_asyncs, "call-with-unblocked-asyncs", 1, 0, 0,
338 (SCM proc),
339 "Call @var{proc} with no arguments and unblock the execution\n"
340 "of system asyncs by one level for the current thread while\n"
341 "it is running. Return the value returned by @var{proc}.\n")
342#define FUNC_NAME s_scm_call_with_unblocked_asyncs
343{
344 if (scm_root->block_asyncs == 0)
345 SCM_MISC_ERROR ("asyncs already unblocked", SCM_EOL);
346 return scm_internal_dynamic_wind (decrease_block,
347 (scm_t_inner) scm_call_0,
348 increase_block,
349 proc, NULL);
350}
351#undef FUNC_NAME
352
353void *
354scm_c_call_with_unblocked_asyncs (void *(*proc) (void *data), void *data)
355{
356 if (scm_root->block_asyncs == 0)
357 scm_misc_error ("scm_c_call_with_unblocked_asyncs",
358 "asyncs already unblocked", SCM_EOL);
359 return scm_internal_dynamic_wind (decrease_block,
360 (scm_t_inner) proc,
361 increase_block,
362 data, NULL);
363}
364
0f2d19dd
JB
365\f
366
0f2d19dd
JB
367void
368scm_init_async ()
0f2d19dd 369{
939794ce 370 scm_asyncs = SCM_EOL;
73ea78af 371 tc16_async = scm_make_smob_type ("async", 0);
2d3179db 372 scm_set_smob_mark (tc16_async, async_gc_mark);
73ea78af 373
a0599745 374#include "libguile/async.x"
0f2d19dd 375}
89e00824
ML
376
377/*
378 Local Variables:
379 c-file-style: "gnu"
380 End:
381*/