1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
|
/* -*- C++ -*-
* Copyright (C) 2003-2005 Thiago Macieira <[email protected]>
*
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef KRESOLVER_P_H
#define KRESOLVER_P_H
#include <config.h>
#include <sys/types.h>
#include <tqstring.h>
#include <tqcstring.h>
#include <tqvaluelist.h>
#include <tqptrlist.h>
#include <tqptrqueue.h>
#include <tqthread.h>
#include <tqmutex.h>
#include <tqwaitcondition.h>
#include <tqsemaphore.h>
#include <tqevent.h>
#include "kresolver.h"
/* decide whether we need a mutex */
#if !defined(HAVE_GETPROTOBYNAME_R) || !defined(HAVE_GETSERVBYNAME_R) || !defined(HAVE_GETHOSTBYNAME_R) || !defined(HAVE_GETSERVBYPORT_R)
# define NEED_MUTEX
extern TQMutex getXXbyYYmutex;
#endif
/* some systems have the functions, but don't declare them */
#ifndef __OpenBSD__
#if defined(HAVE_GETSERVBYNAME_R) && !HAVE_DECL_GETSERVBYNAME_R
extern "C" {
struct servent;
extern int getservbyname_r(const char* serv, const char* proto,
struct servent* servbuf,
char* buf, size_t buflen,
struct servent** result);
extern int getservbyport_r(int port, const char* proto,
struct servent* servbuf,
char* buf, size_t buflen,
struct servent** result);
struct protoent;
extern int getprotobyname_r(const char* proto, struct protoent* protobuf,
char *buf, size_t buflen,
struct protoent** result);
extern int getprotobynumber_r(int proto, struct protoent* protobuf,
char *buf, size_t buflen,
struct protoent** result);
}
#endif
#endif
/* decide whether res_init is thread-safe or not */
#if defined(__GLIBC__)
# undef RES_INIT_THREADSAFE
#endif
namespace KNetwork
{
// defined in network/qresolverworkerbase.h
class KResolverWorkerBase;
class KResolverWorkerFactoryBase;
class KResolverPrivate;
namespace Internal
{
class KResolverManager;
class KResolverThread;
struct RequestData;
struct InputData
{
TQString node, service;
TQCString protocolName;
int flags;
int familyMask;
int socktype;
int protocol;
};
}
class KResolverPrivate
{
public:
// parent class. Should never be changed!
KResolver* parent;
bool deleteWhenDone : 1;
bool waiting : 1;
// class status. Should not be changed by worker threads!
volatile int status;
volatile int errorcode, syserror;
// input data. Should not be changed by worker threads!
Internal::InputData input;
// mutex
TQMutex mutex;
// output data
KResolverResults results;
KResolverPrivate(KResolver* _parent,
const TQString& _node = TQString::null,
const TQString& _service = TQString::null)
: parent(_parent), deleteWhenDone(false), waiting(false),
status(0), errorcode(0), syserror(0)
{
input.node = _node;
input.service = _service;
input.flags = 0;
input.familyMask = KResolver::AnyFamily;
input.socktype = 0;
input.protocol = 0;
results.setAddress(_node, _service);
}
};
namespace Internal
{
struct RequestData
{
// worker threads should not change values in the input data
KNetwork::KResolverPrivate *obj;
const KNetwork::Internal::InputData *input;
KNetwork::KResolverWorkerBase *worker; // worker class
RequestData *requestor; // class that requested us
volatile int nRequests; // how many requests that we made we still have left
};
/*
* @internal
* This class is the resolver manager
*/
class KResolverManager
{
public:
enum EventTypes
{ ResolutionCompleted = 1576 }; // arbitrary value;
/*
* This wait condition is used to notify wait states (KResolver::wait) that
* the resolver manager has finished processing one or more objects. All
* objects in wait state will be woken up and will check if they are done.
* If they aren't, they will go back to sleeping.
*/
TQWaitCondition notifyWaiters;
private:
/*
* This variable is used to count the number of threads that are running
*/
volatile unsigned short runningThreads;
/*
* This variable is used to count the number of threads that are currently
* waiting for data.
*/
unsigned short availableThreads;
/*
* This wait condition is used to notify worker threads that there is new
* data available that has to be processed. All worker threads wait on this
* waitcond for a limited amount of time.
*/
TQWaitCondition feedWorkers;
// this mutex protects the data in this object
TQMutex mutex;
// hold a list of all the current threads we have
TQPtrList<KResolverThread> workers;
// hold a list of all the new requests we have
TQPtrList<RequestData> newRequests;
// hold a list of all the requests in progress we have
TQPtrList<RequestData> currentRequests;
// hold a list of all the workers we have
TQPtrList<KNetwork::KResolverWorkerFactoryBase> workerFactories;
// private constructor
KResolverManager();
public:
static KResolverManager* manager() KDE_NO_EXPORT; // creates and returns the global manager
// destructor
~KResolverManager();
/*
* Register this thread in the pool
*/
void registerThread(KResolverThread* id);
/*
* Unregister this thread from the pool
*/
void unregisterThread(KResolverThread* id);
/*
* Requests new data to work on.
*
* This function should only be called from a worker thread. This function
* is thread-safe.
*
* If there is data to be worked on, this function will return it. If there is
* none, this function will return a null pointer.
*/
RequestData* requestData(KResolverThread* id, int maxWaitTime);
/*
* Releases the resources and returns the resolved data.
*
* This function should only be called from a worker thread. It is
* thread-safe. It does not post the event to the manager.
*/
void releaseData(KResolverThread *id, RequestData* data);
/*
* Registers a new worker class by way of its factory.
*
* This function is NOT thread-safe.
*/
void registerNewWorker(KNetwork::KResolverWorkerFactoryBase *factory);
/*
* Enqueues new resolutions.
*/
void enqueue(KNetwork::KResolver *obj, RequestData* requestor);
/*
* Dispatch a new request
*/
void dispatch(RequestData* data);
/*
* Dequeues a resolution.
*/
void dequeue(KNetwork::KResolver *obj);
/*
* Notifies the manager that the given resolution is about to
* be deleted. This function should only be called by the
* KResolver destructor.
*/
void aboutToBeDeleted(KNetwork::KResolver *obj);
/*
* Notifies the manager that new events are ready.
*/
void newEvent();
/*
* This function is called by the manager to receive a new event. It operates
* on the @ref eventSemaphore semaphore, which means it will block till there
* is at least one event to go.
*/
void receiveEvent();
private:
/*
* finds a suitable worker for this request
*/
KNetwork::KResolverWorkerBase *findWorker(KNetwork::KResolverPrivate *p);
/*
* finds data for this request
*/
RequestData* findData(KResolverThread*);
/*
* Handle completed requests.
*
* This function is called by releaseData above
*/
void handleFinished();
/*
* Handle one completed request.
*
* This function is called by handleFinished above.
*/
bool handleFinishedItem(RequestData* item);
/*
* Notifies the parent class that this request is done.
*
* This function deletes the request
*/
void doNotifying(RequestData *p);
/*
* Dequeues and notifies an object that is in Queued state
* Returns true if the object is no longer queued; false if it could not
* be dequeued (i.e., it's running)
*/
bool dequeueNew(KNetwork::KResolver* obj);
};
/*
* @internal
* This class is a worker thread in the resolver system.
* This class must be thread-safe.
*/
class KResolverThread: public TQThread
{
private:
// private constructor. Only the manager can create worker threads
KResolverThread();
RequestData* data;
protected:
virtual void run(); // here the thread starts
friend class KNetwork::Internal::KResolverManager;
friend class KNetwork::KResolverWorkerBase;
public:
bool checkResolver(); // see KResolverWorkerBase::checkResolver
void acquireResolver(); // see KResolverWorkerBase::acquireResolver
void releaseResolver(); // see KResolverWorkerBase::releaseResolver
};
} // namespace Internal
} // namespace KNetwork
#endif
|