qthread_win32.cpp
Go to the documentation of this file.
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 
50  running(FALSE), finished(FALSE), terminated(FALSE), stackSize(0)
51 {
52  handle = NULL;
53  waiters = 0;
54 }
55 
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;
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;
142  }
143 
144  if (d->finished && d->waiters)
145  {
146  CloseHandle(d->handle);
147  d->handle = 0;
148  }
149 }
150 
152 {
153  SYSTEM_INFO sysinfo;
154  GetSystemInfo(&sysinfo);
155  return sysinfo.dwNumberOfProcessors;
156 }
157 
158 
static void * start(void *arg)
void lock()
Definition: qmutex.cpp:58
G4double thr[100]
Definition: G4S2Light.cc:59
const bool FALSE
Definition: qglobal.h:370
void start()
static void finish(void *arg)
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
int errno
Contains the last error code.
Definition: structcmd.h:53
virtual void started()
Definition: qthread.h:65
virtual void finished()
Definition: qthread.h:66
virtual void run()
Definition: qthread.h:70
virtual void terminated()
Definition: qthread.h:67
void unlock()
Definition: qmutex.cpp:85
QThreadPrivate * d
Definition: qthread.h:73
QMutex mutex
Definition: qthread_p.h:63
void terminate()
uint stackSize
Definition: qthread_p.h:68
void wait()
const bool TRUE
Definition: qglobal.h:371
bool terminated
Definition: qthread_p.h:67
static int idealThreadCount()