Qore Programming Language 1.19.1
Loading...
Searching...
No Matches
ThreadLocalVariableData.h
1/* -*- mode: c++; indent-tabs-mode: nil -*- */
2/*
3 ThreadLocalVariableData.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_THREADLOCALVARIABLEDATA_H
33#define _QORE_INTERN_THREADLOCALVARIABLEDATA_H
34
35class ThreadLocalVariableData : public ThreadLocalData<LocalVarValue> {
36public:
37 // clears and marks all variables as finalized on the stack
38 DLLLOCAL void finalize(SafeDerefHelper& sdh) {
39 ThreadLocalVariableData::iterator i(curr);
40 while (i.next()) {
41 sdh.deref(i.get().finalize());
42 }
43 }
44
45 // deletes everything on the stack
46 DLLLOCAL void del(ExceptionSink* xsink) {
47 //printd(5, "ThreadLocalVariableData::del() this: %p empty: %d prev: %p pos: %d\n", this, empty(), curr->prev, curr->pos);
48
49 // then we uninstantiate
50 while (curr->prev || curr->pos)
51 uninstantiate(xsink);
52 }
53
54 DLLLOCAL LocalVarValue* instantiate() {
55 if (curr->pos == QORE_THREAD_STACK_BLOCK) {
56 if (curr->next)
57 curr = curr->next;
58 else {
59 curr->next = new Block(curr);
60 //printf("this: %p: add curr: %p, curr->next: %p\n", this, curr, curr->next);
61 curr = curr->next;
62 }
63 }
64 return &curr->var[curr->pos++];
65 }
66
67 DLLLOCAL void uninstantiate(ExceptionSink* xsink) {
68 uninstantiateIntern();
69 //printd(5, "ThreadLocalVariableData::uninstantiate() this: %p '%s' pos: %d\n", this, curr->var[curr->pos].id, curr->pos);
70 curr->var[curr->pos].uninstantiate(xsink);
71 }
72
73 DLLLOCAL void uninstantiateSelf() {
74 uninstantiateIntern();
75 curr->var[curr->pos].uninstantiateSelf();
76 }
77
78 DLLLOCAL void uninstantiateIntern() {
79 if (!curr->pos) {
80 if (curr->next) {
81 //printf("this %p: del curr: %p, curr->next: %p\n", this, curr, curr->next);
82 delete curr->next;
83 curr->next = 0;
84 }
85 curr = curr->prev;
86 assert(curr);
87 }
88 --curr->pos;
89 }
90
91 DLLLOCAL LocalVarValue* find(const char* id) {
92 Block* w = curr;
93 while (true) {
94 int p = w->pos;
95 while (p) {
96 --p;
97 LocalVarValue* var = &w->var[p];
98 if (var->id == id && !var->frame_boundary)
99 return var;
100 }
101 w = w->prev;
102#ifdef DEBUG
103 if (!w) {
104 p = curr->pos - 1;
105 printd(0, "ThreadLocalVariableData::find() this: %p no local variable '%s' (%p) on stack (pgm: %p) p: %d\n", this, id, id, getProgram(), p);
106 while (p >= 0) {
107 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);
108 --p;
109 }
110 }
111#endif
112 assert(w);
113 }
114 // to avoid a warning on most compilers - note that this generates a warning on recent versions of aCC!
115 return 0;
116 }
117
118 DLLLOCAL void pushFrameBoundary() {
119 ++frame_count;
120 //printd(5, "ThreadLocalVariableData::pushFrameBoundary(): fc:%d\n", frame_count);
121 LocalVarValue* v = instantiate();
122 v->setFrameBoundary();
123 }
124
125 DLLLOCAL void popFrameBoundary() {
126 assert(frame_count >= 0);
127 --frame_count;
128 //printd(5, "ThreadLocalVariableData::popFrameBoundary(): fc:%d\n", frame_count);
129 uninstantiateIntern();
130 assert(curr->var[curr->pos].frame_boundary);
131 curr->var[curr->pos].frame_boundary = false;
132 }
133
134 DLLLOCAL int getFrame(int frame, Block*& w, int& p);
135
136 DLLLOCAL void getLocalVars(QoreHashNode& h, int frame, ExceptionSink* xsink);
137
138 // returns 0 = OK, 1 = no such variable, -1 exception setting variable
139 DLLLOCAL int setVarValue(int frame, const char* name, const QoreValue& val, ExceptionSink* xsink);
140};
141
142#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