summaryrefslogtreecommitdiffstats
path: root/src/tsthread/tsthread.h
blob: 8468db858eceee837e0177906bc56ce53d3f1344 (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
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
384
385
386
387
388
/****************************************************************************

 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.

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

#ifndef TSTHREAD_H
#define TSTHREAD_H

#include <tqobject.h>
#include <tqthread.h>
#include <tqwaitcondition.h>
#include <tqdeepcopy.h>
#include <tqmutex.h>
#include <tqucomextra_p.h>

#ifdef TS_TQTHREADSTORAGE
#include <tqthreadstorage.h>
#else
#include <pthread.h>
#endif

#include "tswaitcondition.h"

// how difficult ...
template< typename T >
T TSDeepCopy( const T& t )
{
    return TQDeepCopy< T >( t );
}

class TSCurrentThread;

/**
 Thread class, internally based on TQThread, which intentionally doesn't have
 dangerous crap like TQThread::terminate() and intentionally has useful features
 like emitting signals to the main thread, currentThread() or support
 for cancelling.
 */
class TSThread
    : public TQObject
    {
    Q_OBJECT
  TQ_OBJECT
    public:
        TSThread();
        virtual ~TSThread();
        /**
         * Starts the thread.
         * @see TQThread::start()
         */
        void start();
        /**
         * Waits for the thread to finish.
         * @see TQThread::wait()
         */
        void wait( unsigned long time = ULONG_MAX );
        /**
         * Returns true if the thread has finished.
         * @see TQThread::finished()
         */
        bool finished() const;
        /**
         * Returns true if the thread is running.
         * @see TQThread::running()
         */
        bool running() const;
        /**
         * Sends the thread a request to terminate.
         * The thread must check for cancellation using testCancel()
         * and finish execution (return from run()).
         * @see testCancel()
         */
        void cancel();
        // TODO suspend + resume?
        /**
         * Returns true if a request to terminate is pending.
         * @see cancel()
         */
        bool testCancel() const;
        /**
         * Returns pointer to the current thread, i.e. thread from which
         * this function is called.
         */
        static TSThread* currentThread();
        /**
         * Returns a pointer to the main thread. Mostly useful for currentThread().
         */
        static TSThread* mainThread();
        /**
         * Emits the specified signal in the main thread. This function returns
         * only after all slots connected to the signal have been executed (i.e.
         * it works like normal signal). The signal can have one pointer argument,
         * which can be used for communication in either direction. TQObject::sender()
         * in slots is valid.
         * Example:
         * \code
         *   emitSignal( this, TQT_SIGNAL( result( int* )), &result_data );
         * \endcode
         * @see postSignal
         * @see emitCancellableSignal
         */
        void emitSignal( TQObject* obj, const char* signal );
        template< typename T1 >
        void emitSignal( TQObject* obj, const char* signal, const T1& p1 );
        template< typename T1, typename T2 >
        void emitSignal( TQObject* obj, const char* signal, const T1& p1, const T2& p2 );
        /**
         * This function works like emitSignal(), but additionally acts as a cancellation
         * point, i.e. calling cancel() on the thread causes premature return.
         * @see emitSignal
         * @see postSignal
         */        
        void emitCancellableSignal( TQObject* obj, const char* signal );
        template< typename T1 >
        void emitCancellableSignal( TQObject* obj, const char* signal, const T1& p1 );
        template< typename T1, typename T2 >
        void emitCancellableSignal( TQObject* obj, const char* signal, const T1& p1, const T2& p2 );
        /**
         * Posts (i.e. it is not executed immediatelly like normal signals)
         * a signal to be emitted in the main thread. The signal cannot
         * have any parameters, use your TSThread derived class instance
         * data members instead. TQObject::sender() in slots is valid, unless
         * the thread instance is destroyed before the signal is processed.
         * @see emitSignal
         */
        void postSignal( TQObject* obj, const char* signal ); // is emitted _always_ in main thread
    protected:
        /**
         * The code to be executed in the started thread.
         * @see TQThread::run()
         */
        virtual void run() = 0;
    signals:
        /**
         * Emitted after the thread is terminated.
         */
        void terminated(); // is emitted _always_ in main thread
    protected:
        /**
         * @internal
         */
        void customEvent( TQCustomEvent* e );
    private:
        class SignalEvent
            : public TQCustomEvent
            {
            public:
                SignalEvent( const char* sig, TQObject* obj, TQUObject* o )
                    : TQCustomEvent( TQEvent::User ), signal( sig ), object( obj ), args( o )
                    {
                    }
                const TQCString signal;
                TQObject* object;
                TQUObject* args;
            };
        class Helper
            : public TQThread
            {
            public:
                Helper( TSThread* parent );
            protected:
                virtual void run();
            private:
                TSThread* thread;
            };
        void executeThread();
        static void initCurrentThread();
        bool setCancelData( TQMutex*m, TQWaitCondition* c );
        void setSignalData( TQUObject* o, int i );
        void setSignalData( TQUObject* o, const TQImage& i );
        void setSignalData( TQUObject* o, const TQString& s );
        void setSignalData( TQUObject* o, bool b );
        void setSignalData( TQUObject* o, const TQColor& c );
        void setSignalData( TQUObject* o, const char* s );
        void setSignalData( TQUObject* o, const TQSize& );
        void emitSignalInternal( TQObject* obj, const char* signal, TQUObject* o );
        void emitCancellableSignalInternal( TQObject* obj, const char* signal, TQUObject* o );
        friend class Helper;
        friend class TSWaitCondition;
        Helper thread;
        bool cancelling;
        bool emit_pending;
        mutable TQMutex mutex;
        TQMutex signal_mutex;
        TSWaitCondition signal_cond;
        TQMutex* cancel_mutex;
        TQWaitCondition* cancel_cond;
        bool* deleted_flag;
#ifdef TS_TQTHREADSTORAGE
        static TQThreadStorage< TSThread** >* current_thread;
#else
        static TSCurrentThread* current_thread;
#endif
        static TSThread* main_thread;
    private:
        TSThread( const TSThread& );
        TSThread& operator=( const TSThread& );
    };

#ifndef TS_TQTHREADSTORAGE
/**
 * @internal
 */
class TSCurrentThread
    {
    public:
        TSCurrentThread();
        ~TSCurrentThread();
        TSThread* localData() const;
        void setLocalData( TSThread* t );
    private:
        pthread_key_t key;
    };


inline TSCurrentThread::TSCurrentThread()
    {
    pthread_key_create( &key, NULL );
    }

inline TSCurrentThread::~TSCurrentThread()
    {
    pthread_key_delete( key );
    }

inline void TSCurrentThread::setLocalData( TSThread* t )
    {
    pthread_setspecific( key, t );
    }

inline TSThread* TSCurrentThread::localData() const
    {
    return static_cast< TSThread* >( pthread_getspecific( key ));
    }
#endif

inline
bool TSThread::testCancel() const
    {
    TQMutexLocker lock( &mutex );
    return cancelling;
    }

#include <kdebug.h>
inline
bool TSThread::setCancelData( TQMutex* m, TQWaitCondition* c )
    {
    TQMutexLocker lock( &mutex );
    if( cancelling && m != NULL )
        return false;
    cancel_mutex = m;
    cancel_cond = c;
    return true;
    }

inline
TSThread* TSThread::currentThread()
    {
    if( current_thread == NULL )
        initCurrentThread();
#ifdef TS_TQTHREADSTORAGE
    return *current_thread->localData();
#else
    return current_thread->localData();
#endif
    }

inline
TSThread* TSThread::mainThread()
    {
    return main_thread;
    }

inline
void TSThread::setSignalData( TQUObject* o, int i )
    {
    static_TQUType_int.set( o, i );
    }

inline
void TSThread::setSignalData( TQUObject* o, const TQImage& i )
    {
    static_TQUType_varptr.set( o, &i );
    }

inline
void TSThread::setSignalData( TQUObject* o, const TQString& s )
    {
    static_TQUType_TQString.set( o, s );
    }

inline
void TSThread::setSignalData( TQUObject* o, bool b )
    {
    static_TQUType_bool.set( o, b );
    }

inline
void TSThread::setSignalData( TQUObject* o, const TQColor& c )
    {
    static_TQUType_varptr.set( o, &c );
    }

inline
void TSThread::setSignalData( TQUObject* o, const char* s )
    {
    static_TQUType_charstar.set( o, s );
    }

inline
void TSThread::setSignalData( TQUObject* o, const TQSize& s )
    {
    static_TQUType_varptr.set( o, &s );
    }

inline
void TSThread::emitSignal( TQObject* obj, const char* signal )
    {
    TQUObject o[ 1 ];
    emitSignalInternal( obj, signal, o );
    }

template< typename T1 >
inline
void TSThread::emitSignal( TQObject* obj, const char* signal, const T1& p1 )
    {
    TQUObject o[ 2 ];
    setSignalData( o + 1, p1 );
    emitSignalInternal( obj, signal, o );
    }

template< typename T1, typename T2 >
inline
void TSThread::emitSignal( TQObject* obj, const char* signal, const T1& p1, const T2& p2 )
    {
    TQUObject o[ 3 ];
    setSignalData( o + 1, p1 );
    setSignalData( o + 2, p2 );
    emitSignalInternal( obj, signal, o );
    }

inline
void TSThread::emitCancellableSignal( TQObject* obj, const char* signal )
    {
    TQUObject o[ 1 ];
    emitCancellableSignalInternal( obj, signal, o );
    }

template< typename T1 >
inline
void TSThread::emitCancellableSignal( TQObject* obj, const char* signal, const T1& p1 )
    {
    TQUObject o[ 2 ];
    setSignalData( o + 1, p1 );
    emitCancellableSignalInternal( obj, signal, o );
    }

template< typename T1, typename T2 >
inline
void TSThread::emitCancellableSignal( TQObject* obj, const char* signal, const T1& p1, const T2& p2 )
    {
    TQUObject o[ 3 ];
    setSignalData( o + 1, p1 );
    setSignalData( o + 2, p2 );
    emitCancellableSignalInternal( obj, signal, o );
    }


#endif