Qore Programming Language 1.19.1
Loading...
Searching...
No Matches
QoreQueueIntern.h
1/* -*- mode: c++; indent-tabs-mode: nil -*- */
2/*
3 QoreQueueIntern.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_QOREQUEUEINTERN_H
33
34#define _QORE_QOREQUEUEINTERN_H
35
36#include <qore/QoreThreadLock.h>
37#include <qore/QoreCondition.h>
38
39#include <string>
40
41class qore_object_private;
42class RSetHelper;
43
44class QoreQueueNode {
45public:
46 QoreValue node;
47 QoreQueueNode* prev,
48 * next;
49 DLLLOCAL QoreQueueNode(QoreValue n, QoreQueueNode* p, QoreQueueNode* nx) : node(n), prev(p), next(nx) {
50 }
51
52#ifdef DEBUG
53 DLLLOCAL ~QoreQueueNode() {
54 assert(!node);
55 }
56#endif
57
58 DLLLOCAL void del(ExceptionSink* xsink) {
59 node.discard(xsink);
60
61#ifdef DEBUG
62 node = QoreValue();
63#endif
64 delete this;
65 }
66
67 DLLLOCAL QoreValue takeAndDel() {
68 QoreValue rv = node;
69#ifdef DEBUG
70 node = QoreValue();
71#endif
72 delete this;
73 return rv;
74 }
75};
76
77#define QW_DEL -1
78#define QW_TIMEOUT -2
79#define QW_ERROR -3
80
81class qore_queue_private {
82 friend class qore_object_private;
83
84private:
85 enum queue_status_e { Queue_Deleted = -1 };
86
87 mutable QoreThreadLock l;
88 QoreCondition read_cond, // read Condition variable
89 write_cond; // write Condition variable
90 QoreQueueNode* head,
91 * tail;
92 std::string err;
93 QoreStringNode* desc;
94 int len, // the number of elements currently in the queue (or -1 for deleted)
95 max; // the maximum size of the queue (or -1 for unlimited)
96 unsigned read_waiting, // number of threads waiting on reads
97 write_waiting; // number of threads waiting on writes
98
99 // issue #3101: maintain a count of all scanable objects in the queue
100 int scan_count = 0;
101
102 DLLLOCAL int waitReadIntern(ExceptionSink *xsink, int timeout_ms);
103 DLLLOCAL int waitWriteIntern(ExceptionSink *xsink, int timeout_ms);
104
105 DLLLOCAL void pushNode(QoreValue v);
106 DLLLOCAL void pushIntern(QoreValue v);
107 DLLLOCAL void insertIntern(QoreValue v);
108
109 DLLLOCAL void clearIntern(ExceptionSink* xsink);
110
111 // called in the lock; returns -1 if not possible (cannot write to the queue) or 0 of OK
112 DLLLOCAL int checkWriteIntern(ExceptionSink* xsink, bool always_error = false);
113
114public:
115 DLLLOCAL qore_queue_private(int n_max = -1) : head(0), tail(0), desc(0), len(0), max(n_max), read_waiting(0), write_waiting(0) {
116 assert(max);
117 //printd(5, "qore_queue_private::qore_queue_private() this: %p max: %d\n", this, max);
118 }
119
120 DLLLOCAL qore_queue_private(const qore_queue_private &orig) : head(0), tail(0), err(orig.err), desc(orig.desc ? orig.desc->stringRefSelf() : 0), len(0), max(orig.max), read_waiting(0), write_waiting(0) {
121 AutoLocker al(orig.l);
122 if (orig.len == Queue_Deleted)
123 return;
124
125 QoreQueueNode* w = orig.head;
126 while (w) {
127 pushIntern(w->node.refSelf());
128 w = w->next;
129 }
130
131 //printd(5, "qore_queue_private::qore_queue_private() this=%p head=%p tail=%p waiting=%d len=%d\n", this, head, tail, waiting, len);
132 }
133
134 // queues should not be deleted when other threads might
135 // be accessing them
136 DLLLOCAL ~qore_queue_private() {
137 //QORE_TRACE("qore_queue_private::~qore_queue_private()");
138 //printd(5, "qore_queue_private::~qore_queue_private() this=%p head=%p tail=%p len=%d\n", this, head, tail, len);
139 assert(!head);
140 assert(!tail);
141 assert(len == Queue_Deleted);
142 assert(!desc);
143 }
144
145 // push at the end of the queue and take the reference - can only be used when len == -1
146 DLLLOCAL void pushAndTakeRef(QoreValue n);
147
148 // push at the end of the queue
149 DLLLOCAL void push(ExceptionSink* xsink, QoreObject* self, QoreValue n, int timeout_ms, bool& to);
150
151 // insert at the beginning of the queue
152 DLLLOCAL void insert(ExceptionSink* xsink, QoreObject* self, QoreValue n, int timeout_ms, bool& to);
153
154 DLLLOCAL QoreValue shift(ExceptionSink* xsink, QoreObject* self, int timeout_ms, bool& to);
155 DLLLOCAL QoreValue pop(ExceptionSink* xsink, QoreObject* self, int timeout_ms, bool& to);
156
157 DLLLOCAL bool empty() const {
158 return !len;
159 }
160
161 DLLLOCAL int size() const {
162 return len;
163 }
164
165 DLLLOCAL int getMax() const {
166 return max;
167 }
168
169 DLLLOCAL unsigned getReadWaiting() const {
170 return read_waiting;
171 }
172
173 DLLLOCAL unsigned getWriteWaiting() const {
174 return write_waiting;
175 }
176
177 DLLLOCAL void clear(ExceptionSink* xsink, QoreObject* self = nullptr);
178 DLLLOCAL void destructor(ExceptionSink* xsink);
179
180 DLLLOCAL void setError(const char* err, const QoreStringNode* desc, QoreObject* self, ExceptionSink* xsink);
181
182 DLLLOCAL void clearError();
183
184 DLLLOCAL bool scanMembers(RObject& obj, RSetHelper& rsh);
185
186 DLLLOCAL static void destructor(QoreQueue& q, ExceptionSink* xsink) {
187 q.priv->destructor(xsink);
188 }
189
190 DLLLOCAL static qore_queue_private* get(QoreQueue& q) {
191 return q.priv;
192 }
193};
194
195#endif // _QORE_QOREQUEUEINTERN_H
provides a safe and exception-safe way to hold locks in Qore, only to be used on the stack,...
Definition: QoreThreadLock.h:136
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:50
a thread condition class implementing a wrapper for pthread_cond_t
Definition: QoreCondition.h:45
the implementation of Qore's object data type, reference counted, dynamically-allocated only
Definition: QoreObject.h:60
Qore's string value type, reference counted, dynamically-allocated only.
Definition: QoreStringNode.h:50
provides a mutually-exclusive thread lock
Definition: QoreThreadLock.h:49
The main value class in Qore, designed to be passed by value.
Definition: QoreValue.h:276
DLLEXPORT void discard(ExceptionSink *xsink)
dereferences any contained AbstractQoreNode pointer and sets to 0; does not modify other values