Commit | Line | Data |
---|---|---|
d223c3fc | 1 | /* Copyright (C) 1996,1997,2000,2001, 2004, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc. |
9482a297 | 2 | * |
73be1d9e | 3 | * This library is free software; you can redistribute it and/or |
53befeb7 NJ |
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. | |
9482a297 | 7 | * |
53befeb7 NJ |
8 | * This library is distributed in the hope that it will be useful, but |
9 | * 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. | |
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 | |
53befeb7 NJ |
15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
16 | * 02110-1301 USA | |
73be1d9e | 17 | */ |
9482a297 | 18 | |
dbb605f5 LC |
19 | #ifdef HAVE_CONFIG_H |
20 | # include <config.h> | |
21 | #endif | |
22 | ||
cdd47ec7 | 23 | #include <alloca.h> |
9de87eea MV |
24 | #include <stdio.h> |
25 | #include <string.h> | |
1bbd0b84 | 26 | |
a0599745 MD |
27 | #include "libguile/_scm.h" |
28 | #include "libguile/print.h" | |
a0599745 MD |
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" | |
143e0902 | 34 | #include "libguile/deprecation.h" |
a0599745 | 35 | #include "libguile/validate.h" |
bd5a75dc | 36 | #include "libguile/bdw-gc.h" |
9482a297 | 37 | |
bd5a75dc LC |
38 | /* Number of additional slots to allocate when ALLOCATED_FLUIDS is full. */ |
39 | #define FLUID_GROW 128 | |
9de87eea | 40 | |
bd5a75dc LC |
41 | /* Vector of allocated fluids indexed by fluid numbers. Access is protected by |
42 | FLUID_ADMIN_MUTEX. */ | |
43 | static void **allocated_fluids = NULL; | |
44 | static size_t allocated_fluids_len = 0; | |
9482a297 | 45 | |
9de87eea MV |
46 | static scm_i_pthread_mutex_t fluid_admin_mutex = SCM_I_PTHREAD_MUTEX_INITIALIZER; |
47 | ||
6f8d7b12 | 48 | #define IS_FLUID(x) SCM_FLUID_P (x) |
5ef71027 | 49 | #define FLUID_NUM(x) SCM_I_FLUID_NUM (x) |
9de87eea | 50 | |
5ef71027 AW |
51 | #define IS_DYNAMIC_STATE(x) SCM_I_DYNAMIC_STATE_P (x) |
52 | #define DYNAMIC_STATE_FLUIDS(x) SCM_I_DYNAMIC_STATE_FLUIDS (x) | |
9ea31741 | 53 | #define SET_DYNAMIC_STATE_FLUIDS(x, y) SCM_SET_CELL_WORD_1 ((x), (SCM_UNPACK (y))) |
9de87eea | 54 | |
9de87eea | 55 | |
8b039053 | 56 | \f |
bd5a75dc LC |
57 | /* Grow STATE so that it can hold up to ALLOCATED_FLUIDS_LEN fluids. This may |
58 | be more than necessary since ALLOCATED_FLUIDS is sparse and the current | |
59 | thread may not access all the fluids anyway. Memory usage could be improved | |
60 | by using a 2-level array as is done in glibc for pthread keys (TODO). */ | |
9482a297 | 61 | static void |
8b039053 | 62 | grow_dynamic_state (SCM state) |
9de87eea | 63 | { |
8b039053 LC |
64 | SCM new_fluids; |
65 | SCM old_fluids = DYNAMIC_STATE_FLUIDS (state); | |
bd5a75dc | 66 | size_t i, len, old_len = SCM_SIMPLE_VECTOR_LENGTH (old_fluids); |
9de87eea | 67 | |
bd5a75dc LC |
68 | /* Assume the assignment below is atomic. */ |
69 | len = allocated_fluids_len; | |
9de87eea | 70 | |
bd5a75dc | 71 | new_fluids = scm_c_make_vector (len, SCM_BOOL_F); |
8b039053 LC |
72 | |
73 | for (i = 0; i < old_len; i++) | |
74 | SCM_SIMPLE_VECTOR_SET (new_fluids, i, | |
75 | SCM_SIMPLE_VECTOR_REF (old_fluids, i)); | |
76 | SET_DYNAMIC_STATE_FLUIDS (state, new_fluids); | |
9482a297 MV |
77 | } |
78 | ||
9ea31741 AW |
79 | void |
80 | scm_i_fluid_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED) | |
9482a297 | 81 | { |
ed4d7cee | 82 | scm_puts ("#<fluid ", port); |
9de87eea | 83 | scm_intprint ((int) FLUID_NUM (exp), 10, port); |
ed4d7cee | 84 | scm_putc ('>', port); |
9482a297 MV |
85 | } |
86 | ||
45cf2428 AW |
87 | void |
88 | scm_i_dynamic_state_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED) | |
89 | { | |
90 | scm_puts ("#<dynamic-state ", port); | |
91 | scm_intprint (SCM_UNPACK (exp), 16, port); | |
92 | scm_putc ('>', port); | |
93 | } | |
94 | ||
bbb2ecd1 AW |
95 | void |
96 | scm_i_with_fluids_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED) | |
97 | { | |
98 | scm_puts ("#<with-fluids ", port); | |
99 | scm_intprint (SCM_UNPACK (exp), 16, port); | |
100 | scm_putc ('>', port); | |
101 | } | |
102 | ||
bd5a75dc LC |
103 | \f |
104 | /* Return a new fluid. */ | |
105 | static SCM | |
106 | new_fluid () | |
9482a297 | 107 | { |
bd5a75dc LC |
108 | SCM fluid; |
109 | size_t trial, n; | |
110 | ||
111 | /* Fluids are pointerless cells: the first word is the type tag; the second | |
112 | word is the fluid number. */ | |
113 | fluid = PTR2SCM (scm_gc_malloc_pointerless (sizeof (scm_t_cell), "fluid")); | |
114 | SCM_SET_CELL_TYPE (fluid, scm_tc7_fluid); | |
9de87eea | 115 | |
661ae7ab MV |
116 | scm_dynwind_begin (0); |
117 | scm_i_dynwind_pthread_mutex_lock (&fluid_admin_mutex); | |
9de87eea | 118 | |
bd5a75dc | 119 | for (trial = 0; trial < 2; trial++) |
9de87eea | 120 | { |
bd5a75dc | 121 | /* Look for a free fluid number. */ |
9de87eea | 122 | for (n = 0; n < allocated_fluids_len; n++) |
bd5a75dc LC |
123 | /* TODO: Use `__sync_bool_compare_and_swap' where available. */ |
124 | if (allocated_fluids[n] == NULL) | |
9de87eea | 125 | break; |
bd5a75dc LC |
126 | |
127 | if (trial == 0 && n >= allocated_fluids_len) | |
128 | /* All fluid numbers are in use. Run a GC and retry. Explicitly | |
129 | running the GC is costly and bad-style. We only do this because | |
130 | dynamic state fluid vectors would grow unreasonably if fluid numbers | |
131 | weren't reused. */ | |
132 | scm_i_gc ("fluids"); | |
9de87eea | 133 | } |
bd5a75dc LC |
134 | |
135 | if (n >= allocated_fluids_len) | |
9de87eea | 136 | { |
8b039053 | 137 | /* Grow the vector of allocated fluids. */ |
bd5a75dc LC |
138 | void **new_allocated_fluids = |
139 | scm_gc_malloc_pointerless ((allocated_fluids_len + FLUID_GROW) | |
140 | * sizeof (*allocated_fluids), | |
141 | "allocated fluids"); | |
9de87eea MV |
142 | |
143 | /* Copy over old values and initialize rest. GC can not run | |
144 | during these two operations since there is no safe point in | |
bd5a75dc LC |
145 | them. */ |
146 | memcpy (new_allocated_fluids, allocated_fluids, | |
147 | allocated_fluids_len * sizeof (*allocated_fluids)); | |
148 | memset (new_allocated_fluids + allocated_fluids_len, 0, | |
149 | FLUID_GROW * sizeof (*allocated_fluids)); | |
9de87eea | 150 | n = allocated_fluids_len; |
d3075c52 | 151 | |
8b039053 LC |
152 | /* Update the vector of allocated fluids. Dynamic states will |
153 | eventually be lazily grown to accomodate the new value of | |
154 | ALLOCATED_FLUIDS_LEN in `fluid-ref' and `fluid-set!'. */ | |
9de87eea MV |
155 | allocated_fluids = new_allocated_fluids; |
156 | allocated_fluids_len += FLUID_GROW; | |
9de87eea | 157 | } |
bd5a75dc LC |
158 | |
159 | allocated_fluids[n] = SCM2PTR (fluid); | |
160 | SCM_SET_CELL_WORD_1 (fluid, (scm_t_bits) n); | |
161 | ||
162 | GC_GENERAL_REGISTER_DISAPPEARING_LINK (&allocated_fluids[n], | |
163 | SCM2PTR (fluid)); | |
164 | ||
661ae7ab | 165 | scm_dynwind_end (); |
bd5a75dc | 166 | return fluid; |
9482a297 MV |
167 | } |
168 | ||
a1ec6916 | 169 | SCM_DEFINE (scm_make_fluid, "make-fluid", 0, 0, 0, |
ed4d7cee GB |
170 | (), |
171 | "Return a newly created fluid.\n" | |
9de87eea MV |
172 | "Fluids are objects that can hold one\n" |
173 | "value per dynamic state. That is, modifications to this value are\n" | |
174 | "only visible to code that executes with the same dynamic state as\n" | |
175 | "the modifying code. When a new dynamic state is constructed, it\n" | |
176 | "inherits the values from its parent. Because each thread normally executes\n" | |
177 | "with its own dynamic state, you can use fluids for thread local storage.") | |
1bbd0b84 | 178 | #define FUNC_NAME s_scm_make_fluid |
9482a297 | 179 | { |
bd5a75dc | 180 | return new_fluid (); |
9482a297 | 181 | } |
1bbd0b84 | 182 | #undef FUNC_NAME |
9482a297 | 183 | |
e01163b5 | 184 | SCM_DEFINE (scm_make_unbound_fluid, "make-unbound-fluid", 0, 0, 0, |
ef94624e | 185 | (), |
e01163b5 AW |
186 | "Make a fluid that is initially unbound.") |
187 | #define FUNC_NAME s_scm_make_unbound_fluid | |
ef94624e BT |
188 | { |
189 | SCM f = new_fluid (); | |
190 | scm_fluid_set_x (f, SCM_UNDEFINED); | |
191 | return f; | |
192 | } | |
193 | #undef FUNC_NAME | |
194 | ||
a1ec6916 | 195 | SCM_DEFINE (scm_fluid_p, "fluid?", 1, 0, 0, |
ed4d7cee | 196 | (SCM obj), |
1e6808ea MG |
197 | "Return @code{#t} iff @var{obj} is a fluid; otherwise, return\n" |
198 | "@code{#f}.") | |
1bbd0b84 | 199 | #define FUNC_NAME s_scm_fluid_p |
b3460a50 | 200 | { |
9de87eea | 201 | return scm_from_bool (IS_FLUID (obj)); |
b3460a50 | 202 | } |
1bbd0b84 | 203 | #undef FUNC_NAME |
b3460a50 | 204 | |
9de87eea MV |
205 | int |
206 | scm_is_fluid (SCM obj) | |
207 | { | |
208 | return IS_FLUID (obj); | |
209 | } | |
210 | ||
ef94624e BT |
211 | /* Does not check type of `fluid'! */ |
212 | static SCM | |
213 | fluid_ref (SCM fluid) | |
9482a297 | 214 | { |
9de87eea | 215 | SCM fluids = DYNAMIC_STATE_FLUIDS (SCM_I_CURRENT_THREAD->dynamic_state); |
9482a297 | 216 | |
8b039053 LC |
217 | if (SCM_UNLIKELY (FLUID_NUM (fluid) >= SCM_SIMPLE_VECTOR_LENGTH (fluids))) |
218 | { | |
8b039053 LC |
219 | /* Lazily grow the current thread's dynamic state. */ |
220 | grow_dynamic_state (SCM_I_CURRENT_THREAD->dynamic_state); | |
221 | ||
222 | fluids = DYNAMIC_STATE_FLUIDS (SCM_I_CURRENT_THREAD->dynamic_state); | |
223 | } | |
224 | ||
9de87eea | 225 | return SCM_SIMPLE_VECTOR_REF (fluids, FLUID_NUM (fluid)); |
9482a297 | 226 | } |
ef94624e BT |
227 | |
228 | SCM_DEFINE (scm_fluid_ref, "fluid-ref", 1, 0, 0, | |
229 | (SCM fluid), | |
230 | "Return the value associated with @var{fluid} in the current\n" | |
231 | "dynamic root. If @var{fluid} has not been set, then return\n" | |
232 | "@code{#f}.") | |
233 | #define FUNC_NAME s_scm_fluid_ref | |
234 | { | |
235 | SCM val; | |
236 | SCM_VALIDATE_FLUID (1, fluid); | |
237 | val = fluid_ref (fluid); | |
238 | if (SCM_UNBNDP (val)) | |
239 | SCM_MISC_ERROR ("unbound fluid: ~S", | |
240 | scm_list_1 (fluid)); | |
241 | return val; | |
242 | } | |
1bbd0b84 | 243 | #undef FUNC_NAME |
9482a297 | 244 | |
a1ec6916 | 245 | SCM_DEFINE (scm_fluid_set_x, "fluid-set!", 2, 0, 0, |
ed4d7cee GB |
246 | (SCM fluid, SCM value), |
247 | "Set the value associated with @var{fluid} in the current dynamic root.") | |
1bbd0b84 | 248 | #define FUNC_NAME s_scm_fluid_set_x |
9482a297 | 249 | { |
9de87eea | 250 | SCM fluids = DYNAMIC_STATE_FLUIDS (SCM_I_CURRENT_THREAD->dynamic_state); |
9482a297 | 251 | |
ed4d7cee | 252 | SCM_VALIDATE_FLUID (1, fluid); |
8b039053 LC |
253 | |
254 | if (SCM_UNLIKELY (FLUID_NUM (fluid) >= SCM_SIMPLE_VECTOR_LENGTH (fluids))) | |
255 | { | |
8b039053 LC |
256 | /* Lazily grow the current thread's dynamic state. */ |
257 | grow_dynamic_state (SCM_I_CURRENT_THREAD->dynamic_state); | |
258 | ||
259 | fluids = DYNAMIC_STATE_FLUIDS (SCM_I_CURRENT_THREAD->dynamic_state); | |
260 | } | |
261 | ||
9de87eea | 262 | SCM_SIMPLE_VECTOR_SET (fluids, FLUID_NUM (fluid), value); |
86f9f9ae | 263 | return SCM_UNSPECIFIED; |
9482a297 | 264 | } |
1bbd0b84 | 265 | #undef FUNC_NAME |
9482a297 | 266 | |
ef94624e BT |
267 | SCM_DEFINE (scm_fluid_unset_x, "fluid-unset!", 1, 0, 0, |
268 | (SCM fluid), | |
269 | "Unset the value associated with @var{fluid}.") | |
270 | #define FUNC_NAME s_scm_fluid_unset_x | |
271 | { | |
272 | return scm_fluid_set_x (fluid, SCM_UNDEFINED); | |
273 | } | |
274 | #undef FUNC_NAME | |
275 | ||
276 | SCM_DEFINE (scm_fluid_bound_p, "fluid-bound?", 1, 0, 0, | |
277 | (SCM fluid), | |
278 | "Return @code{#t} iff @var{fluid} is bound to a value.\n" | |
279 | "Throw an error if @var{fluid} is not a fluid.") | |
280 | #define FUNC_NAME s_scm_fluid_bound_p | |
281 | { | |
282 | SCM val; | |
283 | SCM_VALIDATE_FLUID (1, fluid); | |
284 | val = fluid_ref (fluid); | |
285 | return scm_from_bool (! (SCM_UNBNDP (val))); | |
286 | } | |
287 | #undef FUNC_NAME | |
288 | ||
bb0229b5 AW |
289 | static SCM |
290 | apply_thunk (void *thunk) | |
b3460a50 | 291 | { |
bb0229b5 AW |
292 | return scm_call_0 (SCM_PACK (thunk)); |
293 | } | |
294 | ||
295 | SCM | |
296 | scm_i_make_with_fluids (size_t n, SCM *fluids, SCM *vals) | |
297 | { | |
298 | SCM ret; | |
299 | ||
300 | /* Ensure that there are no duplicates in the fluids set -- an N^2 operation, | |
301 | but N will usually be small, so perhaps that's OK. */ | |
302 | { | |
303 | size_t i, j = n; | |
304 | ||
305 | while (j--) | |
306 | for (i = 0; i < j; i++) | |
d223c3fc | 307 | if (scm_is_eq (fluids[i], fluids[j])) |
bb0229b5 AW |
308 | { |
309 | vals[i] = vals[j]; /* later bindings win */ | |
310 | n--; | |
311 | break; | |
312 | } | |
313 | } | |
314 | ||
315 | ret = scm_words (scm_tc7_with_fluids | (n << 8), 1 + n*2); | |
316 | SCM_SET_CELL_WORD_1 (ret, n); | |
317 | ||
318 | while (n--) | |
b3460a50 | 319 | { |
bb0229b5 AW |
320 | if (SCM_UNLIKELY (!IS_FLUID (fluids[n]))) |
321 | scm_wrong_type_arg ("with-fluids", 0, fluids[n]); | |
322 | SCM_SET_CELL_OBJECT (ret, 1 + n * 2, fluids[n]); | |
323 | SCM_SET_CELL_OBJECT (ret, 2 + n * 2, vals[n]); | |
b3460a50 | 324 | } |
bb0229b5 AW |
325 | |
326 | return ret; | |
b3460a50 | 327 | } |
bb0229b5 AW |
328 | |
329 | void | |
330 | scm_i_swap_with_fluids (SCM wf, SCM dynstate) | |
331 | { | |
332 | SCM fluids; | |
333 | size_t i, max = 0; | |
b3460a50 | 334 | |
bb0229b5 | 335 | fluids = DYNAMIC_STATE_FLUIDS (dynstate); |
b3460a50 | 336 | |
bb0229b5 AW |
337 | /* We could cache the max in the with-fluids, but that would take more mem, |
338 | and we're touching all the fluids anyway, so this per-swap traversal should | |
339 | be OK. */ | |
340 | for (i = 0; i < SCM_WITH_FLUIDS_LEN (wf); i++) | |
b3460a50 | 341 | { |
bb0229b5 AW |
342 | size_t num = FLUID_NUM (SCM_WITH_FLUIDS_NTH_FLUID (wf, i)); |
343 | max = (max > num) ? max : num; | |
b3460a50 | 344 | } |
b3460a50 | 345 | |
bb0229b5 AW |
346 | if (SCM_UNLIKELY (max >= SCM_SIMPLE_VECTOR_LENGTH (fluids))) |
347 | { | |
bb0229b5 AW |
348 | /* Lazily grow the current thread's dynamic state. */ |
349 | grow_dynamic_state (dynstate); | |
350 | ||
351 | fluids = DYNAMIC_STATE_FLUIDS (dynstate); | |
352 | } | |
1bbd0b84 | 353 | |
bb0229b5 AW |
354 | /* Bind the fluids. Order doesn't matter, as all fluids are distinct. */ |
355 | for (i = 0; i < SCM_WITH_FLUIDS_LEN (wf); i++) | |
356 | { | |
357 | size_t fluid_num; | |
358 | SCM x; | |
359 | ||
360 | fluid_num = FLUID_NUM (SCM_WITH_FLUIDS_NTH_FLUID (wf, i)); | |
361 | x = SCM_SIMPLE_VECTOR_REF (fluids, fluid_num); | |
362 | SCM_SIMPLE_VECTOR_SET (fluids, fluid_num, | |
363 | SCM_WITH_FLUIDS_NTH_VAL (wf, i)); | |
364 | SCM_WITH_FLUIDS_SET_NTH_VAL (wf, i, x); | |
365 | } | |
366 | } | |
367 | ||
a1ec6916 | 368 | SCM_DEFINE (scm_with_fluids, "with-fluids*", 3, 0, 0, |
ed4d7cee GB |
369 | (SCM fluids, SCM values, SCM thunk), |
370 | "Set @var{fluids} to @var{values} temporary, and call @var{thunk}.\n" | |
371 | "@var{fluids} must be a list of fluids and @var{values} must be the same\n" | |
372 | "number of their values to be applied. Each substitution is done\n" | |
373 | "one after another. @var{thunk} must be a procedure with no argument.") | |
1bbd0b84 GB |
374 | #define FUNC_NAME s_scm_with_fluids |
375 | { | |
bebd3fba MV |
376 | return scm_c_with_fluids (fluids, values, |
377 | apply_thunk, (void *) SCM_UNPACK (thunk)); | |
1bbd0b84 GB |
378 | } |
379 | #undef FUNC_NAME | |
b3460a50 MV |
380 | |
381 | SCM | |
143e0902 MV |
382 | scm_c_with_fluids (SCM fluids, SCM values, SCM (*cproc) (), void *cdata) |
383 | #define FUNC_NAME "scm_c_with_fluids" | |
b3460a50 | 384 | { |
bb0229b5 AW |
385 | SCM wf, ans; |
386 | long flen, vlen, i; | |
387 | SCM *fluidsv, *valuesv; | |
b3460a50 | 388 | |
c1bfcf60 | 389 | SCM_VALIDATE_LIST_COPYLEN (1, fluids, flen); |
ed4d7cee | 390 | SCM_VALIDATE_LIST_COPYLEN (2, values, vlen); |
b3460a50 | 391 | if (flen != vlen) |
ed4d7cee | 392 | scm_out_of_range (s_scm_with_fluids, values); |
b3460a50 | 393 | |
bb0229b5 AW |
394 | if (SCM_UNLIKELY (flen == 0)) |
395 | return cproc (cdata); | |
396 | ||
397 | fluidsv = alloca (sizeof(SCM)*flen); | |
398 | valuesv = alloca (sizeof(SCM)*flen); | |
bebd3fba | 399 | |
bb0229b5 AW |
400 | for (i = 0; i < flen; i++) |
401 | { | |
402 | fluidsv[i] = SCM_CAR (fluids); | |
403 | fluids = SCM_CDR (fluids); | |
404 | valuesv[i] = SCM_CAR (values); | |
405 | values = SCM_CDR (values); | |
406 | } | |
407 | ||
408 | wf = scm_i_make_with_fluids (flen, fluidsv, valuesv); | |
409 | scm_i_swap_with_fluids (wf, SCM_I_CURRENT_THREAD->dynamic_state); | |
410 | scm_i_set_dynwinds (scm_cons (wf, scm_i_dynwinds ())); | |
b3460a50 | 411 | ans = cproc (cdata); |
bb0229b5 AW |
412 | scm_i_swap_with_fluids (wf, SCM_I_CURRENT_THREAD->dynamic_state); |
413 | scm_i_set_dynwinds (scm_cdr (scm_i_dynwinds ())); | |
414 | ||
b3460a50 MV |
415 | return ans; |
416 | } | |
c1bfcf60 | 417 | #undef FUNC_NAME |
b3460a50 | 418 | |
bebd3fba MV |
419 | SCM_DEFINE (scm_with_fluid, "with-fluid*", 3, 0, 0, |
420 | (SCM fluid, SCM value, SCM thunk), | |
421 | "Set @var{fluid} to @var{value} temporarily, and call @var{thunk}.\n" | |
422 | "@var{thunk} must be a procedure with no argument.") | |
423 | #define FUNC_NAME s_scm_with_fluid | |
424 | { | |
425 | return scm_c_with_fluid (fluid, value, | |
426 | apply_thunk, (void *) SCM_UNPACK (thunk)); | |
427 | } | |
428 | #undef FUNC_NAME | |
429 | ||
143e0902 MV |
430 | SCM |
431 | scm_c_with_fluid (SCM fluid, SCM value, SCM (*cproc) (), void *cdata) | |
432 | #define FUNC_NAME "scm_c_with_fluid" | |
433 | { | |
bb0229b5 | 434 | SCM ans, wf; |
bebd3fba | 435 | |
bb0229b5 AW |
436 | wf = scm_i_make_with_fluids (1, &fluid, &value); |
437 | scm_i_swap_with_fluids (wf, SCM_I_CURRENT_THREAD->dynamic_state); | |
438 | scm_i_set_dynwinds (scm_cons (wf, scm_i_dynwinds ())); | |
bebd3fba | 439 | ans = cproc (cdata); |
bb0229b5 AW |
440 | scm_i_swap_with_fluids (wf, SCM_I_CURRENT_THREAD->dynamic_state); |
441 | scm_i_set_dynwinds (scm_cdr (scm_i_dynwinds ())); | |
442 | ||
bebd3fba | 443 | return ans; |
143e0902 MV |
444 | } |
445 | #undef FUNC_NAME | |
b3460a50 | 446 | |
ef20bf70 MV |
447 | static void |
448 | swap_fluid (SCM data) | |
449 | { | |
450 | SCM f = SCM_CAR (data); | |
ef94624e | 451 | SCM t = fluid_ref (f); |
ef20bf70 MV |
452 | scm_fluid_set_x (f, SCM_CDR (data)); |
453 | SCM_SETCDR (data, t); | |
454 | } | |
455 | ||
456 | void | |
661ae7ab | 457 | scm_dynwind_fluid (SCM fluid, SCM value) |
ef20bf70 MV |
458 | { |
459 | SCM data = scm_cons (fluid, value); | |
661ae7ab MV |
460 | scm_dynwind_rewind_handler_with_scm (swap_fluid, data, SCM_F_WIND_EXPLICITLY); |
461 | scm_dynwind_unwind_handler_with_scm (swap_fluid, data, SCM_F_WIND_EXPLICITLY); | |
ef20bf70 MV |
462 | } |
463 | ||
9de87eea MV |
464 | SCM |
465 | scm_i_make_initial_dynamic_state () | |
466 | { | |
467 | SCM fluids = scm_c_make_vector (allocated_fluids_len, SCM_BOOL_F); | |
9ea31741 | 468 | return scm_cell (scm_tc7_dynamic_state, SCM_UNPACK (fluids)); |
9de87eea MV |
469 | } |
470 | ||
471 | SCM_DEFINE (scm_make_dynamic_state, "make-dynamic-state", 0, 1, 0, | |
472 | (SCM parent), | |
473 | "Return a copy of the dynamic state object @var{parent}\n" | |
474 | "or of the current dynamic state when @var{parent} is omitted.") | |
475 | #define FUNC_NAME s_scm_make_dynamic_state | |
476 | { | |
9ea31741 | 477 | SCM fluids; |
9de87eea MV |
478 | |
479 | if (SCM_UNBNDP (parent)) | |
480 | parent = scm_current_dynamic_state (); | |
481 | ||
9ea31741 | 482 | SCM_ASSERT (IS_DYNAMIC_STATE (parent), parent, SCM_ARG1, FUNC_NAME); |
9de87eea | 483 | fluids = scm_vector_copy (DYNAMIC_STATE_FLUIDS (parent)); |
9ea31741 | 484 | return scm_cell (scm_tc7_dynamic_state, SCM_UNPACK (fluids)); |
9de87eea MV |
485 | } |
486 | #undef FUNC_NAME | |
487 | ||
488 | SCM_DEFINE (scm_dynamic_state_p, "dynamic-state?", 1, 0, 0, | |
489 | (SCM obj), | |
490 | "Return @code{#t} if @var{obj} is a dynamic state object;\n" | |
491 | "return @code{#f} otherwise") | |
492 | #define FUNC_NAME s_scm_dynamic_state_p | |
493 | { | |
494 | return scm_from_bool (IS_DYNAMIC_STATE (obj)); | |
495 | } | |
496 | #undef FUNC_NAME | |
497 | ||
498 | int | |
499 | scm_is_dynamic_state (SCM obj) | |
500 | { | |
501 | return IS_DYNAMIC_STATE (obj); | |
502 | } | |
503 | ||
504 | SCM_DEFINE (scm_current_dynamic_state, "current-dynamic-state", 0, 0, 0, | |
505 | (), | |
506 | "Return the current dynamic state object.") | |
507 | #define FUNC_NAME s_scm_current_dynamic_state | |
508 | { | |
509 | return SCM_I_CURRENT_THREAD->dynamic_state; | |
510 | } | |
511 | #undef FUNC_NAME | |
512 | ||
513 | SCM_DEFINE (scm_set_current_dynamic_state, "set-current-dynamic-state", 1,0,0, | |
514 | (SCM state), | |
515 | "Set the current dynamic state object to @var{state}\n" | |
516 | "and return the previous current dynamic state object.") | |
517 | #define FUNC_NAME s_scm_set_current_dynamic_state | |
518 | { | |
519 | scm_i_thread *t = SCM_I_CURRENT_THREAD; | |
520 | SCM old = t->dynamic_state; | |
9ea31741 | 521 | SCM_ASSERT (IS_DYNAMIC_STATE (state), state, SCM_ARG1, FUNC_NAME); |
9de87eea MV |
522 | t->dynamic_state = state; |
523 | return old; | |
524 | } | |
525 | #undef FUNC_NAME | |
526 | ||
527 | static void | |
528 | swap_dynamic_state (SCM loc) | |
529 | { | |
530 | SCM_SETCAR (loc, scm_set_current_dynamic_state (SCM_CAR (loc))); | |
531 | } | |
532 | ||
533 | void | |
661ae7ab | 534 | scm_dynwind_current_dynamic_state (SCM state) |
9de87eea MV |
535 | { |
536 | SCM loc = scm_cons (state, SCM_EOL); | |
9ea31741 | 537 | SCM_ASSERT (IS_DYNAMIC_STATE (state), state, SCM_ARG1, NULL); |
661ae7ab | 538 | scm_dynwind_rewind_handler_with_scm (swap_dynamic_state, loc, |
9de87eea | 539 | SCM_F_WIND_EXPLICITLY); |
661ae7ab | 540 | scm_dynwind_unwind_handler_with_scm (swap_dynamic_state, loc, |
9de87eea MV |
541 | SCM_F_WIND_EXPLICITLY); |
542 | } | |
543 | ||
544 | void * | |
545 | scm_c_with_dynamic_state (SCM state, void *(*func)(void *), void *data) | |
546 | { | |
547 | void *result; | |
661ae7ab MV |
548 | scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE); |
549 | scm_dynwind_current_dynamic_state (state); | |
9de87eea | 550 | result = func (data); |
661ae7ab | 551 | scm_dynwind_end (); |
9de87eea MV |
552 | return result; |
553 | } | |
554 | ||
555 | SCM_DEFINE (scm_with_dynamic_state, "with-dynamic-state", 2, 0, 0, | |
556 | (SCM state, SCM proc), | |
557 | "Call @var{proc} while @var{state} is the current dynamic\n" | |
558 | "state object.") | |
559 | #define FUNC_NAME s_scm_with_dynamic_state | |
560 | { | |
561 | SCM result; | |
661ae7ab MV |
562 | scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE); |
563 | scm_dynwind_current_dynamic_state (state); | |
9de87eea | 564 | result = scm_call_0 (proc); |
661ae7ab | 565 | scm_dynwind_end (); |
9de87eea MV |
566 | return result; |
567 | } | |
568 | #undef FUNC_NAME | |
569 | ||
9de87eea | 570 | |
9482a297 MV |
571 | void |
572 | scm_init_fluids () | |
573 | { | |
a0599745 | 574 | #include "libguile/fluids.x" |
9482a297 | 575 | } |
89e00824 ML |
576 | |
577 | /* | |
578 | Local Variables: | |
579 | c-file-style: "gnu" | |
580 | End: | |
581 | */ |