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