File: WorkerThread.h

package info (click to toggle)
kwave 25.04.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,272 kB
  • sloc: cpp: 56,173; xml: 817; perl: 688; sh: 57; makefile: 11
file content (109 lines) | stat: -rw-r--r-- 3,431 bytes parent folder | download
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
/***************************************************************************
    libkwave/WorkerThread.h  -  worker thread for Kwave
                             -------------------
    begin                : Sun Apr 06 2008
    copyright            : (C) 2008 by Thomas Eschenbacher
    email                : Thomas.Eschenbacher@gmx.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef WORKER_THREAD_H
#define WORKER_THREAD_H

#include "config.h"
#include "libkwave_export.h"

#include <pthread.h>

#include <QtGlobal>
#include <QMutex>
#include <QObject>
#include <QThread>
#include <QVariant>

namespace Kwave
{
    class Runnable;

    class LIBKWAVE_EXPORT WorkerThread: public QThread
    {
        Q_OBJECT
    public:

        /** Constructor */
        explicit WorkerThread(Kwave::Runnable *runnable, QVariant params);

        /** Destructor, calls stop() if the thread is still running. */
        virtual ~WorkerThread() override;

        /**
         * Stops the thread execution. Please note that you <b>MUST</b> call
         * this function at the end if you derived a class from this one.
         * @param timeout the timeout in milliseconds, default = 10s
         * @return zero if successful or an error code if failed
         * @see errno.h
         */
        virtual int stop(unsigned int timeout = 10000);

        /**
         * A wrapper for the run() function, calls the run_wrapper(...)
         * of m_runnable with the parameters passed in the constructor.
         */
        void run() override;

    public slots:

        /**
         * Can be connected to a progress dialog to cancel the current
         * operation.
         */
        virtual void cancel();

    signals:

        /**
         * emitted when cancel() is called, can be connected
         * to the cancel() slot of child objects
         */
        void sigCancel();

    private:

        /** pointer to the object that has a run() function */
        Kwave::Runnable *m_runnable;

        /** parameter pointer passed to the run() function */
        QVariant m_params;

        /** Mutex to control access to the thread itself */
        QMutex m_lock;

        /** Mutex for protecting SIGHUP <-> thread exit */
        QMutex m_lock_sighup;

        /**
         * POSIX compatible thread ID of the worker thread.
         * only needed and only valid while the thread is running.
         * (needs a POSIX 1003.1-2001 system libc)
         */
        pthread_t m_tid;

        /** POSIX compatible thread ID of the owner thread. */
        pthread_t m_owner_tid;

    };

}

#endif /* WORKER_THREAD_H */

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