[go: up one dir, main page]

File: tothread.cpp

package info (click to toggle)
tora 1.3.21-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 14,252 kB
  • ctags: 10,397
  • sloc: cpp: 108,822; sh: 10,861; makefile: 766; xml: 69; perl: 6
file content (383 lines) | stat: -rw-r--r-- 8,388 bytes parent folder | download | duplicates (2)
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*****
*
* TOra - An Oracle Toolkit for DBA's and developers
* Copyright (C) 2003-2005 Quest Software, Inc
* Portions Copyright (C) 2005 Other Contributors
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation;  only version 2 of
* the License is valid for this program.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*
*      As a special exception, you have permission to link this program
*      with the Oracle Client libraries and distribute executables, as long
*      as you follow the requirements of the GNU GPL in regard to all of the
*      software in the executable aside from Oracle client libraries.
*
*      Specifically you are not permitted to link this program with the
*      Qt/UNIX, Qt/Windows or Qt Non Commercial products of TrollTech.
*      And you are not permitted to distribute binaries compiled against
*      these libraries without written consent from Quest Software, Inc.
*      Observe that this does not disallow linking to the Qt Free Edition.
*
*      You may link this product with any GPL'd Qt library such as Qt/Free
*
* All trademarks belong to their respective owners.
*
*****/

#include "utils.h"

#include "tomain.h"
#include "tothread.h"

#include <stdio.h>
#include <errno.h>

#include <qapplication.h>
#include <qstring.h>

#ifdef QT_THREAD_SUPPORT


void toSemaphore::up(void)
{
    Mutex.lock();
    Value++;
    if (Value > 0)
        Condition.wakeOne();
    Mutex.unlock();
}

void toSemaphore::down(void)
{
    Mutex.lock();
    while (Value <= 0)
    {
        Condition.wait(&Mutex);
    }
    Value--;
    Mutex.unlock();
}

int toSemaphore::getValue(void)
{
    Mutex.lock();
    int val = Value;
    Mutex.unlock();
    return val;
}

#else

