Commit | Line | Data |
---|---|---|
22a52da1 | 1 | /* Copyright (C) 1996,1997,2000,2001 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 | |
15 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
16 | */ | |
9482a297 | 17 | |
1bbd0b84 GB |
18 | |
19 | ||
a0599745 MD |
20 | #include "libguile/_scm.h" |
21 | #include "libguile/print.h" | |
22 | #include "libguile/smob.h" | |
23 | #include "libguile/dynwind.h" | |
24 | #include "libguile/fluids.h" | |
25 | #include "libguile/alist.h" | |
26 | #include "libguile/eval.h" | |
27 | #include "libguile/ports.h" | |
143e0902 | 28 | #include "libguile/deprecation.h" |
c96d76b8 | 29 | #include "libguile/lang.h" |
9482a297 MV |
30 | |
31 | #define INITIAL_FLUIDS 10 | |
a0599745 | 32 | #include "libguile/validate.h" |
9482a297 | 33 | |
c014a02e | 34 | static volatile long n_fluids; |
92c2555f | 35 | scm_t_bits scm_tc16_fluid; |
9482a297 MV |
36 | |
37 | SCM | |
a52dbe01 | 38 | scm_i_make_initial_fluids () |
9482a297 | 39 | { |
00ffa0e7 | 40 | return scm_c_make_vector (INITIAL_FLUIDS, SCM_BOOL_F); |
9482a297 MV |
41 | } |
42 | ||
9482a297 | 43 | static void |
ed4d7cee | 44 | grow_fluids (scm_root_state *root_state, int new_length) |
9482a297 MV |
45 | { |
46 | SCM old_fluids, new_fluids; | |
c014a02e | 47 | long old_length, i; |
9482a297 MV |
48 | |
49 | old_fluids = root_state->fluids; | |
bfa974f0 | 50 | old_length = SCM_VECTOR_LENGTH (old_fluids); |
00ffa0e7 | 51 | new_fluids = scm_c_make_vector (new_length, SCM_BOOL_F); |
9482a297 MV |
52 | i = 0; |
53 | while (i < old_length) | |
54 | { | |
34d19ef6 | 55 | SCM_VECTOR_SET (new_fluids, i, SCM_VELTS(old_fluids)[i]); |
9482a297 MV |
56 | i++; |
57 | } | |
58 | while (i < new_length) | |
59 | { | |
34d19ef6 | 60 | SCM_VECTOR_SET (new_fluids, i, SCM_BOOL_F); |
9482a297 MV |
61 | i++; |
62 | } | |
63 | ||
64 | root_state->fluids = new_fluids; | |
65 | } | |
66 | ||
67 | void | |
a52dbe01 | 68 | scm_i_copy_fluids (scm_root_state *root_state) |
9482a297 | 69 | { |
bfa974f0 | 70 | grow_fluids (root_state, SCM_VECTOR_LENGTH (root_state->fluids)); |
9482a297 MV |
71 | } |
72 | ||
9482a297 | 73 | static int |
e81d98ec | 74 | fluid_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED) |
9482a297 | 75 | { |
ed4d7cee GB |
76 | scm_puts ("#<fluid ", port); |
77 | scm_intprint ((int) SCM_FLUID_NUM (exp), 10, port); | |
78 | scm_putc ('>', port); | |
79 | return 1; | |
9482a297 MV |
80 | } |
81 | ||
c014a02e | 82 | static long |
ed4d7cee | 83 | next_fluid_num () |
9482a297 | 84 | { |
c014a02e | 85 | long n; |
216eedfc | 86 | SCM_CRITICAL_SECTION_START; |
9482a297 | 87 | n = n_fluids++; |
216eedfc | 88 | SCM_CRITICAL_SECTION_END; |
9482a297 MV |
89 | return n; |
90 | } | |
91 | ||
a1ec6916 | 92 | SCM_DEFINE (scm_make_fluid, "make-fluid", 0, 0, 0, |
ed4d7cee GB |
93 | (), |
94 | "Return a newly created fluid.\n" | |
95 | "Fluids are objects of a certain type (a smob) that can hold one SCM\n" | |
96 | "value per dynamic root. That is, modifications to this value are\n" | |
97 | "only visible to code that executes within the same dynamic root as\n" | |
98 | "the modifying code. When a new dynamic root is constructed, it\n" | |
99 | "inherits the values from its parent. Because each thread executes\n" | |
100 | "in its own dynamic root, you can use fluids for thread local storage.") | |
1bbd0b84 | 101 | #define FUNC_NAME s_scm_make_fluid |
9482a297 | 102 | { |
c014a02e | 103 | long n; |
9482a297 | 104 | |
9482a297 | 105 | n = next_fluid_num (); |
23a62151 | 106 | SCM_RETURN_NEWSMOB (scm_tc16_fluid, n); |
9482a297 | 107 | } |
1bbd0b84 | 108 | #undef FUNC_NAME |
9482a297 | 109 | |
a1ec6916 | 110 | SCM_DEFINE (scm_fluid_p, "fluid?", 1, 0, 0, |
ed4d7cee | 111 | (SCM obj), |
1e6808ea MG |
112 | "Return @code{#t} iff @var{obj} is a fluid; otherwise, return\n" |
113 | "@code{#f}.") | |
1bbd0b84 | 114 | #define FUNC_NAME s_scm_fluid_p |
b3460a50 | 115 | { |
ed4d7cee | 116 | return SCM_BOOL(SCM_FLUIDP (obj)); |
b3460a50 | 117 | } |
1bbd0b84 | 118 | #undef FUNC_NAME |
b3460a50 | 119 | |
a1ec6916 | 120 | SCM_DEFINE (scm_fluid_ref, "fluid-ref", 1, 0, 0, |
ed4d7cee | 121 | (SCM fluid), |
1e6808ea MG |
122 | "Return the value associated with @var{fluid} in the current\n" |
123 | "dynamic root. If @var{fluid} has not been set, then return\n" | |
124 | "@code{#f}.") | |
1bbd0b84 | 125 | #define FUNC_NAME s_scm_fluid_ref |
9482a297 | 126 | { |
5843e5c9 | 127 | unsigned long int n; |
9482a297 | 128 | |
ed4d7cee | 129 | SCM_VALIDATE_FLUID (1, fluid); |
ed4d7cee | 130 | n = SCM_FLUID_NUM (fluid); |
9482a297 | 131 | |
bfa974f0 | 132 | if (SCM_VECTOR_LENGTH (scm_root->fluids) <= n) |
9482a297 | 133 | grow_fluids (scm_root, n+1); |
7e73eaee | 134 | return SCM_VELTS (scm_root->fluids)[n]; |
9482a297 | 135 | } |
1bbd0b84 | 136 | #undef FUNC_NAME |
9482a297 | 137 | |
a1ec6916 | 138 | SCM_DEFINE (scm_fluid_set_x, "fluid-set!", 2, 0, 0, |
ed4d7cee GB |
139 | (SCM fluid, SCM value), |
140 | "Set the value associated with @var{fluid} in the current dynamic root.") | |
1bbd0b84 | 141 | #define FUNC_NAME s_scm_fluid_set_x |
9482a297 | 142 | { |
5843e5c9 | 143 | unsigned long int n; |
9482a297 | 144 | |
ed4d7cee GB |
145 | SCM_VALIDATE_FLUID (1, fluid); |
146 | n = SCM_FLUID_NUM (fluid); | |
9482a297 | 147 | |
bfa974f0 | 148 | if (SCM_VECTOR_LENGTH (scm_root->fluids) <= n) |
9482a297 | 149 | grow_fluids (scm_root, n+1); |
34d19ef6 | 150 | SCM_VECTOR_SET (scm_root->fluids, n, value); |
86f9f9ae | 151 | return SCM_UNSPECIFIED; |
9482a297 | 152 | } |
1bbd0b84 | 153 | #undef FUNC_NAME |
9482a297 | 154 | |
b3460a50 | 155 | void |
a52dbe01 | 156 | scm_i_swap_fluids (SCM fluids, SCM vals) |
b3460a50 | 157 | { |
c96d76b8 | 158 | while (!SCM_NULL_OR_NIL_P (fluids)) |
b3460a50 MV |
159 | { |
160 | SCM fl = SCM_CAR (fluids); | |
161 | SCM old_val = scm_fluid_ref (fl); | |
162 | scm_fluid_set_x (fl, SCM_CAR (vals)); | |
163 | SCM_SETCAR (vals, old_val); | |
164 | fluids = SCM_CDR (fluids); | |
165 | vals = SCM_CDR (vals); | |
166 | } | |
167 | } | |
168 | ||
169 | /* Swap the fluid values in reverse order. This is important when the | |
170 | same fluid appears multiple times in the fluids list. */ | |
171 | ||
172 | void | |
a52dbe01 | 173 | scm_i_swap_fluids_reverse (SCM fluids, SCM vals) |
b3460a50 | 174 | { |
c96d76b8 | 175 | if (!SCM_NULL_OR_NIL_P (fluids)) |
b3460a50 MV |
176 | { |
177 | SCM fl, old_val; | |
178 | ||
a52dbe01 | 179 | scm_i_swap_fluids_reverse (SCM_CDR (fluids), SCM_CDR (vals)); |
b3460a50 MV |
180 | fl = SCM_CAR (fluids); |
181 | old_val = scm_fluid_ref (fl); | |
182 | scm_fluid_set_x (fl, SCM_CAR (vals)); | |
183 | SCM_SETCAR (vals, old_val); | |
184 | } | |
185 | } | |
186 | ||
1bbd0b84 GB |
187 | |
188 | static SCM | |
189 | apply_thunk (void *thunk) | |
190 | { | |
fdc28395 | 191 | return scm_call_0 (SCM_PACK (thunk)); |
1bbd0b84 GB |
192 | } |
193 | ||
a1ec6916 | 194 | SCM_DEFINE (scm_with_fluids, "with-fluids*", 3, 0, 0, |
ed4d7cee GB |
195 | (SCM fluids, SCM values, SCM thunk), |
196 | "Set @var{fluids} to @var{values} temporary, and call @var{thunk}.\n" | |
197 | "@var{fluids} must be a list of fluids and @var{values} must be the same\n" | |
198 | "number of their values to be applied. Each substitution is done\n" | |
199 | "one after another. @var{thunk} must be a procedure with no argument.") | |
1bbd0b84 GB |
200 | #define FUNC_NAME s_scm_with_fluids |
201 | { | |
143e0902 | 202 | return scm_c_with_fluids (fluids, values, apply_thunk, (void *) SCM_UNPACK (thunk)); |
1bbd0b84 GB |
203 | } |
204 | #undef FUNC_NAME | |
b3460a50 MV |
205 | |
206 | SCM | |
143e0902 MV |
207 | scm_c_with_fluids (SCM fluids, SCM values, SCM (*cproc) (), void *cdata) |
208 | #define FUNC_NAME "scm_c_with_fluids" | |
b3460a50 MV |
209 | { |
210 | SCM ans; | |
c014a02e | 211 | long flen, vlen; |
b3460a50 | 212 | |
c1bfcf60 | 213 | SCM_VALIDATE_LIST_COPYLEN (1, fluids, flen); |
ed4d7cee | 214 | SCM_VALIDATE_LIST_COPYLEN (2, values, vlen); |
b3460a50 | 215 | if (flen != vlen) |
ed4d7cee | 216 | scm_out_of_range (s_scm_with_fluids, values); |
b3460a50 | 217 | |
a52dbe01 | 218 | scm_i_swap_fluids (fluids, values); |
ed4d7cee | 219 | scm_dynwinds = scm_acons (fluids, values, scm_dynwinds); |
b3460a50 MV |
220 | ans = cproc (cdata); |
221 | scm_dynwinds = SCM_CDR (scm_dynwinds); | |
a52dbe01 | 222 | scm_i_swap_fluids_reverse (fluids, values); |
b3460a50 MV |
223 | return ans; |
224 | } | |
c1bfcf60 | 225 | #undef FUNC_NAME |
b3460a50 | 226 | |
143e0902 MV |
227 | SCM |
228 | scm_c_with_fluid (SCM fluid, SCM value, SCM (*cproc) (), void *cdata) | |
229 | #define FUNC_NAME "scm_c_with_fluid" | |
230 | { | |
df1ad0d1 | 231 | return scm_c_with_fluids (scm_list_1 (fluid), scm_list_1 (value), |
143e0902 MV |
232 | cproc, cdata); |
233 | } | |
234 | #undef FUNC_NAME | |
b3460a50 | 235 | |
ef20bf70 MV |
236 | static void |
237 | swap_fluid (SCM data) | |
238 | { | |
239 | SCM f = SCM_CAR (data); | |
240 | SCM t = scm_fluid_ref (f); | |
241 | scm_fluid_set_x (f, SCM_CDR (data)); | |
242 | SCM_SETCDR (data, t); | |
243 | } | |
244 | ||
245 | void | |
246 | scm_frame_fluid (SCM fluid, SCM value) | |
247 | { | |
248 | SCM data = scm_cons (fluid, value); | |
249 | scm_frame_rewind_with_scm (swap_fluid, data, SCM_F_WIND_EXPLICITLY); | |
250 | scm_frame_unwind_with_scm (swap_fluid, data, SCM_F_WIND_EXPLICITLY); | |
251 | } | |
252 | ||
9482a297 MV |
253 | void |
254 | scm_init_fluids () | |
255 | { | |
e841c3e0 KN |
256 | scm_tc16_fluid = scm_make_smob_type ("fluid", 0); |
257 | scm_set_smob_print (scm_tc16_fluid, fluid_print); | |
a0599745 | 258 | #include "libguile/fluids.x" |
9482a297 | 259 | } |
89e00824 ML |
260 | |
261 | /* | |
262 | Local Variables: | |
263 | c-file-style: "gnu" | |
264 | End: | |
265 | */ |