]> git.sesse.net Git - vlc/blob - modules/gui/skins2/commands/async_queue.cpp
- all: the text variable "$N" is now the media name, not just a truncated URI
[vlc] / modules / gui / skins2 / commands / async_queue.cpp
1 /*****************************************************************************
2  * async_queue.cpp
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
5  * $Id$
6  *
7  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
8  *          Olivier Teulière <ipkiss@via.ecp.fr>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 #include "async_queue.hpp"
26 #include "../src/os_factory.hpp"
27 #include "../src/os_timer.hpp"
28
29
30 AsyncQueue::AsyncQueue( intf_thread_t *pIntf ): SkinObject( pIntf )
31 {
32     // Initialize the mutex
33     vlc_mutex_init( pIntf, &m_lock );
34
35     // Create a timer
36     OSFactory *pOsFactory = OSFactory::instance( pIntf );
37     m_pTimer = pOsFactory->createOSTimer( Callback( this, &doFlush ) );
38
39     // Flush the queue every 10 ms
40     m_pTimer->start( 10, false );
41 }
42
43
44 AsyncQueue::~AsyncQueue()
45 {
46     delete( m_pTimer );
47     vlc_mutex_destroy( &m_lock );
48 }
49
50
51 AsyncQueue *AsyncQueue::instance( intf_thread_t *pIntf )
52 {
53     if( ! pIntf->p_sys->p_queue )
54     {
55         AsyncQueue *pQueue;
56         pQueue = new AsyncQueue( pIntf );
57         if( pQueue )
58         {
59              // Initialization succeeded
60              pIntf->p_sys->p_queue = pQueue;
61         }
62      }
63      return pIntf->p_sys->p_queue;
64 }
65
66
67 void AsyncQueue::destroy( intf_thread_t *pIntf )
68 {
69     if( pIntf->p_sys->p_queue )
70     {
71         delete pIntf->p_sys->p_queue;
72         pIntf->p_sys->p_queue = NULL;
73     }
74 }
75
76
77 void AsyncQueue::push( const CmdGenericPtr &rcCommand )
78 {
79     m_cmdList.push_back( rcCommand );
80 }
81
82
83 void AsyncQueue::remove( const string &rType )
84 {
85     vlc_mutex_lock( &m_lock );
86
87     list<CmdGenericPtr>::iterator it;
88     for( it = m_cmdList.begin(); it != m_cmdList.end(); it++ )
89     {
90         // Remove the command if it is of the given type
91         if( (*it).get()->getType() == rType )
92         {
93             list<CmdGenericPtr>::iterator itNew = it;
94             itNew++;
95             m_cmdList.erase( it );
96             it = itNew;
97         }
98     }
99
100     vlc_mutex_unlock( &m_lock );
101 }
102
103
104 void AsyncQueue::flush()
105 {
106     while (true)
107     {
108         vlc_mutex_lock( &m_lock );
109
110         if( m_cmdList.size() > 0 )
111         {
112             // Pop the first command from the queue
113             CmdGenericPtr cCommand = m_cmdList.front();
114             m_cmdList.pop_front();
115
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 );
119
120             // Execute the command
121             cCommand.get()->execute();
122         }
123         else
124         {
125             vlc_mutex_unlock( &m_lock );
126             break;
127         }
128     }
129 }
130
131
132 void AsyncQueue::doFlush( SkinObject *pObj )
133 {
134     AsyncQueue *pThis = (AsyncQueue*)pObj;
135     // Flush the queue
136     pThis->flush();
137 }