int mythdir_opendir(const char *dirname) { LOG(VB_FILE, LOG_DEBUG, LOC + QString("mythdir_opendir(%1)").arg(dirname)); int id = 0; if (strncmp(dirname, "myth://", 7)) { DIR *dir = opendir(dirname); m_dirWrapperLock.lockForWrite(); id = getNextDirID(); m_localdirs[id] = dir; m_dirnames[id] = dirname; m_dirWrapperLock.unlock(); } else { QStringList list; QUrl qurl(dirname); QString storageGroup = qurl.userName(); list.clear(); if (storageGroup.isEmpty()) storageGroup = "Default"; list << "QUERY_SG_GETFILELIST"; list << qurl.host(); list << storageGroup; QString path = qurl.path(); if (!qurl.fragment().isEmpty()) path += "#" + qurl.fragment(); list << path; list << "1"; bool ok = gCoreContext->SendReceiveStringList(list); if ((!ok) || ((list.size() == 1) && (list[0] == "EMPTY LIST"))) list.clear(); m_dirWrapperLock.lockForWrite(); id = getNextDirID(); m_remotedirs[id] = list; m_remotedirPositions[id] = 0; m_dirnames[id] = dirname; m_dirWrapperLock.unlock(); } return id; }
void tst_QReadWriteLock::readLockLoop() { QReadWriteLock rwlock; int runs=10000; int i; for (i=0; i<runs; ++i) { rwlock.lockForRead(); } for (i=0; i<runs; ++i) { rwlock.unlock(); } }
void TonemapperThread::terminateRequested() { if (forciblyTerminated) return; lock.lockForWrite(); pregamma=-1; xsize=-1; lock.unlock(); forciblyTerminated=true; //HACK oddly enough for the other operators we cannot emit finished (it segfaults) if (opts.tmoperator==mantiuk || opts.tmoperator==ashikhmin || opts.tmoperator==pattanaik ) emit finished(); terminate(); }
int mythdir_check(int id) { LOG(VB_FILE, LOG_DEBUG, QString("mythdir_check(%1)").arg(id)); int result = 0; m_dirWrapperLock.lockForWrite(); if (m_localdirs.contains(id)) result = 1; else if (m_remotedirs.contains(id)) result = 1; m_dirWrapperLock.unlock(); return result; }
//! Entrypoint for LogThread //! Check to see if there are things to log and if so, write them to disk. //! Otherwise, wait 100ms and check again. //! Exit only when "go" is false AND all queued events are written void LogThread::run() { while(! actionLog.isEmpty() || go) { while(! actionLog.isEmpty()) { // Write all queued log entries lock.lockForRead(); LogEntry entry = actionLog.dequeue(); lock.unlock(); outStream << entry.dat.asInt << entry.position; ++logEntryCountByServo[entry.dat.asBitfield.servoIndex]; yieldCurrentThread(); } msleep(100); } exit(0); }
QByteArray readData() { lock.lockForRead(); ... lock.unlock(); return data; }
char *mythdir_readdir(int dirID) { char *result = NULL; LOG(VB_FILE, LOG_DEBUG, LOC + QString("mythdir_readdir(%1)").arg(dirID)); m_dirWrapperLock.lockForRead(); if (m_remotedirs.contains(dirID)) { int pos = m_remotedirPositions[dirID]; if (m_remotedirs[dirID].size() >= (pos+1)) { result = strdup(m_remotedirs[dirID][pos].toLocal8Bit().constData()); pos++; m_remotedirPositions[dirID] = pos; } } else if (m_localdirs.contains(dirID)) { int sz = offsetof(struct dirent, d_name) + FILENAME_MAX + 1; struct dirent *entry = reinterpret_cast<struct dirent*>(calloc(1, sz)); struct dirent *r = NULL; if ((0 == readdir_r(m_localdirs[dirID], entry, &r)) && (NULL != r)) result = strdup(r->d_name); free(entry); }
int mythfile_stat_fd(int fileID, struct stat *buf) { LOG(VB_FILE, LOG_DEBUG, QString("mythfile_stat_fd(%1, %2)") .arg(fileID).arg((long long)buf)); m_fileWrapperLock.lockForRead(); if (!m_filenames.contains(fileID)) { m_fileWrapperLock.unlock(); return -1; } QString filename = m_filenames[fileID]; m_fileWrapperLock.unlock(); return mythfile_stat(filename.toLocal8Bit().constData(), buf); }
void tst_QReadWriteLock::writeLockLoop() { /* If you include this, the test should print one line and then block. */ #if 0 QReadWriteLock rwlock; int runs=10000; int i; for (i=0; i<runs; ++i) { rwlock.lockForWrite(); qDebug("I am going to block now."); } #endif }
off_t mythfile_tell(int fileID) { off_t result = -1; LOG(VB_FILE, LOG_DEBUG, LOC + QString("mythfile_tell(%1)").arg(fileID)); m_fileWrapperLock.lockForRead(); if (m_ringbuffers.contains(fileID)) result = m_ringbuffers[fileID]->Seek(0, SEEK_CUR); else if (m_remotefiles.contains(fileID)) result = m_remotefiles[fileID]->Seek(0, SEEK_CUR); else if (m_localfiles.contains(fileID)) result = lseek(m_localfiles[fileID], 0, SEEK_CUR); m_fileWrapperLock.unlock(); return result; }
void ReaderThread::run() { ... lock.lockForRead(); read_file(); lock.unlock(); ... }
int mythfile_check(int id) { VERBOSE(VB_FILE+VB_EXTRA, QString("mythfile_check(%1)").arg(id)); int result = 0; m_fileWrapperLock.lockForWrite(); if (m_localfiles.contains(id)) result = 1; else if (m_remotefiles.contains(id)) result = 1; else if (m_ringbuffers.contains(id)) result = 1; m_fileWrapperLock.unlock(); return result; }
void WriterThread::run() { ... lock.lockForWrite(); write_file(); lock.unlock(); ... }
ssize_t mythfile_write(int fileID, void *buf, size_t count) { ssize_t result = -1; LOG(VB_FILE, LOG_DEBUG, LOC + QString("mythfile_write(%1, %2, %3)") .arg(fileID) .arg((long long)buf).arg(count)); m_fileWrapperLock.lockForRead(); if (m_ringbuffers.contains(fileID)) result = m_ringbuffers[fileID]->Write(buf, count); else if (m_remotefiles.contains(fileID)) result = m_remotefiles[fileID]->Write(buf, count); else if (m_localfiles.contains(fileID)) result = write(m_localfiles[fileID], buf, count); m_fileWrapperLock.unlock(); return result; }
ssize_t mythfile_read(int fileID, void *buf, size_t count) { ssize_t result = -1; VERBOSE(VB_FILE+VB_EXTRA, LOC + QString("mythfile_read(%1, %2, %3)").arg(fileID) .arg((long long)buf).arg(count)); m_fileWrapperLock.lockForRead(); if (m_ringbuffers.contains(fileID)) result = m_ringbuffers[fileID]->Read(buf, count); else if (m_remotefiles.contains(fileID)) result = m_remotefiles[fileID]->Read(buf, count); else if (m_localfiles.contains(fileID)) result = read(m_localfiles[fileID], buf, count); m_fileWrapperLock.unlock(); return result; }
static PyObject *meth_QReadWriteLock_tryLockForRead(PyObject *sipSelf, PyObject *sipArgs) { PyObject *sipParseErr = NULL; { QReadWriteLock *sipCpp; if (sipParseArgs(&sipParseErr, sipArgs, "B", &sipSelf, sipType_QReadWriteLock, &sipCpp)) { bool sipRes; Py_BEGIN_ALLOW_THREADS sipRes = sipCpp->tryLockForRead(); Py_END_ALLOW_THREADS return PyBool_FromLong(sipRes); } }
off_t mythfile_seek(int fileID, off_t offset, int whence) { off_t result = -1; LOG(VB_FILE, LOG_DEBUG, LOC + QString("mythfile_seek(%1, %2, %3)") .arg(fileID).arg(offset).arg(whence)); m_fileWrapperLock.lockForRead(); if (m_ringbuffers.contains(fileID)) result = m_ringbuffers[fileID]->Seek(offset, whence); else if (m_remotefiles.contains(fileID)) result = m_remotefiles[fileID]->Seek(offset, whence); else if (m_localfiles.contains(fileID)) result = lseek(m_localfiles[fileID], offset, whence); m_fileWrapperLock.unlock(); return result; }
int main(int argc, char** argv) { int i; const int n_threads = 10; std::vector<QThread*> tid(n_threads); s_iterations = argc > 1 ? atoi(argv[1]) : 1000; fprintf(stderr, "Start of test.\n"); { // Stack-allocated reader-writer lock. QReadWriteLock RWL; RWL.lockForRead(); RWL.unlock(); RWL.lockForWrite(); RWL.unlock(); } pthread_barrier_init(&s_barrier, 0, n_threads); s_pRWlock = new QReadWriteLock(); for (i = 0; i < n_threads; i++) { tid[i] = new IncThread; tid[i]->start(); } for (i = 0; i < n_threads; i++) { tid[i]->wait(); delete tid[i]; } delete s_pRWlock; s_pRWlock = 0; pthread_barrier_destroy(&s_barrier); if (s_counter == n_threads * s_iterations) fprintf(stderr, "Test successful.\n"); else fprintf(stderr, "Test failed: counter = %d, should be %d\n", s_counter, n_threads * s_iterations); return 0; }
void tst_QGlobalStatic::threadStressTest() { class ThreadStressTestThread: public QThread { public: QReadWriteLock *lock; void run() { QReadLocker l(lock); //usleep(qrand() * 200 / RAND_MAX); // thundering herd try { threadStressTestGS(); } catch (int) { } } }; ThrowingType::constructedCount.store(0); ThrowingType::destructedCount.store(0); int expectedConstructionCount = threadStressTestControlVar.load() + 1; if (expectedConstructionCount <= 0) QSKIP("This test cannot be run more than once"); const int numThreads = 200; ThreadStressTestThread threads[numThreads]; QReadWriteLock lock; lock.lockForWrite(); for (int i = 0; i < numThreads; ++i) { threads[i].lock = &lock; threads[i].start(); } // wait for all threads // release the herd lock.unlock(); for (int i = 0; i < numThreads; ++i) threads[i].wait(); QCOMPARE(ThrowingType::constructedCount.loadAcquire(), expectedConstructionCount); QCOMPARE(ThrowingType::destructedCount.loadAcquire(), 0); }
void MainWindow::deleteQueue() { int queue; queue = getSelectedQueue(); if(g_queues.empty() || queue < 0) return; if(QMessageBox::warning(this, tr("Delete queue"), tr("Do you really want to delete the active queue?"), QMessageBox::Yes|QMessageBox::No) == QMessageBox::Yes) { g_queuesLock.lockForWrite(); delete g_queues.takeAt(queue); g_queuesLock.unlock(); Queue::saveQueuesAsync(); refreshQueues(); } }
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(); }
off_t mythfile_tell(int fileID) { off_t result = -1; VERBOSE(VB_FILE+VB_EXTRA, LOC + QString("mythfile_tell(%1)").arg(fileID)); m_fileWrapperLock.lockForRead(); if (m_ringbuffers.contains(fileID)) result = m_ringbuffers[fileID]->Seek(0, SEEK_CUR); else if (m_remotefiles.contains(fileID)) result = m_remotefiles[fileID]->Seek(0, SEEK_CUR); else if (m_localfiles.contains(fileID)) #ifdef USING_MINGW result = lseek64(m_localfiles[fileID], 0, SEEK_CUR); #else result = lseek(m_localfiles[fileID], 0, SEEK_CUR); #endif m_fileWrapperLock.unlock(); return result; }
off_t mythfile_seek(int fileID, off_t offset, int whence) { off_t result = -1; VERBOSE(VB_FILE+VB_EXTRA, LOC + QString("mythfile_seek(%1, %2, %3)") .arg(fileID).arg(offset).arg(whence)); m_fileWrapperLock.lockForRead(); if (m_ringbuffers.contains(fileID)) result = m_ringbuffers[fileID]->Seek(offset, whence); else if (m_remotefiles.contains(fileID)) result = m_remotefiles[fileID]->Seek(offset, whence); else if (m_localfiles.contains(fileID)) #ifdef USING_MINGW result = lseek64(m_localfiles[fileID], offset, whence); #else result = lseek(m_localfiles[fileID], offset, whence); #endif m_fileWrapperLock.unlock(); return result; }
trackdata_ptr TrackData::get( unsigned int id, const QString& artist, const QString& track ) { s_dataidMutex.lockForRead(); if ( s_trackDatasById.contains( id ) ) { trackdata_wptr track = s_trackDatasById.value( id ); s_dataidMutex.unlock(); if ( track ) return track; } s_dataidMutex.unlock(); QMutexLocker lock( &s_datanameCacheMutex ); const QString key = cacheKey( artist, track ); if ( s_trackDatasByName.contains( key ) ) { trackdata_wptr track = s_trackDatasByName.value( key ); if ( track ) return track; } trackdata_ptr t = trackdata_ptr( new TrackData( id, artist, track ), &TrackData::deleteLater ); t->moveToThread( QCoreApplication::instance()->thread() ); t->setWeakRef( t.toWeakRef() ); s_trackDatasByName.insert( key, t ); if ( id > 0 ) { s_dataidMutex.lockForWrite(); s_trackDatasById.insert( id, t ); s_dataidMutex.unlock(); } else t->loadId( false ); return t; }
QList<QString> RuleList::dnsRequest(QString host) { static QReadWriteLock rwLock; static QMap<QString, QList<QString> > dnsCache; rwLock.lockForRead(); QList<QString> ipsCache=dnsCache[host]; rwLock.unlock(); if(ipsCache.size() > 0) { //qDebug("On a %i ips dans le cache pour l'host '%s'", ipsCache.size(), qPrintable(host)); return ipsCache; } else { QList<QString> ips; struct hostent *s_host; if((s_host = gethostbyname(qPrintable(host)))!=NULL) { struct in_addr **a; for (a=(struct in_addr **)s_host->h_addr_list; *a; a++) { ips.push_back(inet_ntoa(**a)); } // add the ips to the cache rwLock.lockForWrite(); dnsCache[host].clear(); dnsCache[host].append(ips); rwLock.unlock(); } else EventDispatcher::instance().sendCritical(QObject::tr("Error while querying the DNS server(Host='%1')").arg(host)); return ips; } }
static PyObject *meth_QReadWriteLock_lockForRead(PyObject *sipSelf, PyObject *sipArgs) { PyObject *sipParseErr = NULL; { QReadWriteLock *sipCpp; if (sipParseArgs(&sipParseErr, sipArgs, "B", &sipSelf, sipType_QReadWriteLock, &sipCpp)) { Py_BEGIN_ALLOW_THREADS sipCpp->lockForRead(); Py_END_ALLOW_THREADS Py_INCREF(Py_None); return Py_None; } } /* Raise an exception if the arguments couldn't be parsed. */ sipNoMethod(sipParseErr, sipName_QReadWriteLock, sipName_lockForRead, doc_QReadWriteLock_lockForRead); return NULL; }
void Album::deleteLater() { QMutexLocker lock( &s_nameCacheMutex ); const QString key = albumCacheKey( m_artist, m_name ); if ( s_albumsByName.contains( key ) ) { s_albumsByName.remove( key ); } if ( m_id > 0 ) { s_idMutex.lockForWrite(); if ( s_albumsById.contains( m_id ) ) { s_albumsById.remove( m_id ); } s_idMutex.unlock(); } QObject::deleteLater(); }
int mythdir_closedir(int dirID) { int result = -1; LOG(VB_FILE, LOG_DEBUG, LOC + QString("mythdir_closedir(%1)").arg(dirID)); m_dirWrapperLock.lockForRead(); if (m_remotedirs.contains(dirID)) { m_remotedirs.remove(dirID); m_remotedirPositions.remove(dirID); result = 0; } else if (m_localdirs.contains(dirID)) { closedir(m_localdirs[dirID]); m_localdirs.remove(dirID); result = 0; } m_dirWrapperLock.unlock(); return -1; }
void TrackData::deleteLater() { QMutexLocker lock( &s_datanameCacheMutex ); const QString key = cacheKey( m_artist, m_track ); if ( s_trackDatasByName.contains( key ) ) { s_trackDatasByName.remove( key ); } if ( m_trackId > 0 ) { s_dataidMutex.lockForWrite(); if ( s_trackDatasById.contains( m_trackId ) ) { s_trackDatasById.remove( m_trackId ); } s_dataidMutex.unlock(); } QObject::deleteLater(); }
album_ptr Album::get( unsigned int id, const QString& name, const Tomahawk::artist_ptr& artist ) { s_idMutex.lockForRead(); if ( s_albumsById.contains( id ) ) { album_wptr album = s_albumsById.value( id ); s_idMutex.unlock(); if ( album ) return album; } s_idMutex.unlock(); QMutexLocker lock( &s_nameCacheMutex ); const QString key = albumCacheKey( artist, name ); if ( s_albumsByName.contains( key ) ) { album_wptr album = s_albumsByName.value( key ); if ( album ) return album; } album_ptr a = album_ptr( new Album( id, name, artist ), &Album::deleteLater ); a->setWeakRef( a.toWeakRef() ); s_albumsByName.insert( key, a ); if ( id > 0 ) { s_idMutex.lockForWrite(); s_albumsById.insert( id, a ); s_idMutex.unlock(); } return a; }