Qore Programming Language  0.9.4.6
ThreadLocalVariableData.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  ThreadLocalVariableData.h
4 
5  Qore Programming Language
6 
7  Copyright (C) 2003 - 2020 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_THREADLOCALVARIABLEDATA_H
33 #define _QORE_INTERN_THREADLOCALVARIABLEDATA_H
34 
35 class ThreadLocalVariableData : public ThreadLocalData<LocalVarValue> {
36 public:
37  // clears and marks all variables as finalized on the stack
38  DLLLOCAL void finalize(arg_vec_t*& cl) {
39  ThreadLocalVariableData::iterator i(curr);
40  while (i.next()) {
41  ValueHolder n(i.get().finalize(), nullptr);
42  if (n->derefCanThrowException()) {
43  if (!cl) {
44  cl = new arg_vec_t;
45  }
46  cl->push_back(n.release());
47  }
48  }
49  }
50 
51  // deletes everything on the stack
52  DLLLOCAL void del(ExceptionSink* xsink) {
53  //printd(5, "ThreadLocalVariableData::del() this: %p empty: %d prev: %p pos: %d\n", this, empty(), curr->prev, curr->pos);
54 
55  // then we uninstantiate
56  while (curr->prev || curr->pos)
57  uninstantiate(xsink);
58  }
59 
60  DLLLOCAL LocalVarValue* instantiate() {
61  if (curr->pos == QORE_THREAD_STACK_BLOCK) {
62  if (curr->next)
63  curr = curr->next;
64  else {
65  curr->next = new Block(curr);
66  //printf("this: %p: add curr: %p, curr->next: %p\n", this, curr, curr->next);
67  curr = curr->next;
68  }
69  }
70  return &curr->var[curr->pos++];
71  }
72 
73  DLLLOCAL void uninstantiate(ExceptionSink* xsink) {
74  uninstantiateIntern();
75  //printd(5, "ThreadLocalVariableData::uninstantiate() this: %p '%s' pos: %d\n", this, curr->var[curr->pos].id, curr->pos);
76  curr->var[curr->pos].uninstantiate(xsink);
77  }
78 
79  DLLLOCAL void uninstantiateSelf() {
80  uninstantiateIntern();
81  curr->var[curr->pos].uninstantiateSelf();
82  }
83 
84  DLLLOCAL void uninstantiateIntern() {
85  if (!curr->pos) {
86  if (curr->next) {
87  //printf("this %p: del curr: %p, curr->next: %p\n", this, curr, curr->next);
88  delete curr->next;
89  curr->next = 0;
90  }
91  curr = curr->prev;
92  assert(curr);
93  }
94  --curr->pos;
95  }
96 
97  DLLLOCAL LocalVarValue* find(const char* id) {
98  Block* w = curr;
99  while (true) {
100  int p = w->pos;
101  while (p) {
102  --p;
103  LocalVarValue* var = &w->var[p];
104  if (var->id == id && !var->frame_boundary)
105  return var;
106  }
107  w = w->prev;
108 #ifdef DEBUG
109  if (!w) {
110  p = curr->pos - 1;
111  printd(0, "ThreadLocalVariableData::find() this: %p no local variable '%s' (%p) on stack (pgm: %p) p: %d\n", this, id, id, getProgram(), p);
112  while (p >= 0) {
113  printd(0, "var p: %d: %s (%p) (frame_boundary: %d)\n", p, curr->var[p].id, curr->var[p].id, curr->var[p].frame_boundary);
114  --p;
115  }
116  }
117 #endif
118  assert(w);
119  }
120  // to avoid a warning on most compilers - note that this generates a warning on recent versions of aCC!
121  return 0;
122  }
123 
124  DLLLOCAL void pushFrameBoundary() {
125  ++frame_count;
126  //printd(5, "ThreadLocalVariableData::pushFrameBoundary(): fc:%d\n", frame_count);
127  LocalVarValue* v = instantiate();
128  v->setFrameBoundary();
129  }
130 
131  DLLLOCAL void popFrameBoundary() {
132  assert(frame_count >= 0);
133  --frame_count;
134  //printd(5, "ThreadLocalVariableData::popFrameBoundary(): fc:%d\n", frame_count);
135  uninstantiateIntern();
136  assert(curr->var[curr->pos].frame_boundary);
137  curr->var[curr->pos].frame_boundary = false;
138  }
139 
140  DLLLOCAL int getFrame(int frame, Block*& w, int& p);
141 
142  DLLLOCAL void getLocalVars(QoreHashNode& h, int frame, ExceptionSink* xsink);
143 
144  // returns 0 = OK, 1 = no such variable, -1 exception setting variable
145  DLLLOCAL int setVarValue(int frame, const char* name, const QoreValue& val, ExceptionSink* xsink);
146 };
147 
148 #endif
This is the hash or associative list container type in Qore, dynamically allocated only...
Definition: QoreHashNode.h:50
DLLEXPORT QoreProgram * getProgram()
returns the current QoreProgram
The main value class in Qore, designed to be passed by value.
Definition: QoreValue.h:262
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:46
std::vector< QoreValue > arg_vec_t
vector of value information for default argument lists
Definition: common.h:254
holds an object and dereferences it in the destructor
Definition: QoreValue.h:452