Root/
Source at commit 1322 created 12 years 8 months ago. By meklort, Add doxygen to utils folder | |
---|---|
1 | /****************************************************************************␊ |
2 | **␊ |
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).␊ |
4 | ** Contact: Nokia Corporation (qt-info@nokia.com)␊ |
5 | **␊ |
6 | ** This file is part of the QtCore module of the Qt Toolkit.␊ |
7 | **␊ |
8 | ** $QT_BEGIN_LICENSE:LGPL$␊ |
9 | ** No Commercial Usage␊ |
10 | ** This file contains pre-release code and may not be distributed.␊ |
11 | ** You may use this file in accordance with the terms and conditions␊ |
12 | ** contained in the either Technology Preview License Agreement or the␊ |
13 | ** Beta Release License Agreement.␊ |
14 | **␊ |
15 | ** GNU Lesser General Public License Usage␊ |
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser␊ |
17 | ** General Public License version 2.1 as published by the Free Software␊ |
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the␊ |
19 | ** packaging of this file. Please review the following information to␊ |
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements␊ |
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.␊ |
22 | **␊ |
23 | ** In addition, as a special exception, Nokia gives you certain␊ |
24 | ** additional rights. These rights are described in the Nokia Qt LGPL␊ |
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this␊ |
26 | ** package.␊ |
27 | **␊ |
28 | ** GNU General Public License Usage␊ |
29 | ** Alternatively, this file may be used under the terms of the GNU␊ |
30 | ** General Public License version 3.0 as published by the Free Software␊ |
31 | ** Foundation and appearing in the file LICENSE.GPL included in the␊ |
32 | ** packaging of this file. Please review the following information to␊ |
33 | ** ensure the GNU General Public License version 3.0 requirements will be␊ |
34 | ** met: http://www.gnu.org/copyleft/gpl.html.␊ |
35 | **␊ |
36 | ** If you are unsure which license is appropriate for your use, please␊ |
37 | ** contact the sales department at http://www.qtsoftware.com/contact.␊ |
38 | ** $QT_END_LICENSE$␊ |
39 | **␊ |
40 | ****************************************************************************/␊ |
41 | ␊ |
42 | #include "qthread.h"␊ |
43 | #include "qthread_p.h"␊ |
44 | ␊ |
45 | /**************************************************************************␊ |
46 | ** QThreadPrivate␊ |
47 | *************************************************************************/␊ |
48 | ␊ |
49 | QThreadPrivate::QThreadPrivate() :␊ |
50 | running(FALSE), finished(FALSE), terminated(FALSE), stackSize(0)␊ |
51 | {␊ |
52 | handle = NULL;␊ |
53 | waiters = 0;␊ |
54 | }␊ |
55 | ␊ |
56 | QThreadPrivate::~QThreadPrivate()␊ |
57 | {␊ |
58 | }␊ |
59 | ␊ |
60 | unsigned int __stdcall QThreadPrivate::start(void *arg)␊ |
61 | {␊ |
62 | QThread *thr = reinterpret_cast<QThread *>(arg);␊ |
63 | thr->started();␊ |
64 | thr->run();␊ |
65 | finish(arg);␊ |
66 | return 0;␊ |
67 | }␊ |
68 | ␊ |
69 | void QThreadPrivate::finish(void *arg,bool lockAnyway)␊ |
70 | {␊ |
71 | QThread *thr = reinterpret_cast<QThread *>(arg);␊ |
72 | QThreadPrivate *d = thr->d;␊ |
73 | ␊ |
74 | if (lockAnyway) d->mutex.lock();␊ |
75 | ␊ |
76 | d->running = FALSE;␊ |
77 | d->finished = TRUE;␊ |
78 | if (d->terminated) thr->terminated();␊ |
79 | d->terminated = FALSE;␊ |
80 | thr->finished();␊ |
81 | ␊ |
82 | if (!d->waiters)␊ |
83 | {␊ |
84 | CloseHandle(d->handle);␊ |
85 | d->handle = 0;␊ |
86 | }␊ |
87 | ␊ |
88 | if (lockAnyway) d->mutex.unlock();␊ |
89 | }␊ |
90 | ␊ |
91 | /**************************************************************************␊ |
92 | ** QThread␊ |
93 | *************************************************************************/␊ |
94 | ␊ |
95 | void QThread::start()␊ |
96 | {␊ |
97 | QMutexLocker locker(&d->mutex);␊ |
98 | ␊ |
99 | if (d->running) return;␊ |
100 | ␊ |
101 | d->running = TRUE;␊ |
102 | d->finished = FALSE;␊ |
103 | d->terminated = FALSE;␊ |
104 | ␊ |
105 | d->handle = CreateThread(NULL,d->stackSize,␊ |
106 | (LPTHREAD_START_ROUTINE)QThreadPrivate::start,this,0,NULL);␊ |
107 | ␊ |
108 | if (!d->handle) ␊ |
109 | {␊ |
110 | qWarning("QThread::start: Failed to create thread: errno=%d",errno);␊ |
111 | d->running = FALSE;␊ |
112 | d->finished = TRUE;␊ |
113 | return;␊ |
114 | }␊ |
115 | }␊ |
116 | ␊ |
117 | void QThread::terminate()␊ |
118 | {␊ |
119 | QMutexLocker locker(&d->mutex);␊ |
120 | if (!d->running) return;␊ |
121 | TerminateThread(d->handle, 0);␊ |
122 | d->terminated = TRUE;␊ |
123 | QThreadPrivate::finish(this);␊ |
124 | }␊ |
125 | ␊ |
126 | void QThread::wait()␊ |
127 | {␊ |
128 | QMutexLocker locker(&d->mutex);␊ |
129 | if (d->finished || !d->running) return;␊ |
130 | ␊ |
131 | ++d->waiters;␊ |
132 | locker.mutex()->unlock();␊ |
133 | ␊ |
134 | WaitForSingleObject(d->handle,INFINITE);␊ |
135 | ␊ |
136 | locker.mutex()->lock();␊ |
137 | --d->waiters;␊ |
138 | if (!d->finished) // thread was terminated by someone else␊ |
139 | {␊ |
140 | d->terminated = TRUE;␊ |
141 | QThreadPrivate::finish(this);␊ |
142 | }␊ |
143 | ␊ |
144 | if (d->finished && d->waiters)␊ |
145 | {␊ |
146 | CloseHandle(d->handle);␊ |
147 | d->handle = 0;␊ |
148 | }␊ |
149 | }␊ |
150 | ␊ |
151 | int QThread::idealThreadCount()␊ |
152 | {␊ |
153 | SYSTEM_INFO sysinfo;␊ |
154 | GetSystemInfo(&sysinfo);␊ |
155 | return sysinfo.dwNumberOfProcessors;␊ |
156 | }␊ |
157 | ␊ |
158 | ␊ |
159 |