TEST(Deadlock, Simple) { const ResourceId resIdA(RESOURCE_DATABASE, std::string("A")); const ResourceId resIdB(RESOURCE_DATABASE, std::string("B")); LockerForTests locker1(MODE_IX); LockerForTests locker2(MODE_IX); ASSERT_EQUALS(LOCK_OK, locker1.lockBegin(nullptr, resIdA, MODE_X)); ASSERT_EQUALS(LOCK_OK, locker2.lockBegin(nullptr, resIdB, MODE_X)); // 1 -> 2 ASSERT_EQUALS(LOCK_WAITING, locker1.lockBegin(nullptr, resIdB, MODE_X)); // 2 -> 1 ASSERT_EQUALS(LOCK_WAITING, locker2.lockBegin(nullptr, resIdA, MODE_X)); DeadlockDetector wfg1(*getGlobalLockManager(), &locker1); ASSERT(wfg1.check().hasCycle()); DeadlockDetector wfg2(*getGlobalLockManager(), &locker2); ASSERT(wfg2.check().hasCycle()); // Cleanup, so that LockerImpl doesn't complain about leaked locks locker1.unlock(resIdB); locker2.unlock(resIdA); }
void DanmakusManager::FetchNewDanmakus(Displayer* displayer) { std::lock_guard<Win32Mutex> locker1(mAllDanmakusMutex); std::lock_guard<Win32Mutex> locker2(mActiveDanmakusMutex); time_t current = mTimer->GetMilliseconds(); for (auto iter = mNextFetchIter; iter != mAllDanmakus.end(); /* ignore */) { if ((*iter)->GetStartTime() < current) { if (!(*iter)->HasMeasured(&mConfig)) { (*iter)->Measure(displayer, &mConfig); } if ((*iter)->IsAlive(current)) { mActiveDanmakus.insert(*iter); } } else { mNextFetchIter = iter; break; } if (++iter == mAllDanmakus.end()) { mNextFetchIter = mAllDanmakus.end(); break; } } mLastFetchTime = current; }
TEST(Deadlock, Indirect) { const ResourceId resIdA(RESOURCE_DATABASE, std::string("A")); const ResourceId resIdB(RESOURCE_DATABASE, std::string("B")); LockerForTests locker1(MODE_IX); LockerForTests locker2(MODE_IX); LockerForTests lockerIndirect(MODE_IX); ASSERT_EQUALS(LOCK_OK, locker1.lockBegin(nullptr, resIdA, MODE_X)); ASSERT_EQUALS(LOCK_OK, locker2.lockBegin(nullptr, resIdB, MODE_X)); // 1 -> 2 ASSERT_EQUALS(LOCK_WAITING, locker1.lockBegin(nullptr, resIdB, MODE_X)); // 2 -> 1 ASSERT_EQUALS(LOCK_WAITING, locker2.lockBegin(nullptr, resIdA, MODE_X)); // 3 -> 2 ASSERT_EQUALS(LOCK_WAITING, lockerIndirect.lockBegin(nullptr, resIdA, MODE_X)); DeadlockDetector wfg1(*getGlobalLockManager(), &locker1); ASSERT(wfg1.check().hasCycle()); DeadlockDetector wfg2(*getGlobalLockManager(), &locker2); ASSERT(wfg2.check().hasCycle()); // Indirect locker should not report the cycle since it does not participate in it DeadlockDetector wfgIndirect(*getGlobalLockManager(), &lockerIndirect); ASSERT(!wfgIndirect.check().hasCycle()); // Cleanup, so that LockerImpl doesn't complain about leaked locks locker1.unlock(resIdB); locker2.unlock(resIdA); }
Structure* ExampleSearch::replaceWithRandom(Structure *s, const QString & reason) { QWriteLocker locker1 (s->lock()); // Generate/Check new structure Structure *structure = generateRandomStructure(); // Copy info over QWriteLocker locker2 (structure->lock()); s->copyStructure(*structure); s->resetEnergy(); s->resetEnthalpy(); s->setPV(0); s->setCurrentOptStep(1); QString parents = "Randomly generated"; if (!reason.isEmpty()) parents += " (" + reason + ")"; s->setParents(parents); s->resetFailCount(); // Delete random structure structure->deleteLater(); return s; }
void SavingThread::stop() { QMutexLocker locker1(&doStopMutex); QMutexLocker locker2(&processingMutex); doStop = true; releaseFile(); }
// Destructor SavingThread::~SavingThread() { QMutexLocker locker1(&doStopMutex); QMutexLocker locker2(&processingMutex); doStop = true; releaseFile(); doStopMutex.unlock(); wait(); }
void SavingThread::resetSaver() { QMutexLocker locker1(&doStopMutex); QMutexLocker locker2(&processingMutex); processingBuffer.clear(); magnificator.clearBuffer(); currentWriteIndex = 0; releaseFile(); cap.set(CV_CAP_PROP_POS_FRAMES,0); doStop = true; }
TEST(LockerImpl, ConflictWithTimeout) { const ResourceId resId(RESOURCE_COLLECTION, std::string("TestDB.collection")); LockerImpl locker1(1); ASSERT(LOCK_OK == locker1.lock(resId, MODE_X)); LockerImpl locker2(2); ASSERT(LOCK_TIMEOUT == locker2.lock(resId, MODE_S, 0)); ASSERT(locker2.isLockHeldForMode(resId, MODE_NONE)); ASSERT(locker1.unlock(resId)); }
void ReactionTimeTracker::updateFollowingVehicleAcceleration( float acceleration ) { QMutexLocker locker1( &leadVehicleIsStoppingMutex ); QMutexLocker locker2( &timeSinceBrakingEventMutex ); /* We wait at least thirty seconds between events to make sure every event is unique */ if ( leadVehicleIsStopping && (timeSinceBrakingEvent->elapsed() > 30000) ) { if ( acceleration < ACCELERATION_THRESHOLD ) { /* Okay, the following vehicle is braking in response to the lead vehicle */ QMutexLocker locker1( &timerMutex ); QMutexLocker locker2( &reactionTimeAccumulatorMutex ); reactionTimeAccumulator = 0.5 * reactionTimeAccumulator + 0.5 * (timer->elapsed() / 1000 ); emit gotReactionTime( reactionTimeAccumulator ); timer = new QTime(); timeSinceBrakingEvent = new QTime(); timeSinceBrakingEvent->start(); } else { /* The following vehicle hasn't stopped braking yet */ } } }
void FrontEnd::stop() { { WriteLocker locker(m_stateLock); if (m_state != State_Running) { return; } m_state = State_Stopping; ReadLocker locker1(m_cpLock); broadcastLocked(NULL, false); } m_taskGroup->abort(); { for (NPT_Ordinal i = 0; i < m_controlPointList.GetItemCount(); i++) { ControlPointInfo *info = *m_controlPointList.GetItem(i); info->m_controlPoint->implDetach(); } for (NPT_Ordinal i = 0; i < m_deviceImplList.GetItemCount(); i++) { DeviceImplInfo *info = *m_deviceImplList.GetItem(i); info->m_deviceImpl->implDetach(); } } m_taskGroup->wait(); for (NPT_Ordinal i = 0; i != m_ifList.GetItemCount(); i++) { Interface *intf = *m_ifList.GetItem(i); intf->m_httpConnector->stop(); intf->m_ssdpConnector->stop(); } { WriteLocker locker(m_stateLock); m_interfaceContextList.Clear(); for (NPT_Ordinal i = 0; i != m_ifList.GetItemCount(); i++) { Interface *intf = *m_ifList.GetItem(i); delete intf->m_httpConnector; delete intf->m_ssdpConnector; delete intf->m_nif; delete intf; } m_ifList.Clear(); m_state = State_Stopped; } }
TEST(Deadlock, NoDeadlock) { const ResourceId resId(RESOURCE_DATABASE, std::string("A")); LockerForTests locker1(MODE_IS); LockerForTests locker2(MODE_IS); ASSERT_EQUALS(LOCK_OK, locker1.lockBegin(nullptr, resId, MODE_S)); ASSERT_EQUALS(LOCK_OK, locker2.lockBegin(nullptr, resId, MODE_S)); DeadlockDetector wfg1(*getGlobalLockManager(), &locker1); ASSERT(!wfg1.check().hasCycle()); DeadlockDetector wfg2(*getGlobalLockManager(), &locker2); ASSERT(!wfg2.check().hasCycle()); }
void FrontEnd::removeControlPoint(ControlPoint *controlPoint) { ReadLocker locker1(m_stateLock); WriteLocker locker2(m_cpLock); if (controlPoint) { NPT_List<ControlPointInfo*>::Iterator it = m_controlPointList.Find(ControlPointInfoFinder(controlPoint)); if (it) { ControlPointInfo *controlPointInfo = *it; if (m_state == State_Running) { controlPoint->implDetach(); } m_controlPointList.Erase(it); delete controlPointInfo; } } }
TEST(LockerImpl, ConflictUpgradeWithTimeout) { const ResourceId resId(RESOURCE_COLLECTION, std::string("TestDB.collection")); MMAPV1LockerImpl locker1(1); ASSERT(LOCK_OK == locker1.lockGlobal(MODE_IS)); ASSERT(LOCK_OK == locker1.lock(resId, MODE_S)); MMAPV1LockerImpl locker2(2); ASSERT(LOCK_OK == locker2.lockGlobal(MODE_IS)); ASSERT(LOCK_OK == locker2.lock(resId, MODE_S)); // Try upgrading locker 1, which should block and timeout ASSERT(LOCK_TIMEOUT == locker1.lock(resId, MODE_X, 1)); locker1.unlockAll(); locker2.unlockAll(); }
void FrontEnd::removeDeviceImpl(DeviceImpl *deviceImpl) { ReadLocker locker1(m_stateLock); WriteLocker locker2(m_dsLock); if (deviceImpl) { NPT_List<DeviceImplInfo*>::Iterator it = m_deviceImplList.Find(DeviceImplInfoFinder(deviceImpl)); if (it) { DeviceImplInfo *deviceImplInfo = *it; if (m_state == State_Running) { broadcastLocked(deviceImplInfo, false); deviceImpl->implDetach(); } m_deviceImplIndex.Erase(deviceImpl->uuid()); m_deviceImplList.Erase(it); delete deviceImplInfo; } } }
void FrontEnd::addControlPoint(ControlPoint *controlPoint) { ReadLocker locker1(m_stateLock); WriteLocker locker2(m_cpLock); if (controlPoint) { NPT_List<ControlPointInfo*>::Iterator it = m_controlPointList.Find(ControlPointInfoFinder(controlPoint)); if (!it) { ControlPointInfo *controlPointInfo = new ControlPointInfo(); controlPointInfo->m_controlPoint = controlPoint; controlPointInfo->m_context.m_httpRoot = NPT_String::Format("/cp%p/", controlPoint); controlPointInfo->m_context.m_userAgentHeader = m_userAgentHeader; controlPointInfo->m_context.m_serverHeader = m_serverHeader; m_controlPointList.Add(controlPointInfo); if (m_state == State_Running) { controlPoint->implAttach(this, controlPointInfo->m_context); } } } }
void MTPSessionData::clear() { RPCCallbackClears clearCallbacks; { QReadLocker locker1(haveSentMutex()), locker2(toResendMutex()), locker3(haveReceivedMutex()), locker4(wereAckedMutex()); mtpResponseMap::const_iterator end = haveReceived.cend(); clearCallbacks.reserve(haveSent.size() + wereAcked.size()); for (mtpRequestMap::const_iterator i = haveSent.cbegin(), e = haveSent.cend(); i != e; ++i) { mtpRequestId requestId = i.value()->requestId; if (haveReceived.find(requestId) == end) { clearCallbacks.push_back(requestId); } } for (mtpRequestIdsMap::const_iterator i = toResend.cbegin(), e = toResend.cend(); i != e; ++i) { mtpRequestId requestId = i.value(); if (haveReceived.find(requestId) == end) { clearCallbacks.push_back(requestId); } } for (mtpRequestIdsMap::const_iterator i = wereAcked.cbegin(), e = wereAcked.cend(); i != e; ++i) { mtpRequestId requestId = i.value(); if (haveReceived.find(requestId) == end) { clearCallbacks.push_back(requestId); } } } { QWriteLocker locker(haveSentMutex()); haveSent.clear(); } { QWriteLocker locker(toResendMutex()); toResend.clear(); } { QWriteLocker locker(wereAckedMutex()); wereAcked.clear(); } { QWriteLocker locker(receivedIdsMutex()); receivedIds.clear(); } _mtp_internal::clearCallbacksDelayed(clearCallbacks); }
void ReactionTimeTracker::updateTimeHeadway( float time ) { QMutexLocker locker( &smallTimeHeadwayMutex ); /* TODO: What constitutes a "small" time headway */ if ( time < 3.5 ) { smallTimeHeadway = true; } else { /* Vehicle is out of range; reset everything */ QMutexLocker locker1( &timerMutex ); QMutexLocker locker2( &leadVehicleIsStoppingMutex ); smallTimeHeadway = false; leadVehicleIsStopping = false; if ( timer->elapsed() > 0 ) { timer = new QTime(); } } }
void FrontEnd::addDeviceImpl(DeviceImpl *deviceImpl) { ReadLocker locker1(m_stateLock); WriteLocker locker2(m_dsLock); if (deviceImpl) { NPT_List<DeviceImplInfo*>::Iterator it = m_deviceImplList.Find(DeviceImplInfoFinder(deviceImpl)); if (!it) { DeviceImplInfo *deviceImplInfo = new DeviceImplInfo; deviceImplInfo->m_deviceImpl = deviceImpl; deviceImplInfo->m_context.m_httpRoot = NPT_String::Format("/devices/%s/", deviceImpl->uuid().toString().GetChars()); deviceImplInfo->m_context.m_userAgentHeader = m_userAgentHeader; deviceImplInfo->m_context.m_serverHeader = m_serverHeader; m_deviceImplList.Add(deviceImplInfo); m_deviceImplIndex.Put(deviceImpl->uuid(), deviceImplInfo); if (m_state == State_Running) { deviceImpl->implAttach(this, deviceImplInfo->m_context); //broadcastLocked(deviceImplInfo, false); broadcastLocked(deviceImplInfo, true); } } } }
void FrontEnd::broadcastIfNecessary() { ReadLocker locker1(m_stateLock); if (m_state != State_Running) { return; } NPT_TimeStamp ts; NPT_System::GetCurrentTimeStamp(ts); NPT_Int64 tsMillis = ts.ToMillis(); WriteLocker locker2(m_dsLock); for (NPT_Ordinal i = 0; i < m_deviceImplList.GetItemCount(); i++) { NPT_List<DeviceImplInfo*>::Iterator it = m_deviceImplList.GetItem(i); DeviceImplInfo *info = *it; //if (info->m_updateTS.ToMillis() + info->m_deviceImpl->m_expireSeconds * 1000 - 11000 < tsMillis) if (tsMillis - info->m_updateTS.ToMillis() > 30000) { broadcastLocked(info, true); } } }
TEST(Deadlock, SimpleUpgrade) { const ResourceId resId(RESOURCE_DATABASE, std::string("A")); LockerForTests locker1(MODE_IX); LockerForTests locker2(MODE_IX); // Both acquire lock in intent mode ASSERT_EQUALS(LOCK_OK, locker1.lockBegin(nullptr, resId, MODE_IX)); ASSERT_EQUALS(LOCK_OK, locker2.lockBegin(nullptr, resId, MODE_IX)); // Both try to upgrade ASSERT_EQUALS(LOCK_WAITING, locker1.lockBegin(nullptr, resId, MODE_X)); ASSERT_EQUALS(LOCK_WAITING, locker2.lockBegin(nullptr, resId, MODE_X)); DeadlockDetector wfg1(*getGlobalLockManager(), &locker1); ASSERT(wfg1.check().hasCycle()); DeadlockDetector wfg2(*getGlobalLockManager(), &locker2); ASSERT(wfg2.check().hasCycle()); // Cleanup, so that LockerImpl doesn't complain about leaked locks locker1.unlock(resId); locker2.unlock(resId); }
wxThread::ExitCode SymbolParserThread::Entry() { while (!TestDestroy() && !m_exit) { // Wait for something to show up in the queue. m_itemsAvailable.Wait(); while (!TestDestroy()) { m_itemsLock.Enter(); if (m_items.empty()) { m_itemsLock.Leave(); break; } wxCriticalSectionLocker locker(m_headLock); m_headItem = m_items.back(); m_items.pop_back(); m_itemsLock.Leave(); if (m_eventHandler != NULL) { std::vector<Symbol*> symbols; wxStringInputStream input(m_headItem->code); ParseFileSymbols(input, symbols); m_itemsLock.Enter(); bool isLastItem=m_items.empty(); m_itemsLock.Leave(); // Dispatch the message to event handler. SymbolParserEvent event(m_headItem->fileId, symbols, isLastItem); m_eventHandler->AddPendingEvent(event); } delete m_headItem; m_headItem = NULL; } } wxCriticalSectionLocker locker1(m_itemsLock); ClearVector(m_items); wxCriticalSectionLocker locker2(m_headLock); delete m_headItem; m_headItem = NULL; return 0; }
NPT_Result FrontEnd::start() { WriteLocker locker(m_stateLock); if (m_state != State_Stopped) { return NPT_ERROR_INVALID_STATE; } NPT_Result nr; NPT_List<NPT_NetworkInterface*> ifList; nr = NPT_NetworkInterface::GetNetworkInterfaces(ifList); if (NPT_FAILED(nr)) { return nr; } for (NPT_Ordinal i = 0; i < ifList.GetItemCount(); i++) { NPT_NetworkInterface *nif = *ifList.GetItem(i); if (nif->GetAddresses().GetFirstItem() && (m_includeLoopback || ((nif->GetFlags() & NPT_NETWORK_INTERFACE_FLAG_LOOPBACK) == 0))) { Interface *intf = new Interface(); intf->m_owner = this; intf->m_nif = nif; intf->m_context.m_ifAddr = nif->GetAddresses().GetFirstItem()->GetPrimaryAddress(); intf->m_httpConnector = new HttpConnector(intf, intf->m_context.m_ifAddr); intf->m_ssdpConnector = new SsdpConnector(intf, intf->m_context.m_ifAddr); intf->m_context.m_httpPort = 0; intf->m_context.m_ssdpPort = 0; m_ifList.Add(intf); m_interfaceContextList.Add(&intf->m_context); } else { delete nif; } } if (m_ifList.GetItemCount() == 0) { return NPT_FAILURE; } for (NPT_Ordinal i = 0; i < m_ifList.GetItemCount(); i++) { Interface *intf = *m_ifList.GetItem(i); if (NPT_SUCCEEDED(intf->m_httpConnector->start())) { intf->m_context.m_httpPort = intf->m_httpConnector->port(); } if (NPT_SUCCEEDED(intf->m_ssdpConnector->start())) { intf->m_context.m_ssdpPort = intf->m_ssdpConnector->port(); } } m_taskGroup->reset(); { ReadLocker locker1(m_cpLock); for (NPT_Ordinal i = 0; i < m_controlPointList.GetItemCount(); i++) { ControlPointInfo *info = *m_controlPointList.GetItem(i); info->m_controlPoint->implAttach(this, info->m_context); } for (NPT_Ordinal i = 0; i < m_deviceImplList.GetItemCount(); i++) { DeviceImplInfo *info = *m_deviceImplList.GetItem(i); info->m_deviceImpl->implAttach(this, info->m_context); } broadcastLocked(NULL, true); } m_taskGroup->startTask(new SsdpBroadcastTask(this)); m_state = State_Running; return NPT_SUCCESS; }