Removed a lot of now-useless SMOB mark/free functions.
[bpt/guile.git] / libguile / fluids.c
1 /* Copyright (C) 1996,1997,2000,2001, 2004, 2006 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 /* FIXME: What to do here? */
137 #if 0
138 SCM *statep, *fluidp;
139
140 /* Scan all fluids and deallocate the unmarked ones.
141 */
142 fluidp = &all_fluids;
143 while (!scm_is_null (*fluidp))
144 {
145 if (!SCM_GC_MARK_P (*fluidp))
146 {
147 allocated_fluids_num -= 1;
148 allocated_fluids[FLUID_NUM (*fluidp)] = 0;
149 *fluidp = FLUID_NEXT (*fluidp);
150 }
151 else
152 fluidp = FLUID_NEXT_LOC (*fluidp);
153 }
154
155 /* Scan all dynamic states and remove the unmarked ones. The live
156 ones are updated for unallocated fluids.
157 */
158 statep = &all_dynamic_states;
159 while (!scm_is_null (*statep))
160 {
161 if (!SCM_GC_MARK_P (*statep))
162 *statep = DYNAMIC_STATE_NEXT (*statep);
163 else
164 {
165 SCM fluids = DYNAMIC_STATE_FLUIDS (*statep);
166 size_t len, i;
167
168 len = SCM_SIMPLE_VECTOR_LENGTH (fluids);
169 for (i = 0; i < len && i < allocated_fluids_len; i++)
170 if (allocated_fluids[i] == 0)
171 SCM_SIMPLE_VECTOR_SET (fluids, i, SCM_BOOL_F);
172
173 statep = DYNAMIC_STATE_NEXT_LOC (*statep);
174 }
175 }
176
177 #endif
178 return NULL;
179 }
180
181 static int
182 fluid_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
183 {
184 scm_puts ("#<fluid ", port);
185 scm_intprint ((int) FLUID_NUM (exp), 10, port);
186 scm_putc ('>', port);
187 return 1;
188 }
189
190 static size_t
191 next_fluid_num ()
192 {
193 size_t n;
194
195 scm_dynwind_begin (0);
196 scm_i_dynwind_pthread_mutex_lock (&fluid_admin_mutex);
197
198 if ((allocated_fluids_len > 0) &&
199 (allocated_fluids_num == allocated_fluids_len))
200 {
201 /* All fluid numbers are in use. Run a GC to try to free some
202 up.
203 */
204 scm_gc ();
205 }
206
207 if (allocated_fluids_num < allocated_fluids_len)
208 {
209 for (n = 0; n < allocated_fluids_len; n++)
210 if (allocated_fluids[n] == 0)
211 break;
212 }
213 else
214 {
215 /* During the following call, the GC might run and elements of
216 allocated_fluids might bet set to zero. Also,
217 allocated_fluids and allocated_fluids_len are used to scan
218 all dynamic states during GC. Thus we need to make sure that
219 no GC can run while updating these two variables.
220 */
221
222 char *new_allocated_fluids =
223 scm_malloc (allocated_fluids_len + FLUID_GROW);
224
225 /* Copy over old values and initialize rest. GC can not run
226 during these two operations since there is no safe point in
227 them.
228 */
229 memcpy (new_allocated_fluids, allocated_fluids, allocated_fluids_len);
230 memset (new_allocated_fluids + allocated_fluids_len, 0, FLUID_GROW);
231 n = allocated_fluids_len;
232 allocated_fluids = new_allocated_fluids;
233 allocated_fluids_len += FLUID_GROW;
234
235 /* Now allocated_fluids and allocated_fluids_len are valid again
236 and we can allow GCs to occur.
237 */
238 resize_all_states ();
239 }
240
241 allocated_fluids_num += 1;
242 allocated_fluids[n] = 1;
243
244 scm_dynwind_end ();
245 return n;
246 }
247
248 SCM_DEFINE (scm_make_fluid, "make-fluid", 0, 0, 0,
249 (),
250 "Return a newly created fluid.\n"
251 "Fluids are objects that can hold one\n"
252 "value per dynamic state. That is, modifications to this value are\n"
253 "only visible to code that executes with the same dynamic state as\n"
254 "the modifying code. When a new dynamic state is constructed, it\n"
255 "inherits the values from its parent. Because each thread normally executes\n"
256 "with its own dynamic state, you can use fluids for thread local storage.")
257 #define FUNC_NAME s_scm_make_fluid
258 {
259 SCM fluid;
260
261 SCM_NEWSMOB2 (fluid, tc16_fluid,
262 (scm_t_bits) next_fluid_num (), SCM_UNPACK (SCM_EOL));
263
264 /* The GC must not run until the fluid is properly entered into the
265 list.
266 */
267 scm_i_scm_pthread_mutex_lock (&fluid_admin_mutex);
268 SET_FLUID_NEXT (fluid, all_fluids);
269 all_fluids = fluid;
270 scm_i_pthread_mutex_unlock (&fluid_admin_mutex);
271
272 return fluid;
273 }
274 #undef FUNC_NAME
275
276 SCM_DEFINE (scm_fluid_p, "fluid?", 1, 0, 0,
277 (SCM obj),
278 "Return @code{#t} iff @var{obj} is a fluid; otherwise, return\n"
279 "@code{#f}.")
280 #define FUNC_NAME s_scm_fluid_p
281 {
282 return scm_from_bool (IS_FLUID (obj));
283 }
284 #undef FUNC_NAME
285
286 int
287 scm_is_fluid (SCM obj)
288 {
289 return IS_FLUID (obj);
290 }
291
292 size_t
293 scm_i_fluid_num (SCM fluid)
294 {
295 return FLUID_NUM (fluid);
296 }
297
298 SCM_DEFINE (scm_fluid_ref, "fluid-ref", 1, 0, 0,
299 (SCM fluid),
300 "Return the value associated with @var{fluid} in the current\n"
301 "dynamic root. If @var{fluid} has not been set, then return\n"
302 "@code{#f}.")
303 #define FUNC_NAME s_scm_fluid_ref
304 {
305 SCM fluids = DYNAMIC_STATE_FLUIDS (SCM_I_CURRENT_THREAD->dynamic_state);
306
307 SCM_VALIDATE_FLUID (1, fluid);
308 return SCM_SIMPLE_VECTOR_REF (fluids, FLUID_NUM (fluid));
309 }
310 #undef FUNC_NAME
311
312 SCM
313 scm_i_fast_fluid_ref (size_t n)
314 {
315 SCM fluids = DYNAMIC_STATE_FLUIDS (SCM_I_CURRENT_THREAD->dynamic_state);
316 return SCM_SIMPLE_VECTOR_REF (fluids, n);
317 }
318
319 SCM_DEFINE (scm_fluid_set_x, "fluid-set!", 2, 0, 0,
320 (SCM fluid, SCM value),
321 "Set the value associated with @var{fluid} in the current dynamic root.")
322 #define FUNC_NAME s_scm_fluid_set_x
323 {
324 SCM fluids = DYNAMIC_STATE_FLUIDS (SCM_I_CURRENT_THREAD->dynamic_state);
325
326 SCM_VALIDATE_FLUID (1, fluid);
327 SCM_SIMPLE_VECTOR_SET (fluids, FLUID_NUM (fluid), value);
328 return SCM_UNSPECIFIED;
329 }
330 #undef FUNC_NAME
331
332 void
333 scm_i_fast_fluid_set_x (size_t n, SCM value)
334 {
335 SCM fluids = DYNAMIC_STATE_FLUIDS (SCM_I_CURRENT_THREAD->dynamic_state);
336 SCM_SIMPLE_VECTOR_SET (fluids, n, value);
337 }
338
339 static void
340 swap_fluids (SCM data)
341 {
342 SCM fluids = SCM_CAR (data), vals = SCM_CDR (data);
343
344 while (!SCM_NULL_OR_NIL_P (fluids))
345 {
346 SCM fl = SCM_CAR (fluids);
347 SCM old_val = scm_fluid_ref (fl);
348 scm_fluid_set_x (fl, SCM_CAR (vals));
349 SCM_SETCAR (vals, old_val);
350 fluids = SCM_CDR (fluids);
351 vals = SCM_CDR (vals);
352 }
353 }
354
355 /* Swap the fluid values in reverse order. This is important when the
356 same fluid appears multiple times in the fluids list.
357 */
358
359 static void
360 swap_fluids_reverse_aux (SCM fluids, SCM vals)
361 {
362 if (!SCM_NULL_OR_NIL_P (fluids))
363 {
364 SCM fl, old_val;
365
366 swap_fluids_reverse_aux (SCM_CDR (fluids), SCM_CDR (vals));
367 fl = SCM_CAR (fluids);
368 old_val = scm_fluid_ref (fl);
369 scm_fluid_set_x (fl, SCM_CAR (vals));
370 SCM_SETCAR (vals, old_val);
371 }
372 }
373
374 static void
375 swap_fluids_reverse (SCM data)
376 {
377 swap_fluids_reverse_aux (SCM_CAR (data), SCM_CDR (data));
378 }
379
380 static SCM
381 apply_thunk (void *thunk)
382 {
383 return scm_call_0 (SCM_PACK (thunk));
384 }
385
386 SCM_DEFINE (scm_with_fluids, "with-fluids*", 3, 0, 0,
387 (SCM fluids, SCM values, SCM thunk),
388 "Set @var{fluids} to @var{values} temporary, and call @var{thunk}.\n"
389 "@var{fluids} must be a list of fluids and @var{values} must be the same\n"
390 "number of their values to be applied. Each substitution is done\n"
391 "one after another. @var{thunk} must be a procedure with no argument.")
392 #define FUNC_NAME s_scm_with_fluids
393 {
394 return scm_c_with_fluids (fluids, values,
395 apply_thunk, (void *) SCM_UNPACK (thunk));
396 }
397 #undef FUNC_NAME
398
399 SCM
400 scm_c_with_fluids (SCM fluids, SCM values, SCM (*cproc) (), void *cdata)
401 #define FUNC_NAME "scm_c_with_fluids"
402 {
403 SCM ans, data;
404 long flen, vlen;
405
406 SCM_VALIDATE_LIST_COPYLEN (1, fluids, flen);
407 SCM_VALIDATE_LIST_COPYLEN (2, values, vlen);
408 if (flen != vlen)
409 scm_out_of_range (s_scm_with_fluids, values);
410
411 if (flen == 1)
412 return scm_c_with_fluid (SCM_CAR (fluids), SCM_CAR (values),
413 cproc, cdata);
414
415 data = scm_cons (fluids, values);
416 scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
417 scm_dynwind_rewind_handler_with_scm (swap_fluids, data,
418 SCM_F_WIND_EXPLICITLY);
419 scm_dynwind_unwind_handler_with_scm (swap_fluids_reverse, data,
420 SCM_F_WIND_EXPLICITLY);
421 ans = cproc (cdata);
422 scm_dynwind_end ();
423 return ans;
424 }
425 #undef FUNC_NAME
426
427 SCM_DEFINE (scm_with_fluid, "with-fluid*", 3, 0, 0,
428 (SCM fluid, SCM value, SCM thunk),
429 "Set @var{fluid} to @var{value} temporarily, and call @var{thunk}.\n"
430 "@var{thunk} must be a procedure with no argument.")
431 #define FUNC_NAME s_scm_with_fluid
432 {
433 return scm_c_with_fluid (fluid, value,
434 apply_thunk, (void *) SCM_UNPACK (thunk));
435 }
436 #undef FUNC_NAME
437
438 SCM
439 scm_c_with_fluid (SCM fluid, SCM value, SCM (*cproc) (), void *cdata)
440 #define FUNC_NAME "scm_c_with_fluid"
441 {
442 SCM ans;
443
444 scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
445 scm_dynwind_fluid (fluid, value);
446 ans = cproc (cdata);
447 scm_dynwind_end ();
448 return ans;
449 }
450 #undef FUNC_NAME
451
452 static void
453 swap_fluid (SCM data)
454 {
455 SCM f = SCM_CAR (data);
456 SCM t = scm_fluid_ref (f);
457 scm_fluid_set_x (f, SCM_CDR (data));
458 SCM_SETCDR (data, t);
459 }
460
461 void
462 scm_dynwind_fluid (SCM fluid, SCM value)
463 {
464 SCM data = scm_cons (fluid, value);
465 scm_dynwind_rewind_handler_with_scm (swap_fluid, data, SCM_F_WIND_EXPLICITLY);
466 scm_dynwind_unwind_handler_with_scm (swap_fluid, data, SCM_F_WIND_EXPLICITLY);
467 }
468
469 SCM
470 scm_i_make_initial_dynamic_state ()
471 {
472 SCM fluids = scm_c_make_vector (allocated_fluids_len, SCM_BOOL_F);
473 SCM state;
474 SCM_NEWSMOB2 (state, tc16_dynamic_state,
475 SCM_UNPACK (fluids), SCM_UNPACK (SCM_EOL));
476 all_dynamic_states = state;
477 return state;
478 }
479
480 SCM_DEFINE (scm_make_dynamic_state, "make-dynamic-state", 0, 1, 0,
481 (SCM parent),
482 "Return a copy of the dynamic state object @var{parent}\n"
483 "or of the current dynamic state when @var{parent} is omitted.")
484 #define FUNC_NAME s_scm_make_dynamic_state
485 {
486 SCM fluids, state;
487
488 if (SCM_UNBNDP (parent))
489 parent = scm_current_dynamic_state ();
490
491 scm_assert_smob_type (tc16_dynamic_state, parent);
492 fluids = scm_vector_copy (DYNAMIC_STATE_FLUIDS (parent));
493 SCM_NEWSMOB2 (state, tc16_dynamic_state,
494 SCM_UNPACK (fluids), SCM_UNPACK (SCM_EOL));
495
496 /* The GC must not run until the state is properly entered into the
497 list.
498 */
499 scm_i_scm_pthread_mutex_lock (&fluid_admin_mutex);
500 SET_DYNAMIC_STATE_NEXT (state, all_dynamic_states);
501 all_dynamic_states = state;
502 scm_i_pthread_mutex_unlock (&fluid_admin_mutex);
503
504 return state;
505 }
506 #undef FUNC_NAME
507
508 SCM_DEFINE (scm_dynamic_state_p, "dynamic-state?", 1, 0, 0,
509 (SCM obj),
510 "Return @code{#t} if @var{obj} is a dynamic state object;\n"
511 "return @code{#f} otherwise")
512 #define FUNC_NAME s_scm_dynamic_state_p
513 {
514 return scm_from_bool (IS_DYNAMIC_STATE (obj));
515 }
516 #undef FUNC_NAME
517
518 int
519 scm_is_dynamic_state (SCM obj)
520 {
521 return IS_DYNAMIC_STATE (obj);
522 }
523
524 SCM_DEFINE (scm_current_dynamic_state, "current-dynamic-state", 0, 0, 0,
525 (),
526 "Return the current dynamic state object.")
527 #define FUNC_NAME s_scm_current_dynamic_state
528 {
529 return SCM_I_CURRENT_THREAD->dynamic_state;
530 }
531 #undef FUNC_NAME
532
533 SCM_DEFINE (scm_set_current_dynamic_state, "set-current-dynamic-state", 1,0,0,
534 (SCM state),
535 "Set the current dynamic state object to @var{state}\n"
536 "and return the previous current dynamic state object.")
537 #define FUNC_NAME s_scm_set_current_dynamic_state
538 {
539 scm_i_thread *t = SCM_I_CURRENT_THREAD;
540 SCM old = t->dynamic_state;
541 scm_assert_smob_type (tc16_dynamic_state, state);
542 t->dynamic_state = state;
543 return old;
544 }
545 #undef FUNC_NAME
546
547 static void
548 swap_dynamic_state (SCM loc)
549 {
550 SCM_SETCAR (loc, scm_set_current_dynamic_state (SCM_CAR (loc)));
551 }
552
553 void
554 scm_dynwind_current_dynamic_state (SCM state)
555 {
556 SCM loc = scm_cons (state, SCM_EOL);
557 scm_assert_smob_type (tc16_dynamic_state, state);
558 scm_dynwind_rewind_handler_with_scm (swap_dynamic_state, loc,
559 SCM_F_WIND_EXPLICITLY);
560 scm_dynwind_unwind_handler_with_scm (swap_dynamic_state, loc,
561 SCM_F_WIND_EXPLICITLY);
562 }
563
564 void *
565 scm_c_with_dynamic_state (SCM state, void *(*func)(void *), void *data)
566 {
567 void *result;
568 scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
569 scm_dynwind_current_dynamic_state (state);
570 result = func (data);
571 scm_dynwind_end ();
572 return result;
573 }
574
575 SCM_DEFINE (scm_with_dynamic_state, "with-dynamic-state", 2, 0, 0,
576 (SCM state, SCM proc),
577 "Call @var{proc} while @var{state} is the current dynamic\n"
578 "state object.")
579 #define FUNC_NAME s_scm_with_dynamic_state
580 {
581 SCM result;
582 scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
583 scm_dynwind_current_dynamic_state (state);
584 result = scm_call_0 (proc);
585 scm_dynwind_end ();
586 return result;
587 }
588 #undef FUNC_NAME
589
590 void
591 scm_fluids_prehistory ()
592 {
593 tc16_fluid = scm_make_smob_type ("fluid", 0);
594 scm_set_smob_print (tc16_fluid, fluid_print);
595
596 tc16_dynamic_state = scm_make_smob_type ("dynamic-state", 0);
597
598 scm_c_hook_add (&scm_after_sweep_c_hook, scan_dynamic_states_and_fluids,
599 0, 0);
600 }
601
602 void
603 scm_init_fluids ()
604 {
605 #include "libguile/fluids.x"
606 }
607
608 /*
609 Local Variables:
610 c-file-style: "gnu"
611 End:
612 */