merge from master to elisp
[bpt/guile.git] / libguile / fluids.c
1 /* Copyright (C) 1996,1997,2000,2001, 2004, 2006, 2007, 2008, 2009 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 License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful, but
9 * 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
16 * 02110-1301 USA
17 */
18
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <assert.h>
26
27 #include "libguile/_scm.h"
28 #include "libguile/print.h"
29 #include "libguile/dynwind.h"
30 #include "libguile/fluids.h"
31 #include "libguile/alist.h"
32 #include "libguile/eval.h"
33 #include "libguile/ports.h"
34 #include "libguile/deprecation.h"
35 #include "libguile/lang.h"
36 #include "libguile/validate.h"
37
38 #define FLUID_GROW 20
39
40 /* A lot of the complexity below stems from the desire to reuse fluid
41 slots. Normally, fluids should be pretty global and long-lived
42 things, so that reusing their slots should not be overly critical,
43 but it is the right thing to do nevertheless. The code therefore
44 puts the burdon on allocating and collection fluids and keeps
45 accessing fluids lock free. This is achieved by manipulating the
46 global state of the fluid machinery mostly in single threaded
47 sections.
48
49 Reusing a fluid slot means that it must be reset to #f in all
50 dynamic states. We do this by maintaining a weak list of all
51 dynamic states, which is used after a GC to do the resetting.
52
53 Also, the fluid vectors in the dynamic states need to grow from
54 time to time when more fluids are created. We do this in a single
55 threaded section so that threads do not need to lock when accessing
56 a fluid in the normal way.
57 */
58
59 static scm_i_pthread_mutex_t fluid_admin_mutex = SCM_I_PTHREAD_MUTEX_INITIALIZER;
60
61 /* Protected by fluid_admin_mutex, but also accessed during GC. See
62 next_fluid_num for a discussion of this.
63 */
64 static size_t allocated_fluids_len = 0;
65 static size_t allocated_fluids_num = 0;
66 static char *allocated_fluids = NULL;
67
68 #define IS_FLUID(x) (!SCM_IMP (x) && SCM_TYP7 (x) == scm_tc7_fluid)
69 #define FLUID_NUM(x) ((size_t)SCM_CELL_WORD_1(x))
70
71 #define IS_DYNAMIC_STATE(x) (!SCM_IMP (x) && SCM_TYP7 (x) == scm_tc7_dynamic_state)
72 #define DYNAMIC_STATE_FLUIDS(x) SCM_PACK (SCM_CELL_WORD_1 (x))
73 #define SET_DYNAMIC_STATE_FLUIDS(x, y) SCM_SET_CELL_WORD_1 ((x), (SCM_UNPACK (y)))
74
75
76 \f
77 /* Grow STATE so that it can hold up to ALLOCATED_FLUIDS_NUM fluids. */
78 static void
79 grow_dynamic_state (SCM state)
80 {
81 SCM new_fluids;
82 SCM old_fluids = DYNAMIC_STATE_FLUIDS (state);
83 size_t i, new_len, old_len = SCM_SIMPLE_VECTOR_LENGTH (old_fluids);
84
85 retry:
86 new_len = allocated_fluids_num;
87 new_fluids = scm_c_make_vector (new_len, SCM_BOOL_F);
88
89 scm_i_pthread_mutex_lock (&fluid_admin_mutex);
90 if (new_len != allocated_fluids_num)
91 {
92 /* We lost the race. */
93 scm_i_pthread_mutex_unlock (&fluid_admin_mutex);
94 goto retry;
95 }
96
97 assert (allocated_fluids_num > old_len);
98
99 for (i = 0; i < old_len; i++)
100 SCM_SIMPLE_VECTOR_SET (new_fluids, i,
101 SCM_SIMPLE_VECTOR_REF (old_fluids, i));
102 SET_DYNAMIC_STATE_FLUIDS (state, new_fluids);
103
104 scm_i_pthread_mutex_unlock (&fluid_admin_mutex);
105 }
106
107 void
108 scm_i_fluid_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
109 {
110 scm_puts ("#<fluid ", port);
111 scm_intprint ((int) FLUID_NUM (exp), 10, port);
112 scm_putc ('>', port);
113 }
114
115 void
116 scm_i_dynamic_state_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
117 {
118 scm_puts ("#<dynamic-state ", port);
119 scm_intprint (SCM_UNPACK (exp), 16, port);
120 scm_putc ('>', port);
121 }
122
123 static size_t
124 next_fluid_num ()
125 {
126 size_t n;
127
128 scm_dynwind_begin (0);
129 scm_i_dynwind_pthread_mutex_lock (&fluid_admin_mutex);
130
131 if ((allocated_fluids_len > 0) &&
132 (allocated_fluids_num == allocated_fluids_len))
133 {
134 /* All fluid numbers are in use. Run a GC to try to free some
135 up.
136 */
137 scm_gc ();
138 }
139
140 if (allocated_fluids_num < allocated_fluids_len)
141 {
142 for (n = 0; n < allocated_fluids_len; n++)
143 if (allocated_fluids[n] == 0)
144 break;
145 }
146 else
147 {
148 /* Grow the vector of allocated fluids. */
149 /* FIXME: Since we use `scm_malloc ()', ALLOCATED_FLUIDS is scanned by
150 the GC; therefore, all fluids remain reachable for the entire
151 program lifetime. Hopefully this is not a problem in practice. */
152 char *new_allocated_fluids =
153 scm_gc_malloc (allocated_fluids_len + FLUID_GROW,
154 "allocated fluids");
155
156 /* Copy over old values and initialize rest. GC can not run
157 during these two operations since there is no safe point in
158 them.
159 */
160 memcpy (new_allocated_fluids, allocated_fluids, allocated_fluids_len);
161 memset (new_allocated_fluids + allocated_fluids_len, 0, FLUID_GROW);
162 n = allocated_fluids_len;
163
164 /* Update the vector of allocated fluids. Dynamic states will
165 eventually be lazily grown to accomodate the new value of
166 ALLOCATED_FLUIDS_LEN in `fluid-ref' and `fluid-set!'. */
167 allocated_fluids = new_allocated_fluids;
168 allocated_fluids_len += FLUID_GROW;
169 }
170
171 allocated_fluids_num += 1;
172 allocated_fluids[n] = 1;
173
174 scm_dynwind_end ();
175 return n;
176 }
177
178 SCM_DEFINE (scm_make_fluid, "make-fluid", 0, 0, 0,
179 (),
180 "Return a newly created fluid.\n"
181 "Fluids are objects that can hold one\n"
182 "value per dynamic state. That is, modifications to this value are\n"
183 "only visible to code that executes with the same dynamic state as\n"
184 "the modifying code. When a new dynamic state is constructed, it\n"
185 "inherits the values from its parent. Because each thread normally executes\n"
186 "with its own dynamic state, you can use fluids for thread local storage.")
187 #define FUNC_NAME s_scm_make_fluid
188 {
189 return scm_cell (scm_tc7_fluid, (scm_t_bits) next_fluid_num ());
190 }
191 #undef FUNC_NAME
192
193 SCM_DEFINE (scm_fluid_p, "fluid?", 1, 0, 0,
194 (SCM obj),
195 "Return @code{#t} iff @var{obj} is a fluid; otherwise, return\n"
196 "@code{#f}.")
197 #define FUNC_NAME s_scm_fluid_p
198 {
199 return scm_from_bool (IS_FLUID (obj));
200 }
201 #undef FUNC_NAME
202
203 int
204 scm_is_fluid (SCM obj)
205 {
206 return IS_FLUID (obj);
207 }
208
209
210
211 SCM_DEFINE (scm_fluid_ref, "fluid-ref", 1, 0, 0,
212 (SCM fluid),
213 "Return the value associated with @var{fluid} in the current\n"
214 "dynamic root. If @var{fluid} has not been set, then return\n"
215 "@code{#f}.")
216 #define FUNC_NAME s_scm_fluid_ref
217 {
218 SCM fluids = DYNAMIC_STATE_FLUIDS (SCM_I_CURRENT_THREAD->dynamic_state);
219
220 SCM_VALIDATE_FLUID (1, fluid);
221
222 if (SCM_UNLIKELY (FLUID_NUM (fluid) >= SCM_SIMPLE_VECTOR_LENGTH (fluids)))
223 {
224 /* We should only get there when the current thread's dynamic state
225 turns out to be too small compared to the set of currently allocated
226 fluids. */
227 assert (SCM_SIMPLE_VECTOR_LENGTH (fluids) < allocated_fluids_num);
228
229 /* Lazily grow the current thread's dynamic state. */
230 grow_dynamic_state (SCM_I_CURRENT_THREAD->dynamic_state);
231
232 fluids = DYNAMIC_STATE_FLUIDS (SCM_I_CURRENT_THREAD->dynamic_state);
233 }
234
235 return SCM_SIMPLE_VECTOR_REF (fluids, FLUID_NUM (fluid));
236 }
237 #undef FUNC_NAME
238
239 SCM_DEFINE (scm_fluid_set_x, "fluid-set!", 2, 0, 0,
240 (SCM fluid, SCM value),
241 "Set the value associated with @var{fluid} in the current dynamic root.")
242 #define FUNC_NAME s_scm_fluid_set_x
243 {
244 SCM fluids = DYNAMIC_STATE_FLUIDS (SCM_I_CURRENT_THREAD->dynamic_state);
245
246 SCM_VALIDATE_FLUID (1, fluid);
247
248 if (SCM_UNLIKELY (FLUID_NUM (fluid) >= SCM_SIMPLE_VECTOR_LENGTH (fluids)))
249 {
250 /* We should only get there when the current thread's dynamic state
251 turns out to be too small compared to the set of currently allocated
252 fluids. */
253 assert (SCM_SIMPLE_VECTOR_LENGTH (fluids) < allocated_fluids_num);
254
255 /* Lazily grow the current thread's dynamic state. */
256 grow_dynamic_state (SCM_I_CURRENT_THREAD->dynamic_state);
257
258 fluids = DYNAMIC_STATE_FLUIDS (SCM_I_CURRENT_THREAD->dynamic_state);
259 }
260
261 SCM_SIMPLE_VECTOR_SET (fluids, FLUID_NUM (fluid), value);
262 return SCM_UNSPECIFIED;
263 }
264 #undef FUNC_NAME
265
266 static void
267 swap_fluids (SCM data)
268 {
269 SCM fluids = SCM_CAR (data), vals = SCM_CDR (data);
270
271 while (!SCM_NULL_OR_NIL_P (fluids))
272 {
273 SCM fl = SCM_CAR (fluids);
274 SCM old_val = scm_fluid_ref (fl);
275 scm_fluid_set_x (fl, SCM_CAR (vals));
276 SCM_SETCAR (vals, old_val);
277 fluids = SCM_CDR (fluids);
278 vals = SCM_CDR (vals);
279 }
280 }
281
282 /* Swap the fluid values in reverse order. This is important when the
283 same fluid appears multiple times in the fluids list.
284 */
285
286 static void
287 swap_fluids_reverse_aux (SCM fluids, SCM vals)
288 {
289 if (!SCM_NULL_OR_NIL_P (fluids))
290 {
291 SCM fl, old_val;
292
293 swap_fluids_reverse_aux (SCM_CDR (fluids), SCM_CDR (vals));
294 fl = SCM_CAR (fluids);
295 old_val = scm_fluid_ref (fl);
296 scm_fluid_set_x (fl, SCM_CAR (vals));
297 SCM_SETCAR (vals, old_val);
298 }
299 }
300
301 static void
302 swap_fluids_reverse (SCM data)
303 {
304 swap_fluids_reverse_aux (SCM_CAR (data), SCM_CDR (data));
305 }
306
307 static SCM
308 apply_thunk (void *thunk)
309 {
310 return scm_call_0 (SCM_PACK (thunk));
311 }
312
313 SCM_DEFINE (scm_with_fluids, "with-fluids*", 3, 0, 0,
314 (SCM fluids, SCM values, SCM thunk),
315 "Set @var{fluids} to @var{values} temporary, and call @var{thunk}.\n"
316 "@var{fluids} must be a list of fluids and @var{values} must be the same\n"
317 "number of their values to be applied. Each substitution is done\n"
318 "one after another. @var{thunk} must be a procedure with no argument.")
319 #define FUNC_NAME s_scm_with_fluids
320 {
321 return scm_c_with_fluids (fluids, values,
322 apply_thunk, (void *) SCM_UNPACK (thunk));
323 }
324 #undef FUNC_NAME
325
326 SCM
327 scm_c_with_fluids (SCM fluids, SCM values, SCM (*cproc) (), void *cdata)
328 #define FUNC_NAME "scm_c_with_fluids"
329 {
330 SCM ans, data;
331 long flen, vlen;
332
333 SCM_VALIDATE_LIST_COPYLEN (1, fluids, flen);
334 SCM_VALIDATE_LIST_COPYLEN (2, values, vlen);
335 if (flen != vlen)
336 scm_out_of_range (s_scm_with_fluids, values);
337
338 if (flen == 1)
339 return scm_c_with_fluid (SCM_CAR (fluids), SCM_CAR (values),
340 cproc, cdata);
341
342 data = scm_cons (fluids, values);
343 scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
344 scm_dynwind_rewind_handler_with_scm (swap_fluids, data,
345 SCM_F_WIND_EXPLICITLY);
346 scm_dynwind_unwind_handler_with_scm (swap_fluids_reverse, data,
347 SCM_F_WIND_EXPLICITLY);
348 ans = cproc (cdata);
349 scm_dynwind_end ();
350 return ans;
351 }
352 #undef FUNC_NAME
353
354 SCM_DEFINE (scm_with_fluid, "with-fluid*", 3, 0, 0,
355 (SCM fluid, SCM value, SCM thunk),
356 "Set @var{fluid} to @var{value} temporarily, and call @var{thunk}.\n"
357 "@var{thunk} must be a procedure with no argument.")
358 #define FUNC_NAME s_scm_with_fluid
359 {
360 return scm_c_with_fluid (fluid, value,
361 apply_thunk, (void *) SCM_UNPACK (thunk));
362 }
363 #undef FUNC_NAME
364
365 SCM
366 scm_c_with_fluid (SCM fluid, SCM value, SCM (*cproc) (), void *cdata)
367 #define FUNC_NAME "scm_c_with_fluid"
368 {
369 SCM ans;
370
371 scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
372 scm_dynwind_fluid (fluid, value);
373 ans = cproc (cdata);
374 scm_dynwind_end ();
375 return ans;
376 }
377 #undef FUNC_NAME
378
379 static void
380 swap_fluid (SCM data)
381 {
382 SCM f = SCM_CAR (data);
383 SCM t = scm_fluid_ref (f);
384 scm_fluid_set_x (f, SCM_CDR (data));
385 SCM_SETCDR (data, t);
386 }
387
388 void
389 scm_dynwind_fluid (SCM fluid, SCM value)
390 {
391 SCM data = scm_cons (fluid, value);
392 scm_dynwind_rewind_handler_with_scm (swap_fluid, data, SCM_F_WIND_EXPLICITLY);
393 scm_dynwind_unwind_handler_with_scm (swap_fluid, data, SCM_F_WIND_EXPLICITLY);
394 }
395
396 SCM
397 scm_i_make_initial_dynamic_state ()
398 {
399 SCM fluids = scm_c_make_vector (allocated_fluids_len, SCM_BOOL_F);
400 return scm_cell (scm_tc7_dynamic_state, SCM_UNPACK (fluids));
401 }
402
403 SCM_DEFINE (scm_make_dynamic_state, "make-dynamic-state", 0, 1, 0,
404 (SCM parent),
405 "Return a copy of the dynamic state object @var{parent}\n"
406 "or of the current dynamic state when @var{parent} is omitted.")
407 #define FUNC_NAME s_scm_make_dynamic_state
408 {
409 SCM fluids;
410
411 if (SCM_UNBNDP (parent))
412 parent = scm_current_dynamic_state ();
413
414 SCM_ASSERT (IS_DYNAMIC_STATE (parent), parent, SCM_ARG1, FUNC_NAME);
415 fluids = scm_vector_copy (DYNAMIC_STATE_FLUIDS (parent));
416 return scm_cell (scm_tc7_dynamic_state, SCM_UNPACK (fluids));
417 }
418 #undef FUNC_NAME
419
420 SCM_DEFINE (scm_dynamic_state_p, "dynamic-state?", 1, 0, 0,
421 (SCM obj),
422 "Return @code{#t} if @var{obj} is a dynamic state object;\n"
423 "return @code{#f} otherwise")
424 #define FUNC_NAME s_scm_dynamic_state_p
425 {
426 return scm_from_bool (IS_DYNAMIC_STATE (obj));
427 }
428 #undef FUNC_NAME
429
430 int
431 scm_is_dynamic_state (SCM obj)
432 {
433 return IS_DYNAMIC_STATE (obj);
434 }
435
436 SCM_DEFINE (scm_current_dynamic_state, "current-dynamic-state", 0, 0, 0,
437 (),
438 "Return the current dynamic state object.")
439 #define FUNC_NAME s_scm_current_dynamic_state
440 {
441 return SCM_I_CURRENT_THREAD->dynamic_state;
442 }
443 #undef FUNC_NAME
444
445 SCM_DEFINE (scm_set_current_dynamic_state, "set-current-dynamic-state", 1,0,0,
446 (SCM state),
447 "Set the current dynamic state object to @var{state}\n"
448 "and return the previous current dynamic state object.")
449 #define FUNC_NAME s_scm_set_current_dynamic_state
450 {
451 scm_i_thread *t = SCM_I_CURRENT_THREAD;
452 SCM old = t->dynamic_state;
453 SCM_ASSERT (IS_DYNAMIC_STATE (state), state, SCM_ARG1, FUNC_NAME);
454 t->dynamic_state = state;
455 return old;
456 }
457 #undef FUNC_NAME
458
459 static void
460 swap_dynamic_state (SCM loc)
461 {
462 SCM_SETCAR (loc, scm_set_current_dynamic_state (SCM_CAR (loc)));
463 }
464
465 void
466 scm_dynwind_current_dynamic_state (SCM state)
467 {
468 SCM loc = scm_cons (state, SCM_EOL);
469 SCM_ASSERT (IS_DYNAMIC_STATE (state), state, SCM_ARG1, NULL);
470 scm_dynwind_rewind_handler_with_scm (swap_dynamic_state, loc,
471 SCM_F_WIND_EXPLICITLY);
472 scm_dynwind_unwind_handler_with_scm (swap_dynamic_state, loc,
473 SCM_F_WIND_EXPLICITLY);
474 }
475
476 void *
477 scm_c_with_dynamic_state (SCM state, void *(*func)(void *), void *data)
478 {
479 void *result;
480 scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
481 scm_dynwind_current_dynamic_state (state);
482 result = func (data);
483 scm_dynwind_end ();
484 return result;
485 }
486
487 SCM_DEFINE (scm_with_dynamic_state, "with-dynamic-state", 2, 0, 0,
488 (SCM state, SCM proc),
489 "Call @var{proc} while @var{state} is the current dynamic\n"
490 "state object.")
491 #define FUNC_NAME s_scm_with_dynamic_state
492 {
493 SCM result;
494 scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
495 scm_dynwind_current_dynamic_state (state);
496 result = scm_call_0 (proc);
497 scm_dynwind_end ();
498 return result;
499 }
500 #undef FUNC_NAME
501
502
503 void
504 scm_init_fluids ()
505 {
506 #include "libguile/fluids.x"
507 }
508
509 /*
510 Local Variables:
511 c-file-style: "gnu"
512 End:
513 */