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