#define SEM_ASSERT(x) if((x)!=0) toStatusMessage(\
qApp->translate("toSemaphore","The semaphore function \"%1\" failed").arg(QString::fromLatin1( #x )));

void toSemaphore::init(int ival)
{
    SEM_ASSERT(sem_init(&Semaphore, 0, ival));
}

toSemaphore::toSemaphore()
{
    init(0);
}


toSemaphore::toSemaphore(int i)
{
    init(i);
}

toSemaphore::~toSemaphore()
{
    SEM_ASSERT(sem_destroy(&Semaphore));
}

void toSemaphore::up()
{
    SEM_ASSERT(sem_post(&Semaphore));
}

void toSemaphore::down()
{
    SEM_ASSERT(sem_wait(&Semaphore));
}

int toSemaphore::getValue()
{
    int r;
    SEM_ASSERT(sem_getvalue(&Semaphore, &r));
    return r;
}

#endif

#ifndef WIN32

#include <time.h>

pthread_t toThread::MainThread = pthread_self();
toThread *toThread::DeleteThread;
toLock *toThread::Lock;

#define MUTEX_ASSERT(x) if((x)!=0) toStatusMessage(\
qApp->translate("toLock","The mutex function \"%1\" failed").arg(QString::fromLatin1( #x )));

toLock::toLock(void)
{
    MUTEX_ASSERT(pthread_mutex_init(&Mutex, NULL));
}

toLock::~toLock()
{
    MUTEX_ASSERT(pthread_mutex_destroy(&Mutex));
}

void toLock::lock ()
{
    MUTEX_ASSERT(pthread_mutex_lock(&Mutex));
}

void toLock::unlock()
{
    MUTEX_ASSERT(pthread_mutex_unlock(&Mutex));
}

#define THREAD_ASSERT(x) if((x)!=0) { \
  throw (qApp->translate("toThread","Thread function \"%1\" failed.").arg(QString::fromLatin1( #x ))); }

void toThread::initAttr()
{
    //create the thread detached, so everything is cleaned up
    //after it's finished.
    try
    {
        THREAD_ASSERT(pthread_attr_init(&ThreadAttr));
        THREAD_ASSERT(pthread_attr_setdetachstate(&ThreadAttr,
                      PTHREAD_CREATE_DETACHED));
    }
    TOCATCH
}

toThread::toThread(toTask *t)
        : Task(t)
{
    if (!Lock)
        Lock = new toLock;

    initAttr();
}

toThread::~toThread()
{
    THREAD_ASSERT(pthread_attr_destroy(&ThreadAttr));
}

void toThread::start()
{
    if (pthread_create(&(Thread),
                       &(ThreadAttr),
                       toThreadStartWrapper,
                       (void *)this) == 0)
        StartSemaphore.down();
    else
        throw qApp->translate("toThread", "Thread function \"%1\" failed.").arg("toThread::start()");
}

void toThread::startAsync()
{
    THREAD_ASSERT(pthread_create(&(Thread),
                                 &(ThreadAttr),
                                 toThreadStartWrapper,
                                 (void *)this));
}

void *toThreadStartWrapper(void *t)
{
    toThread *thread = (toThread*)t;
    //tell whoever called this->start that we're running
    sigset_t Sigs;
    sigfillset(&Sigs);
#ifndef TO_NO_PTHREADSIGMASK

    sigdelset(&Sigs, SIGHUP);
    // I hope this signal will cancel subqueries
    sigdelset(&Sigs, SIGQUIT);
    sigdelset(&Sigs, SIGALRM);
    sigdelset(&Sigs, SIGTERM);
    sigdelset(&Sigs, SIGCHLD);
    sigdelset(&Sigs, SIGWINCH);
    sigdelset(&Sigs, SIGSEGV);
    sigdelset(&Sigs, SIGPIPE);
    sigdelset(&Sigs, SIGUSR1);
    pthread_sigmask(SIG_BLOCK, &Sigs, NULL);
    sigemptyset(&Sigs);
    sigaddset(&Sigs, SIGINT);
    pthread_sigmask(SIG_UNBLOCK, &Sigs, NULL);
#endif

    try
    {
        thread->StartSemaphore.up();
        thread->Task->run();
    }
    catch (const QString &exc)
    {
        fprintf(stderr, "Unhandled exception in thread:\n%s\n", (const char *)exc);
    }
    catch (...)
    {
        fprintf(stderr, "Unhandled exception in thread:\nUnknown type\n");
    }
    delete thread->Task;
    toThread::Lock->lock ()
    ;
    if (toThread::DeleteThread)
        delete toThread::DeleteThread;
    toThread::DeleteThread = thread;
    toThread::Lock->unlock();
    return NULL;
}

bool toThread::mainThread(void)
{
    return pthread_equal(MainThread, pthread_self());
}

#ifdef QT_THREAD_SUPPORT
class toThreadWrapper : public QThread
{
public:
    static void msleep(int num)
    {
        QThread::msleep(num);
    }
};

#endif

void toThread::msleep(int msec)
{
#ifdef QT_THREAD_SUPPORT
    toThreadWrapper::msleep(msec);
#else

    struct timespec req;
    req.tv_sec = msec / 1000;
    req.tv_nsec = (msec % 1000) * 1000000;
    nanosleep(&req, &req);
#endif
}

#else

std::list<toThread *> *toThread::Threads;
toLock *toThread::Lock;
int toThread::LastID = 0;
__declspec( thread ) int toThread::ThreadID = 0;
int toThread::MainThread = -1;

bool toThread::mainThread(void)
{
    return MainThread == ThreadID;
}

toThread::toThread(toTask *task)
        : Thread(task)
{
    if (!Threads)
        Threads = new std::list<toThread *>;
    if (!Lock)
        Lock = new toLock;
}

toThread::~toThread()
{}

void toThread::start(void)
{
    Thread.start();
    Thread.StartSemaphore.down();
    Lock->lock ()
    ;
    Threads->insert(Threads->end(), this);
    Lock->unlock();
}

void toThread::startAsync(void)
{
    Thread.start();
    Lock->lock ()
    ;
    Threads->insert(Threads->end(), this);
    Lock->unlock();
}

void toThread::msleep(int msec)
{
    taskRunner::msleep(msec);
}

toThread::taskRunner::taskRunner(toTask *task)
        : Task(task)
{}

void toThread::taskRunner::run(void)
{
    try
    {
        Lock->lock ()
        ;
        StartSemaphore.up();
        toThread::LastID++;
        toThread::ThreadID = LastID;
        if (toThread::MainThread==-1)
          toThread::MainThread=LastID;
        Lock->unlock();
        Task->run();
        Lock->lock ()
        ;
        delete Task;
        Task = NULL;
        Lock->unlock();
    }
    catch (const QString &exc)
    {
        fprintf(stderr, "Unhandled exception in thread:\n%s\n", (const char *)exc);
    }
    catch (...)
    {
        fprintf(stderr, "Unhandled exception in thread:\nUnknown type\n");
    }


    // This is a cludge to clean up finnished threads, there won't be many hanging at least

    Lock->lock ()
    ;
    for (std::list<toThread *>::iterator i = toThread::Threads->begin();
            i != toThread::Threads->end();)
    {
        if ((*i)->Thread.finished())
        {
            delete (*i);
            Threads->erase(i);
            i = Threads->begin();
        }
        else
            i++;
    }
    Lock->unlock();
}

#endif