Check the return value of libc's functions to make `_FORTIFY_SOURCE=2' work.
[bpt/guile.git] / libguile / async.c
CommitLineData
634aa8de 1/* Copyright (C) 1995,1996,1997,1998,2000,2001, 2002, 2004, 2006, 2008 Free Software Foundation, Inc.
843e4e9d 2 *
73be1d9e
MV
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
843e4e9d 7 *
73be1d9e 8 * This library is distributed in the hope that it will be useful,
0f2d19dd 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
843e4e9d 12 *
73be1d9e
MV
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
92205699 15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
73be1d9e 16 */
1bbd0b84 17
1bbd0b84 18
0f2d19dd 19\f
dbb605f5 20#ifdef HAVE_CONFIG_H
ef92a2a2
RB
21# include <config.h>
22#endif
0f2d19dd 23
0f2d19dd 24#include <signal.h>
a0599745
MD
25#include "libguile/_scm.h"
26#include "libguile/eval.h"
27#include "libguile/throw.h"
28#include "libguile/root.h"
29#include "libguile/smob.h"
c96d76b8 30#include "libguile/lang.h"
e292f7aa 31#include "libguile/dynwind.h"
2d3179db 32#include "libguile/deprecation.h"
20e6290e 33
a0599745
MD
34#include "libguile/validate.h"
35#include "libguile/async.h"
0f2d19dd 36
95b88819
GH
37#ifdef HAVE_STRING_H
38#include <string.h>
39#endif
0f2d19dd
JB
40#ifdef HAVE_UNISTD_H
41#include <unistd.h>
42#endif
43
634aa8de
LC
44#include <full-write.h>
45
0f2d19dd
JB
46\f
47/* {Asynchronous Events}
48 *
2d3179db
MV
49 * There are two kinds of asyncs: system asyncs and user asyncs. The
50 * two kinds have some concepts in commen but work slightly
51 * differently and are not interchangeable.
0f2d19dd 52 *
2d3179db
MV
53 * System asyncs are used to run arbitrary code at the next safe point
54 * in a specified thread. You can use them to trigger execution of
55 * Scheme code from signal handlers or to interrupt a thread, for
56 * example.
0f2d19dd 57 *
2d3179db
MV
58 * Each thread has a list of 'activated asyncs', which is a normal
59 * Scheme list of procedures with zero arguments. When a thread
60 * executes a SCM_ASYNC_TICK statement (which is included in
61 * SCM_TICK), it will call all procedures on this list.
843e4e9d 62 *
2d3179db
MV
63 * Also, a thread will wake up when a procedure is added to its list
64 * of active asyncs and call them. After that, it will go to sleep
65 * again. (Not implemented yet.)
0f2d19dd 66 *
0f2d19dd 67 *
2d3179db
MV
68 * User asyncs are a little data structure that consists of a
69 * procedure of zero arguments and a mark. There are functions for
70 * setting the mark of a user async and for calling all procedures of
71 * marked asyncs in a given list. Nothing you couldn't quickly
72 * implement yourself.
0f2d19dd
JB
73 */
74
0f2d19dd 75
2d3179db 76\f
0f2d19dd 77
2d3179db 78/* User asyncs. */
e94e3f21 79
2d3179db 80static scm_t_bits tc16_async;
e94e3f21
ML
81
82/* cmm: this has SCM_ prefix because SCM_MAKE_VALIDATE expects it.
83 this is ugly. */
e841c3e0 84#define SCM_ASYNCP(X) SCM_TYP16_PREDICATE (tc16_async, X)
6182ceac 85#define VALIDATE_ASYNC(pos, a) SCM_MAKE_VALIDATE_MSG(pos, a, ASYNCP, "user async")
e94e3f21
ML
86
87#define ASYNC_GOT_IT(X) (SCM_CELL_WORD_0 (X) >> 16)
d1ca2c64 88#define SET_ASYNC_GOT_IT(X, V) (SCM_SET_CELL_WORD_0 ((X), SCM_TYP16 (X) | ((V) << 16)))
e94e3f21 89#define ASYNC_THUNK(X) SCM_CELL_OBJECT_1 (X)
0f2d19dd 90
0f2d19dd 91static SCM
2d3179db 92async_gc_mark (SCM obj)
0f2d19dd 93{
e94e3f21 94 return ASYNC_THUNK (obj);
0f2d19dd
JB
95}
96
843e4e9d 97SCM_DEFINE (scm_async, "async", 1, 0, 0,
811cf846
MG
98 (SCM thunk),
99 "Create a new async for the procedure @var{thunk}.")
1bbd0b84 100#define FUNC_NAME s_scm_async
0f2d19dd 101{
e94e3f21 102 SCM_RETURN_NEWSMOB (tc16_async, SCM_UNPACK (thunk));
0f2d19dd 103}
1bbd0b84 104#undef FUNC_NAME
0f2d19dd 105
843e4e9d 106SCM_DEFINE (scm_async_mark, "async-mark", 1, 0, 0,
1bbd0b84 107 (SCM a),
811cf846 108 "Mark the async @var{a} for future execution.")
1bbd0b84 109#define FUNC_NAME s_scm_async_mark
0f2d19dd 110{
e94e3f21 111 VALIDATE_ASYNC (1, a);
e94e3f21 112 SET_ASYNC_GOT_IT (a, 1);
0f2d19dd
JB
113 return SCM_UNSPECIFIED;
114}
1bbd0b84 115#undef FUNC_NAME
0f2d19dd 116
843e4e9d 117SCM_DEFINE (scm_run_asyncs, "run-asyncs", 1, 0, 0,
811cf846
MG
118 (SCM list_of_a),
119 "Execute all thunks from the asyncs of the list @var{list_of_a}.")
1bbd0b84 120#define FUNC_NAME s_scm_run_asyncs
0f2d19dd 121{
c96d76b8 122 while (! SCM_NULL_OR_NIL_P (list_of_a))
0f2d19dd
JB
123 {
124 SCM a;
9f0e55a6 125 SCM_VALIDATE_CONS (1, list_of_a);
1bbd0b84 126 a = SCM_CAR (list_of_a);
e94e3f21 127 VALIDATE_ASYNC (SCM_ARG1, a);
e94e3f21 128 if (ASYNC_GOT_IT (a))
0f2d19dd 129 {
e94e3f21 130 SET_ASYNC_GOT_IT (a, 0);
fdc28395 131 scm_call_0 (ASYNC_THUNK (a));
0f2d19dd 132 }
1bbd0b84 133 list_of_a = SCM_CDR (list_of_a);
0f2d19dd
JB
134 }
135 return SCM_BOOL_T;
136}
1bbd0b84 137#undef FUNC_NAME
0f2d19dd
JB
138
139\f
140
9de87eea
MV
141static scm_i_pthread_mutex_t async_mutex = SCM_I_PTHREAD_MUTEX_INITIALIZER;
142
2d3179db 143/* System asyncs. */
0f2d19dd 144
2d3179db
MV
145void
146scm_async_click ()
0f2d19dd 147{
9de87eea
MV
148 scm_i_thread *t = SCM_I_CURRENT_THREAD;
149 SCM asyncs;
150
402858a4 151 /* Reset pending_asyncs even when asyncs are blocked and not really
9de87eea
MV
152 executed since this will avoid future futile calls to this
153 function. When asyncs are unblocked again, this function is
154 invoked even when pending_asyncs is zero.
402858a4 155 */
2d3179db 156
9de87eea
MV
157 scm_i_scm_pthread_mutex_lock (&async_mutex);
158 t->pending_asyncs = 0;
159 if (t->block_asyncs == 0)
2d3179db 160 {
9de87eea
MV
161 asyncs = t->active_asyncs;
162 t->active_asyncs = SCM_EOL;
163 }
164 else
165 asyncs = SCM_EOL;
166 scm_i_pthread_mutex_unlock (&async_mutex);
167
168 while (scm_is_pair (asyncs))
169 {
170 SCM next = SCM_CDR (asyncs);
171 SCM_SETCDR (asyncs, SCM_BOOL_F);
172 scm_call_0 (SCM_CAR (asyncs));
173 asyncs = next;
2d3179db 174 }
0f2d19dd
JB
175}
176
100ae50d
DH
177#if (SCM_ENABLE_DEPRECATED == 1)
178
2d3179db
MV
179SCM_DEFINE (scm_system_async, "system-async", 1, 0, 0,
180 (SCM thunk),
181 "This function is deprecated. You can use @var{thunk} directly\n"
100ae50d 182 "instead of explicitely creating an async object.\n")
2d3179db
MV
183#define FUNC_NAME s_scm_system_async
184{
185 scm_c_issue_deprecation_warning
186 ("'system-async' is deprecated. "
187 "Use the procedure directly with 'system-async-mark'.");
188 return thunk;
189}
190#undef FUNC_NAME
0f2d19dd 191
100ae50d
DH
192#endif /* SCM_ENABLE_DEPRECATED == 1 */
193
2d3179db 194void
9de87eea 195scm_i_queue_async_cell (SCM c, scm_i_thread *t)
2d3179db 196{
9de87eea
MV
197 SCM sleep_object;
198 scm_i_pthread_mutex_t *sleep_mutex;
199 int sleep_fd;
200 SCM p;
201
202 scm_i_scm_pthread_mutex_lock (&async_mutex);
203 p = t->active_asyncs;
402858a4 204 SCM_SETCDR (c, SCM_EOL);
9de87eea
MV
205 if (!scm_is_pair (p))
206 t->active_asyncs = c;
402858a4 207 else
2d3179db 208 {
402858a4 209 SCM pp;
9de87eea 210 while (scm_is_pair (pp = SCM_CDR (p)))
f6b44bd9 211 {
9de87eea
MV
212 if (scm_is_eq (SCM_CAR (p), SCM_CAR (c)))
213 {
214 scm_i_pthread_mutex_unlock (&async_mutex);
215 return;
216 }
402858a4 217 p = pp;
f6b44bd9 218 }
402858a4 219 SCM_SETCDR (p, c);
2d3179db 220 }
9de87eea
MV
221 t->pending_asyncs = 1;
222 sleep_object = t->sleep_object;
223 sleep_mutex = t->sleep_mutex;
224 sleep_fd = t->sleep_fd;
225 scm_i_pthread_mutex_unlock (&async_mutex);
226
227 if (sleep_mutex)
228 {
229 /* By now, the thread T might be out of its sleep already, or
230 might even be in the next, unrelated sleep. Interrupting it
231 anyway does no harm, however.
232
233 The important thing to prevent here is to signal sleep_cond
234 before T waits on it. This can not happen since T has
235 sleep_mutex locked while setting t->sleep_mutex and will only
236 unlock it again while waiting on sleep_cond.
237 */
238 scm_i_scm_pthread_mutex_lock (sleep_mutex);
239 scm_i_pthread_cond_signal (&t->sleep_cond);
240 scm_i_pthread_mutex_unlock (sleep_mutex);
241 }
242
243 if (sleep_fd >= 0)
244 {
245 char dummy = 0;
634aa8de 246
9de87eea
MV
247 /* Likewise, T might already been done with sleeping here, but
248 interrupting it once too often does no harm. T might also
249 not yet have started sleeping, but this is no problem either
250 since the data written to a pipe will not be lost, unlike a
634aa8de
LC
251 condition variable signal. */
252 full_write (sleep_fd, &dummy, 1);
9de87eea
MV
253 }
254
255 /* This is needed to protect sleep_mutex.
256 */
257 scm_remember_upto_here_1 (sleep_object);
258}
259
260int
261scm_i_setup_sleep (scm_i_thread *t,
262 SCM sleep_object, scm_i_pthread_mutex_t *sleep_mutex,
263 int sleep_fd)
264{
265 int pending;
266
267 scm_i_scm_pthread_mutex_lock (&async_mutex);
268 pending = t->pending_asyncs;
269 if (!pending)
270 {
271 t->sleep_object = sleep_object;
272 t->sleep_mutex = sleep_mutex;
273 t->sleep_fd = sleep_fd;
274 }
275 scm_i_pthread_mutex_unlock (&async_mutex);
276 return pending;
277}
278
279void
280scm_i_reset_sleep (scm_i_thread *t)
281{
282 scm_i_scm_pthread_mutex_lock (&async_mutex);
283 t->sleep_object = SCM_BOOL_F;
284 t->sleep_mutex = NULL;
285 t->sleep_fd = -1;
286 scm_i_pthread_mutex_unlock (&async_mutex);
2d3179db 287}
0f2d19dd 288
2d3179db
MV
289SCM_DEFINE (scm_system_async_mark_for_thread, "system-async-mark", 1, 1, 0,
290 (SCM proc, SCM thread),
0a50eeaa
NJ
291 "Mark @var{proc} (a procedure with zero arguments) for future execution\n"
292 "in @var{thread}. If @var{proc} has already been marked for\n"
293 "@var{thread} but has not been executed yet, this call has no effect.\n"
294 "If @var{thread} is omitted, the thread that called\n"
295 "@code{system-async-mark} is used.\n\n"
296 "This procedure is not safe to be called from C signal handlers. Use\n"
297 "@code{scm_sigaction} or @code{scm_sigaction_for_thread} to install\n"
298 "signal handlers.")
2d3179db
MV
299#define FUNC_NAME s_scm_system_async_mark_for_thread
300{
9de87eea
MV
301 /* The current thread might not have a handle yet. This can happen
302 when the GC runs immediately before allocating the handle. At
303 the end of that GC, a system async might be marked. Thus, we can
304 not use scm_current_thread here.
305 */
306
307 scm_i_thread *t;
308
028e573c 309 if (SCM_UNBNDP (thread))
9de87eea 310 t = SCM_I_CURRENT_THREAD;
028e573c 311 else
402858a4
MV
312 {
313 SCM_VALIDATE_THREAD (2, thread);
314 if (scm_c_thread_exited_p (thread))
315 SCM_MISC_ERROR ("thread has already exited", SCM_EOL);
9de87eea 316 t = SCM_I_THREAD_DATA (thread);
402858a4 317 }
9de87eea 318 scm_i_queue_async_cell (scm_cons (proc, SCM_BOOL_F), t);
2d3179db
MV
319 return SCM_UNSPECIFIED;
320}
321#undef FUNC_NAME
9f0e55a6 322
2d3179db
MV
323SCM
324scm_system_async_mark (SCM proc)
325#define FUNC_NAME s_scm_system_async_mark_for_thread
0f2d19dd 326{
2d3179db 327 return scm_system_async_mark_for_thread (proc, SCM_UNDEFINED);
0f2d19dd 328}
1bbd0b84 329#undef FUNC_NAME
0f2d19dd
JB
330
331\f
332
333
2d3179db
MV
334SCM_DEFINE (scm_noop, "noop", 0, 0, 1,
335 (SCM args),
336 "Do nothing. When called without arguments, return @code{#f},\n"
337 "otherwise return the first argument.")
338#define FUNC_NAME s_scm_noop
0f2d19dd 339{
2d3179db
MV
340 SCM_VALIDATE_REST_ARGUMENT (args);
341 return (SCM_NULL_OR_NIL_P (args) ? SCM_BOOL_F : SCM_CAR (args));
0f2d19dd 342}
1bbd0b84 343#undef FUNC_NAME
0f2d19dd 344
0f2d19dd 345
0f2d19dd
JB
346\f
347
100ae50d 348#if (SCM_ENABLE_DEPRECATED == 1)
e292f7aa 349
843e4e9d 350SCM_DEFINE (scm_unmask_signals, "unmask-signals", 0, 0, 0,
811cf846
MG
351 (),
352 "Unmask signals. The returned value is not specified.")
1bbd0b84 353#define FUNC_NAME s_scm_unmask_signals
0f2d19dd 354{
9de87eea
MV
355 scm_i_thread *t = SCM_I_CURRENT_THREAD;
356
e292f7aa
MV
357 scm_c_issue_deprecation_warning
358 ("'unmask-signals' is deprecated. "
359 "Use 'call-with-blocked-asyncs' instead.");
360
9de87eea 361 if (t->block_asyncs == 0)
e292f7aa 362 SCM_MISC_ERROR ("signals already unmasked", SCM_EOL);
9de87eea 363 t->block_asyncs = 0;
402858a4 364 scm_async_click ();
0f2d19dd
JB
365 return SCM_UNSPECIFIED;
366}
1bbd0b84 367#undef FUNC_NAME
0f2d19dd
JB
368
369
843e4e9d 370SCM_DEFINE (scm_mask_signals, "mask-signals", 0, 0, 0,
811cf846
MG
371 (),
372 "Mask signals. The returned value is not specified.")
1bbd0b84 373#define FUNC_NAME s_scm_mask_signals
0f2d19dd 374{
9de87eea
MV
375 scm_i_thread *t = SCM_I_CURRENT_THREAD;
376
e292f7aa
MV
377 scm_c_issue_deprecation_warning
378 ("'mask-signals' is deprecated. Use 'call-with-blocked-asyncs' instead.");
379
9de87eea 380 if (t->block_asyncs > 0)
e292f7aa 381 SCM_MISC_ERROR ("signals already masked", SCM_EOL);
9de87eea 382 t->block_asyncs = 1;
0f2d19dd
JB
383 return SCM_UNSPECIFIED;
384}
1bbd0b84 385#undef FUNC_NAME
0f2d19dd 386
100ae50d 387#endif /* SCM_ENABLE_DEPRECATED == 1 */
e292f7aa
MV
388
389static void
9de87eea 390increase_block (void *data)
e292f7aa 391{
9de87eea 392 ((scm_i_thread *)data)->block_asyncs++;
e292f7aa
MV
393}
394
395static void
9de87eea 396decrease_block (void *data)
e292f7aa 397{
9de87eea 398 if (--((scm_i_thread *)data)->block_asyncs == 0)
402858a4 399 scm_async_click ();
e292f7aa
MV
400}
401
402SCM_DEFINE (scm_call_with_blocked_asyncs, "call-with-blocked-asyncs", 1, 0, 0,
403 (SCM proc),
404 "Call @var{proc} with no arguments and block the execution\n"
405 "of system asyncs by one level for the current thread while\n"
406 "it is running. Return the value returned by @var{proc}.\n")
407#define FUNC_NAME s_scm_call_with_blocked_asyncs
408{
409 return scm_internal_dynamic_wind (increase_block,
410 (scm_t_inner) scm_call_0,
411 decrease_block,
9de87eea
MV
412 (void *)proc,
413 SCM_I_CURRENT_THREAD);
e292f7aa
MV
414}
415#undef FUNC_NAME
416
417void *
418scm_c_call_with_blocked_asyncs (void *(*proc) (void *data), void *data)
419{
402858a4
MV
420 return (void *)scm_internal_dynamic_wind (increase_block,
421 (scm_t_inner) proc,
422 decrease_block,
9de87eea
MV
423 data,
424 SCM_I_CURRENT_THREAD);
e292f7aa
MV
425}
426
427
428SCM_DEFINE (scm_call_with_unblocked_asyncs, "call-with-unblocked-asyncs", 1, 0, 0,
429 (SCM proc),
430 "Call @var{proc} with no arguments and unblock the execution\n"
431 "of system asyncs by one level for the current thread while\n"
432 "it is running. Return the value returned by @var{proc}.\n")
433#define FUNC_NAME s_scm_call_with_unblocked_asyncs
434{
9de87eea 435 if (SCM_I_CURRENT_THREAD->block_asyncs == 0)
e292f7aa
MV
436 SCM_MISC_ERROR ("asyncs already unblocked", SCM_EOL);
437 return scm_internal_dynamic_wind (decrease_block,
438 (scm_t_inner) scm_call_0,
439 increase_block,
9de87eea
MV
440 (void *)proc,
441 SCM_I_CURRENT_THREAD);
e292f7aa
MV
442}
443#undef FUNC_NAME
444
445void *
446scm_c_call_with_unblocked_asyncs (void *(*proc) (void *data), void *data)
447{
9de87eea 448 if (SCM_I_CURRENT_THREAD->block_asyncs == 0)
e292f7aa
MV
449 scm_misc_error ("scm_c_call_with_unblocked_asyncs",
450 "asyncs already unblocked", SCM_EOL);
402858a4
MV
451 return (void *)scm_internal_dynamic_wind (decrease_block,
452 (scm_t_inner) proc,
453 increase_block,
9de87eea
MV
454 data,
455 SCM_I_CURRENT_THREAD);
e292f7aa
MV
456}
457
b57a0953 458void
661ae7ab 459scm_dynwind_block_asyncs ()
b57a0953 460{
9de87eea 461 scm_i_thread *t = SCM_I_CURRENT_THREAD;
661ae7ab
MV
462 scm_dynwind_rewind_handler (increase_block, t, SCM_F_WIND_EXPLICITLY);
463 scm_dynwind_unwind_handler (decrease_block, t, SCM_F_WIND_EXPLICITLY);
b57a0953
MV
464}
465
466void
661ae7ab 467scm_dynwind_unblock_asyncs ()
b57a0953 468{
9de87eea
MV
469 scm_i_thread *t = SCM_I_CURRENT_THREAD;
470 if (t->block_asyncs == 0)
b57a0953
MV
471 scm_misc_error ("scm_with_unblocked_asyncs",
472 "asyncs already unblocked", SCM_EOL);
661ae7ab
MV
473 scm_dynwind_rewind_handler (decrease_block, t, SCM_F_WIND_EXPLICITLY);
474 scm_dynwind_unwind_handler (increase_block, t, SCM_F_WIND_EXPLICITLY);
b57a0953
MV
475}
476
477
0f2d19dd
JB
478\f
479
0f2d19dd
JB
480void
481scm_init_async ()
0f2d19dd 482{
939794ce 483 scm_asyncs = SCM_EOL;
73ea78af 484 tc16_async = scm_make_smob_type ("async", 0);
2d3179db 485 scm_set_smob_mark (tc16_async, async_gc_mark);
73ea78af 486
a0599745 487#include "libguile/async.x"
0f2d19dd 488}
89e00824
ML
489
490/*
491 Local Variables:
492 c-file-style: "gnu"
493 End:
494*/