Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

async_queue.cpp

00001 /*****************************************************************************
00002  * async_queue.cpp
00003  *****************************************************************************
00004  * Copyright (C) 2003 the VideoLAN team
00005  * $Id: async_queue.cpp 12207 2005-08-15 15:54:32Z asmax $
00006  *
00007  * Authors: Cyril Deguet     <[email protected]>
00008  *          Olivier Teulière <[email protected]>
00009  *
00010  * This program is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version.
00014  *
00015  * This program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00023  *****************************************************************************/
00024 
00025 #include "async_queue.hpp"
00026 #include "../src/os_factory.hpp"
00027 #include "../src/os_timer.hpp"
00028 
00029 
00030 AsyncQueue::AsyncQueue( intf_thread_t *pIntf ): SkinObject( pIntf ),
00031     m_cmdFlush( this )
00032 {
00033     // Initialize the mutex
00034     vlc_mutex_init( pIntf, &m_lock );
00035 
00036     // Create a timer
00037     OSFactory *pOsFactory = OSFactory::instance( pIntf );
00038     m_pTimer = pOsFactory->createOSTimer( m_cmdFlush );
00039 
00040     // Flush the queue every 10 ms
00041     m_pTimer->start( 10, false );
00042 }
00043 
00044 
00045 AsyncQueue::~AsyncQueue()
00046 {
00047     delete( m_pTimer );
00048     vlc_mutex_destroy( &m_lock );
00049 }
00050 
00051 
00052 AsyncQueue *AsyncQueue::instance( intf_thread_t *pIntf )
00053 {
00054     if( ! pIntf->p_sys->p_queue )
00055     {
00056         AsyncQueue *pQueue;
00057         pQueue = new AsyncQueue( pIntf );
00058         if( pQueue )
00059         {
00060              // Initialization succeeded
00061              pIntf->p_sys->p_queue = pQueue;
00062         }
00063      }
00064      return pIntf->p_sys->p_queue;
00065 }
00066 
00067 
00068 void AsyncQueue::destroy( intf_thread_t *pIntf )
00069 {
00070     if( pIntf->p_sys->p_queue )
00071     {
00072         delete pIntf->p_sys->p_queue;
00073         pIntf->p_sys->p_queue = NULL;
00074     }
00075 }
00076 
00077 
00078 void AsyncQueue::push( const CmdGenericPtr &rcCommand )
00079 {
00080     m_cmdList.push_back( rcCommand );
00081 }
00082 
00083 
00084 void AsyncQueue::remove( const string &rType )
00085 {
00086     vlc_mutex_lock( &m_lock );
00087 
00088     list<CmdGenericPtr>::iterator it;
00089     for( it = m_cmdList.begin(); it != m_cmdList.end(); it++ )
00090     {
00091         // Remove the command if it is of the given type
00092         if( (*it).get()->getType() == rType )
00093         {
00094             list<CmdGenericPtr>::iterator itNew = it;
00095             itNew++;
00096             m_cmdList.erase( it );
00097             it = itNew;
00098         }
00099     }
00100 
00101     vlc_mutex_unlock( &m_lock );
00102 }
00103 
00104 
00105 void AsyncQueue::flush()
00106 {
00107     while (true)
00108     {
00109         vlc_mutex_lock( &m_lock );
00110 
00111         if( m_cmdList.size() > 0 )
00112         {
00113             // Pop the first command from the queue
00114             CmdGenericPtr cCommand = m_cmdList.front();
00115             m_cmdList.pop_front();
00116 
00117             // Unlock the mutex to avoid deadlocks if another thread wants to
00118             // enqueue/remove a command while this one is processed
00119             vlc_mutex_unlock( &m_lock );
00120 
00121             // Execute the command
00122             cCommand.get()->execute();
00123         }
00124         else
00125         {
00126             vlc_mutex_unlock( &m_lock );
00127             break;
00128         }
00129     }
00130 }
00131 
00132 
00133 void AsyncQueue::CmdFlush::execute()
00134 {
00135     // Flush the queue
00136     m_pParent->flush();
00137 }

Generated on Tue Dec 20 10:14:41 2005 for vlc-0.8.4a by  doxygen 1.4.2