1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
7 * Authors: Cyril Deguet <asmax@via.ecp.fr>
8 * Olivier Teulière <ipkiss@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 #include "async_queue.hpp"
26 #include "../src/os_factory.hpp"
27 #include "../src/os_timer.hpp"
30 AsyncQueue::AsyncQueue( intf_thread_t *pIntf ): SkinObject( pIntf )
32 // Initialize the mutex
33 vlc_mutex_init( pIntf, &m_lock );
36 OSFactory *pOsFactory = OSFactory::instance( pIntf );
37 m_pTimer = pOsFactory->createOSTimer( Callback( this, &doFlush ) );
39 // Flush the queue every 10 ms
40 m_pTimer->start( 10, false );
44 AsyncQueue::~AsyncQueue()
47 vlc_mutex_destroy( &m_lock );
51 AsyncQueue *AsyncQueue::instance( intf_thread_t *pIntf )
53 if( ! pIntf->p_sys->p_queue )
56 pQueue = new AsyncQueue( pIntf );
59 // Initialization succeeded
60 pIntf->p_sys->p_queue = pQueue;
63 return pIntf->p_sys->p_queue;
67 void AsyncQueue::destroy( intf_thread_t *pIntf )
69 if( pIntf->p_sys->p_queue )
71 delete pIntf->p_sys->p_queue;
72 pIntf->p_sys->p_queue = NULL;
77 void AsyncQueue::push( const CmdGenericPtr &rcCommand )
79 m_cmdList.push_back( rcCommand );
83 void AsyncQueue::remove( const string &rType )
85 vlc_mutex_lock( &m_lock );
87 list<CmdGenericPtr>::iterator it;
88 for( it = m_cmdList.begin(); it != m_cmdList.end(); it++ )
90 // Remove the command if it is of the given type
91 if( (*it).get()->getType() == rType )
93 list<CmdGenericPtr>::iterator itNew = it;
95 m_cmdList.erase( it );
100 vlc_mutex_unlock( &m_lock );
104 void AsyncQueue::flush()
108 vlc_mutex_lock( &m_lock );
110 if( m_cmdList.size() > 0 )
112 // Pop the first command from the queue
113 CmdGenericPtr cCommand = m_cmdList.front();
114 m_cmdList.pop_front();
116 // Unlock the mutex to avoid deadlocks if another thread wants to
117 // enqueue/remove a command while this one is processed
118 vlc_mutex_unlock( &m_lock );
120 // Execute the command
121 cCommand.get()->execute();
125 vlc_mutex_unlock( &m_lock );
132 void AsyncQueue::doFlush( SkinObject *pObj )
134 AsyncQueue *pThis = (AsyncQueue*)pObj;