Commit | Line | Data |
---|---|---|
bb0229b5 | 1 | /* Copyright (C) 1996,1997,2000,2001, 2004, 2006, 2007, 2008, 2009, 2010 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 | ||
9de87eea MV |
23 | #include <stdio.h> |
24 | #include <string.h> | |
8b039053 | 25 | #include <assert.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" |
c96d76b8 | 35 | #include "libguile/lang.h" |
a0599745 | 36 | #include "libguile/validate.h" |
9482a297 | 37 | |
9de87eea MV |
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 | */ | |
9482a297 | 58 | |
9de87eea MV |
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 | ||
5ef71027 AW |
68 | #define IS_FLUID(x) SCM_I_FLUID_P (x) |
69 | #define FLUID_NUM(x) SCM_I_FLUID_NUM (x) | |
9de87eea | 70 | |
5ef71027 AW |
71 | #define IS_DYNAMIC_STATE(x) SCM_I_DYNAMIC_STATE_P (x) |
72 | #define DYNAMIC_STATE_FLUIDS(x) SCM_I_DYNAMIC_STATE_FLUIDS (x) | |
9ea31741 | 73 | #define SET_DYNAMIC_STATE_FLUIDS(x, y) SCM_SET_CELL_WORD_1 ((x), (SCM_UNPACK (y))) |
9de87eea | 74 | |
9de87eea | 75 | |
8b039053 LC |
76 | \f |
77 | /* Grow STATE so that it can hold up to ALLOCATED_FLUIDS_NUM fluids. */ | |
9482a297 | 78 | static void |
8b039053 | 79 | grow_dynamic_state (SCM state) |
9de87eea | 80 | { |
8b039053 LC |
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); | |
9de87eea | 84 | |
8b039053 LC |
85 | retry: |
86 | new_len = allocated_fluids_num; | |
87 | new_fluids = scm_c_make_vector (new_len, SCM_BOOL_F); | |
9de87eea | 88 | |
8b039053 LC |
89 | scm_i_pthread_mutex_lock (&fluid_admin_mutex); |
90 | if (new_len != allocated_fluids_num) | |
9482a297 | 91 | { |
8b039053 LC |
92 | /* We lost the race. */ |
93 | scm_i_pthread_mutex_unlock (&fluid_admin_mutex); | |
94 | goto retry; | |
9482a297 MV |
95 | } |
96 | ||
8b039053 LC |
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); | |
9482a297 MV |
105 | } |
106 | ||
9ea31741 AW |
107 | void |
108 | scm_i_fluid_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED) | |
9482a297 | 109 | { |
ed4d7cee | 110 | scm_puts ("#<fluid ", port); |
9de87eea | 111 | scm_intprint ((int) FLUID_NUM (exp), 10, port); |
ed4d7cee | 112 | scm_putc ('>', port); |
9482a297 MV |
113 | } |
114 | ||
45cf2428 AW |
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 | ||
bbb2ecd1 AW |
123 | void |
124 | scm_i_with_fluids_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED) | |
125 | { | |
126 | scm_puts ("#<with-fluids ", port); | |
127 | scm_intprint (SCM_UNPACK (exp), 16, port); | |
128 | scm_putc ('>', port); | |
129 | } | |
130 | ||
9de87eea | 131 | static size_t |
ed4d7cee | 132 | next_fluid_num () |
9482a297 | 133 | { |
9de87eea MV |
134 | size_t n; |
135 | ||
661ae7ab MV |
136 | scm_dynwind_begin (0); |
137 | scm_i_dynwind_pthread_mutex_lock (&fluid_admin_mutex); | |
9de87eea | 138 | |
29295b0c NJ |
139 | if ((allocated_fluids_len > 0) && |
140 | (allocated_fluids_num == allocated_fluids_len)) | |
9de87eea MV |
141 | { |
142 | /* All fluid numbers are in use. Run a GC to try to free some | |
143 | up. | |
144 | */ | |
145 | scm_gc (); | |
146 | } | |
147 | ||
148 | if (allocated_fluids_num < allocated_fluids_len) | |
149 | { | |
150 | for (n = 0; n < allocated_fluids_len; n++) | |
151 | if (allocated_fluids[n] == 0) | |
152 | break; | |
153 | } | |
154 | else | |
155 | { | |
8b039053 LC |
156 | /* Grow the vector of allocated fluids. */ |
157 | /* FIXME: Since we use `scm_malloc ()', ALLOCATED_FLUIDS is scanned by | |
158 | the GC; therefore, all fluids remain reachable for the entire | |
159 | program lifetime. Hopefully this is not a problem in practice. */ | |
d3075c52 | 160 | char *new_allocated_fluids = |
33ed7a16 LC |
161 | scm_gc_malloc (allocated_fluids_len + FLUID_GROW, |
162 | "allocated fluids"); | |
9de87eea MV |
163 | |
164 | /* Copy over old values and initialize rest. GC can not run | |
165 | during these two operations since there is no safe point in | |
166 | them. | |
167 | */ | |
168 | memcpy (new_allocated_fluids, allocated_fluids, allocated_fluids_len); | |
169 | memset (new_allocated_fluids + allocated_fluids_len, 0, FLUID_GROW); | |
170 | n = allocated_fluids_len; | |
d3075c52 | 171 | |
8b039053 LC |
172 | /* Update the vector of allocated fluids. Dynamic states will |
173 | eventually be lazily grown to accomodate the new value of | |
174 | ALLOCATED_FLUIDS_LEN in `fluid-ref' and `fluid-set!'. */ | |
9de87eea MV |
175 | allocated_fluids = new_allocated_fluids; |
176 | allocated_fluids_len += FLUID_GROW; | |
9de87eea MV |
177 | } |
178 | ||
179 | allocated_fluids_num += 1; | |
180 | allocated_fluids[n] = 1; | |
181 | ||
661ae7ab | 182 | scm_dynwind_end (); |
9482a297 MV |
183 | return n; |
184 | } | |
185 | ||
a1ec6916 | 186 | SCM_DEFINE (scm_make_fluid, "make-fluid", 0, 0, 0, |
ed4d7cee GB |
187 | (), |
188 | "Return a newly created fluid.\n" | |
9de87eea MV |
189 | "Fluids are objects that can hold one\n" |
190 | "value per dynamic state. That is, modifications to this value are\n" | |
191 | "only visible to code that executes with the same dynamic state as\n" | |
192 | "the modifying code. When a new dynamic state is constructed, it\n" | |
193 | "inherits the values from its parent. Because each thread normally executes\n" | |
194 | "with its own dynamic state, you can use fluids for thread local storage.") | |
1bbd0b84 | 195 | #define FUNC_NAME s_scm_make_fluid |
9482a297 | 196 | { |
9ea31741 | 197 | return scm_cell (scm_tc7_fluid, (scm_t_bits) next_fluid_num ()); |
9482a297 | 198 | } |
1bbd0b84 | 199 | #undef FUNC_NAME |
9482a297 | 200 | |
a1ec6916 | 201 | SCM_DEFINE (scm_fluid_p, "fluid?", 1, 0, 0, |
ed4d7cee | 202 | (SCM obj), |
1e6808ea MG |
203 | "Return @code{#t} iff @var{obj} is a fluid; otherwise, return\n" |
204 | "@code{#f}.") | |
1bbd0b84 | 205 | #define FUNC_NAME s_scm_fluid_p |
b3460a50 | 206 | { |
9de87eea | 207 | return scm_from_bool (IS_FLUID (obj)); |
b3460a50 | 208 | } |
1bbd0b84 | 209 | #undef FUNC_NAME |
b3460a50 | 210 | |
9de87eea MV |
211 | int |
212 | scm_is_fluid (SCM obj) | |
213 | { | |
214 | return IS_FLUID (obj); | |
215 | } | |
216 | ||
8b039053 | 217 | |
9de87eea | 218 | |
a1ec6916 | 219 | SCM_DEFINE (scm_fluid_ref, "fluid-ref", 1, 0, 0, |
ed4d7cee | 220 | (SCM fluid), |
1e6808ea MG |
221 | "Return the value associated with @var{fluid} in the current\n" |
222 | "dynamic root. If @var{fluid} has not been set, then return\n" | |
223 | "@code{#f}.") | |
1bbd0b84 | 224 | #define FUNC_NAME s_scm_fluid_ref |
9482a297 | 225 | { |
9de87eea | 226 | SCM fluids = DYNAMIC_STATE_FLUIDS (SCM_I_CURRENT_THREAD->dynamic_state); |
9482a297 | 227 | |
ed4d7cee | 228 | SCM_VALIDATE_FLUID (1, fluid); |
8b039053 LC |
229 | |
230 | if (SCM_UNLIKELY (FLUID_NUM (fluid) >= SCM_SIMPLE_VECTOR_LENGTH (fluids))) | |
231 | { | |
232 | /* We should only get there when the current thread's dynamic state | |
233 | turns out to be too small compared to the set of currently allocated | |
234 | fluids. */ | |
235 | assert (SCM_SIMPLE_VECTOR_LENGTH (fluids) < allocated_fluids_num); | |
236 | ||
237 | /* Lazily grow the current thread's dynamic state. */ | |
238 | grow_dynamic_state (SCM_I_CURRENT_THREAD->dynamic_state); | |
239 | ||
240 | fluids = DYNAMIC_STATE_FLUIDS (SCM_I_CURRENT_THREAD->dynamic_state); | |
241 | } | |
242 | ||
9de87eea | 243 | return SCM_SIMPLE_VECTOR_REF (fluids, FLUID_NUM (fluid)); |
9482a297 | 244 | } |
1bbd0b84 | 245 | #undef FUNC_NAME |
9482a297 | 246 | |
a1ec6916 | 247 | SCM_DEFINE (scm_fluid_set_x, "fluid-set!", 2, 0, 0, |
ed4d7cee GB |
248 | (SCM fluid, SCM value), |
249 | "Set the value associated with @var{fluid} in the current dynamic root.") | |
1bbd0b84 | 250 | #define FUNC_NAME s_scm_fluid_set_x |
9482a297 | 251 | { |
9de87eea | 252 | SCM fluids = DYNAMIC_STATE_FLUIDS (SCM_I_CURRENT_THREAD->dynamic_state); |
9482a297 | 253 | |
ed4d7cee | 254 | SCM_VALIDATE_FLUID (1, fluid); |
8b039053 LC |
255 | |
256 | if (SCM_UNLIKELY (FLUID_NUM (fluid) >= SCM_SIMPLE_VECTOR_LENGTH (fluids))) | |
257 | { | |
258 | /* We should only get there when the current thread's dynamic state | |
259 | turns out to be too small compared to the set of currently allocated | |
260 | fluids. */ | |
261 | assert (SCM_SIMPLE_VECTOR_LENGTH (fluids) < allocated_fluids_num); | |
262 | ||
263 | /* Lazily grow the current thread's dynamic state. */ | |
264 | grow_dynamic_state (SCM_I_CURRENT_THREAD->dynamic_state); | |
265 | ||
266 | fluids = DYNAMIC_STATE_FLUIDS (SCM_I_CURRENT_THREAD->dynamic_state); | |
267 | } | |
268 | ||
9de87eea | 269 | SCM_SIMPLE_VECTOR_SET (fluids, FLUID_NUM (fluid), value); |
86f9f9ae | 270 | return SCM_UNSPECIFIED; |
9482a297 | 271 | } |
1bbd0b84 | 272 | #undef FUNC_NAME |
9482a297 | 273 | |
bb0229b5 AW |
274 | static SCM |
275 | apply_thunk (void *thunk) | |
b3460a50 | 276 | { |
bb0229b5 AW |
277 | return scm_call_0 (SCM_PACK (thunk)); |
278 | } | |
279 | ||
280 | SCM | |
281 | scm_i_make_with_fluids (size_t n, SCM *fluids, SCM *vals) | |
282 | { | |
283 | SCM ret; | |
284 | ||
285 | /* Ensure that there are no duplicates in the fluids set -- an N^2 operation, | |
286 | but N will usually be small, so perhaps that's OK. */ | |
287 | { | |
288 | size_t i, j = n; | |
289 | ||
290 | while (j--) | |
291 | for (i = 0; i < j; i++) | |
292 | if (fluids[i] == fluids[j]) | |
293 | { | |
294 | vals[i] = vals[j]; /* later bindings win */ | |
295 | n--; | |
296 | break; | |
297 | } | |
298 | } | |
299 | ||
300 | ret = scm_words (scm_tc7_with_fluids | (n << 8), 1 + n*2); | |
301 | SCM_SET_CELL_WORD_1 (ret, n); | |
302 | ||
303 | while (n--) | |
b3460a50 | 304 | { |
bb0229b5 AW |
305 | if (SCM_UNLIKELY (!IS_FLUID (fluids[n]))) |
306 | scm_wrong_type_arg ("with-fluids", 0, fluids[n]); | |
307 | SCM_SET_CELL_OBJECT (ret, 1 + n * 2, fluids[n]); | |
308 | SCM_SET_CELL_OBJECT (ret, 2 + n * 2, vals[n]); | |
b3460a50 | 309 | } |
bb0229b5 AW |
310 | |
311 | return ret; | |
b3460a50 | 312 | } |
bb0229b5 AW |
313 | |
314 | void | |
315 | scm_i_swap_with_fluids (SCM wf, SCM dynstate) | |
316 | { | |
317 | SCM fluids; | |
318 | size_t i, max = 0; | |
b3460a50 | 319 | |
bb0229b5 | 320 | fluids = DYNAMIC_STATE_FLUIDS (dynstate); |
b3460a50 | 321 | |
bb0229b5 AW |
322 | /* We could cache the max in the with-fluids, but that would take more mem, |
323 | and we're touching all the fluids anyway, so this per-swap traversal should | |
324 | be OK. */ | |
325 | for (i = 0; i < SCM_WITH_FLUIDS_LEN (wf); i++) | |
b3460a50 | 326 | { |
bb0229b5 AW |
327 | size_t num = FLUID_NUM (SCM_WITH_FLUIDS_NTH_FLUID (wf, i)); |
328 | max = (max > num) ? max : num; | |
b3460a50 | 329 | } |
b3460a50 | 330 | |
bb0229b5 AW |
331 | if (SCM_UNLIKELY (max >= SCM_SIMPLE_VECTOR_LENGTH (fluids))) |
332 | { | |
333 | /* We should only get there when the current thread's dynamic state turns | |
334 | out to be too small compared to the set of currently allocated | |
335 | fluids. */ | |
336 | assert (SCM_SIMPLE_VECTOR_LENGTH (fluids) < allocated_fluids_num); | |
1bbd0b84 | 337 | |
bb0229b5 AW |
338 | /* Lazily grow the current thread's dynamic state. */ |
339 | grow_dynamic_state (dynstate); | |
340 | ||
341 | fluids = DYNAMIC_STATE_FLUIDS (dynstate); | |
342 | } | |
1bbd0b84 | 343 | |
bb0229b5 AW |
344 | /* Bind the fluids. Order doesn't matter, as all fluids are distinct. */ |
345 | for (i = 0; i < SCM_WITH_FLUIDS_LEN (wf); i++) | |
346 | { | |
347 | size_t fluid_num; | |
348 | SCM x; | |
349 | ||
350 | fluid_num = FLUID_NUM (SCM_WITH_FLUIDS_NTH_FLUID (wf, i)); | |
351 | x = SCM_SIMPLE_VECTOR_REF (fluids, fluid_num); | |
352 | SCM_SIMPLE_VECTOR_SET (fluids, fluid_num, | |
353 | SCM_WITH_FLUIDS_NTH_VAL (wf, i)); | |
354 | SCM_WITH_FLUIDS_SET_NTH_VAL (wf, i, x); | |
355 | } | |
356 | } | |
357 | ||
a1ec6916 | 358 | SCM_DEFINE (scm_with_fluids, "with-fluids*", 3, 0, 0, |
ed4d7cee GB |
359 | (SCM fluids, SCM values, SCM thunk), |
360 | "Set @var{fluids} to @var{values} temporary, and call @var{thunk}.\n" | |
361 | "@var{fluids} must be a list of fluids and @var{values} must be the same\n" | |
362 | "number of their values to be applied. Each substitution is done\n" | |
363 | "one after another. @var{thunk} must be a procedure with no argument.") | |
1bbd0b84 GB |
364 | #define FUNC_NAME s_scm_with_fluids |
365 | { | |
bebd3fba MV |
366 | return scm_c_with_fluids (fluids, values, |
367 | apply_thunk, (void *) SCM_UNPACK (thunk)); | |
1bbd0b84 GB |
368 | } |
369 | #undef FUNC_NAME | |
b3460a50 MV |
370 | |
371 | SCM | |
143e0902 MV |
372 | scm_c_with_fluids (SCM fluids, SCM values, SCM (*cproc) (), void *cdata) |
373 | #define FUNC_NAME "scm_c_with_fluids" | |
b3460a50 | 374 | { |
bb0229b5 AW |
375 | SCM wf, ans; |
376 | long flen, vlen, i; | |
377 | SCM *fluidsv, *valuesv; | |
b3460a50 | 378 | |
c1bfcf60 | 379 | SCM_VALIDATE_LIST_COPYLEN (1, fluids, flen); |
ed4d7cee | 380 | SCM_VALIDATE_LIST_COPYLEN (2, values, vlen); |
b3460a50 | 381 | if (flen != vlen) |
ed4d7cee | 382 | scm_out_of_range (s_scm_with_fluids, values); |
b3460a50 | 383 | |
bb0229b5 AW |
384 | if (SCM_UNLIKELY (flen == 0)) |
385 | return cproc (cdata); | |
386 | ||
387 | fluidsv = alloca (sizeof(SCM)*flen); | |
388 | valuesv = alloca (sizeof(SCM)*flen); | |
bebd3fba | 389 | |
bb0229b5 AW |
390 | for (i = 0; i < flen; i++) |
391 | { | |
392 | fluidsv[i] = SCM_CAR (fluids); | |
393 | fluids = SCM_CDR (fluids); | |
394 | valuesv[i] = SCM_CAR (values); | |
395 | values = SCM_CDR (values); | |
396 | } | |
397 | ||
398 | wf = scm_i_make_with_fluids (flen, fluidsv, valuesv); | |
399 | scm_i_swap_with_fluids (wf, SCM_I_CURRENT_THREAD->dynamic_state); | |
400 | scm_i_set_dynwinds (scm_cons (wf, scm_i_dynwinds ())); | |
b3460a50 | 401 | ans = cproc (cdata); |
bb0229b5 AW |
402 | scm_i_swap_with_fluids (wf, SCM_I_CURRENT_THREAD->dynamic_state); |
403 | scm_i_set_dynwinds (scm_cdr (scm_i_dynwinds ())); | |
404 | ||
b3460a50 MV |
405 | return ans; |
406 | } | |
c1bfcf60 | 407 | #undef FUNC_NAME |
b3460a50 | 408 | |
bebd3fba MV |
409 | SCM_DEFINE (scm_with_fluid, "with-fluid*", 3, 0, 0, |
410 | (SCM fluid, SCM value, SCM thunk), | |
411 | "Set @var{fluid} to @var{value} temporarily, and call @var{thunk}.\n" | |
412 | "@var{thunk} must be a procedure with no argument.") | |
413 | #define FUNC_NAME s_scm_with_fluid | |
414 | { | |
415 | return scm_c_with_fluid (fluid, value, | |
416 | apply_thunk, (void *) SCM_UNPACK (thunk)); | |
417 | } | |
418 | #undef FUNC_NAME | |
419 | ||
143e0902 MV |
420 | SCM |
421 | scm_c_with_fluid (SCM fluid, SCM value, SCM (*cproc) (), void *cdata) | |
422 | #define FUNC_NAME "scm_c_with_fluid" | |
423 | { | |
bb0229b5 | 424 | SCM ans, wf; |
bebd3fba | 425 | |
bb0229b5 AW |
426 | wf = scm_i_make_with_fluids (1, &fluid, &value); |
427 | scm_i_swap_with_fluids (wf, SCM_I_CURRENT_THREAD->dynamic_state); | |
428 | scm_i_set_dynwinds (scm_cons (wf, scm_i_dynwinds ())); | |
bebd3fba | 429 | ans = cproc (cdata); |
bb0229b5 AW |
430 | scm_i_swap_with_fluids (wf, SCM_I_CURRENT_THREAD->dynamic_state); |
431 | scm_i_set_dynwinds (scm_cdr (scm_i_dynwinds ())); | |
432 | ||
bebd3fba | 433 | return ans; |
143e0902 MV |
434 | } |
435 | #undef FUNC_NAME | |
b3460a50 | 436 | |
ef20bf70 MV |
437 | static void |
438 | swap_fluid (SCM data) | |
439 | { | |
440 | SCM f = SCM_CAR (data); | |
441 | SCM t = scm_fluid_ref (f); | |
442 | scm_fluid_set_x (f, SCM_CDR (data)); | |
443 | SCM_SETCDR (data, t); | |
444 | } | |
445 | ||
446 | void | |
661ae7ab | 447 | scm_dynwind_fluid (SCM fluid, SCM value) |
ef20bf70 MV |
448 | { |
449 | SCM data = scm_cons (fluid, value); | |
661ae7ab MV |
450 | scm_dynwind_rewind_handler_with_scm (swap_fluid, data, SCM_F_WIND_EXPLICITLY); |
451 | scm_dynwind_unwind_handler_with_scm (swap_fluid, data, SCM_F_WIND_EXPLICITLY); | |
ef20bf70 MV |
452 | } |
453 | ||
9de87eea MV |
454 | SCM |
455 | scm_i_make_initial_dynamic_state () | |
456 | { | |
457 | SCM fluids = scm_c_make_vector (allocated_fluids_len, SCM_BOOL_F); | |
9ea31741 | 458 | return scm_cell (scm_tc7_dynamic_state, SCM_UNPACK (fluids)); |
9de87eea MV |
459 | } |
460 | ||
461 | SCM_DEFINE (scm_make_dynamic_state, "make-dynamic-state", 0, 1, 0, | |
462 | (SCM parent), | |
463 | "Return a copy of the dynamic state object @var{parent}\n" | |
464 | "or of the current dynamic state when @var{parent} is omitted.") | |
465 | #define FUNC_NAME s_scm_make_dynamic_state | |
466 | { | |
9ea31741 | 467 | SCM fluids; |
9de87eea MV |
468 | |
469 | if (SCM_UNBNDP (parent)) | |
470 | parent = scm_current_dynamic_state (); | |
471 | ||
9ea31741 | 472 | SCM_ASSERT (IS_DYNAMIC_STATE (parent), parent, SCM_ARG1, FUNC_NAME); |
9de87eea | 473 | fluids = scm_vector_copy (DYNAMIC_STATE_FLUIDS (parent)); |
9ea31741 | 474 | return scm_cell (scm_tc7_dynamic_state, SCM_UNPACK (fluids)); |
9de87eea MV |
475 | } |
476 | #undef FUNC_NAME | |
477 | ||
478 | SCM_DEFINE (scm_dynamic_state_p, "dynamic-state?", 1, 0, 0, | |
479 | (SCM obj), | |
480 | "Return @code{#t} if @var{obj} is a dynamic state object;\n" | |
481 | "return @code{#f} otherwise") | |
482 | #define FUNC_NAME s_scm_dynamic_state_p | |
483 | { | |
484 | return scm_from_bool (IS_DYNAMIC_STATE (obj)); | |
485 | } | |
486 | #undef FUNC_NAME | |
487 | ||
488 | int | |
489 | scm_is_dynamic_state (SCM obj) | |
490 | { | |
491 | return IS_DYNAMIC_STATE (obj); | |
492 | } | |
493 | ||
494 | SCM_DEFINE (scm_current_dynamic_state, "current-dynamic-state", 0, 0, 0, | |
495 | (), | |
496 | "Return the current dynamic state object.") | |
497 | #define FUNC_NAME s_scm_current_dynamic_state | |
498 | { | |
499 | return SCM_I_CURRENT_THREAD->dynamic_state; | |
500 | } | |
501 | #undef FUNC_NAME | |
502 | ||
503 | SCM_DEFINE (scm_set_current_dynamic_state, "set-current-dynamic-state", 1,0,0, | |
504 | (SCM state), | |
505 | "Set the current dynamic state object to @var{state}\n" | |
506 | "and return the previous current dynamic state object.") | |
507 | #define FUNC_NAME s_scm_set_current_dynamic_state | |
508 | { | |
509 | scm_i_thread *t = SCM_I_CURRENT_THREAD; | |
510 | SCM old = t->dynamic_state; | |
9ea31741 | 511 | SCM_ASSERT (IS_DYNAMIC_STATE (state), state, SCM_ARG1, FUNC_NAME); |
9de87eea MV |
512 | t->dynamic_state = state; |
513 | return old; | |
514 | } | |
515 | #undef FUNC_NAME | |
516 | ||
517 | static void | |
518 | swap_dynamic_state (SCM loc) | |
519 | { | |
520 | SCM_SETCAR (loc, scm_set_current_dynamic_state (SCM_CAR (loc))); | |
521 | } | |
522 | ||
523 | void | |
661ae7ab | 524 | scm_dynwind_current_dynamic_state (SCM state) |
9de87eea MV |
525 | { |
526 | SCM loc = scm_cons (state, SCM_EOL); | |
9ea31741 | 527 | SCM_ASSERT (IS_DYNAMIC_STATE (state), state, SCM_ARG1, NULL); |
661ae7ab | 528 | scm_dynwind_rewind_handler_with_scm (swap_dynamic_state, loc, |
9de87eea | 529 | SCM_F_WIND_EXPLICITLY); |
661ae7ab | 530 | scm_dynwind_unwind_handler_with_scm (swap_dynamic_state, loc, |
9de87eea MV |
531 | SCM_F_WIND_EXPLICITLY); |
532 | } | |
533 | ||
534 | void * | |
535 | scm_c_with_dynamic_state (SCM state, void *(*func)(void *), void *data) | |
536 | { | |
537 | void *result; | |
661ae7ab MV |
538 | scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE); |
539 | scm_dynwind_current_dynamic_state (state); | |
9de87eea | 540 | result = func (data); |
661ae7ab | 541 | scm_dynwind_end (); |
9de87eea MV |
542 | return result; |
543 | } | |
544 | ||
545 | SCM_DEFINE (scm_with_dynamic_state, "with-dynamic-state", 2, 0, 0, | |
546 | (SCM state, SCM proc), | |
547 | "Call @var{proc} while @var{state} is the current dynamic\n" | |
548 | "state object.") | |
549 | #define FUNC_NAME s_scm_with_dynamic_state | |
550 | { | |
551 | SCM result; | |
661ae7ab MV |
552 | scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE); |
553 | scm_dynwind_current_dynamic_state (state); | |
9de87eea | 554 | result = scm_call_0 (proc); |
661ae7ab | 555 | scm_dynwind_end (); |
9de87eea MV |
556 | return result; |
557 | } | |
558 | #undef FUNC_NAME | |
559 | ||
9de87eea | 560 | |
9482a297 MV |
561 | void |
562 | scm_init_fluids () | |
563 | { | |
a0599745 | 564 | #include "libguile/fluids.x" |
9482a297 | 565 | } |
89e00824 ML |
566 | |
567 | /* | |
568 | Local Variables: | |
569 | c-file-style: "gnu" | |
570 | End: | |
571 | */ |