Debian Codegraph @

src/parameter.c

raw
1 /*
2 * parameter.c - parameter support
3 *
4 * Copyright (c) 2000-2024 Shiro Kawai <[email protected]>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the authors nor the names of its contributors
18 * may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #define LIBGAUCHE_BODY
35 #include "gauche.h"
36 #include "gauche/priv/configP.h"
37 #include "gauche/vm.h"
38 #include "gauche/priv/vmP.h"
39 #include "gauche/priv/atomicP.h"
40 #include "gauche/priv/builtin-syms.h"
41 #include "gauche/priv/parameterP.h"
42
43 /*
44 * Note: We're moving towards splitting thread local storage and parameters.
45 * The following is a description of old model. Eventually parameters won't
46 * be thread-specific.
47 *
48 *
49 * Parameters keep thread-local states. When a thread is created,
50 * it inherits the set of parameters from its creator (except the
51 * primordial thread).
52 * Parameters have additional features, such as guard procedure
53 * and observer callbacks. They are implemented in Scheme level
54 * (see lib/gauche/parameter.scm). C level only provides low-level
55 * accessor and modifier methods.
56 *
57 * It is debatable how to implement the inheritance semantics. MzScheme
58 * keeps user-defined parameters in a hash table, and uses
59 * copy-on-write mechanism to delay the copy of the table. It is nice,
60 * but difficult to use in preemptive threads, for it requires lock of
61 * the table every time even in reading parameters. Guile uses the
62 * vector (Guile calls them fluids, but it's semantically equivalent
63 * to parameters), and eagerly copies the vector at the creation of the
64 * thread. Since thread creation in Gauche is already heavy anyway,
65 * I take Guile's approach.
66 */
67
68 /* :name, :initial-value, and :type. These keywords are set on-demand,
69 since at the time of Scm_InitParameter we haven't initialized
70 symbol subsystem yet. */
71 static ScmObj key_name = SCM_FALSE;
72 static ScmObj key_initial_value = SCM_FALSE;
73 static ScmObj key_type = SCM_FALSE;
74
75 /* #:parameter, to mark parameter procedure.
76 Initialized in Scm__InitParameter
77 A parameter needs to be a procedure (R7RS); in Gauche, it is a SUBR
78 holding a parameter object as data. It is recognized as a parameter
79 by having (#:parameter <name>) in the procedure info of the subr.
80 */
81 static ScmObj sym_parameter = SCM_FALSE;
82
83 /* Class stuff */
84 static void pparam_print(ScmObj obj, ScmPort *out, ScmWriteContext *ctx);
85 static ScmObj pparam_allocate(ScmClass *klass, ScmObj initargs SCM_UNUSED);
86
87 SCM_DEFINE_BASE_CLASS(Scm_PrimitiveParameterClass, ScmPrimitiveParameter,
88 pparam_print, NULL, NULL, pparam_allocate,
89 SCM_CLASS_OBJECT_CPL);
90
91 SCM_DEFINE_BUILTIN_CLASS(Scm_ParameterizationClass,
92 NULL, NULL, NULL, NULL,
93 SCM_CLASS_OBJECT_CPL);
94
95 static void pparam_print(ScmObj obj,
96 ScmPort *out,
97 ScmWriteContext *ctx SCM_UNUSED)
98 {
99 ScmPrimitiveParameter *p = SCM_PRIMITIVE_PARAMETER(obj);
100 Scm_Printf(out, "#<%A %S%s@%p>",
101 Scm_ShortClassName(Scm_ClassOf(obj)),
102 p->name,
103 (p->flags & SCM_PARAMETER_SHARED
104 ? ""
105 : (p->flags & SCM_PARAMETER_THREAD
106 ? " (thread)"
107 : " (legacy)")),
108 obj);
109 }
110
111 static void ensure_parameter_init_keywords()
112 {
113 /* idempotency is ensured in SCM_MAKE_KEYWORD. */
114 if (SCM_FALSEP(key_name)) {
115 key_name = SCM_MAKE_KEYWORD("name");
116 }
117 if (SCM_FALSEP(key_initial_value)) {
118 key_initial_value = SCM_MAKE_KEYWORD("initial-value");
119 }
120 if (SCM_FALSEP(key_type)) {
121 key_type = SCM_MAKE_KEYWORD("type");
122 }
123 }
124
125 static ScmObj pparam_allocate(ScmClass *klass, ScmObj initargs)
126 {
127 ensure_parameter_init_keywords();
128 ScmObj name = Scm_GetKeyword(key_name, initargs, SCM_FALSE);
129 ScmObj initval = Scm_GetKeyword(key_initial_value, initargs, SCM_FALSE);
130 ScmObj type = Scm_GetKeyword(key_type, initargs, SCM_FALSE);
131 u_long flags = 0;
132
133 if (SCM_EQ(type, SCM_SYM_SHARED)) {
134 flags |= SCM_PARAMETER_SHARED;
135 } else if (SCM_EQ(type, SCM_SYM_THREAD)) {
136 flags |= SCM_PARAMETER_THREAD;
137 } else if (!SCM_EQ(type, SCM_SYM_LEGACY)) {
138 Scm_Error("Invalid value for :type argument: Must be one of "
139 "shared, thread, or legacy, but got: %S", type);
140 }
141 ScmPrimitiveParameter *p =
142 Scm_MakePrimitiveParameter(klass, name, initval, flags);
143 return SCM_OBJ(p);
144 }
145
146 /*
147 * Create a primitive parameter
148 * For now, it is just the same as thread local, but we'll gradually
149 * modify it for the new parameter semantics.
150 */
151 ScmPrimitiveParameter *Scm_MakePrimitiveParameter(ScmClass *klass,
152 ScmObj name,
153 ScmObj initval,
154 u_long flags)
155 {
156 /* This is called _before_ class stuff is initialized, in which case
157 we can't call SCM_NEW_INSTANCE. We know such cases only happens
158 with klass == SCM_CLASS_PRIMIVITE_PARAMETER, so we hard-wire the
159 case. */
160 ScmPrimitiveParameter *p;
161 if (SCM_EQ(klass, SCM_CLASS_PRIMITIVE_PARAMETER)) {
162 p = SCM_NEW(ScmPrimitiveParameter);
163 SCM_SET_CLASS(p, klass);
164 SCM_INSTANCE(p)->slots = NULL; /* no extra slots */
165 } else {
166 p = SCM_NEW_INSTANCE(ScmPrimitiveParameter, klass);
167 }
168 p->flags = flags;
169 p->name = name;
170 if (flags & SCM_PARAMETER_SHARED) {
171 p->g.v = Scm_MakeBox(initval);
172 } else {
173 p->g.tl = Scm_MakeThreadLocal(name, initval,
174 SCM_THREAD_LOCAL_INHERITABLE);
175 }
176 return p;
177 }
178
179 /*
180 * External accessor
181 */
182 int Scm_PrimitiveParameterSharedP(const ScmPrimitiveParameter *p)
183 {
184 return (p->flags & SCM_PARAMETER_SHARED)? TRUE : FALSE;
185 }
186
187 /*
188 * Create a SUBR that embeds a primitive parameter.
189 */
190 static ScmObj prim_param_proc(ScmObj *argv, int argc, void *data)
191 {
192 ScmPrimitiveParameter *p = SCM_PRIMITIVE_PARAMETER(data);
193 SCM_ASSERT(SCM_PRIMITIVE_PARAMETER_P(p));
194 SCM_ASSERT(argc == 1);
195 if (SCM_PAIRP(argv[0])) {
196 if (SCM_PAIRP(SCM_CDR(argv[0]))) {
197 Scm_Error("Wrong number of arguments for a parameter:"
198 " 0 or 1 argument(s) expected, but got %S", argv[0]);
199 }
200 return Scm_PrimitiveParameterSet(Scm_VM(), p, SCM_CAR(argv[0]));
201 } else {
202 return Scm_PrimitiveParameterRef(Scm_VM(), p);
203 }
204 }
205
206 static ScmObj general_param_proc(ScmObj *argv, int argc, void *data)
207 {
208 ScmPrimitiveParameter *p = SCM_PRIMITIVE_PARAMETER(data);
209 SCM_ASSERT(SCM_PRIMITIVE_PARAMETER_P(p));
210 SCM_ASSERT(argc == 1);
211
212 if (SCM_PAIRP(argv[0])) {
213 if (SCM_PAIRP(SCM_CDR(argv[0]))) {
214 Scm_Error("Wrong number of arguments for a parameter:"
215 " 0 or 1 argument(s) expected, but got %S", argv[0]);
216 }
217
218 static ScmObj parameter_set_proc = SCM_UNDEFINED;
219 SCM_BIND_PROC(parameter_set_proc, "%parameter-set!",
220 Scm_GaucheInternalModule());
221 return Scm_VMApply2(parameter_set_proc, SCM_OBJ(p), SCM_CAR(argv[0]));
222 } else {
223 return Scm_PrimitiveParameterRef(Scm_VM(), p);
224 }
225 }
226
227 ScmObj Scm_MakePrimitiveParameterSubr(ScmPrimitiveParameter *p)
228 {
229 ScmObj info = SCM_LIST2(sym_parameter, p->name);
230 if (SCM_EQ(Scm_ClassOf(SCM_OBJ(p)), SCM_CLASS_PRIMITIVE_PARAMETER)) {
231 return Scm_MakeSubr(prim_param_proc, p, 0, 1, info);
232 } else {
233 return Scm_MakeSubr(general_param_proc, p, 0, 1, info);
234 }
235 }
236
237 /*
238 * Parameterization
239 *
240 * A parameterization is kept in DENV.
241 * (#:parameterization (<parameter> . <value>) ...)
242 * Note that the tail of parameter alist is shared with the
243 * outer parameterization.
244 *
245 * If <parameter> is shared, the pair (<parameter> . <value>) is shared.
246 * Otherwise, the pair is copied when a new thread is created.
247 * (See get_denv() in vm.c)
248 */
249
250 ScmObj Scm_CurrentParameterization()
251 {
252 ScmObj k = Scm__GetDenvKey(SCM_DENV_KEY_PARAMETERIZATION);
253 ScmObj parameterization = Scm_VMFindDynamicEnv(k, SCM_NIL);
254
255 ScmParameterization *pz = SCM_NEW(ScmParameterization);
256 SCM_SET_CLASS(pz, SCM_CLASS_PARAMETERIZATION);
257 pz->parameterization = parameterization;
258 return SCM_OBJ(pz);
259 }
260
261 void Scm_PushParameterization(ScmObj params, ScmObj vals)
262 {
263 if (Scm_Length(params) != Scm_Length(vals)) {
264 Scm_Error("[internal] parameterize: parameters and values don't match.");
265 }
266
267 ScmObj k = Scm__GetDenvKey(SCM_DENV_KEY_PARAMETERIZATION);
268 ScmObj prev = Scm_VMFindDynamicEnv(k, SCM_NIL);
269 ScmObj h = SCM_NIL, t = SCM_NIL;
270
271 while (SCM_PAIRP(params)) {
272 ScmObj p = SCM_CAR(params);
273 SCM_ASSERT(SCM_PRIMITIVE_PARAMETER_P(p));
274 if (SCM_PRIMITIVE_PARAMETER(p)->flags & SCM_PARAMETER_THREAD) {
275 ScmThreadLocal *tl =
276 Scm_MakeThreadLocal(SCM_PRIMITIVE_PARAMETER(p)->name,
277 SCM_CAR(vals),
278 SCM_THREAD_LOCAL_INHERITABLE);
279 SCM_APPEND1(h, t, Scm_Cons(p, SCM_OBJ(tl)));
280 } else {
281 SCM_APPEND1(h, t, Scm_Cons(p, SCM_CAR(vals)));
282 }
283 params = SCM_CDR(params);
284 vals = SCM_CDR(vals);
285 }
286 SCM_APPEND(h, t, prev);
287 Scm_VMPushDynamicEnv(k, h);
288 }
289
290 /* Replace VM's current parameterization with PZ.
291 Note that the PZ is effective until the current continuation frame
292 is popped. So typically you have to call Scm_VMApply etc.
293 */
294 void Scm_InstallParameterization(ScmParameterization *pz)
295 {
296 ScmObj k = Scm__GetDenvKey(SCM_DENV_KEY_PARAMETERIZATION);
297 Scm_VMPushDynamicEnv(k, pz->parameterization);
298 }
299
300 /*
301 * Accessor & modifier
302 */
303 ScmObj Scm_PrimitiveParameterRef(ScmVM *vm, const ScmPrimitiveParameter *p)
304 {
305 ScmObj k = Scm__GetDenvKey(SCM_DENV_KEY_PARAMETERIZATION);
306 ScmObj parameterization = Scm_VMFindDynamicEnv(k, SCM_NIL);
307 ScmObj r = Scm_Assq(SCM_OBJ(p), parameterization);
308
309 if (SCM_PAIRP(r)) {
310 /* dynamically bound */
311 if (p->flags & SCM_PARAMETER_THREAD) {
312 ScmObj tl = SCM_CDR(r);
313 SCM_ASSERT(SCM_THREAD_LOCAL_P(tl));
314 return Scm_ThreadLocalRef(vm, SCM_THREAD_LOCAL(tl));
315 } else {
316 return SCM_CDR(r);
317 }
318 }
319
320 /* global value */
321 ScmObj v;
322 if (p->flags & SCM_PARAMETER_SHARED) {
323 v = SCM_BOX_VALUE(p->g.v);
324 } else {
325 v = Scm_ThreadLocalRef(vm, p->g.tl);
326 }
327 if (p->flags & SCM_PARAMETER_LAZY) return Scm_Force(v);
328 else return v;
329 }
330
331 ScmObj Scm_PrimitiveParameterSet(ScmVM *vm, const ScmPrimitiveParameter *p,
332 ScmObj val)
333 {
334 ScmObj k = Scm__GetDenvKey(SCM_DENV_KEY_PARAMETERIZATION);
335 ScmObj parameterization = Scm_VMFindDynamicEnv(k, SCM_NIL);
336 ScmObj r = Scm_Assq(SCM_OBJ(p), parameterization);
337 ScmObj old;
338
339 if (SCM_PAIRP(r)) {
340 if (p->flags & SCM_PARAMETER_THREAD) {
341 ScmObj tl = SCM_CDR(r);
342 SCM_ASSERT(SCM_THREAD_LOCAL_P(tl));
343 old = Scm_ThreadLocalSet(vm, SCM_THREAD_LOCAL(tl), val);
344 } else {
345 old = SCM_CDR(r);
346 SCM_SET_CDR(r, val);
347 }
348 return old;
349 }
350
351 if (p->flags & SCM_PARAMETER_SHARED) {
352 /* TODO: need atomic operation */
353 old = SCM_BOX_VALUE(p->g.v);
354 SCM_BOX_SET(p->g.v, val);
355 } else {
356 old = Scm_ThreadLocalSet(vm, p->g.tl, val);
357 }
358
359 if (p->flags & SCM_PARAMETER_LAZY) return Scm_Force(old);
360 else return old;
361 }
362
363 /* Convenience function. Create a primitive parameter subr and bind
364 it to NAME in MOD. */
365 ScmPrimitiveParameter *Scm_BindPrimitiveParameter(ScmModule *mod,
366 const char *name,
367 ScmObj initval,
368 u_long flags)
369 {
370 ScmPrimitiveParameter *p =
371 Scm_MakePrimitiveParameter(SCM_CLASS_PRIMITIVE_PARAMETER,
372 SCM_INTERN(name), initval, flags);
373 ScmObj subr = Scm_MakePrimitiveParameterSubr(p);
374 Scm_Define(mod, SCM_SYMBOL(p->name), subr);
375 return p;
376 }
377
378 ScmObj Scm__GetParameterSymbol()
379 {
380 return sym_parameter;
381 }
382
383 void Scm__InitParameter(void)
384 {
385 /* We don't initialize Scm_PrimitiveParameterClass yet, since class
386 stuff is not initialized yet. The class is initialized in
387 class.c. */
388
389 /* NB: At this moment, symbols are not initialized, but making uninterned
390 symbol is ok. */
391 sym_parameter =
392 Scm_MakeSymbol(SCM_STRING(SCM_MAKE_STR("parameter")), FALSE);
393 }