void run() { quint64 i = 0; while (t.elapsed() < one_minute) { i++; uint nb = (i * 9 + lockCount.load() * 13) % threadCount; QMutexLocker locker(&mutex[nb]); if (sentinel[nb].load()) errorCount.ref(); if (sentinel[nb].fetchAndAddRelaxed(5)) errorCount.ref(); if (!sentinel[nb].testAndSetRelaxed(5, 0)) errorCount.ref(); if (sentinel[nb].load()) errorCount.ref(); lockCount.ref(); nb = (nb * 17 + i * 5 + lockCount.load() * 3) % threadCount; if (mutex[nb].tryLock()) { if (sentinel[nb].load()) errorCount.ref(); if (sentinel[nb].fetchAndAddRelaxed(16)) errorCount.ref(); if (!sentinel[nb].testAndSetRelaxed(16, 0)) errorCount.ref(); if (sentinel[nb].load()) errorCount.ref(); lockCount.ref(); mutex[nb].unlock(); } nb = (nb * 15 + i * 47 + lockCount.load() * 31) % threadCount; if (mutex[nb].tryLock(2)) { if (sentinel[nb].load()) errorCount.ref(); if (sentinel[nb].fetchAndAddRelaxed(53)) errorCount.ref(); if (!sentinel[nb].testAndSetRelaxed(53, 0)) errorCount.ref(); if (sentinel[nb].load()) errorCount.ref(); lockCount.ref(); mutex[nb].unlock(); } } }
void tst_QThreadPool::destroyingWaitsForTasksToFinish() { QTime total, pass; total.start(); while (total.elapsed() < 10000) { int runs; count.store(runs = 0); { QThreadPool threadPool; pass.restart(); while (pass.elapsed() < 100) { threadPool.start(new CountingRunnable()); ++runs; } } QCOMPARE(count.load(), runs); count.store(runs = 0); { QThreadPool threadPool; pass.restart(); while (pass.elapsed() < 100) { threadPool.start(new CountingRunnable()); threadPool.start(new CountingRunnable()); runs += 2; } } QCOMPARE(count.load(), runs); } }
void tst_QThreadPool::waitForDone() { QTime total, pass; total.start(); QThreadPool threadPool; while (total.elapsed() < 10000) { int runs; count.store(runs = 0); pass.restart(); while (pass.elapsed() < 100) { threadPool.start(new CountingRunnable()); ++runs; } threadPool.waitForDone(); QCOMPARE(count.load(), runs); count.store(runs = 0); pass.restart(); while (pass.elapsed() < 100) { threadPool.start(new CountingRunnable()); threadPool.start(new CountingRunnable()); runs += 2; } threadPool.waitForDone(); QCOMPARE(count.load(), runs); } }
void tst_QtConcurrentIterateKernel::cancel() { { QFuture<void> f = startThreadEngine(new SleepPrintFor(0, 40)).startAsynchronously(); f.cancel(); f.waitForFinished(); QVERIFY(f.isCanceled()); // the threads might run one iteration each before they are canceled. QVERIFY2(iterations.load() <= QThread::idealThreadCount(), (QByteArray::number(iterations.load()) + ' ' + QByteArray::number(QThread::idealThreadCount()))); } }
// Test that a user task with a thread function that always // want to be throttled still completes. The thread engine // should make keep one thread running at all times. void tst_QtConcurrentThreadEngine::throttle() { const int repeats = 10; for (int i = 0; i < repeats; ++i) { QFuture<void> f = (new ThrottleAlwaysUser())->startAsynchronously(); f.waitForFinished(); QCOMPARE(count.load(), 0); } for (int i = 0; i < repeats; ++i) { ThrottleAlwaysUser t; t.startBlocking(); QCOMPARE(count.load(), 0); } }
/// Take an object and prevent timer resource leaks when the object is about /// to become threadless. void takeObject(QObject *obj) { // Work around to prevent // QBasicTimer::stop: Failed. Possibly trying to stop from a different thread static constexpr char kRegistered[] = "__ThreadRegistered"; static constexpr char kMoved[] = "__Moved"; if (!obj->property(kRegistered).isValid()) { QObject::connect(this, &Thread::finished, obj, [this, obj]{ if (!inDestructor.load() || obj->thread() != this) return; // The object is about to become threadless Q_ASSERT(obj->thread() == QThread::currentThread()); obj->setProperty(kMoved, true); obj->moveToThread(this->thread()); }, Qt::DirectConnection); QObject::connect(this, &QObject::destroyed, obj, [obj]{ if (!obj->thread()) { obj->moveToThread(QThread::currentThread()); obj->setProperty(kRegistered, {}); } else if (obj->thread() == QThread::currentThread() && obj->property(kMoved).isValid()) { obj->setProperty(kMoved, {}); QCoreApplication::sendPostedEvents(obj, QEvent::MetaCall); } else if (obj->thread()->eventDispatcher()) QTimer::singleShot(0, obj, [obj]{ obj->setProperty(kRegistered, {}); }); }, Qt::DirectConnection); obj->setProperty(kRegistered, true); } obj->moveToThread(this); }
void tst_QThreadPool::tryStart() { class WaitingTask : public QRunnable { public: QSemaphore semaphore; WaitingTask() { setAutoDelete(false); } void run() { semaphore.acquire(); count.ref(); } }; count.store(0); WaitingTask task; QThreadPool threadPool; for (int i = 0; i < threadPool.maxThreadCount(); ++i) { threadPool.start(&task); } QVERIFY(!threadPool.tryStart(&task)); task.semaphore.release(threadPool.maxThreadCount()); threadPool.waitForDone(); QCOMPARE(count.load(), threadPool.maxThreadCount()); }
void TestMallocPrivate::updatePeak() { if (now_usable.load() > peak_usable.load()) { peak_usable.fetchAndStoreOrdered(now_usable.load()); } if (now_usable.load() + now_overhead.load() > peak_total.load()) { peak_total.fetchAndStoreOrdered(now_usable.load() + now_overhead.load()); } }
int TActionWorker::workerCount() { #if QT_VERSION >= 0x050000 return workerCounter.load(); #else return (int)workerCounter; #endif }
int TActionThread::threadCount() { #if QT_VERSION >= 0x050000 return threadCounter.load(); #else return (int)threadCounter; #endif }
void run() { testRwlock.lockForWrite(); while(release.load()==false) { RWTESTSLEEP } testRwlock.unlock(); }
void tst_QtConcurrentRun::recursive() { int levels = 15; for (int i = 0; i < QThread::idealThreadCount(); ++i) { count.store(0); QThreadPool::globalInstance()->setMaxThreadCount(i); recursiveRun(levels); QCOMPARE(count.load(), (int)pow(2.0, levels) - 1); } for (int i = 0; i < QThread::idealThreadCount(); ++i) { count.store(0); QThreadPool::globalInstance()->setMaxThreadCount(i); recursiveResult(levels); QCOMPARE(count.load(), (int)pow(2.0, levels) - 1); } }
void tst_QtConcurrentIterateKernel::stresstest() { const int iterations = 1000; const int times = 50; for (int i = 0; i < times; ++i) { counter.store(0); CountFor f(0, iterations); f.startBlocking(); QCOMPARE(counter.load(), iterations); } }
void tst_QThreadPool::start() { const int runs = 1000; count.store(0); { QThreadPool threadPool; for (int i = 0; i< runs; ++i) { threadPool.start(new CountingRunnable()); } } QCOMPARE(count.load(), runs); }
void tst_QtConcurrentIterateKernel::throttling() { const int totalIterations = 400; iterations.store(0); threads.clear(); ThrottleFor f(0, totalIterations); f.startBlocking(); QCOMPARE(iterations.load(), totalIterations); QCOMPARE(threads.count(), 1); }
void EventReader::run() { Qt::MouseButtons buttons; xcb_generic_event_t *event; while (running.load() && (event = xcb_wait_for_event(m_hooks->connection()))) { uint response_type = event->response_type & ~0x80; switch (response_type) { case XCB_BUTTON_PRESS: { xcb_button_press_event_t *press = (xcb_button_press_event_t *)event; QPoint p(press->event_x, press->event_y); buttons = (buttons & ~0x7) | translateMouseButtons(press->state); buttons |= translateMouseButton(press->detail); QWindowSystemInterface::handleMouseEvent(0, press->time, p, p, buttons); break; } case XCB_BUTTON_RELEASE: { xcb_button_release_event_t *release = (xcb_button_release_event_t *)event; QPoint p(release->event_x, release->event_y); buttons = (buttons & ~0x7) | translateMouseButtons(release->state); buttons &= ~translateMouseButton(release->detail); QWindowSystemInterface::handleMouseEvent(0, release->time, p, p, buttons); break; } case XCB_MOTION_NOTIFY: { xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *)event; QPoint p(motion->event_x, motion->event_y); QWindowSystemInterface::handleMouseEvent(0, motion->time, p, p, buttons); break; } case XCB_CLIENT_MESSAGE: { xcb_client_message_event_t *client = (xcb_client_message_event_t *) event; const xcb_atom_t *atoms = m_hooks->atoms(); if (client->format == 32 && client->type == atoms[Atoms::WM_PROTOCOLS] && client->data.data32[0] == atoms[Atoms::WM_DELETE_WINDOW]) { QWindow *window = m_hooks->platformWindow() ? m_hooks->platformWindow()->window() : 0; if (window) QWindowSystemInterface::handleCloseEvent(window); } break; } default: break; } } }
QAudioBufferPrivate *QAudioBufferPrivate::clone() { // We want to create a single bufferprivate with a // single qaab // This should only be called when the count is > 1 Q_ASSERT(mCount.load() > 1); if (mProvider) { QAbstractAudioBuffer *abuf = mProvider->clone(); if (!abuf) { abuf = new QMemoryAudioBufferProvider(mProvider->constData(), mProvider->frameCount(), mProvider->format(), mProvider->startTime()); } if (abuf) { return new QAudioBufferPrivate(abuf); } } return 0; }
int QcepObject::allocatedObjects() { return s_ObjectAllocateCount.load(); }
bool shouldThrottleThread() { const int load = iterations.load(); return (load > 100 && load < 200); }
void tst_QtConcurrentIterateKernel::instantiate() { startThreadEngine(new PrintFor(0, 40)).startBlocking(); QCOMPARE(iterations.load(), 40); }
int QcepObject::deletedObjects() { return s_ObjectDeleteCount.load(); }