summaryrefslogtreecommitdiffstats
path: root/src/tsthread/tsthread.cpp
blob: 5bbd56658911e035fc398841b9c2f9c1368dd792 (plain)
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
/****************************************************************************

 Copyright (C) 2004 Lubos Lunak        <l.lunak@kde.org>

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.

****************************************************************************/

#include "tsthread.h"

#include <tqapplication.h>
#include <tqmetaobject.h>
#include <kdebug.h>
#include <tqguardedptr.h>

#include <assert.h>

#ifdef TS_TQTHREADSTORAGE
TQThreadStorage< TSThread** >* TSThread::current_thread;
#else
TSCurrentThread* TSThread::current_thread;
#endif
TSThread* TSThread::main_thread = NULL;

class TSMainThread
    : public TSThread
    {
    protected:
        virtual void run() { assert( false ); }
    };

TSThread::Helper::Helper( TSThread* tqparent )
    : thread( tqparent )
    {
    assert( tqparent );
    }

void TSThread::Helper::run()
    {
    thread->executeThread();
    }


TSThread::TSThread()
    : thread( this )
    , cancelling( false )
    , emit_pending( false )
    , cancel_mutex( NULL )
    , cancel_cond( NULL )
    , deleted_flag( NULL )
    {
    }

TSThread::~TSThread()
    {
    if( deleted_flag != NULL )
        *deleted_flag = true;
    }

void TSThread::start()
    {
    if( current_thread == NULL )
        initCurrentThread();
    thread.start();
    }

void TSThread::cancel()
    {
    TQMutexLocker lock( &mutex );
    cancelling = true;
    if( cancel_mutex != NULL )
        {
        TQMutexLocker lock( cancel_mutex );
        cancel_cond->wakeAll();
        }
    }

void TSThread::wait( unsigned long time )
    {
    thread.wait( time );
    }

bool TSThread::finished() const
    {
    return thread.finished();
    }

bool TSThread::running() const
    {
    return thread.running();
    }

void TSThread::initCurrentThread()
    {
#ifdef TS_TQTHREADSTORAGE
    current_thread = new TQThreadStorage< TSThread** >();
    typedef TSThread* TSThreadPtr;
    // set pointer for main thread (this must be main thread)
    current_thread->setLocalData( new TSThreadPtr( NULL )); // TODO NULL ?
#else
    current_thread = new TSCurrentThread();
    main_thread = new TSMainThread;
    // set pointer for main thread (this must be main thread)
    current_thread->setLocalData( main_thread );
#endif
    }

void TSThread::executeThread()
    {
#ifdef TS_TQTHREADSTORAGE
    // store dynamically allocated pointer, so that
    // TQThreadStorage deletes the pointer and not TSThread
    typedef TSThread* TSThreadPtr;
    current_thread->setLocalData( new TSThreadPtr( this ));
#else
    current_thread->setLocalData( this );
#endif
    run();
    postSignal( this, NULL ); // = terminated()
    }

void TSThread::postSignal( TQObject* obj, const char* signal )
    {
    assert( currentThread() == this );
    tqApp->postEvent( this, new SignalEvent( signal, obj, NULL ));
    }

void TSThread::emitSignalInternal( TQObject* obj, const char* signal, TQUObject* o )
    {
    assert( currentThread() == this );
    TQMutexLocker locker( &signal_mutex );
    emit_pending = true;
    tqApp->postEvent( this, new SignalEvent( signal, obj, o ));
    while( emit_pending )
        signal_cond.wait( &signal_mutex );
    }

void TSThread::emitCancellableSignalInternal( TQObject* obj, const char* signal, TQUObject* o )
    {
    assert( currentThread() == this );
    // can't use this->mutex, because its locking will be triggered by TSWaitCondition
    TQMutexLocker locker( &signal_mutex );
    emit_pending = true;
    tqApp->postEvent( this, new SignalEvent( signal, obj, o ));
    while( emit_pending && !testCancel())
        signal_cond.cancellableWait( &signal_mutex );
    emit_pending = false; // in case of cancel
    }

void TSThread::customEvent( TQCustomEvent* ev )
    {
    SignalEvent* e = static_cast< SignalEvent* >( ev );
    if( e->signal.isEmpty()) // = terminated()
        { // threadExecute() finishes before the actual thread terminates
        if( !finished())
            wait();
        emit terminated();
        return;
        }
    bool deleted = false;
    deleted_flag = &deleted; // this is like TQGuardedPtr for self, but faster
    int signal_id = e->object->tqmetaObject()->findSignal( normalizeSignalSlot( e->signal ).data() + 1, true );
    if( signal_id >= 0 )
        e->object->qt_emit( signal_id, e->args );
    else
        kdWarning() << "Cannot emit signal \"" << e->signal << "\"." << endl;
    if( deleted ) // some slot deleted 'this'
        return;
    deleted_flag = NULL;
    TQMutexLocker locker( &signal_mutex );
    if( emit_pending )
        {
        emit_pending = false;
        signal_cond.wakeOne();
        }
    }

#include "tsthread.moc"