Ticket #4301: upnp_threading_r15096.patch

File upnp_threading_r15096.patch, 9.0 KB (added by mythtv@…, 16 years ago)
  • libs/libmythupnp/threadpool.h

     
    6363
    6464        CEvent              m_Initialized;
    6565        bool                m_bInitialized;
    66                          
     66
    6767        ThreadPool         *m_pThreadPool;
    6868
    69         bool                m_bTermRequested;
     69        volatile bool       m_bTermRequested;
    7070        QString             m_sName;
    7171
    7272        long                m_nIdleTimeoutMS;
     
    7878        virtual void  run();
    7979        virtual void  ProcessWork() = 0;
    8080
    81         bool    IsTermRequested     ();
    82 
    8381    public:
    8482
    8583                 WorkerThread( ThreadPool *pThreadPool, const QString &sName );
    8684        virtual ~WorkerThread();
    8785
    8886        bool     WaitForInitialized( unsigned long msecs );
    89         void     RequestTerminate  ();
    9087        void     SignalWork        ();
    9188        void     SetTimeout        ( long nIdleTimeout );
    9289
  • libs/libmythupnp/httpserver.cpp

     
    239239
    240240        pSocket->SocketDevice()->setBlocking( true );
    241241
    242         while( !IsTermRequested() && bKeepAlive && pSocket->IsValid())
     242        while( !m_bTermRequested && bKeepAlive && pSocket->IsValid())
    243243        {
    244244            bTimeout = 0;
    245245
  • libs/libmythupnp/taskqueue.cpp

     
    3838
    3939Task::Task()
    4040{
    41     m_mutex.lock();
    4241    m_nTaskId = m_nTaskCount++;
    43     m_mutex.unlock();
    4442}
    4543
    4644/////////////////////////////////////////////////////////////////////////////
     
    7472
    7573TaskQueue::~TaskQueue()
    7674{
    77     Clear();
    78 }
    79 
    80 /////////////////////////////////////////////////////////////////////////////
    81 //
    82 /////////////////////////////////////////////////////////////////////////////
    83 
    84 bool TaskQueue::IsTermRequested()
    85 {
    86     m_mutex.lock();
    87     bool bTermRequested = m_bTermRequested;
    88     m_mutex.unlock();
    89 
    90     return( bTermRequested );
    91 }
    92 
    93 /////////////////////////////////////////////////////////////////////////////
    94 //
    95 /////////////////////////////////////////////////////////////////////////////
    96 
    97 void TaskQueue::RequestTerminate( )
    98 {
    99     m_mutex.lock(); 
    10075    m_bTermRequested = true;
    101     m_mutex.unlock();
    10276
    103     // Wait for thread to terminate.
     77    wait();
    10478
    105     wait( 1000 );
     79    Clear();
    10680}
    10781
    10882/////////////////////////////////////////////////////////////////////////////
     
    11387{
    11488    Task *pTask;
    11589
    116     while ( !IsTermRequested() )
     90    while ( !m_bTermRequested )
    11791    {
    11892        // ------------------------------------------------------------------
    11993        // Process Any Tasks that may need to be executed.
  • libs/libmythupnp/ssdp.cpp

     
    6767{
    6868    DisableNotifications();
    6969
     70    m_bTermRequested = true;
     71
     72    wait();
     73
    7074    if (m_pNotifyTask != NULL)
    7175        m_pNotifyTask->Release();
    7276
     
    8387//
    8488/////////////////////////////////////////////////////////////////////////////
    8589
    86 bool SSDP::IsTermRequested()
    87 {
    88     m_lock.lock();
    89     bool bTermRequested = m_bTermRequested;
    90     m_lock.unlock();
    91 
    92     return( bTermRequested );
    93 }
    94 
    95 /////////////////////////////////////////////////////////////////////////////
    96 //
    97 /////////////////////////////////////////////////////////////////////////////
    98 
    99 void SSDP::RequestTerminate(void)
    100 {
    101     m_lock.lock(); 
    102     m_bTermRequested = true;
    103     m_lock.unlock();
    104 
    105     // Call wait to give thread time to terminate.
    106 
    107     wait( 500 );
    108 }
    109 
    110 /////////////////////////////////////////////////////////////////////////////
    111 //
    112 /////////////////////////////////////////////////////////////////////////////
    113 
    11490void SSDP::EnableNotifications()
    11591{
    11692    if ( m_pNotifyTask == NULL )
     
    202178    // Listen for new Requests
    203179    // ----------------------------------------------------------------------
    204180
    205     while (!IsTermRequested())
     181    while (!m_bTermRequested)
    206182    {
    207183        int nMaxSocket = 0;
    208184
  • libs/libmythupnp/upnp.cpp

     
    165165
    166166    if (g_pTaskQueue)
    167167    {
    168         g_pTaskQueue->Clear();
    169         g_pTaskQueue->RequestTerminate();
    170 
    171168        delete g_pTaskQueue;
    172169        g_pTaskQueue = NULL;
    173170    }
    174171
    175172    if (g_pSSDP)
    176173    {
    177         VERBOSE(VB_UPNP, "UPnp::CleanUp() - disabling SSDP notifications");
    178         g_pSSDP->DisableNotifications();
    179         VERBOSE(VB_UPNP, "UPnp::CleanUp() - requesting SSDP terminate");
    180         g_pSSDP->RequestTerminate();
    181 
    182174        delete g_pSSDP;
    183175        g_pSSDP = NULL;
    184176        VERBOSE(VB_UPNP, "UPnp::CleanUp() - deleted SSDP");
  • libs/libmythupnp/threadpool.cpp

     
    129129
    130130WorkerThread::~WorkerThread()
    131131{
    132 }
     132    m_bTermRequested = true;
    133133
    134 /////////////////////////////////////////////////////////////////////////////
    135 //
    136 /////////////////////////////////////////////////////////////////////////////
     134    m_WorkAvailable.SetEvent();
    137135
    138 bool WorkerThread::IsTermRequested()
    139 {
    140     m_mutex.lock();
    141     bool bTermRequested = m_bTermRequested;
    142     m_mutex.unlock();
    143 
    144     return( bTermRequested );
     136    wait();
    145137}
    146138
    147139/////////////////////////////////////////////////////////////////////////////
     
    164156//
    165157/////////////////////////////////////////////////////////////////////////////
    166158
    167 void WorkerThread::RequestTerminate()
    168 {
    169     m_mutex.lock(); 
    170     m_bTermRequested = true;
    171     m_mutex.unlock();
    172 
    173     m_WorkAvailable.SetEvent();
    174 
    175     // Give time for thread to terminate.
    176 
    177     wait( 500 );
    178 }
    179 
    180 /////////////////////////////////////////////////////////////////////////////
    181 //
    182 /////////////////////////////////////////////////////////////////////////////
    183 
    184159void WorkerThread::SignalWork()
    185160{
    186161    m_WorkAvailable.SetEvent();
     
    219194
    220195    timer.start();
    221196
    222     while( !IsTermRequested() )
     197    while( !m_bTermRequested )
    223198    {
    224199        if (m_bAllowTimeout && (timer.elapsed() > m_nIdleTimeoutMS) )
    225200            break;
     
    228203        {
    229204            m_WorkAvailable.ResetEvent();
    230205
    231             if ( !IsTermRequested() )
     206            if ( !m_bTermRequested )
    232207            {
    233208                try
    234209                {
     
    307282
    308283        if (pThread != NULL)
    309284        {
    310             pThread->RequestTerminate();
    311 
    312285            delete pThread;
    313286        }
    314287
  • libs/libmythupnp/taskqueue.h

     
    7373{
    7474    protected:
    7575
    76         TaskMap     m_mapTasks;
    77         QMutex      m_mutex;
    78         bool        m_bTermRequested;
     76        TaskMap             m_mapTasks;
     77        QMutex              m_mutex;
     78        volatile bool       m_bTermRequested;
    7979
    8080    protected:
    8181
    82         bool  IsTermRequested();
    83 
    8482        virtual void run    ();
    8583
    8684    public:
     
    8886                 TaskQueue();
    8987        virtual ~TaskQueue();
    9088
    91         void  RequestTerminate   ( );
    92 
    9389        void  Clear              ( );
    9490        void  AddTask            ( long msec  , Task *pTask );
    9591        void  AddTask            ( TaskTime tt, Task *pTask );
    9692        void  AddTask            ( Task *pTask );
    97                                                          
     93
    9894        Task *GetNextExpiredTask ( TaskTime tt, long nWithinMilliSecs = 50 );
    99                                                                
     95
    10096};
    10197
    10298#endif
  • libs/libmythupnp/ssdp.h

     
    6868
    6969        UPnpNotifyTask     *m_pNotifyTask;
    7070
    71         bool                m_bTermRequested;
     71        volatile bool       m_bTermRequested;
    7272        QMutex              m_lock;
    7373
    7474    protected:
     
    7979        bool    ProcessSearchResponse( const QStringMap &sHeaders );
    8080        bool    ProcessNotify        ( const QStringMap &sHeaders );
    8181
    82         bool    IsTermRequested      ();
    83 
    8482        QString GetHeaderValue    ( const QStringMap &headers,
    8583                                    const QString    &sKey,
    8684                                    const QString    &sDefault );
     
    9694
    9795        virtual void run    ();
    9896
    99                 void RequestTerminate(void);
    100 
    10197                void EnableNotifications ();
    10298                void DisableNotifications();
    10399