PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/qt/qtbase/src/corelib/thread/qthread_win.cpp

https://gitlab.com/x33n/phantomjs
C++ | 786 lines | 590 code | 113 blank | 83 comment | 86 complexity | e1e7676ed0281a5d2d109c368a440846 MD5 | raw file
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
  4. ** Contact: http://www.qt-project.org/legal
  5. **
  6. ** This file is part of the QtCore module of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:LGPL$
  9. ** Commercial License Usage
  10. ** Licensees holding valid commercial Qt licenses may use this file in
  11. ** accordance with the commercial license agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and Digia. For licensing terms and
  14. ** conditions see http://qt.digia.com/licensing. For further information
  15. ** use the contact form at http://qt.digia.com/contact-us.
  16. **
  17. ** GNU Lesser General Public License Usage
  18. ** Alternatively, this file may be used under the terms of the GNU Lesser
  19. ** General Public License version 2.1 as published by the Free Software
  20. ** Foundation and appearing in the file LICENSE.LGPL included in the
  21. ** packaging of this file. Please review the following information to
  22. ** ensure the GNU Lesser General Public License version 2.1 requirements
  23. ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  24. **
  25. ** In addition, as a special exception, Digia gives you certain additional
  26. ** rights. These rights are described in the Digia Qt LGPL Exception
  27. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  28. **
  29. ** GNU General Public License Usage
  30. ** Alternatively, this file may be used under the terms of the GNU
  31. ** General Public License version 3.0 as published by the Free Software
  32. ** Foundation and appearing in the file LICENSE.GPL included in the
  33. ** packaging of this file. Please review the following information to
  34. ** ensure the GNU General Public License version 3.0 requirements will be
  35. ** met: http://www.gnu.org/copyleft/gpl.html.
  36. **
  37. **
  38. ** $QT_END_LICENSE$
  39. **
  40. ****************************************************************************/
  41. //#define WINVER 0x0500
  42. #if (_WIN32_WINNT < 0x0400) && !defined(Q_OS_WINRT)
  43. #define _WIN32_WINNT 0x0400
  44. #endif
  45. #include "qthread.h"
  46. #include "qthread_p.h"
  47. #include "qthreadstorage.h"
  48. #include "qmutex.h"
  49. #include <qcoreapplication.h>
  50. #include <qpointer.h>
  51. #include <private/qcoreapplication_p.h>
  52. #ifdef Q_OS_WINRT
  53. #include <private/qeventdispatcher_winrt_p.h>
  54. #else
  55. #include <private/qeventdispatcher_win_p.h>
  56. #endif
  57. #include <qt_windows.h>
  58. #ifdef Q_OS_WINRT
  59. #include <qelapsedtimer.h>
  60. #include <thread>
  61. #endif
  62. #ifndef Q_OS_WINCE
  63. #ifndef _MT
  64. #define _MT
  65. #endif
  66. #include <process.h>
  67. #else
  68. #include "qfunctions_wince.h"
  69. #endif
  70. #ifndef QT_NO_THREAD
  71. QT_BEGIN_NAMESPACE
  72. #ifndef Q_OS_WINRT
  73. void qt_watch_adopted_thread(const HANDLE adoptedThreadHandle, QThread *qthread);
  74. DWORD WINAPI qt_adopted_thread_watcher_function(LPVOID);
  75. static DWORD qt_current_thread_data_tls_index = TLS_OUT_OF_INDEXES;
  76. void qt_create_tls()
  77. {
  78. if (qt_current_thread_data_tls_index != TLS_OUT_OF_INDEXES)
  79. return;
  80. static QMutex mutex;
  81. QMutexLocker locker(&mutex);
  82. qt_current_thread_data_tls_index = TlsAlloc();
  83. }
  84. static void qt_free_tls()
  85. {
  86. if (qt_current_thread_data_tls_index != TLS_OUT_OF_INDEXES) {
  87. TlsFree(qt_current_thread_data_tls_index);
  88. qt_current_thread_data_tls_index = TLS_OUT_OF_INDEXES;
  89. }
  90. }
  91. Q_DESTRUCTOR_FUNCTION(qt_free_tls)
  92. #else // !Q_OS_WINRT
  93. __declspec(thread) static QThreadData* qt_current_thread_data_tls_index = 0;
  94. void qt_create_tls()
  95. {
  96. }
  97. static void qt_free_tls()
  98. {
  99. if (qt_current_thread_data_tls_index) {
  100. qt_current_thread_data_tls_index->deref();
  101. qt_current_thread_data_tls_index = 0;
  102. }
  103. }
  104. QThreadData* TlsGetValue(QThreadData*& tls)
  105. {
  106. Q_ASSERT(tls == qt_current_thread_data_tls_index);
  107. return tls;
  108. }
  109. void TlsSetValue(QThreadData*& tls, QThreadData* data)
  110. {
  111. Q_ASSERT(tls == qt_current_thread_data_tls_index);
  112. if (tls)
  113. tls->deref();
  114. tls = data;
  115. if (tls)
  116. tls->ref();
  117. }
  118. Q_DESTRUCTOR_FUNCTION(qt_free_tls)
  119. #endif // Q_OS_WINRT
  120. /*
  121. QThreadData
  122. */
  123. void QThreadData::clearCurrentThreadData()
  124. {
  125. TlsSetValue(qt_current_thread_data_tls_index, 0);
  126. }
  127. QThreadData *QThreadData::current(bool createIfNecessary)
  128. {
  129. qt_create_tls();
  130. QThreadData *threadData = reinterpret_cast<QThreadData *>(TlsGetValue(qt_current_thread_data_tls_index));
  131. if (!threadData && createIfNecessary) {
  132. threadData = new QThreadData;
  133. // This needs to be called prior to new AdoptedThread() to
  134. // avoid recursion.
  135. TlsSetValue(qt_current_thread_data_tls_index, threadData);
  136. QT_TRY {
  137. threadData->thread = new QAdoptedThread(threadData);
  138. } QT_CATCH(...) {
  139. TlsSetValue(qt_current_thread_data_tls_index, 0);
  140. threadData->deref();
  141. threadData = 0;
  142. QT_RETHROW;
  143. }
  144. threadData->deref();
  145. threadData->isAdopted = true;
  146. threadData->threadId = reinterpret_cast<Qt::HANDLE>(GetCurrentThreadId());
  147. if (!QCoreApplicationPrivate::theMainThread) {
  148. QCoreApplicationPrivate::theMainThread = threadData->thread;
  149. #ifndef Q_OS_WINRT
  150. // TODO: is there a way to reflect the branch's behavior using
  151. // WinRT API?
  152. } else {
  153. HANDLE realHandle = INVALID_HANDLE_VALUE;
  154. #if !defined(Q_OS_WINCE) || (defined(_WIN32_WCE) && (_WIN32_WCE>=0x600))
  155. DuplicateHandle(GetCurrentProcess(),
  156. GetCurrentThread(),
  157. GetCurrentProcess(),
  158. &realHandle,
  159. 0,
  160. FALSE,
  161. DUPLICATE_SAME_ACCESS);
  162. #else
  163. realHandle = reinterpret_cast<HANDLE>(GetCurrentThreadId());
  164. #endif
  165. qt_watch_adopted_thread(realHandle, threadData->thread);
  166. #endif // !Q_OS_WINRT
  167. }
  168. }
  169. return threadData;
  170. }
  171. void QAdoptedThread::init()
  172. {
  173. #ifndef Q_OS_WINRT
  174. d_func()->handle = GetCurrentThread();
  175. d_func()->id = GetCurrentThreadId();
  176. #else
  177. d_func()->handle = nullptr;
  178. d_func()->id = std::this_thread::get_id();
  179. #endif
  180. }
  181. #ifndef Q_OS_WINRT
  182. static QVector<HANDLE> qt_adopted_thread_handles;
  183. static QVector<QThread *> qt_adopted_qthreads;
  184. static QMutex qt_adopted_thread_watcher_mutex;
  185. static DWORD qt_adopted_thread_watcher_id = 0;
  186. static HANDLE qt_adopted_thread_wakeup = 0;
  187. /*!
  188. \internal
  189. Adds an adopted thread to the list of threads that Qt watches to make sure
  190. the thread data is properly cleaned up. This function starts the watcher
  191. thread if necessary.
  192. */
  193. void qt_watch_adopted_thread(const HANDLE adoptedThreadHandle, QThread *qthread)
  194. {
  195. QMutexLocker lock(&qt_adopted_thread_watcher_mutex);
  196. if (GetCurrentThreadId() == qt_adopted_thread_watcher_id) {
  197. #if !defined(Q_OS_WINCE) || (defined(_WIN32_WCE) && (_WIN32_WCE>=0x600))
  198. CloseHandle(adoptedThreadHandle);
  199. #endif
  200. return;
  201. }
  202. qt_adopted_thread_handles.append(adoptedThreadHandle);
  203. qt_adopted_qthreads.append(qthread);
  204. // Start watcher thread if it is not already running.
  205. if (qt_adopted_thread_watcher_id == 0) {
  206. if (qt_adopted_thread_wakeup == 0) {
  207. qt_adopted_thread_wakeup = CreateEvent(0, false, false, 0);
  208. qt_adopted_thread_handles.prepend(qt_adopted_thread_wakeup);
  209. }
  210. CloseHandle(CreateThread(0, 0, qt_adopted_thread_watcher_function, 0, 0, &qt_adopted_thread_watcher_id));
  211. } else {
  212. SetEvent(qt_adopted_thread_wakeup);
  213. }
  214. }
  215. /*
  216. This function loops and waits for native adopted threads to finish.
  217. When this happens it derefs the QThreadData for the adopted thread
  218. to make sure it gets cleaned up properly.
  219. */
  220. DWORD WINAPI qt_adopted_thread_watcher_function(LPVOID)
  221. {
  222. forever {
  223. qt_adopted_thread_watcher_mutex.lock();
  224. if (qt_adopted_thread_handles.count() == 1) {
  225. qt_adopted_thread_watcher_id = 0;
  226. qt_adopted_thread_watcher_mutex.unlock();
  227. break;
  228. }
  229. QVector<HANDLE> handlesCopy = qt_adopted_thread_handles;
  230. qt_adopted_thread_watcher_mutex.unlock();
  231. DWORD ret = WAIT_TIMEOUT;
  232. int count;
  233. int offset;
  234. int loops = handlesCopy.size() / MAXIMUM_WAIT_OBJECTS;
  235. if (handlesCopy.size() % MAXIMUM_WAIT_OBJECTS)
  236. ++loops;
  237. if (loops == 1) {
  238. // no need to loop, no timeout
  239. offset = 0;
  240. count = handlesCopy.count();
  241. ret = WaitForMultipleObjects(handlesCopy.count(), handlesCopy.constData(), false, INFINITE);
  242. } else {
  243. int loop = 0;
  244. do {
  245. offset = loop * MAXIMUM_WAIT_OBJECTS;
  246. count = qMin(handlesCopy.count() - offset, MAXIMUM_WAIT_OBJECTS);
  247. ret = WaitForMultipleObjects(count, handlesCopy.constData() + offset, false, 100);
  248. loop = (loop + 1) % loops;
  249. } while (ret == WAIT_TIMEOUT);
  250. }
  251. if (ret == WAIT_FAILED || ret >= WAIT_OBJECT_0 + uint(count)) {
  252. qWarning("QThread internal error while waiting for adopted threads: %d", int(GetLastError()));
  253. continue;
  254. }
  255. const int handleIndex = offset + ret - WAIT_OBJECT_0;
  256. if (handleIndex == 0){
  257. // New handle to watch was added.
  258. continue;
  259. } else {
  260. // printf("(qt) - qt_adopted_thread_watcher_function... called\n");
  261. const int qthreadIndex = handleIndex - 1;
  262. qt_adopted_thread_watcher_mutex.lock();
  263. QThreadData *data = QThreadData::get2(qt_adopted_qthreads.at(qthreadIndex));
  264. qt_adopted_thread_watcher_mutex.unlock();
  265. if (data->isAdopted) {
  266. QThread *thread = data->thread;
  267. Q_ASSERT(thread);
  268. QThreadPrivate *thread_p = static_cast<QThreadPrivate *>(QObjectPrivate::get(thread));
  269. Q_UNUSED(thread_p)
  270. Q_ASSERT(!thread_p->finished);
  271. thread_p->finish(thread);
  272. }
  273. data->deref();
  274. QMutexLocker lock(&qt_adopted_thread_watcher_mutex);
  275. #if !defined(Q_OS_WINCE) || (defined(_WIN32_WCE) && (_WIN32_WCE>=0x600))
  276. CloseHandle(qt_adopted_thread_handles.at(handleIndex));
  277. #endif
  278. qt_adopted_thread_handles.remove(handleIndex);
  279. qt_adopted_qthreads.remove(qthreadIndex);
  280. }
  281. }
  282. QThreadData *threadData = reinterpret_cast<QThreadData *>(TlsGetValue(qt_current_thread_data_tls_index));
  283. if (threadData)
  284. threadData->deref();
  285. return 0;
  286. }
  287. #if !defined(QT_NO_DEBUG) && defined(Q_CC_MSVC) && !defined(Q_OS_WINCE)
  288. #ifndef Q_OS_WIN64
  289. # define ULONG_PTR DWORD
  290. #endif
  291. typedef struct tagTHREADNAME_INFO
  292. {
  293. DWORD dwType; // must be 0x1000
  294. LPCSTR szName; // pointer to name (in user addr space)
  295. HANDLE dwThreadID; // thread ID (-1=caller thread)
  296. DWORD dwFlags; // reserved for future use, must be zero
  297. } THREADNAME_INFO;
  298. void qt_set_thread_name(HANDLE threadId, LPCSTR threadName)
  299. {
  300. THREADNAME_INFO info;
  301. info.dwType = 0x1000;
  302. info.szName = threadName;
  303. info.dwThreadID = threadId;
  304. info.dwFlags = 0;
  305. __try
  306. {
  307. RaiseException(0x406D1388, 0, sizeof(info)/sizeof(DWORD), (const ULONG_PTR*)&info);
  308. }
  309. __except (EXCEPTION_CONTINUE_EXECUTION)
  310. {
  311. }
  312. }
  313. #endif // !QT_NO_DEBUG && Q_CC_MSVC && !Q_OS_WINCE
  314. #endif // !Q_OS_WINRT
  315. /**************************************************************************
  316. ** QThreadPrivate
  317. *************************************************************************/
  318. #endif // QT_NO_THREAD
  319. void QThreadPrivate::createEventDispatcher(QThreadData *data)
  320. {
  321. #ifdef Q_OS_WINRT
  322. QEventDispatcherWinRT *theEventDispatcher = new QEventDispatcherWinRT;
  323. #else
  324. QEventDispatcherWin32 *theEventDispatcher = new QEventDispatcherWin32;
  325. #endif
  326. data->eventDispatcher.storeRelease(theEventDispatcher);
  327. theEventDispatcher->startingUp();
  328. }
  329. #ifndef QT_NO_THREAD
  330. unsigned int __stdcall QT_ENSURE_STACK_ALIGNED_FOR_SSE QThreadPrivate::start(void *arg)
  331. {
  332. QThread *thr = reinterpret_cast<QThread *>(arg);
  333. QThreadData *data = QThreadData::get2(thr);
  334. qt_create_tls();
  335. TlsSetValue(qt_current_thread_data_tls_index, data);
  336. data->threadId = reinterpret_cast<Qt::HANDLE>(GetCurrentThreadId());
  337. QThread::setTerminationEnabled(false);
  338. {
  339. QMutexLocker locker(&thr->d_func()->mutex);
  340. data->quitNow = thr->d_func()->exited;
  341. }
  342. if (data->eventDispatcher.load()) // custom event dispatcher set?
  343. data->eventDispatcher.load()->startingUp();
  344. else
  345. createEventDispatcher(data);
  346. #if !defined(QT_NO_DEBUG) && defined(Q_CC_MSVC) && !defined(Q_OS_WINCE) && !defined(Q_OS_WINRT)
  347. // sets the name of the current thread.
  348. QByteArray objectName = thr->objectName().toLocal8Bit();
  349. qt_set_thread_name((HANDLE)-1,
  350. objectName.isEmpty() ?
  351. thr->metaObject()->className() : objectName.constData());
  352. #endif
  353. emit thr->started(QThread::QPrivateSignal());
  354. QThread::setTerminationEnabled(true);
  355. thr->run();
  356. finish(arg);
  357. return 0;
  358. }
  359. void QThreadPrivate::finish(void *arg, bool lockAnyway)
  360. {
  361. QThread *thr = reinterpret_cast<QThread *>(arg);
  362. QThreadPrivate *d = thr->d_func();
  363. QMutexLocker locker(lockAnyway ? &d->mutex : 0);
  364. d->isInFinish = true;
  365. d->priority = QThread::InheritPriority;
  366. void **tls_data = reinterpret_cast<void **>(&d->data->tls);
  367. locker.unlock();
  368. emit thr->finished(QThread::QPrivateSignal());
  369. QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete);
  370. QThreadStorageData::finish(tls_data);
  371. locker.relock();
  372. QAbstractEventDispatcher *eventDispatcher = d->data->eventDispatcher.load();
  373. if (eventDispatcher) {
  374. d->data->eventDispatcher = 0;
  375. locker.unlock();
  376. eventDispatcher->closingDown();
  377. delete eventDispatcher;
  378. locker.relock();
  379. }
  380. d->running = false;
  381. d->finished = true;
  382. d->isInFinish = false;
  383. d->interruptionRequested = false;
  384. if (!d->waiters) {
  385. #ifndef Q_OS_WINRT
  386. CloseHandle(d->handle);
  387. #else
  388. d->handle->detach();
  389. delete d->handle;
  390. #endif
  391. d->handle = 0;
  392. }
  393. #ifndef Q_OS_WINRT
  394. d->id = 0;
  395. #else
  396. d->id = std::thread::id();
  397. #endif
  398. }
  399. /**************************************************************************
  400. ** QThread
  401. *************************************************************************/
  402. Qt::HANDLE QThread::currentThreadId() Q_DECL_NOTHROW
  403. {
  404. return reinterpret_cast<Qt::HANDLE>(GetCurrentThreadId());
  405. }
  406. int QThread::idealThreadCount() Q_DECL_NOTHROW
  407. {
  408. SYSTEM_INFO sysinfo;
  409. #ifndef Q_OS_WINRT
  410. GetSystemInfo(&sysinfo);
  411. #else
  412. GetNativeSystemInfo(&sysinfo);
  413. #endif
  414. return sysinfo.dwNumberOfProcessors;
  415. }
  416. #ifndef Q_OS_WINRT
  417. void QThread::yieldCurrentThread()
  418. {
  419. #ifndef Q_OS_WINCE
  420. SwitchToThread();
  421. #else
  422. ::Sleep(0);
  423. #endif
  424. }
  425. void QThread::sleep(unsigned long secs)
  426. {
  427. ::Sleep(secs * 1000);
  428. }
  429. void QThread::msleep(unsigned long msecs)
  430. {
  431. ::Sleep(msecs);
  432. }
  433. void QThread::usleep(unsigned long usecs)
  434. {
  435. ::Sleep((usecs / 1000) + 1);
  436. }
  437. #else // !Q_OS_WINRT
  438. void QThread::yieldCurrentThread()
  439. {
  440. msleep(1);
  441. }
  442. void QThread::sleep(unsigned long secs)
  443. {
  444. msleep(secs * 1000);
  445. }
  446. void QThread::msleep(unsigned long msecs)
  447. {
  448. WaitForSingleObjectEx(GetCurrentThread(), msecs, FALSE);
  449. }
  450. void QThread::usleep(unsigned long usecs)
  451. {
  452. msleep((usecs / 1000) + 1);
  453. }
  454. #endif // Q_OS_WINRT
  455. void QThread::start(Priority priority)
  456. {
  457. Q_D(QThread);
  458. QMutexLocker locker(&d->mutex);
  459. if (d->isInFinish) {
  460. locker.unlock();
  461. wait();
  462. locker.relock();
  463. }
  464. if (d->running)
  465. return;
  466. d->running = true;
  467. d->finished = false;
  468. d->exited = false;
  469. d->returnCode = 0;
  470. d->interruptionRequested = false;
  471. #ifndef Q_OS_WINRT
  472. /*
  473. NOTE: we create the thread in the suspended state, set the
  474. priority and then resume the thread.
  475. since threads are created with normal priority by default, we
  476. could get into a case where a thread (with priority less than
  477. NormalPriority) tries to create a new thread (also with priority
  478. less than NormalPriority), but the newly created thread preempts
  479. its 'parent' and runs at normal priority.
  480. */
  481. d->handle = (Qt::HANDLE) _beginthreadex(NULL, d->stackSize, QThreadPrivate::start,
  482. this, CREATE_SUSPENDED, &(d->id));
  483. if (!d->handle) {
  484. qErrnoWarning(errno, "QThread::start: Failed to create thread");
  485. d->running = false;
  486. d->finished = true;
  487. return;
  488. }
  489. int prio;
  490. d->priority = priority;
  491. switch (d->priority) {
  492. case IdlePriority:
  493. prio = THREAD_PRIORITY_IDLE;
  494. break;
  495. case LowestPriority:
  496. prio = THREAD_PRIORITY_LOWEST;
  497. break;
  498. case LowPriority:
  499. prio = THREAD_PRIORITY_BELOW_NORMAL;
  500. break;
  501. case NormalPriority:
  502. prio = THREAD_PRIORITY_NORMAL;
  503. break;
  504. case HighPriority:
  505. prio = THREAD_PRIORITY_ABOVE_NORMAL;
  506. break;
  507. case HighestPriority:
  508. prio = THREAD_PRIORITY_HIGHEST;
  509. break;
  510. case TimeCriticalPriority:
  511. prio = THREAD_PRIORITY_TIME_CRITICAL;
  512. break;
  513. case InheritPriority:
  514. default:
  515. prio = GetThreadPriority(GetCurrentThread());
  516. break;
  517. }
  518. if (!SetThreadPriority(d->handle, prio)) {
  519. qErrnoWarning("QThread::start: Failed to set thread priority");
  520. }
  521. if (ResumeThread(d->handle) == (DWORD) -1) {
  522. qErrnoWarning("QThread::start: Failed to resume new thread");
  523. }
  524. #else // !Q_OS_WINRT
  525. d->handle = new std::thread(QThreadPrivate::start, this);
  526. if (!d->handle) {
  527. qErrnoWarning(errno, "QThread::start: Failed to create thread");
  528. d->running = false;
  529. d->finished = true;
  530. return;
  531. }
  532. d->id = d->handle->get_id();
  533. if (priority != NormalPriority || priority != InheritPriority) {
  534. qWarning("QThread::start: Failed to set thread priority (not implemented)");
  535. d->priority = NormalPriority;
  536. }
  537. #endif // Q_OS_WINRT
  538. }
  539. void QThread::terminate()
  540. {
  541. Q_D(QThread);
  542. QMutexLocker locker(&d->mutex);
  543. if (!d->running)
  544. return;
  545. if (!d->terminationEnabled) {
  546. d->terminatePending = true;
  547. return;
  548. }
  549. #ifndef Q_OS_WINRT
  550. TerminateThread(d->handle, 0);
  551. #else // !Q_OS_WINRT
  552. qWarning("QThread::terminate: Terminate is not supported on WinRT");
  553. #endif // Q_OS_WINRT
  554. QThreadPrivate::finish(this, false);
  555. }
  556. bool QThread::wait(unsigned long time)
  557. {
  558. Q_D(QThread);
  559. QMutexLocker locker(&d->mutex);
  560. #ifndef Q_OS_WINRT
  561. if (d->id == GetCurrentThreadId()) {
  562. #else
  563. if (d->id == std::this_thread::get_id()) {
  564. #endif
  565. qWarning("QThread::wait: Thread tried to wait on itself");
  566. return false;
  567. }
  568. if (d->finished || !d->running)
  569. return true;
  570. ++d->waiters;
  571. locker.mutex()->unlock();
  572. bool ret = false;
  573. #ifndef Q_OS_WINRT
  574. switch (WaitForSingleObject(d->handle, time)) {
  575. case WAIT_OBJECT_0:
  576. ret = true;
  577. break;
  578. case WAIT_FAILED:
  579. qErrnoWarning("QThread::wait: Thread wait failure");
  580. break;
  581. case WAIT_ABANDONED:
  582. case WAIT_TIMEOUT:
  583. default:
  584. break;
  585. }
  586. #else // !Q_OS_WINRT
  587. if (!d->finished) {
  588. QElapsedTimer timer;
  589. timer.start();
  590. while (timer.elapsed() < time && !d->finished)
  591. yieldCurrentThread();
  592. }
  593. #endif // Q_OS_WINRT
  594. locker.mutex()->lock();
  595. --d->waiters;
  596. if (ret && !d->finished) {
  597. // thread was terminated by someone else
  598. QThreadPrivate::finish(this, false);
  599. }
  600. if (d->finished && !d->waiters) {
  601. #ifndef Q_OS_WINRT
  602. CloseHandle(d->handle);
  603. #else
  604. d->handle->detach();
  605. delete d->handle;
  606. #endif
  607. d->handle = 0;
  608. }
  609. return ret;
  610. }
  611. void QThread::setTerminationEnabled(bool enabled)
  612. {
  613. QThread *thr = currentThread();
  614. Q_ASSERT_X(thr != 0, "QThread::setTerminationEnabled()",
  615. "Current thread was not started with QThread.");
  616. QThreadPrivate *d = thr->d_func();
  617. QMutexLocker locker(&d->mutex);
  618. d->terminationEnabled = enabled;
  619. if (enabled && d->terminatePending) {
  620. QThreadPrivate::finish(thr, false);
  621. locker.unlock(); // don't leave the mutex locked!
  622. #ifndef Q_OS_WINRT
  623. _endthreadex(0);
  624. #endif
  625. }
  626. }
  627. // Caller must hold the mutex
  628. void QThreadPrivate::setPriority(QThread::Priority threadPriority)
  629. {
  630. #ifndef Q_OS_WINRT
  631. // copied from start() with a few modifications:
  632. int prio;
  633. priority = threadPriority;
  634. switch (priority) {
  635. case QThread::IdlePriority:
  636. prio = THREAD_PRIORITY_IDLE;
  637. break;
  638. case QThread::LowestPriority:
  639. prio = THREAD_PRIORITY_LOWEST;
  640. break;
  641. case QThread::LowPriority:
  642. prio = THREAD_PRIORITY_BELOW_NORMAL;
  643. break;
  644. case QThread::NormalPriority:
  645. prio = THREAD_PRIORITY_NORMAL;
  646. break;
  647. case QThread::HighPriority:
  648. prio = THREAD_PRIORITY_ABOVE_NORMAL;
  649. break;
  650. case QThread::HighestPriority:
  651. prio = THREAD_PRIORITY_HIGHEST;
  652. break;
  653. case QThread::TimeCriticalPriority:
  654. prio = THREAD_PRIORITY_TIME_CRITICAL;
  655. break;
  656. case QThread::InheritPriority:
  657. default:
  658. qWarning("QThread::setPriority: Argument cannot be InheritPriority");
  659. return;
  660. }
  661. if (!SetThreadPriority(handle, prio)) {
  662. qErrnoWarning("QThread::setPriority: Failed to set thread priority");
  663. }
  664. #else // !Q_OS_WINRT
  665. if (priority != threadPriority) {
  666. qWarning("QThread::setPriority: Failed to set thread priority (not implemented)");
  667. return;
  668. }
  669. #endif // Q_OS_WINRT
  670. }
  671. QT_END_NAMESPACE
  672. #endif // QT_NO_THREAD