Add the quitlock feature to QThread.
[profile/ivi/qtbase.git] / src / corelib / thread / qthread.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QTHREAD_H
43 #define QTHREAD_H
44
45 #include <QtCore/qobject.h>
46
47 #include <limits.h>
48
49 QT_BEGIN_HEADER
50
51 QT_BEGIN_NAMESPACE
52
53
54 class QThreadData;
55 class QThreadPrivate;
56 class QAbstractEventDispatcher;
57
58 #ifndef QT_NO_THREAD
59 class Q_CORE_EXPORT QThread : public QObject
60 {
61 public:
62     static Qt::HANDLE currentThreadId();
63     static QThread *currentThread();
64     static int idealThreadCount();
65     static void yieldCurrentThread();
66
67     explicit QThread(QObject *parent = 0);
68     ~QThread();
69
70     enum Priority {
71         IdlePriority,
72
73         LowestPriority,
74         LowPriority,
75         NormalPriority,
76         HighPriority,
77         HighestPriority,
78
79         TimeCriticalPriority,
80
81         InheritPriority
82     };
83
84     void setPriority(Priority priority);
85     Priority priority() const;
86
87     bool isFinished() const;
88     bool isRunning() const;
89
90     void setStackSize(uint stackSize);
91     uint stackSize() const;
92
93     void exit(int retcode = 0);
94
95     QAbstractEventDispatcher *eventDispatcher() const;
96     void setEventDispatcher(QAbstractEventDispatcher *eventDispatcher);
97
98     bool event(QEvent *event);
99
100 public Q_SLOTS:
101     void start(Priority = InheritPriority);
102     void terminate();
103     void quit();
104
105 public:
106     // default argument causes thread to block indefinately
107     bool wait(unsigned long time = ULONG_MAX);
108
109     static void sleep(unsigned long);
110     static void msleep(unsigned long);
111     static void usleep(unsigned long);
112
113 Q_SIGNALS:
114     void started();
115     void finished();
116     void terminated();
117
118 protected:
119     virtual void run();
120     int exec();
121
122     static void setTerminationEnabled(bool enabled = true);
123
124 protected:
125     QThread(QThreadPrivate &dd, QObject *parent = 0);
126
127 private:
128     Q_OBJECT
129     Q_DECLARE_PRIVATE(QThread)
130
131     static void initialize();
132     static void cleanup();
133
134     friend class QCoreApplication;
135     friend class QThreadData;
136 };
137
138 #else // QT_NO_THREAD
139
140 class Q_CORE_EXPORT QThread : public QObject
141 {
142 public:
143     static Qt::HANDLE currentThreadId() { return Qt::HANDLE(currentThread()); }
144     static QThread* currentThread();
145     
146 protected:
147     QThread(QThreadPrivate &dd, QObject *parent = 0);
148
149 private:
150     explicit QThread(QObject *parent = 0);
151     static QThread *instance;
152
153     friend class QCoreApplication;
154     friend class QThreadData;
155     friend class QAdoptedThread;
156     Q_DECLARE_PRIVATE(QThread)
157 };
158
159 #endif // QT_NO_THREAD
160
161 QT_END_NAMESPACE
162
163 QT_END_HEADER
164
165 #endif // QTHREAD_H