1 /* Copyright (C) 1996,1997,2000,2001 Free Software Foundation, Inc.
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)
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.
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
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
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.
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
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.
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. */
42 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
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 #include "libguile/deprecation.h"
56 #define INITIAL_FLUIDS 10
57 #include "libguile/validate.h"
59 static volatile long n_fluids
;
60 scm_bits_t scm_tc16_fluid
;
63 scm_make_initial_fluids ()
65 return scm_c_make_vector (INITIAL_FLUIDS
, SCM_BOOL_F
);
69 grow_fluids (scm_root_state
*root_state
, int new_length
)
71 SCM old_fluids
, new_fluids
;
74 old_fluids
= root_state
->fluids
;
75 old_length
= SCM_VECTOR_LENGTH (old_fluids
);
76 new_fluids
= scm_c_make_vector (new_length
, SCM_BOOL_F
);
78 while (i
< old_length
)
80 SCM_VELTS(new_fluids
)[i
] = SCM_VELTS(old_fluids
)[i
];
83 while (i
< new_length
)
85 SCM_VELTS(new_fluids
)[i
] = SCM_BOOL_F
;
89 root_state
->fluids
= new_fluids
;
93 scm_copy_fluids (scm_root_state
*root_state
)
95 grow_fluids (root_state
, SCM_VECTOR_LENGTH (root_state
->fluids
));
99 fluid_print (SCM exp
, SCM port
, scm_print_state
*pstate SCM_UNUSED
)
101 scm_puts ("#<fluid ", port
);
102 scm_intprint ((int) SCM_FLUID_NUM (exp
), 10, port
);
103 scm_putc ('>', port
);
111 SCM_CRITICAL_SECTION_START
;
113 SCM_CRITICAL_SECTION_END
;
117 SCM_DEFINE (scm_make_fluid
, "make-fluid", 0, 0, 0,
119 "Return a newly created fluid.\n"
120 "Fluids are objects of a certain type (a smob) that can hold one SCM\n"
121 "value per dynamic root. That is, modifications to this value are\n"
122 "only visible to code that executes within the same dynamic root as\n"
123 "the modifying code. When a new dynamic root is constructed, it\n"
124 "inherits the values from its parent. Because each thread executes\n"
125 "in its own dynamic root, you can use fluids for thread local storage.")
126 #define FUNC_NAME s_scm_make_fluid
130 n
= next_fluid_num ();
131 SCM_RETURN_NEWSMOB (scm_tc16_fluid
, n
);
135 SCM_DEFINE (scm_fluid_p
, "fluid?", 1, 0, 0,
137 "Return @code{#t} iff @var{obj} is a fluid; otherwise, return\n"
139 #define FUNC_NAME s_scm_fluid_p
141 return SCM_BOOL(SCM_FLUIDP (obj
));
145 SCM_DEFINE (scm_fluid_ref
, "fluid-ref", 1, 0, 0,
147 "Return the value associated with @var{fluid} in the current\n"
148 "dynamic root. If @var{fluid} has not been set, then return\n"
150 #define FUNC_NAME s_scm_fluid_ref
154 SCM_VALIDATE_FLUID (1, fluid
);
156 n
= SCM_FLUID_NUM (fluid
);
158 if (SCM_VECTOR_LENGTH (scm_root
->fluids
) <= n
)
159 grow_fluids (scm_root
, n
+1);
160 return SCM_VELTS (scm_root
->fluids
)[n
];
164 SCM_DEFINE (scm_fluid_set_x
, "fluid-set!", 2, 0, 0,
165 (SCM fluid
, SCM value
),
166 "Set the value associated with @var{fluid} in the current dynamic root.")
167 #define FUNC_NAME s_scm_fluid_set_x
171 SCM_VALIDATE_FLUID (1, fluid
);
172 n
= SCM_FLUID_NUM (fluid
);
174 if (SCM_VECTOR_LENGTH (scm_root
->fluids
) <= n
)
175 grow_fluids (scm_root
, n
+1);
176 SCM_VELTS (scm_root
->fluids
)[n
] = value
;
177 return SCM_UNSPECIFIED
;
182 scm_swap_fluids (SCM fluids
, SCM vals
)
184 while (!SCM_NULLP (fluids
))
186 SCM fl
= SCM_CAR (fluids
);
187 SCM old_val
= scm_fluid_ref (fl
);
188 scm_fluid_set_x (fl
, SCM_CAR (vals
));
189 SCM_SETCAR (vals
, old_val
);
190 fluids
= SCM_CDR (fluids
);
191 vals
= SCM_CDR (vals
);
195 /* Swap the fluid values in reverse order. This is important when the
196 same fluid appears multiple times in the fluids list. */
199 scm_swap_fluids_reverse (SCM fluids
, SCM vals
)
201 if (!SCM_NULLP (fluids
))
205 scm_swap_fluids_reverse (SCM_CDR (fluids
), SCM_CDR (vals
));
206 fl
= SCM_CAR (fluids
);
207 old_val
= scm_fluid_ref (fl
);
208 scm_fluid_set_x (fl
, SCM_CAR (vals
));
209 SCM_SETCAR (vals
, old_val
);
215 apply_thunk (void *thunk
)
217 return scm_apply (SCM_PACK (thunk
), SCM_EOL
, SCM_EOL
);
220 SCM_DEFINE (scm_with_fluids
, "with-fluids*", 3, 0, 0,
221 (SCM fluids
, SCM values
, SCM thunk
),
222 "Set @var{fluids} to @var{values} temporary, and call @var{thunk}.\n"
223 "@var{fluids} must be a list of fluids and @var{values} must be the same\n"
224 "number of their values to be applied. Each substitution is done\n"
225 "one after another. @var{thunk} must be a procedure with no argument.")
226 #define FUNC_NAME s_scm_with_fluids
228 return scm_c_with_fluids (fluids
, values
, apply_thunk
, (void *) SCM_UNPACK (thunk
));
233 scm_c_with_fluids (SCM fluids
, SCM values
, SCM (*cproc
) (), void *cdata
)
234 #define FUNC_NAME "scm_c_with_fluids"
239 SCM_VALIDATE_LIST_COPYLEN (1, fluids
, flen
);
240 SCM_VALIDATE_LIST_COPYLEN (2, values
, vlen
);
242 scm_out_of_range (s_scm_with_fluids
, values
);
244 scm_swap_fluids (fluids
, values
);
245 scm_dynwinds
= scm_acons (fluids
, values
, scm_dynwinds
);
247 scm_dynwinds
= SCM_CDR (scm_dynwinds
);
248 scm_swap_fluids_reverse (fluids
, values
);
254 scm_c_with_fluid (SCM fluid
, SCM value
, SCM (*cproc
) (), void *cdata
)
255 #define FUNC_NAME "scm_c_with_fluid"
257 return scm_c_with_fluids (SCM_LIST1 (fluid
), SCM_LIST1 (value
),
265 scm_tc16_fluid
= scm_make_smob_type ("fluid", 0);
266 scm_set_smob_print (scm_tc16_fluid
, fluid_print
);
267 #ifndef SCM_MAGIC_SNARFER
268 #include "libguile/fluids.x"
272 #if SCM_DEBUG_DEPRECATED == 0
275 scm_internal_with_fluids (SCM fluids
, SCM values
, SCM (*cproc
) (), void *cdata
)
277 scm_c_issue_deprecation_warning ("`scm_internal_with_fluids' is deprecated. "
278 "Use `scm_c_with_fluids' instead.");
280 return scm_c_with_fluids (fluids
, values
, cproc
, cdata
);