void run() { testsTurn.release(); threadsTurn.acquire(); QVERIFY(!readWriteLock.tryLockForRead()); testsTurn.release(); threadsTurn.acquire(); QVERIFY(readWriteLock.tryLockForRead()); lockCount.ref(); QVERIFY(readWriteLock.tryLockForRead()); lockCount.ref(); lockCount.deref(); readWriteLock.unlock(); lockCount.deref(); readWriteLock.unlock(); testsTurn.release(); threadsTurn.acquire(); QTime timer; timer.start(); QVERIFY(!readWriteLock.tryLockForRead(1000)); QVERIFY(timer.elapsed() >= 1000); testsTurn.release(); threadsTurn.acquire(); timer.start(); QVERIFY(readWriteLock.tryLockForRead(1000)); QVERIFY(timer.elapsed() <= 1000); lockCount.ref(); QVERIFY(readWriteLock.tryLockForRead(1000)); lockCount.ref(); lockCount.deref(); readWriteLock.unlock(); lockCount.deref(); readWriteLock.unlock(); testsTurn.release(); threadsTurn.acquire(); }
static void deleteItem(LoggingItem *item) { if (!item) return; if (!item->refcount.deref()) { if (item->threadName) free(item->threadName); item_count.deref(); delete item; } }
void QEglFSX11Hooks::platformDestroy() { running.deref(); sendConnectionEvent(XCB_ATOM_NONE); m_eventReader->wait(); delete m_eventReader; m_eventReader = 0; XCloseDisplay(m_display); m_display = 0; m_connection = 0; }
/// \brief Expire any clients in the delete list void LogForwardThread::expireClients(void) { #ifndef NOLOGSERVER QMutexLocker lock(&logClientMapMutex); QMutexLocker lock2(&logRevClientMapMutex); QMutexLocker lock3(&logClientToDelMutex); while (!logClientToDel.isEmpty()) { QString clientId = logClientToDel.takeFirst(); logClientCount.deref(); LOG(VB_GENERAL, LOG_INFO, QString("Expiring client %1 (#%2)") .arg(clientId).arg(logClientCount.fetchAndAddOrdered(0))); LoggerListItem *item = logClientMap.take(clientId); if (!item) continue; LoggerList *list = item->list; delete item; while (!list->isEmpty()) { LoggerBase *logger = list->takeFirst(); ClientList *clientList = logRevClientMap.value(logger, NULL); if (!clientList || clientList->size() == 1) { if (clientList) { logRevClientMap.remove(logger); delete clientList; } delete logger; continue; } clientList->removeAll(clientId); } delete list; } // TODO FIXME: This is not thread-safe! // just this daemon left if (logClientCount.fetchAndAddOrdered(0) == 1 && m_shutdownTimer && !m_shutdownTimer->isActive()) { LOG(VB_GENERAL, LOG_INFO, "Starting 5min shutdown timer"); m_shutdownTimer->start(5*60*1000); } #endif }
void Destroy(void) { if (!m_createdThread) return; m_createdThread = false; gAdminRunLoopRunning.deref(); if (gAdminRunLoop) CFRunLoopStop(gAdminRunLoop); if (m_thread) { m_thread->quit(); m_thread->wait(); } delete m_thread; m_thread = NULL; }
bool subRef() { return (ref.deref()); }
void tst_QReadWriteLock::tryWriteLock() { { QReadWriteLock rwlock; QVERIFY(rwlock.tryLockForWrite()); rwlock.unlock(); QVERIFY(rwlock.tryLockForWrite()); rwlock.unlock(); rwlock.lockForWrite(); QVERIFY(!rwlock.tryLockForWrite()); QVERIFY(!rwlock.tryLockForWrite()); rwlock.unlock(); rwlock.lockForRead(); QVERIFY(!rwlock.tryLockForWrite()); rwlock.unlock(); } { QReadWriteLock rwlock(QReadWriteLock::Recursive); QVERIFY(rwlock.tryLockForWrite()); rwlock.unlock(); QVERIFY(rwlock.tryLockForWrite()); rwlock.unlock(); rwlock.lockForWrite(); QVERIFY(rwlock.tryLockForWrite()); QVERIFY(rwlock.tryLockForWrite()); rwlock.unlock(); rwlock.unlock(); rwlock.unlock(); rwlock.lockForRead(); QVERIFY(!rwlock.tryLockForWrite()); rwlock.unlock(); } // functionality test { class Thread : public QThread { public: Thread() : failureCount(0) { } void run() { testsTurn.release(); threadsTurn.acquire(); if (readWriteLock.tryLockForWrite()) failureCount++; testsTurn.release(); threadsTurn.acquire(); if (!readWriteLock.tryLockForWrite()) failureCount++; if (!lockCount.testAndSetRelaxed(0, 1)) failureCount++; if (!lockCount.testAndSetRelaxed(1, 0)) failureCount++; readWriteLock.unlock(); testsTurn.release(); threadsTurn.acquire(); if (readWriteLock.tryLockForWrite(1000)) failureCount++; testsTurn.release(); threadsTurn.acquire(); if (!readWriteLock.tryLockForWrite(1000)) failureCount++; if (!lockCount.testAndSetRelaxed(0, 1)) failureCount++; if (!lockCount.testAndSetRelaxed(1, 0)) failureCount++; readWriteLock.unlock(); testsTurn.release(); threadsTurn.acquire(); } int failureCount; }; Thread thread; thread.start(); testsTurn.acquire(); readWriteLock.lockForRead(); lockCount.ref(); threadsTurn.release(); testsTurn.acquire(); lockCount.deref(); readWriteLock.unlock(); threadsTurn.release(); testsTurn.acquire(); readWriteLock.lockForRead(); lockCount.ref(); threadsTurn.release(); testsTurn.acquire(); lockCount.deref(); readWriteLock.unlock(); threadsTurn.release(); // stop thread testsTurn.acquire(); threadsTurn.release(); thread.wait(); QCOMPARE(thread.failureCount, 0); } }