Qore Programming Language 1.19.1
Loading...
Searching...
No Matches
ThreadClosureVariableStack.h
1/* -*- mode: c++; indent-tabs-mode: nil -*- */
2/*
3 ThreadClosureVariableStack.h
4
5 Qore Programming Language
6
7 Copyright (C) 2003 - 2023 Qore Technologies, s.r.o.
8
9 Permission is hereby granted, free of charge, to any person obtaining a
10 copy of this software and associated documentation files (the "Software"),
11 to deal in the Software without restriction, including without limitation
12 the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 and/or sell copies of the Software, and to permit persons to whom the
14 Software is furnished to do so, subject to the following conditions:
15
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 DEALINGS IN THE SOFTWARE.
26
27 Note that the Qore library is released under a choice of three open-source
28 licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
29 information.
30*/
31
32#ifndef _QORE_INTERN_THREADCLOSUREVARIABLESTACK_H
33#define _QORE_INTERN_THREADCLOSUREVARIABLESTACK_H
34
35class ThreadClosureVariableStack : public ThreadLocalData<ClosureVarValue*> {
36private:
37 DLLLOCAL void instantiateIntern(ClosureVarValue* cvar) {
38 //printd(5, "ThreadClosureVariableStack::instantiateIntern(%p = '%s') this: %p pgm: %p\n", cvar->id, cvar->id, this, getProgram());
39
40 if (curr->pos == QORE_THREAD_STACK_BLOCK) {
41 if (curr->next)
42 curr = curr->next;
43 else {
44 curr->next = new Block(curr);
45 //printf("this: %p: add curr: %p, curr->next: %p\n", this, curr, curr->next);
46 curr = curr->next;
47 }
48 }
49 curr->var[curr->pos++] = cvar;
50 }
51
52public:
53 // clears and marks all variables as finalized on the stack
54 DLLLOCAL void finalize(SafeDerefHelper& sdh) {
55 //printd(5, "ThreadClosureVariableStack::finalize() this: %p\n", this);
56 ThreadClosureVariableStack::iterator i(curr);
57 while (i.next()) {
58 //printd(5, "ThreadClosureVariableStack::finalize() this: %p %p %s\n", this, i.get(), i.get()->id);
59 sdh.deref(i.get()->finalize());
60 }
61 }
62
63 // deletes everything on the stack
64 DLLLOCAL void del(ExceptionSink* xsink) {
65 while (curr->prev || curr->pos)
66 uninstantiate(xsink);
67 }
68
69 DLLLOCAL ClosureVarValue* instantiate(const char* id, const QoreTypeInfo* typeInfo, QoreValue& nval, bool assign) {
70 ClosureVarValue* cvar = new ClosureVarValue(id, typeInfo, nval, assign);
71 instantiateIntern(cvar);
72 return cvar;
73 }
74
75 DLLLOCAL void instantiate(ClosureVarValue* cvar) {
76 instantiateIntern(cvar);
77 }
78
79 DLLLOCAL void uninstantiateIntern() {
80#if 0
81 if (!curr->pos)
82 printd(0, "ThreadClosureVariableStack::uninstantiate() this: %p pos: %d %p %s\n", this, curr->prev->pos - 1, curr->prev->var[curr->prev->pos - 1]->id, curr->prev->var[curr->prev->pos - 1]->id);
83 else
84 printd(0, "ThreadClosureVariableStack::uninstantiate() this: %p pos: %d %p %s\n", this, curr->pos - 1, curr->var[curr->pos - 1]->id, curr->var[curr->pos - 1]->id);
85#endif
86 if (!curr->pos) {
87 if (curr->next) {
88 //printf("this %p: del curr: %p, curr->next: %p\n", this, curr, curr->next);
89 delete curr->next;
90 curr->next = nullptr;
91 }
92 curr = curr->prev;
93 assert(curr);
94 }
95 --curr->pos;
96 }
97
98 DLLLOCAL void uninstantiate(ExceptionSink* xsink) {
99 uninstantiateIntern();
100 assert(curr->var[curr->pos]);
101 curr->var[curr->pos]->deref(xsink);
102 }
103
104 DLLLOCAL ClosureVarValue* find(const char* id) {
105 printd(5, "ThreadClosureVariableStack::find() this: %p id: %p\n", this, id);
106 Block* w = curr;
107 while (true) {
108 int p = w->pos;
109 while (p) {
110 --p;
111 ClosureVarValue* rv = w->var[p];
112 //printd(5, "ThreadClosureVariableStack::find(%p '%s') this: %p checking %p '%s'\n", id, id, this, rv ? rv->id : nullptr, rv ? rv->id : "n/a");
113 if (rv && rv->id == id) {
114 //printd(5, "ThreadClosureVariableStack::find(%p '%s') this: %p returning: %p\n", id, id, this, rv);
115 return rv;
116 }
117 }
118 w = w->prev;
119#ifdef DEBUG
120 if (!w) {
121 printd(0, "ThreadClosureVariableStack::find() this: %p no closure-bound local variable '%s' (%p) on stack (pgm: %p) p: %d curr->prev: %p\n", this, id, id, getProgram(), p, curr->prev);
122 w = curr;
123 while (w) {
124 p = w->pos;
125 while (p) {
126 --p;
127 ClosureVarValue* cvv = w->var[p];
128 printd(0, "var p: %d: %s (%p)\n", p, cvv ? cvv->id : "frame boundary", cvv ? cvv->id : nullptr);
129 }
130 w = w->prev;
131 }
132 }
133#endif
134 assert(w);
135 }
136 // to avoid a warning on most compilers - note that this generates a warning on aCC!
137 return nullptr;
138 }
139
140 DLLLOCAL cvv_vec_t* getAll() const {
141 cvv_vec_t* cv = 0;
142 Block* w = curr;
143 while (w) {
144 int p = w->pos;
145 while (p) {
146 --p;
147 ClosureVarValue* cvv = w->var[p];
148 // skip frame boundaries
149 if (!cvv)
150 continue;
151 if (!cv)
152 cv = new cvv_vec_t;
153 cv->push_back(cvv->refSelf());
154 }
155 w = w->prev;
156 }
157 //printd(5, "ThreadClosureVariableStack::getAll() this: %p cv: %p size: %d\n", this, cv, cv ? cv->size() : 0);
158 return cv;
159 }
160
161 DLLLOCAL void pushFrameBoundary() {
162 ++frame_count;
163 //printd(5, "ThreadClosureVariableStack::pushFrameBoundary(): fc:%d\n", frame_count);
164 instantiateIntern(nullptr);
165 }
166
167 DLLLOCAL void popFrameBoundary() {
168 assert(frame_count >= 0);
169 --frame_count;
170 //printd(5, "ThreadClosureVariableStack::popFrameBoundary(): fc:%d\n", frame_count);
171 uninstantiateIntern();
172 assert(!curr->var[curr->pos]);
173 }
174
175 DLLLOCAL int getFrame(int frame, Block*& w, int& p);
176
177 DLLLOCAL void getLocalVars(QoreHashNode& h, int frame, ExceptionSink* xsink);
178
179 // returns 0 = OK, 1 = no such variable, -1 exception setting variable
180 DLLLOCAL int setVarValue(int frame, const char* name, const QoreValue& val, ExceptionSink* xsink);
181};
182
183#endif
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:50
This is the hash or associative list container type in Qore, dynamically allocated only,...
Definition: QoreHashNode.h:50
Helps dereference values outside of locks.
Definition: QoreLibIntern.h:543
DLLLOCAL void deref(QoreValue v)
dereferences the value immediately if it cannot throw an exception, or adds it to the list for derefe...
Definition: QoreLibIntern.h:559
DLLEXPORT QoreProgram * getProgram()
returns the current QoreProgram
The main value class in Qore, designed to be passed by value.
Definition: QoreValue.h:276