1 /*
2  * parameter.h - parameter C API
3  *
4  *   Copyright (c) 2007-2020  Shiro Kawai  <shiro@acm.org>
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 /*
35  *  Parameters keep thread-local state.   It is called 'fluids' in some
36  *  Scheme implementations.  A thread inherits the parameters from its
37  *  creator.
38  */
39 
40 /* NB: For the time being, we let all threads share the index into the
41  * parameter array---if one thread creates a parameter, we globally allocate
42  * an index for it.
43  */
44 
45 #ifndef GAUCHE_PARAMETER_H
46 #define GAUCHE_PARAMETER_H
47 
48 /* ScmPrimitiveParameter is an opaque struct (Definition is
49    in priv/parameterP.h) that implements a simple thread-local
50    storage.  It doesn't have extra features such as filter
51    procedure or hooks.  It is useful for the parameter that needs
52    to be accessed from C as well.
53 
54    ScmParameter is Scheme's <parameter> object.  It inherits
55    primitive parameter, but adds some bells and whistles.
56 */
57 typedef struct ScmPrimitiveParameterRec ScmPrimitiveParameter;
58 
59 SCM_CLASS_DECL(Scm_PrimitiveParameterClass);
60 #define SCM_CLASS_PRIMITIVE_PARAMETER  (&Scm_PrimitiveParameterClass)
61 #define SCM_PRIMITIVE_PARAMETER(obj)   ((ScmPrimitiveParameter*)obj)
62 #define SCM_PRIMITIVE_PARAMETER_P(obj) SCM_ISA(obj,SCM_CLASS_PRIMITIVE_PARAMETER)
63 
64 /* Flag value for Scm_MakePrimitiveParameter */
65 enum {
66     /* value may be a promise; dereference automaticlaly forces it */
67     SCM_PARAMETER_LAZY = (1UL << 0)
68 };
69 
70 SCM_EXTERN ScmPrimitiveParameter *Scm_MakePrimitiveParameter(ScmClass *klass,
71                                                              ScmObj name,
72                                                              ScmObj initval,
73                                                              u_long flags);
74 SCM_EXTERN ScmObj Scm_MakePrimitiveParameterSubr(ScmPrimitiveParameter *p);
75 SCM_EXTERN ScmObj Scm_PrimitiveParameterRef(ScmVM *vm,
76                                             const ScmPrimitiveParameter *p);
77 SCM_EXTERN ScmObj Scm_PrimitiveParameterSet(ScmVM *vm,
78                                             const ScmPrimitiveParameter *p,
79                                             ScmObj val);
80 
81 /* A convenience function to create a new ScmPrimitiveParameter
82    and bind it to NAME in MOD.  Returns a newly created primitive
83    parameter so that it is accessible from C.  */
84 SCM_EXTERN ScmPrimitiveParameter *Scm_BindPrimitiveParameter(ScmModule *mod,
85                                                              const char *name,
86                                                              ScmObj initval,
87                                                              u_long flags);
88 
89 /* TRANSIENT - exposed only for the backward compatibility - will be gone by 1.0 */
90 #if GAUCHE_API_VERSION < 1000
91 typedef struct ScmParameterLocRec {
92     ScmPrimitiveParameter *p;
93 } ScmParameterLoc;
94 
95 SCM_EXTERN void Scm_DefinePrimitiveParameter(ScmModule *mod,
96                                              const char *name,
97                                              ScmObj initval,
98                                              ScmParameterLoc *location /*out*/);
99 SCM_EXTERN void   Scm_MakeParameterSlot(ScmVM *vm,
100                                         ScmParameterLoc *location /*out*/);
101 SCM_EXTERN void   Scm_InitParameterLoc(ScmVM *vm,
102                                        ScmParameterLoc *location,
103                                        ScmObj initval);
104 SCM_EXTERN ScmObj Scm_ParameterRef(ScmVM *vm, const ScmParameterLoc *location);
105 SCM_EXTERN ScmObj Scm_ParameterSet(ScmVM *vm, const ScmParameterLoc *location,
106                                    ScmObj value);
107 #endif /*GAUCHE_API_VERSION < 1000*/
108 
109 #endif /*GAUCHE_PARAMETER_H*/
110