*** empty log message ***
[bpt/guile.git] / libguile / fluids.c
1 /* Copyright (C) 1996, 1997, 2000 Free Software Foundation, Inc.
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
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
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"
54
55 #define INITIAL_FLUIDS 10
56 #include "libguile/validate.h"
57
58 static volatile int n_fluids;
59 scm_bits_t scm_tc16_fluid;
60
61 SCM
62 scm_make_initial_fluids ()
63 {
64 return scm_c_make_vector (INITIAL_FLUIDS, SCM_BOOL_F);
65 }
66
67 static void
68 grow_fluids (scm_root_state *root_state, int new_length)
69 {
70 SCM old_fluids, new_fluids;
71 int old_length, i;
72
73 old_fluids = root_state->fluids;
74 old_length = SCM_VECTOR_LENGTH (old_fluids);
75 new_fluids = scm_c_make_vector (new_length, SCM_BOOL_F);
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
92 scm_copy_fluids (scm_root_state *root_state)
93 {
94 grow_fluids (root_state, SCM_VECTOR_LENGTH (root_state->fluids));
95 }
96
97 static int
98 fluid_print (SCM exp, SCM port, scm_print_state *pstate)
99 {
100 scm_puts ("#<fluid ", port);
101 scm_intprint ((int) SCM_FLUID_NUM (exp), 10, port);
102 scm_putc ('>', port);
103 return 1;
104 }
105
106 static int
107 next_fluid_num ()
108 {
109 int n;
110 #ifdef USE_THREADS
111 SCM_THREAD_CRITICAL_SECTION_START;
112 #endif
113 n = n_fluids++;
114 #ifdef USE_THREADS
115 SCM_THREAD_CRITICAL_SECTION_END;
116 #endif
117 return n;
118 }
119
120 SCM_DEFINE (scm_make_fluid, "make-fluid", 0, 0, 0,
121 (),
122 "Return a newly created fluid.\n"
123 "Fluids are objects of a certain type (a smob) that can hold one SCM\n"
124 "value per dynamic root. That is, modifications to this value are\n"
125 "only visible to code that executes within the same dynamic root as\n"
126 "the modifying code. When a new dynamic root is constructed, it\n"
127 "inherits the values from its parent. Because each thread executes\n"
128 "in its own dynamic root, you can use fluids for thread local storage.")
129 #define FUNC_NAME s_scm_make_fluid
130 {
131 int n;
132
133 SCM_DEFER_INTS;
134 n = next_fluid_num ();
135 SCM_RETURN_NEWSMOB (scm_tc16_fluid, n);
136 }
137 #undef FUNC_NAME
138
139 SCM_DEFINE (scm_fluid_p, "fluid?", 1, 0, 0,
140 (SCM obj),
141 "Return #t iff @var{obj} is a fluid; otherwise, return #f.")
142 #define FUNC_NAME s_scm_fluid_p
143 {
144 return SCM_BOOL(SCM_FLUIDP (obj));
145 }
146 #undef FUNC_NAME
147
148 SCM_DEFINE (scm_fluid_ref, "fluid-ref", 1, 0, 0,
149 (SCM fluid),
150 "Return the value associated with @var{fluid} in the current dynamic root.\n"
151 "If @var{fluid} has not been set, then this returns #f.")
152 #define FUNC_NAME s_scm_fluid_ref
153 {
154 int n;
155
156 SCM_VALIDATE_FLUID (1, fluid);
157
158 n = SCM_FLUID_NUM (fluid);
159
160 if (SCM_VECTOR_LENGTH (scm_root->fluids) <= n)
161 grow_fluids (scm_root, n+1);
162 return SCM_VELTS (scm_root->fluids)[n];
163 }
164 #undef FUNC_NAME
165
166 SCM_DEFINE (scm_fluid_set_x, "fluid-set!", 2, 0, 0,
167 (SCM fluid, SCM value),
168 "Set the value associated with @var{fluid} in the current dynamic root.")
169 #define FUNC_NAME s_scm_fluid_set_x
170 {
171 int n;
172
173 SCM_VALIDATE_FLUID (1, fluid);
174 n = SCM_FLUID_NUM (fluid);
175
176 if (SCM_VECTOR_LENGTH (scm_root->fluids) <= n)
177 grow_fluids (scm_root, n+1);
178 SCM_VELTS (scm_root->fluids)[n] = value;
179 return SCM_UNSPECIFIED;
180 }
181 #undef FUNC_NAME
182
183 void
184 scm_swap_fluids (SCM fluids, SCM vals)
185 {
186 while (SCM_NIMP (fluids))
187 {
188 SCM fl = SCM_CAR (fluids);
189 SCM old_val = scm_fluid_ref (fl);
190 scm_fluid_set_x (fl, SCM_CAR (vals));
191 SCM_SETCAR (vals, old_val);
192 fluids = SCM_CDR (fluids);
193 vals = SCM_CDR (vals);
194 }
195 }
196
197 /* Swap the fluid values in reverse order. This is important when the
198 same fluid appears multiple times in the fluids list. */
199
200 void
201 scm_swap_fluids_reverse (SCM fluids, SCM vals)
202 {
203 if (SCM_NIMP (fluids))
204 {
205 SCM fl, old_val;
206
207 scm_swap_fluids_reverse (SCM_CDR (fluids), SCM_CDR (vals));
208 fl = SCM_CAR (fluids);
209 old_val = scm_fluid_ref (fl);
210 scm_fluid_set_x (fl, SCM_CAR (vals));
211 SCM_SETCAR (vals, old_val);
212 }
213 }
214
215
216 static SCM
217 apply_thunk (void *thunk)
218 {
219 return scm_apply (SCM_PACK (thunk), SCM_EOL, SCM_EOL);
220 }
221
222 SCM_DEFINE (scm_with_fluids, "with-fluids*", 3, 0, 0,
223 (SCM fluids, SCM values, SCM thunk),
224 "Set @var{fluids} to @var{values} temporary, and call @var{thunk}.\n"
225 "@var{fluids} must be a list of fluids and @var{values} must be the same\n"
226 "number of their values to be applied. Each substitution is done\n"
227 "one after another. @var{thunk} must be a procedure with no argument.")
228 #define FUNC_NAME s_scm_with_fluids
229 {
230 return scm_internal_with_fluids (fluids, values, apply_thunk, (void *) SCM_UNPACK (thunk));
231 }
232 #undef FUNC_NAME
233
234 SCM
235 scm_internal_with_fluids (SCM fluids, SCM values, SCM (*cproc) (), void *cdata)
236 #define FUNC_NAME "scm_internal_with_fluids"
237 {
238 SCM ans;
239 int flen, vlen;
240
241 SCM_VALIDATE_LIST_COPYLEN (1, fluids, flen);
242 SCM_VALIDATE_LIST_COPYLEN (2, values, vlen);
243 if (flen != vlen)
244 scm_out_of_range (s_scm_with_fluids, values);
245
246 scm_swap_fluids (fluids, values);
247 scm_dynwinds = scm_acons (fluids, values, scm_dynwinds);
248 ans = cproc (cdata);
249 scm_dynwinds = SCM_CDR (scm_dynwinds);
250 scm_swap_fluids_reverse (fluids, values);
251 return ans;
252 }
253 #undef FUNC_NAME
254
255
256
257 void
258 scm_init_fluids ()
259 {
260 scm_tc16_fluid = scm_make_smob_type ("fluid", 0);
261 scm_set_smob_print (scm_tc16_fluid, fluid_print);
262 #ifndef SCM_MAGIC_SNARFER
263 #include "libguile/fluids.x"
264 #endif
265 }
266
267 /*
268 Local Variables:
269 c-file-style: "gnu"
270 End:
271 */