(scm_m_cond): Recognize SRFI 61 cond syntax.
[bpt/guile.git] / libguile / fluids.c
1 /* Copyright (C) 1996,1997,2000,2001, 2004 Free Software Foundation, Inc.
2 *
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.
7 *
8 * This library 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 GNU
11 * Lesser General Public License for more details.
12 *
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
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17
18 #include <stdio.h>
19 #include <string.h>
20
21 #include "libguile/_scm.h"
22 #include "libguile/print.h"
23 #include "libguile/smob.h"
24 #include "libguile/dynwind.h"
25 #include "libguile/fluids.h"
26 #include "libguile/alist.h"
27 #include "libguile/eval.h"
28 #include "libguile/ports.h"
29 #include "libguile/deprecation.h"
30 #include "libguile/lang.h"
31 #include "libguile/validate.h"
32
33 #define FLUID_GROW 20
34
35 /* A lot of the complexity below stems from the desire to reuse fluid
36 slots. Normally, fluids should be pretty global and long-lived
37 things, so that reusing their slots should not be overly critical,
38 but it is the right thing to do nevertheless. The code therefore
39 puts the burdon on allocating and collection fluids and keeps
40 accessing fluids lock free. This is achieved by manipulating the
41 global state of the fluid machinery mostly in single threaded
42 sections.
43
44 Reusing a fluid slot means that it must be reset to #f in all
45 dynamic states. We do this by maintaining a weak list of all
46 dynamic states, which is used after a GC to do the resetting.
47
48 Also, the fluid vectors in the dynamic states need to grow from
49 time to time when more fluids are created. We do this in a single
50 threaded section so that threads do not need to lock when accessing
51 a fluid in the normal way.
52 */
53
54 static scm_i_pthread_mutex_t fluid_admin_mutex = SCM_I_PTHREAD_MUTEX_INITIALIZER;
55
56 /* Protected by fluid_admin_mutex, but also accessed during GC. See
57 next_fluid_num for a discussion of this.
58 */
59 static size_t allocated_fluids_len = 0;
60 static size_t allocated_fluids_num = 0;
61 static char *allocated_fluids = NULL;
62
63 static scm_t_bits tc16_fluid;
64
65 #define IS_FLUID(x) SCM_SMOB_PREDICATE(tc16_fluid, (x))
66 #define FLUID_NUM(x) ((size_t)SCM_SMOB_DATA(x))
67 #define FLUID_NEXT(x) SCM_SMOB_OBJECT_2(x)
68 #define FLUID_NEXT_LOC(x) SCM_SMOB_OBJECT_2_LOC(x)
69 #define SET_FLUID_NEXT(x,y) SCM_SET_SMOB_OBJECT_2((x), (y))
70
71 static scm_t_bits tc16_dynamic_state;
72
73 #define IS_DYNAMIC_STATE(x) SCM_SMOB_PREDICATE(tc16_dynamic_state, (x))
74 #define DYNAMIC_STATE_FLUIDS(x) SCM_SMOB_OBJECT(x)
75 #define SET_DYNAMIC_STATE_FLUIDS(x, y) SCM_SET_SMOB_OBJECT((x), (y))
76 #define DYNAMIC_STATE_NEXT(x) SCM_SMOB_OBJECT_2(x)
77 #define DYNAMIC_STATE_NEXT_LOC(x) SCM_SMOB_OBJECT_2_LOC(x)
78 #define SET_DYNAMIC_STATE_NEXT(x, y) SCM_SET_SMOB_OBJECT_2((x), (y))
79
80 /* Weak lists of all dynamic states and all fluids.
81 */
82 static SCM all_dynamic_states = SCM_EOL;
83 static SCM all_fluids = SCM_EOL;
84
85 /* Make sure that all states have the right size. This must be called
86 while fluid_admin_mutex is held.
87 */
88 static void
89 resize_all_states ()
90 {
91 SCM new_vectors, state;
92
93 /* Replacing the vector of a dynamic state must be done atomically:
94 the old values must be copied into the new vector and the new
95 vector must be installed without someone modifying the old vector
96 concurrently. Since accessing a fluid should be lock-free, we
97 need to put all threads to sleep when replacing a vector.
98 However, when being single threaded, it is best not to do much.
99 Therefore, we allocate the new vectors before going single
100 threaded.
101 */
102
103 new_vectors = SCM_EOL;
104 for (state = all_dynamic_states; !scm_is_null (state);
105 state = DYNAMIC_STATE_NEXT (state))
106 new_vectors = scm_cons (scm_c_make_vector (allocated_fluids_len,
107 SCM_BOOL_F),
108 new_vectors);
109
110 scm_i_thread_put_to_sleep ();
111 for (state = all_dynamic_states; !scm_is_null (state);
112 state = DYNAMIC_STATE_NEXT (state))
113 {
114 SCM old_fluids = DYNAMIC_STATE_FLUIDS (state);
115 SCM new_fluids = SCM_CAR (new_vectors);
116 size_t i, old_len = SCM_SIMPLE_VECTOR_LENGTH (old_fluids);
117
118 for (i = 0; i < old_len; i++)
119 SCM_SIMPLE_VECTOR_SET (new_fluids, i,
120 SCM_SIMPLE_VECTOR_REF (old_fluids, i));
121 SET_DYNAMIC_STATE_FLUIDS (state, new_fluids);
122 new_vectors = SCM_CDR (new_vectors);
123 }
124 scm_i_thread_wake_up ();
125 }
126
127 /* This is called during GC, that is, while being single threaded.
128 See next_fluid_num for a discussion why it is safe to access
129 allocated_fluids here.
130 */
131 static void *
132 scan_dynamic_states_and_fluids (void *dummy1 SCM_UNUSED,
133 void *dummy2 SCM_UNUSED,
134 void *dummy3 SCM_UNUSED)
135 {
136 SCM *statep, *fluidp;
137
138 /* Scan all fluids and deallocate the unmarked ones.
139 */
140 fluidp = &all_fluids;
141 while (!scm_is_null (*fluidp))
142 {
143 if (!SCM_GC_MARK_P (*fluidp))
144 {
145 allocated_fluids_num -= 1;
146 allocated_fluids[FLUID_NUM (*fluidp)] = 0;
147 *fluidp = FLUID_NEXT (*fluidp);
148 }
149 else
150 fluidp = FLUID_NEXT_LOC (*fluidp);
151 }
152
153 /* Scan all dynamic states and remove the unmarked ones. The live
154 ones are updated for unallocated fluids.
155 */
156 statep = &all_dynamic_states;
157 while (!scm_is_null (*statep))
158 {
159 if (!SCM_GC_MARK_P (*statep))
160 *statep = DYNAMIC_STATE_NEXT (*statep);
161 else
162 {
163 SCM fluids = DYNAMIC_STATE_FLUIDS (*statep);
164 size_t len, i;
165
166 len = SCM_SIMPLE_VECTOR_LENGTH (fluids);
167 for (i = 0; i < len && i < allocated_fluids_len; i++)
168 if (allocated_fluids[i] == 0)
169 SCM_SIMPLE_VECTOR_SET (fluids, i, SCM_BOOL_F);
170
171 statep = DYNAMIC_STATE_NEXT_LOC (*statep);
172 }
173 }
174
175 return NULL;
176 }
177
178 static size_t
179 fluid_free (SCM fluid)
180 {
181 /* The real work is done in scan_dynamic_states_and_fluids. We can
182 not touch allocated_fluids etc here since a smob free routine can
183 be run at any time, in any thread.
184 */
185 return 0;
186 }
187
188 static int
189 fluid_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
190 {
191 scm_puts ("#<fluid ", port);
192 scm_intprint ((int) FLUID_NUM (exp), 10, port);
193 scm_putc ('>', port);
194 return 1;
195 }
196
197 static size_t
198 next_fluid_num ()
199 {
200 size_t n;
201
202 scm_frame_begin (0);
203 scm_i_frame_pthread_mutex_lock (&fluid_admin_mutex);
204
205 if (allocated_fluids_num == allocated_fluids_len)
206 {
207 /* All fluid numbers are in use. Run a GC to try to free some
208 up.
209 */
210 scm_gc ();
211 }
212
213 if (allocated_fluids_num < allocated_fluids_len)
214 {
215 for (n = 0; n < allocated_fluids_len; n++)
216 if (allocated_fluids[n] == 0)
217 break;
218 }
219 else
220 {
221 /* During the following call, the GC might run and elements of
222 allocated_fluids might bet set to zero. Also,
223 allocated_fluids and allocated_fluids_len are used to scan
224 all dynamic states during GC. Thus we need to make sure that
225 no GC can run while updating these two variables.
226 */
227
228 char *new_allocated_fluids =
229 scm_malloc (allocated_fluids_len + FLUID_GROW);
230
231 /* Copy over old values and initialize rest. GC can not run
232 during these two operations since there is no safe point in
233 them.
234 */
235 memcpy (new_allocated_fluids, allocated_fluids, allocated_fluids_len);
236 memset (new_allocated_fluids + allocated_fluids_len, 0, FLUID_GROW);
237 n = allocated_fluids_len;
238 allocated_fluids = new_allocated_fluids;
239 allocated_fluids_len += FLUID_GROW;
240
241 /* Now allocated_fluids and allocated_fluids_len are valid again
242 and we can allow GCs to occur.
243 */
244 resize_all_states ();
245 }
246
247 allocated_fluids_num += 1;
248 allocated_fluids[n] = 1;
249
250 scm_frame_end ();
251 return n;
252 }
253
254 SCM_DEFINE (scm_make_fluid, "make-fluid", 0, 0, 0,
255 (),
256 "Return a newly created fluid.\n"
257 "Fluids are objects that can hold one\n"
258 "value per dynamic state. That is, modifications to this value are\n"
259 "only visible to code that executes with the same dynamic state as\n"
260 "the modifying code. When a new dynamic state is constructed, it\n"
261 "inherits the values from its parent. Because each thread normally executes\n"
262 "with its own dynamic state, you can use fluids for thread local storage.")
263 #define FUNC_NAME s_scm_make_fluid
264 {
265 SCM fluid;
266
267 SCM_NEWSMOB2 (fluid, tc16_fluid,
268 (scm_t_bits) next_fluid_num (), SCM_UNPACK (SCM_EOL));
269
270 /* The GC must not run until the fluid is properly entered into the
271 list.
272 */
273 scm_i_scm_pthread_mutex_lock (&fluid_admin_mutex);
274 SET_FLUID_NEXT (fluid, all_fluids);
275 all_fluids = fluid;
276 scm_i_pthread_mutex_unlock (&fluid_admin_mutex);
277
278 return fluid;
279 }
280 #undef FUNC_NAME
281
282 SCM_DEFINE (scm_fluid_p, "fluid?", 1, 0, 0,
283 (SCM obj),
284 "Return @code{#t} iff @var{obj} is a fluid; otherwise, return\n"
285 "@code{#f}.")
286 #define FUNC_NAME s_scm_fluid_p
287 {
288 return scm_from_bool (IS_FLUID (obj));
289 }
290 #undef FUNC_NAME
291
292 int
293 scm_is_fluid (SCM obj)
294 {
295 return IS_FLUID (obj);
296 }
297
298 size_t
299 scm_i_fluid_num (SCM fluid)
300 {
301 return FLUID_NUM (fluid);
302 }
303
304 SCM_DEFINE (scm_fluid_ref, "fluid-ref", 1, 0, 0,
305 (SCM fluid),
306 "Return the value associated with @var{fluid} in the current\n"
307 "dynamic root. If @var{fluid} has not been set, then return\n"
308 "@code{#f}.")
309 #define FUNC_NAME s_scm_fluid_ref
310 {
311 SCM fluids = DYNAMIC_STATE_FLUIDS (SCM_I_CURRENT_THREAD->dynamic_state);
312
313 SCM_VALIDATE_FLUID (1, fluid);
314 return SCM_SIMPLE_VECTOR_REF (fluids, FLUID_NUM (fluid));
315 }
316 #undef FUNC_NAME
317
318 SCM
319 scm_i_fast_fluid_ref (size_t n)
320 {
321 SCM fluids = DYNAMIC_STATE_FLUIDS (SCM_I_CURRENT_THREAD->dynamic_state);
322 return SCM_SIMPLE_VECTOR_REF (fluids, n);
323 }
324
325 SCM_DEFINE (scm_fluid_set_x, "fluid-set!", 2, 0, 0,
326 (SCM fluid, SCM value),
327 "Set the value associated with @var{fluid} in the current dynamic root.")
328 #define FUNC_NAME s_scm_fluid_set_x
329 {
330 SCM fluids = DYNAMIC_STATE_FLUIDS (SCM_I_CURRENT_THREAD->dynamic_state);
331
332 SCM_VALIDATE_FLUID (1, fluid);
333 SCM_SIMPLE_VECTOR_SET (fluids, FLUID_NUM (fluid), value);
334 return SCM_UNSPECIFIED;
335 }
336 #undef FUNC_NAME
337
338 void
339 scm_i_fast_fluid_set_x (size_t n, SCM value)
340 {
341 SCM fluids = DYNAMIC_STATE_FLUIDS (SCM_I_CURRENT_THREAD->dynamic_state);
342 SCM_SIMPLE_VECTOR_SET (fluids, n, value);
343 }
344
345 static void
346 swap_fluids (SCM data)
347 {
348 SCM fluids = SCM_CAR (data), vals = SCM_CDR (data);
349
350 while (!SCM_NULL_OR_NIL_P (fluids))
351 {
352 SCM fl = SCM_CAR (fluids);
353 SCM old_val = scm_fluid_ref (fl);
354 scm_fluid_set_x (fl, SCM_CAR (vals));
355 SCM_SETCAR (vals, old_val);
356 fluids = SCM_CDR (fluids);
357 vals = SCM_CDR (vals);
358 }
359 }
360
361 /* Swap the fluid values in reverse order. This is important when the
362 same fluid appears multiple times in the fluids list.
363 */
364
365 static void
366 swap_fluids_reverse_aux (SCM fluids, SCM vals)
367 {
368 if (!SCM_NULL_OR_NIL_P (fluids))
369 {
370 SCM fl, old_val;
371
372 swap_fluids_reverse_aux (SCM_CDR (fluids), SCM_CDR (vals));
373 fl = SCM_CAR (fluids);
374 old_val = scm_fluid_ref (fl);
375 scm_fluid_set_x (fl, SCM_CAR (vals));
376 SCM_SETCAR (vals, old_val);
377 }
378 }
379
380 static void
381 swap_fluids_reverse (SCM data)
382 {
383 swap_fluids_reverse_aux (SCM_CAR (data), SCM_CDR (data));
384 }
385
386 static SCM
387 apply_thunk (void *thunk)
388 {
389 return scm_call_0 (SCM_PACK (thunk));
390 }
391
392 SCM_DEFINE (scm_with_fluids, "with-fluids*", 3, 0, 0,
393 (SCM fluids, SCM values, SCM thunk),
394 "Set @var{fluids} to @var{values} temporary, and call @var{thunk}.\n"
395 "@var{fluids} must be a list of fluids and @var{values} must be the same\n"
396 "number of their values to be applied. Each substitution is done\n"
397 "one after another. @var{thunk} must be a procedure with no argument.")
398 #define FUNC_NAME s_scm_with_fluids
399 {
400 return scm_c_with_fluids (fluids, values,
401 apply_thunk, (void *) SCM_UNPACK (thunk));
402 }
403 #undef FUNC_NAME
404
405 SCM
406 scm_c_with_fluids (SCM fluids, SCM values, SCM (*cproc) (), void *cdata)
407 #define FUNC_NAME "scm_c_with_fluids"
408 {
409 SCM ans, data;
410 long flen, vlen;
411
412 SCM_VALIDATE_LIST_COPYLEN (1, fluids, flen);
413 SCM_VALIDATE_LIST_COPYLEN (2, values, vlen);
414 if (flen != vlen)
415 scm_out_of_range (s_scm_with_fluids, values);
416
417 if (flen == 1)
418 return scm_c_with_fluid (SCM_CAR (fluids), SCM_CAR (values),
419 cproc, cdata);
420
421 data = scm_cons (fluids, values);
422 scm_frame_begin (SCM_F_FRAME_REWINDABLE);
423 scm_frame_rewind_handler_with_scm (swap_fluids, data,
424 SCM_F_WIND_EXPLICITLY);
425 scm_frame_unwind_handler_with_scm (swap_fluids_reverse, data,
426 SCM_F_WIND_EXPLICITLY);
427 ans = cproc (cdata);
428 scm_frame_end ();
429 return ans;
430 }
431 #undef FUNC_NAME
432
433 SCM_DEFINE (scm_with_fluid, "with-fluid*", 3, 0, 0,
434 (SCM fluid, SCM value, SCM thunk),
435 "Set @var{fluid} to @var{value} temporarily, and call @var{thunk}.\n"
436 "@var{thunk} must be a procedure with no argument.")
437 #define FUNC_NAME s_scm_with_fluid
438 {
439 return scm_c_with_fluid (fluid, value,
440 apply_thunk, (void *) SCM_UNPACK (thunk));
441 }
442 #undef FUNC_NAME
443
444 SCM
445 scm_c_with_fluid (SCM fluid, SCM value, SCM (*cproc) (), void *cdata)
446 #define FUNC_NAME "scm_c_with_fluid"
447 {
448 SCM ans;
449
450 scm_frame_begin (SCM_F_FRAME_REWINDABLE);
451 scm_frame_fluid (fluid, value);
452 ans = cproc (cdata);
453 scm_frame_end ();
454 return ans;
455 }
456 #undef FUNC_NAME
457
458 static void
459 swap_fluid (SCM data)
460 {
461 SCM f = SCM_CAR (data);
462 SCM t = scm_fluid_ref (f);
463 scm_fluid_set_x (f, SCM_CDR (data));
464 SCM_SETCDR (data, t);
465 }
466
467 void
468 scm_frame_fluid (SCM fluid, SCM value)
469 {
470 SCM data = scm_cons (fluid, value);
471 scm_frame_rewind_handler_with_scm (swap_fluid, data, SCM_F_WIND_EXPLICITLY);
472 scm_frame_unwind_handler_with_scm (swap_fluid, data, SCM_F_WIND_EXPLICITLY);
473 }
474
475 SCM
476 scm_i_make_initial_dynamic_state ()
477 {
478 SCM fluids = scm_c_make_vector (allocated_fluids_len, SCM_BOOL_F);
479 SCM state;
480 SCM_NEWSMOB2 (state, tc16_dynamic_state,
481 SCM_UNPACK (fluids), SCM_UNPACK (SCM_EOL));
482 all_dynamic_states = state;
483 return state;
484 }
485
486 SCM_DEFINE (scm_make_dynamic_state, "make-dynamic-state", 0, 1, 0,
487 (SCM parent),
488 "Return a copy of the dynamic state object @var{parent}\n"
489 "or of the current dynamic state when @var{parent} is omitted.")
490 #define FUNC_NAME s_scm_make_dynamic_state
491 {
492 SCM fluids, state;
493
494 if (SCM_UNBNDP (parent))
495 parent = scm_current_dynamic_state ();
496
497 scm_assert_smob_type (tc16_dynamic_state, parent);
498 fluids = scm_vector_copy (DYNAMIC_STATE_FLUIDS (parent));
499 SCM_NEWSMOB2 (state, tc16_dynamic_state,
500 SCM_UNPACK (fluids), SCM_UNPACK (SCM_EOL));
501
502 /* The GC must not run until the state is properly entered into the
503 list.
504 */
505 scm_i_scm_pthread_mutex_lock (&fluid_admin_mutex);
506 SET_DYNAMIC_STATE_NEXT (state, all_dynamic_states);
507 all_dynamic_states = state;
508 scm_i_pthread_mutex_unlock (&fluid_admin_mutex);
509
510 return state;
511 }
512 #undef FUNC_NAME
513
514 SCM_DEFINE (scm_dynamic_state_p, "dynamic-state?", 1, 0, 0,
515 (SCM obj),
516 "Return @code{#t} if @var{obj} is a dynamic state object;\n"
517 "return @code{#f} otherwise")
518 #define FUNC_NAME s_scm_dynamic_state_p
519 {
520 return scm_from_bool (IS_DYNAMIC_STATE (obj));
521 }
522 #undef FUNC_NAME
523
524 int
525 scm_is_dynamic_state (SCM obj)
526 {
527 return IS_DYNAMIC_STATE (obj);
528 }
529
530 SCM_DEFINE (scm_current_dynamic_state, "current-dynamic-state", 0, 0, 0,
531 (),
532 "Return the current dynamic state object.")
533 #define FUNC_NAME s_scm_current_dynamic_state
534 {
535 return SCM_I_CURRENT_THREAD->dynamic_state;
536 }
537 #undef FUNC_NAME
538
539 SCM_DEFINE (scm_set_current_dynamic_state, "set-current-dynamic-state", 1,0,0,
540 (SCM state),
541 "Set the current dynamic state object to @var{state}\n"
542 "and return the previous current dynamic state object.")
543 #define FUNC_NAME s_scm_set_current_dynamic_state
544 {
545 scm_i_thread *t = SCM_I_CURRENT_THREAD;
546 SCM old = t->dynamic_state;
547 scm_assert_smob_type (tc16_dynamic_state, state);
548 t->dynamic_state = state;
549 return old;
550 }
551 #undef FUNC_NAME
552
553 static void
554 swap_dynamic_state (SCM loc)
555 {
556 SCM_SETCAR (loc, scm_set_current_dynamic_state (SCM_CAR (loc)));
557 }
558
559 void
560 scm_frame_current_dynamic_state (SCM state)
561 {
562 SCM loc = scm_cons (state, SCM_EOL);
563 scm_assert_smob_type (tc16_dynamic_state, state);
564 scm_frame_rewind_handler_with_scm (swap_dynamic_state, loc,
565 SCM_F_WIND_EXPLICITLY);
566 scm_frame_unwind_handler_with_scm (swap_dynamic_state, loc,
567 SCM_F_WIND_EXPLICITLY);
568 }
569
570 void *
571 scm_c_with_dynamic_state (SCM state, void *(*func)(void *), void *data)
572 {
573 void *result;
574 scm_frame_begin (SCM_F_FRAME_REWINDABLE);
575 scm_frame_current_dynamic_state (state);
576 result = func (data);
577 scm_frame_end ();
578 return result;
579 }
580
581 SCM_DEFINE (scm_with_dynamic_state, "with-dynamic-state", 2, 0, 0,
582 (SCM state, SCM proc),
583 "Call @var{proc} while @var{state} is the current dynamic\n"
584 "state object.")
585 #define FUNC_NAME s_scm_with_dynamic_state
586 {
587 SCM result;
588 scm_frame_begin (SCM_F_FRAME_REWINDABLE);
589 scm_frame_current_dynamic_state (state);
590 result = scm_call_0 (proc);
591 scm_frame_end ();
592 return result;
593 }
594 #undef FUNC_NAME
595
596 void
597 scm_fluids_prehistory ()
598 {
599 tc16_fluid = scm_make_smob_type ("fluid", 0);
600 scm_set_smob_free (tc16_fluid, fluid_free);
601 scm_set_smob_print (tc16_fluid, fluid_print);
602
603 tc16_dynamic_state = scm_make_smob_type ("dynamic-state", 0);
604 scm_set_smob_mark (tc16_dynamic_state, scm_markcdr);
605
606 scm_c_hook_add (&scm_after_sweep_c_hook, scan_dynamic_states_and_fluids,
607 0, 0);
608 }
609
610 void
611 scm_init_fluids ()
612 {
613 #include "libguile/fluids.x"
614 }
615
616 /*
617 Local Variables:
618 c-file-style: "gnu"
619 End:
620 */