From 671017faca19fc71253f6db119414ec59fb829df Mon Sep 17 00:00:00 2001 From: Daniel Thor Kristjansson Date: Thu, 31 May 2012 17:27:34 -0400 Subject: [PATCH] Refs #10311. Port UPNP code from RefCounted to ReferenceCounter. --- mythtv/libs/libmyth/backendselect.cpp | 29 ++++---- mythtv/libs/libmyth/mythcontext.cpp | 12 +-- mythtv/libs/libmythupnp/eventing.cpp | 2 + mythtv/libs/libmythupnp/httpserver.h | 1 - mythtv/libs/libmythupnp/refcounted.h | 74 ------------------- mythtv/libs/libmythupnp/ssdp.cpp | 15 ++-- mythtv/libs/libmythupnp/ssdpcache.cpp | 62 ++++++++-------- mythtv/libs/libmythupnp/ssdpcache.h | 11 ++- mythtv/libs/libmythupnp/taskqueue.cpp | 9 +-- mythtv/libs/libmythupnp/taskqueue.h | 7 +- mythtv/libs/libmythupnp/upnpdevice.h | 4 +- mythtv/programs/mythbackend/httpstatus.cpp | 20 ++--- .../programs/mythfrontend/mediarenderer.cpp | 20 ++--- 13 files changed, 95 insertions(+), 171 deletions(-) delete mode 100644 mythtv/libs/libmythupnp/refcounted.h diff --git a/mythtv/libs/libmyth/backendselect.cpp b/mythtv/libs/libmyth/backendselect.cpp index 1d010e6a16e..a2bb526c499 100644 --- a/mythtv/libs/libmyth/backendselect.cpp +++ b/mythtv/libs/libmyth/backendselect.cpp @@ -28,10 +28,8 @@ BackendSelection::~BackendSelection() ItemMap::iterator it; for (it = m_devices.begin(); it != m_devices.end(); ++it) { - DeviceLocation *dev = *it; - - if (dev) - dev->Release(); + if (*it) + (*it)->DecrRef(); } m_devices.clear(); @@ -132,7 +130,7 @@ void BackendSelection::AddItem(DeviceLocation *dev) // The devices' USN should be unique. Don't add if it is already there: if (m_devices.find(USN) == m_devices.end()) { - dev->AddRef(); + dev->IncrRef(); m_devices.insert(USN, dev); m_mutex.unlock(); @@ -167,8 +165,6 @@ void BackendSelection::AddItem(DeviceLocation *dev) } else m_mutex.unlock(); - - dev->Release(); } /** @@ -246,11 +242,14 @@ void BackendSelection::Init(void) { EntryMap ourMap; pEntries->GetEntryMap(ourMap); - pEntries->Release(); + pEntries->DecrRef(); EntryMap::const_iterator it; for (it = ourMap.begin(); it != ourMap.end(); ++it) - AddItem(*it); // this does an (*it)->Release() + { + AddItem(*it); + (*it)->DecrRef(); + } } } @@ -267,11 +266,8 @@ void BackendSelection::RemoveItem(QString USN) if (it != m_devices.end()) { - DeviceLocation *dev = *it; - - if (dev) - dev->Release(); - + if (*it) + (*it)->DecrRef(); m_devices.erase(it); } @@ -312,7 +308,10 @@ void BackendSelection::customEvent(QEvent *event) { DeviceLocation *devLoc = SSDP::Instance()->Find(URI, URN); if (devLoc) - AddItem(devLoc); // this does a Release() + { + AddItem(devLoc); + devLoc->DecrRef(); + } } else if (message.startsWith("SSDP_REMOVE")) { diff --git a/mythtv/libs/libmyth/mythcontext.cpp b/mythtv/libs/libmyth/mythcontext.cpp index 1a616e3d442..e6371adefd6 100644 --- a/mythtv/libs/libmyth/mythcontext.cpp +++ b/mythtv/libs/libmyth/mythcontext.cpp @@ -835,19 +835,20 @@ int MythContextPrivate::UPnPautoconf(const int milliSeconds) if (count != 1) { - backends->Release(); + backends->DecrRef(); return count; } // Get this backend's location: DeviceLocation *BE = backends->GetFirst(); - backends->Release(); + backends->DecrRef(); + backends = NULL; // We don't actually know the backend's access PIN, so this will // only work for ones that have PIN access disabled (i.e. 0000) int ret = (UPnPconnect(BE, QString::null)) ? 1 : -1; - BE->Release(); + BE->DecrRef(); return ret; } @@ -915,12 +916,11 @@ bool MythContextPrivate::DefaultUPnP(QString &error) if (UPnPconnect(pDevLoc, PIN)) { - pDevLoc->Release(); - + pDevLoc->DecrRef(); return true; } - pDevLoc->Release(); + pDevLoc->DecrRef(); error = "Cannot connect to default backend via UPnP. Wrong saved PIN?"; return false; diff --git a/mythtv/libs/libmythupnp/eventing.cpp b/mythtv/libs/libmythupnp/eventing.cpp index 7b8305972e1..f3ecff697a5 100644 --- a/mythtv/libs/libmythupnp/eventing.cpp +++ b/mythtv/libs/libmythupnp/eventing.cpp @@ -411,6 +411,8 @@ void Eventing::NotifySubscriber( SubscriberInfo *pInfo ) TaskQueue::Instance()->AddTask( 250, pEventTask ); + pEventTask->DecrRef(); + // ------------------------------------------------------------------ // Update the subscribers Key & last Notified fields // ------------------------------------------------------------------ diff --git a/mythtv/libs/libmythupnp/httpserver.h b/mythtv/libs/libmythupnp/httpserver.h index 87cc30e743f..e466fce20af 100644 --- a/mythtv/libs/libmythupnp/httpserver.h +++ b/mythtv/libs/libmythupnp/httpserver.h @@ -33,7 +33,6 @@ #include "serverpool.h" #include "httprequest.h" #include "mthreadpool.h" -#include "refcounted.h" #include "upnputil.h" #include "compat.h" diff --git a/mythtv/libs/libmythupnp/refcounted.h b/mythtv/libs/libmythupnp/refcounted.h deleted file mode 100644 index 02c2c05c69a..00000000000 --- a/mythtv/libs/libmythupnp/refcounted.h +++ /dev/null @@ -1,74 +0,0 @@ -////////////////////////////////////////////////////////////////////////////// -// Program Name: refcounted.h -// Created : Oct. 21, 2005 -// -// Purpose : Reference Counted Base Class -// -// Copyright (c) 2005 David Blain -// -// Licensed under the GPL v2 or later, see COPYING for details -// -////////////////////////////////////////////////////////////////////////////// - -#ifndef __REFCOUNTED_H__ -#define __REFCOUNTED_H__ - -#include - -///////////////////////////////////////////////////////////////////////////// -///////////////////////////////////////////////////////////////////////////// -// -// RefCounted Class Definition/Implementation -// -///////////////////////////////////////////////////////////////////////////// -///////////////////////////////////////////////////////////////////////////// - -class RefCounted -{ - protected: - - long m_nRefCount; - QMutex m_mutex; - - protected: - - // Destructor protected to force use of release; - - virtual ~RefCounted() {}; - - public: - - // ------------------------------------------------------------------ - - RefCounted() : m_nRefCount( 0 ) - { - } - - // ------------------------------------------------------------------ - - long AddRef() - { - m_mutex.lock(); - long nRef = (++m_nRefCount); - m_mutex.unlock(); - - return( nRef ); - } - - // ------------------------------------------------------------------ - - long Release() - { - - m_mutex.lock(); - long nRef = (--m_nRefCount); - m_mutex.unlock(); - - if (nRef <= 0) - delete this; - - return( nRef ); - } -}; - -#endif diff --git a/mythtv/libs/libmythupnp/ssdp.cpp b/mythtv/libs/libmythupnp/ssdp.cpp index c7269a43abf..0d95178bd8c 100644 --- a/mythtv/libs/libmythupnp/ssdp.cpp +++ b/mythtv/libs/libmythupnp/ssdp.cpp @@ -122,7 +122,10 @@ SSDP::~SSDP() wait(); if (m_pNotifyTask != NULL) - m_pNotifyTask->Release(); + { + m_pNotifyTask->DecrRef(); + m_pNotifyTask = NULL; + } for (int nIdx = 0; nIdx < (int)NumberOfSockets; nIdx++ ) { @@ -154,12 +157,6 @@ void SSDP::EnableNotifications( int nServicePort ) "SSDP::EnableNotifications() - creating new task"); m_pNotifyTask = new UPnpNotifyTask( m_nServicePort ); - // ------------------------------------------------------------------ - // Let's make sure to hold on to a reference of the NotifyTask. - // ------------------------------------------------------------------ - - m_pNotifyTask->AddRef(); - // ------------------------------------------------------------------ // First Send out Notification that we are leaving the network. // ------------------------------------------------------------------ @@ -505,6 +502,8 @@ bool SSDP::ProcessSearchRequest( const QStringMap &sHeaders, TaskQueue::Instance()->AddTask( nNewMX, pTask ); + pTask->DecrRef(); + return true; } @@ -529,6 +528,8 @@ bool SSDP::ProcessSearchRequest( const QStringMap &sHeaders, TaskQueue::Instance()->AddTask( nNewMX, pTask ); + pTask->DecrRef(); + return true; } diff --git a/mythtv/libs/libmythupnp/ssdpcache.cpp b/mythtv/libs/libmythupnp/ssdpcache.cpp index 33db52af856..0700842f817 100644 --- a/mythtv/libs/libmythupnp/ssdpcache.cpp +++ b/mythtv/libs/libmythupnp/ssdpcache.cpp @@ -47,14 +47,14 @@ void SSDPCacheEntries::Clear(void) for (; it != m_mapEntries.end(); ++it) { if ((*it)) - (*it)->Release(); + (*it)->DecrRef(); } m_mapEntries.clear(); } /// Finds the Device in the cache, returns NULL when absent -/// \note Caller must call Release on non-NULL DeviceLocation when done with it. +/// \note Caller must call DecrRef on non-NULL DeviceLocation when done with it. DeviceLocation *SSDPCacheEntries::Find(const QString &sUSN) { QMutexLocker locker(&m_mutex); @@ -62,25 +62,25 @@ DeviceLocation *SSDPCacheEntries::Find(const QString &sUSN) EntryMap::iterator it = m_mapEntries.find(GetNormalizedUSN(sUSN)); DeviceLocation *pEntry = (it != m_mapEntries.end()) ? *it : NULL; if (pEntry) - pEntry->AddRef(); + pEntry->IncrRef(); return pEntry; } /// Returns random entry in cache, returns NULL when list is empty -/// \note Caller must call Release on non-NULL DeviceLocation when done with it. +/// \note Caller must call DecrRef on non-NULL DeviceLocation when done with it. DeviceLocation *SSDPCacheEntries::GetFirst(void) { QMutexLocker locker(&m_mutex); if (m_mapEntries.empty()) return NULL; DeviceLocation *loc = *m_mapEntries.begin(); - loc->AddRef(); + loc->IncrRef(); return loc; } /// Returns a copy of the EntryMap -/// \note Caller must call Release() on each entry in the map. +/// \note Caller must call DecrRef() on each entry in the map. void SSDPCacheEntries::GetEntryMap(EntryMap &map) { QMutexLocker locker(&m_mutex); @@ -88,7 +88,7 @@ void SSDPCacheEntries::GetEntryMap(EntryMap &map) EntryMap::const_iterator it = m_mapEntries.begin(); for (; it != m_mapEntries.end(); ++it) { - (*it)->AddRef(); + (*it)->IncrRef(); map.insert(it.key(), *it); } } @@ -98,9 +98,9 @@ void SSDPCacheEntries::Insert(const QString &sUSN, DeviceLocation *pEntry) { QMutexLocker locker(&m_mutex); - pEntry->AddRef(); + pEntry->IncrRef(); - // Since insert overrights anything already there + // Since insert overwrites anything already there // we need to see if the key already exists and release // it's reference if it does. @@ -108,7 +108,7 @@ void SSDPCacheEntries::Insert(const QString &sUSN, DeviceLocation *pEntry) EntryMap::iterator it = m_mapEntries.find(usn); if ((it != m_mapEntries.end()) && (*it != NULL)) - (*it)->Release(); + (*it)->DecrRef(); m_mapEntries[usn] = pEntry; @@ -130,7 +130,7 @@ void SSDPCacheEntries::Remove( const QString &sUSN ) LOG(VB_UPNP, LOG_INFO, QString("SSDP Cache removing USN: %1 Location %2") .arg((*it)->m_sUSN).arg((*it)->m_sLocation)); - (*it)->Release(); + (*it)->DecrRef(); } // -=>TODO: Need to somehow call SSDPCache::NotifyRemove @@ -156,7 +156,7 @@ uint SSDPCacheEntries::RemoveStale(const TaskTime &ttNow) { // Note: locking is not required above since we hold // one reference to each entry and are holding m_mutex. - (*it)->Release(); + (*it)->DecrRef(); // -=>TODO: Need to somehow call SSDPCache::NotifyRemove @@ -184,7 +184,7 @@ QTextStream &SSDPCacheEntries::OutputXML( if (*it == NULL) continue; - // Note: AddRef,Release not required since SSDPCacheEntries + // Note: IncrRef,DecrRef not required since SSDPCacheEntries // holds one reference to each entry and we are holding m_mutex. os << "