Commit | Line | Data |
---|---|---|
22a52da1 | 1 | /* Copyright (C) 1996,1997,2000,2001 Free Software Foundation, Inc. |
9482a297 MV |
2 | * |
3 | * This program is free software; you can redistribute it and/or modify | |
4 | * it under the terms of the GNU General Public License as published by | |
5 | * the Free Software Foundation; either version 2, or (at your option) | |
6 | * any later version. | |
7 | * | |
8 | * This program 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 | |
11 | * GNU General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License | |
14 | * along with this software; see the file COPYING. If not, write to | |
15 | * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, | |
16 | * Boston, MA 02111-1307 USA | |
17 | * | |
18 | * As a special exception, the Free Software Foundation gives permission | |
19 | * for additional uses of the text contained in its release of GUILE. | |
20 | * | |
21 | * The exception is that, if you link the GUILE library with other files | |
22 | * to produce an executable, this does not by itself cause the | |
23 | * resulting executable to be covered by the GNU General Public License. | |
24 | * Your use of that executable is in no way restricted on account of | |
25 | * linking the GUILE library code into it. | |
26 | * | |
27 | * This exception does not however invalidate any other reasons why | |
28 | * the executable file might be covered by the GNU General Public License. | |
29 | * | |
30 | * This exception applies only to the code released by the | |
31 | * Free Software Foundation under the name GUILE. If you copy | |
32 | * code from other Free Software Foundation releases into a copy of | |
33 | * GUILE, as the General Public License permits, the exception does | |
34 | * not apply to the code that you add in this way. To avoid misleading | |
35 | * anyone as to the status of such modified files, you must delete | |
36 | * this exception notice from them. | |
37 | * | |
38 | * If you write modifications of your own for GUILE, it is your choice | |
39 | * whether to permit this exception to apply to your modifications. | |
40 | * If you do not wish that, delete this exception notice. */ | |
41 | ||
1bbd0b84 GB |
42 | /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999, |
43 | gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */ | |
44 | ||
45 | ||
a0599745 MD |
46 | #include "libguile/_scm.h" |
47 | #include "libguile/print.h" | |
48 | #include "libguile/smob.h" | |
49 | #include "libguile/dynwind.h" | |
50 | #include "libguile/fluids.h" | |
51 | #include "libguile/alist.h" | |
52 | #include "libguile/eval.h" | |
53 | #include "libguile/ports.h" | |
9482a297 MV |
54 | |
55 | #define INITIAL_FLUIDS 10 | |
a0599745 | 56 | #include "libguile/validate.h" |
9482a297 MV |
57 | |
58 | static volatile int n_fluids; | |
e841c3e0 | 59 | scm_bits_t scm_tc16_fluid; |
9482a297 MV |
60 | |
61 | SCM | |
62 | scm_make_initial_fluids () | |
63 | { | |
00ffa0e7 | 64 | return scm_c_make_vector (INITIAL_FLUIDS, SCM_BOOL_F); |
9482a297 MV |
65 | } |
66 | ||
9482a297 | 67 | static void |
ed4d7cee | 68 | grow_fluids (scm_root_state *root_state, int new_length) |
9482a297 MV |
69 | { |
70 | SCM old_fluids, new_fluids; | |
71 | int old_length, i; | |
72 | ||
73 | old_fluids = root_state->fluids; | |
bfa974f0 | 74 | old_length = SCM_VECTOR_LENGTH (old_fluids); |
00ffa0e7 | 75 | new_fluids = scm_c_make_vector (new_length, SCM_BOOL_F); |
9482a297 MV |
76 | i = 0; |
77 | while (i < old_length) | |
78 | { | |
79 | SCM_VELTS(new_fluids)[i] = SCM_VELTS(old_fluids)[i]; | |
80 | i++; | |
81 | } | |
82 | while (i < new_length) | |
83 | { | |
84 | SCM_VELTS(new_fluids)[i] = SCM_BOOL_F; | |
85 | i++; | |
86 | } | |
87 | ||
88 | root_state->fluids = new_fluids; | |
89 | } | |
90 | ||
91 | void | |
6e8d25a6 | 92 | scm_copy_fluids (scm_root_state *root_state) |
9482a297 | 93 | { |
bfa974f0 | 94 | grow_fluids (root_state, SCM_VECTOR_LENGTH (root_state->fluids)); |
9482a297 MV |
95 | } |
96 | ||
9482a297 | 97 | static int |
e841c3e0 | 98 | fluid_print (SCM exp, SCM port, scm_print_state *pstate) |
9482a297 | 99 | { |
ed4d7cee GB |
100 | scm_puts ("#<fluid ", port); |
101 | scm_intprint ((int) SCM_FLUID_NUM (exp), 10, port); | |
102 | scm_putc ('>', port); | |
103 | return 1; | |
9482a297 MV |
104 | } |
105 | ||
ed4d7cee GB |
106 | static int |
107 | next_fluid_num () | |
9482a297 MV |
108 | { |
109 | int n; | |
216eedfc | 110 | SCM_CRITICAL_SECTION_START; |
9482a297 | 111 | n = n_fluids++; |
216eedfc | 112 | SCM_CRITICAL_SECTION_END; |
9482a297 MV |
113 | return n; |
114 | } | |
115 | ||
a1ec6916 | 116 | SCM_DEFINE (scm_make_fluid, "make-fluid", 0, 0, 0, |
ed4d7cee GB |
117 | (), |
118 | "Return a newly created fluid.\n" | |
119 | "Fluids are objects of a certain type (a smob) that can hold one SCM\n" | |
120 | "value per dynamic root. That is, modifications to this value are\n" | |
121 | "only visible to code that executes within the same dynamic root as\n" | |
122 | "the modifying code. When a new dynamic root is constructed, it\n" | |
123 | "inherits the values from its parent. Because each thread executes\n" | |
124 | "in its own dynamic root, you can use fluids for thread local storage.") | |
1bbd0b84 | 125 | #define FUNC_NAME s_scm_make_fluid |
9482a297 | 126 | { |
9482a297 MV |
127 | int n; |
128 | ||
9482a297 | 129 | n = next_fluid_num (); |
23a62151 | 130 | SCM_RETURN_NEWSMOB (scm_tc16_fluid, n); |
9482a297 | 131 | } |
1bbd0b84 | 132 | #undef FUNC_NAME |
9482a297 | 133 | |
a1ec6916 | 134 | SCM_DEFINE (scm_fluid_p, "fluid?", 1, 0, 0, |
ed4d7cee | 135 | (SCM obj), |
1e6808ea MG |
136 | "Return @code{#t} iff @var{obj} is a fluid; otherwise, return\n" |
137 | "@code{#f}.") | |
1bbd0b84 | 138 | #define FUNC_NAME s_scm_fluid_p |
b3460a50 | 139 | { |
ed4d7cee | 140 | return SCM_BOOL(SCM_FLUIDP (obj)); |
b3460a50 | 141 | } |
1bbd0b84 | 142 | #undef FUNC_NAME |
b3460a50 | 143 | |
a1ec6916 | 144 | SCM_DEFINE (scm_fluid_ref, "fluid-ref", 1, 0, 0, |
ed4d7cee | 145 | (SCM fluid), |
1e6808ea MG |
146 | "Return the value associated with @var{fluid} in the current\n" |
147 | "dynamic root. If @var{fluid} has not been set, then return\n" | |
148 | "@code{#f}.") | |
1bbd0b84 | 149 | #define FUNC_NAME s_scm_fluid_ref |
9482a297 MV |
150 | { |
151 | int n; | |
152 | ||
ed4d7cee | 153 | SCM_VALIDATE_FLUID (1, fluid); |
9482a297 | 154 | |
ed4d7cee | 155 | n = SCM_FLUID_NUM (fluid); |
9482a297 | 156 | |
bfa974f0 | 157 | if (SCM_VECTOR_LENGTH (scm_root->fluids) <= n) |
9482a297 | 158 | grow_fluids (scm_root, n+1); |
7e73eaee | 159 | return SCM_VELTS (scm_root->fluids)[n]; |
9482a297 | 160 | } |
1bbd0b84 | 161 | #undef FUNC_NAME |
9482a297 | 162 | |
a1ec6916 | 163 | SCM_DEFINE (scm_fluid_set_x, "fluid-set!", 2, 0, 0, |
ed4d7cee GB |
164 | (SCM fluid, SCM value), |
165 | "Set the value associated with @var{fluid} in the current dynamic root.") | |
1bbd0b84 | 166 | #define FUNC_NAME s_scm_fluid_set_x |
9482a297 MV |
167 | { |
168 | int n; | |
169 | ||
ed4d7cee GB |
170 | SCM_VALIDATE_FLUID (1, fluid); |
171 | n = SCM_FLUID_NUM (fluid); | |
9482a297 | 172 | |
bfa974f0 | 173 | if (SCM_VECTOR_LENGTH (scm_root->fluids) <= n) |
9482a297 | 174 | grow_fluids (scm_root, n+1); |
86f9f9ae MD |
175 | SCM_VELTS (scm_root->fluids)[n] = value; |
176 | return SCM_UNSPECIFIED; | |
9482a297 | 177 | } |
1bbd0b84 | 178 | #undef FUNC_NAME |
9482a297 | 179 | |
b3460a50 | 180 | void |
1bbd0b84 | 181 | scm_swap_fluids (SCM fluids, SCM vals) |
b3460a50 | 182 | { |
22a52da1 | 183 | while (!SCM_NULLP (fluids)) |
b3460a50 MV |
184 | { |
185 | SCM fl = SCM_CAR (fluids); | |
186 | SCM old_val = scm_fluid_ref (fl); | |
187 | scm_fluid_set_x (fl, SCM_CAR (vals)); | |
188 | SCM_SETCAR (vals, old_val); | |
189 | fluids = SCM_CDR (fluids); | |
190 | vals = SCM_CDR (vals); | |
191 | } | |
192 | } | |
193 | ||
194 | /* Swap the fluid values in reverse order. This is important when the | |
195 | same fluid appears multiple times in the fluids list. */ | |
196 | ||
197 | void | |
1bbd0b84 | 198 | scm_swap_fluids_reverse (SCM fluids, SCM vals) |
b3460a50 | 199 | { |
22a52da1 | 200 | if (!SCM_NULLP (fluids)) |
b3460a50 MV |
201 | { |
202 | SCM fl, old_val; | |
203 | ||
204 | scm_swap_fluids_reverse (SCM_CDR (fluids), SCM_CDR (vals)); | |
205 | fl = SCM_CAR (fluids); | |
206 | old_val = scm_fluid_ref (fl); | |
207 | scm_fluid_set_x (fl, SCM_CAR (vals)); | |
208 | SCM_SETCAR (vals, old_val); | |
209 | } | |
210 | } | |
211 | ||
1bbd0b84 GB |
212 | |
213 | static SCM | |
214 | apply_thunk (void *thunk) | |
215 | { | |
54778cd3 | 216 | return scm_apply (SCM_PACK (thunk), SCM_EOL, SCM_EOL); |
1bbd0b84 GB |
217 | } |
218 | ||
a1ec6916 | 219 | SCM_DEFINE (scm_with_fluids, "with-fluids*", 3, 0, 0, |
ed4d7cee GB |
220 | (SCM fluids, SCM values, SCM thunk), |
221 | "Set @var{fluids} to @var{values} temporary, and call @var{thunk}.\n" | |
222 | "@var{fluids} must be a list of fluids and @var{values} must be the same\n" | |
223 | "number of their values to be applied. Each substitution is done\n" | |
224 | "one after another. @var{thunk} must be a procedure with no argument.") | |
1bbd0b84 GB |
225 | #define FUNC_NAME s_scm_with_fluids |
226 | { | |
54778cd3 | 227 | return scm_internal_with_fluids (fluids, values, apply_thunk, (void *) SCM_UNPACK (thunk)); |
1bbd0b84 GB |
228 | } |
229 | #undef FUNC_NAME | |
b3460a50 MV |
230 | |
231 | SCM | |
ed4d7cee | 232 | scm_internal_with_fluids (SCM fluids, SCM values, SCM (*cproc) (), void *cdata) |
c1bfcf60 | 233 | #define FUNC_NAME "scm_internal_with_fluids" |
b3460a50 MV |
234 | { |
235 | SCM ans; | |
c1bfcf60 | 236 | int flen, vlen; |
b3460a50 | 237 | |
c1bfcf60 | 238 | SCM_VALIDATE_LIST_COPYLEN (1, fluids, flen); |
ed4d7cee | 239 | SCM_VALIDATE_LIST_COPYLEN (2, values, vlen); |
b3460a50 | 240 | if (flen != vlen) |
ed4d7cee | 241 | scm_out_of_range (s_scm_with_fluids, values); |
b3460a50 | 242 | |
ed4d7cee GB |
243 | scm_swap_fluids (fluids, values); |
244 | scm_dynwinds = scm_acons (fluids, values, scm_dynwinds); | |
b3460a50 MV |
245 | ans = cproc (cdata); |
246 | scm_dynwinds = SCM_CDR (scm_dynwinds); | |
ed4d7cee | 247 | scm_swap_fluids_reverse (fluids, values); |
b3460a50 MV |
248 | return ans; |
249 | } | |
c1bfcf60 | 250 | #undef FUNC_NAME |
b3460a50 | 251 | |
b3460a50 | 252 | |
b3460a50 | 253 | |
9482a297 MV |
254 | void |
255 | scm_init_fluids () | |
256 | { | |
e841c3e0 KN |
257 | scm_tc16_fluid = scm_make_smob_type ("fluid", 0); |
258 | scm_set_smob_print (scm_tc16_fluid, fluid_print); | |
8dc9439f | 259 | #ifndef SCM_MAGIC_SNARFER |
a0599745 | 260 | #include "libguile/fluids.x" |
8dc9439f | 261 | #endif |
9482a297 | 262 | } |
89e00824 ML |
263 | |
264 | /* | |
265 | Local Variables: | |
266 | c-file-style: "gnu" | |
267 | End: | |
268 | */ |