update dev300-m58
[ooovba.git] / cppu / source / threadpool / jobqueue.cxx
blob120bd86971f2516552b9a20a3aaf736d2c68b93b
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: jobqueue.cxx,v $
10 * $Revision: 1.8 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_cppu.hxx"
33 #include "jobqueue.hxx"
34 #include "threadpool.hxx"
36 #include <osl/diagnose.h>
38 using namespace ::osl;
40 namespace cppu_threadpool {
42 JobQueue::JobQueue() :
43 m_nToDo( 0 ),
44 m_bSuspended( sal_False ),
45 m_cndWait( osl_createCondition() )
47 osl_resetCondition( m_cndWait );
50 JobQueue::~JobQueue()
52 osl_destroyCondition( m_cndWait );
56 void JobQueue::add( void *pThreadSpecificData, RequestFun * doRequest )
58 MutexGuard guard( m_mutex );
59 Job job = { pThreadSpecificData , doRequest };
60 m_lstJob.push_back( job );
61 if( ! m_bSuspended )
63 osl_setCondition( m_cndWait );
65 m_nToDo ++;
68 void *JobQueue::enter( sal_Int64 nDisposeId , sal_Bool bReturnWhenNoJob )
70 void *pReturn = 0;
72 // synchronize with the dispose calls
73 MutexGuard guard( m_mutex );
74 if( DisposedCallerAdmin::getInstance()->isDisposed( nDisposeId ) )
76 return 0;
78 m_lstCallstack.push_front( nDisposeId );
82 while( sal_True )
84 if( bReturnWhenNoJob )
86 MutexGuard guard( m_mutex );
87 if( m_lstJob.empty() )
89 break;
93 osl_waitCondition( m_cndWait , 0 );
95 struct Job job={0,0};
97 // synchronize with add and dispose calls
98 MutexGuard guard( m_mutex );
100 if( 0 == m_lstCallstack.front() )
102 // disposed !
103 break;
106 OSL_ASSERT( ! m_lstJob.empty() );
107 if( ! m_lstJob.empty() )
109 job = m_lstJob.front();
110 m_lstJob.pop_front();
112 if( m_lstJob.empty() )
114 osl_resetCondition( m_cndWait );
118 if( job.doRequest )
120 job.doRequest( job.pThreadSpecificData );
121 m_nToDo --;
123 else
125 m_nToDo --;
126 pReturn = job.pThreadSpecificData;
127 break;
132 // synchronize with the dispose calls
133 MutexGuard guard( m_mutex );
134 m_lstCallstack.pop_front();
137 return pReturn;
140 void JobQueue::dispose( sal_Int64 nDisposeId )
142 MutexGuard guard( m_mutex );
143 for( CallStackList::iterator ii = m_lstCallstack.begin() ;
144 ii != m_lstCallstack.end() ;
145 ++ii )
147 if( (*ii) == nDisposeId )
149 (*ii) = 0;
153 if( !m_lstCallstack.empty() && ! m_lstCallstack.front() )
155 // The thread is waiting for a disposed pCallerId, let it go
156 osl_setCondition( m_cndWait );
160 void JobQueue::suspend()
162 MutexGuard guard( m_mutex );
163 m_bSuspended = sal_True;
166 void JobQueue::resume()
168 MutexGuard guard( m_mutex );
169 m_bSuspended = sal_False;
170 if( ! m_lstJob.empty() )
172 osl_setCondition( m_cndWait );
176 sal_Bool JobQueue::isEmpty()
178 MutexGuard guard( m_mutex );
179 return m_lstJob.empty();
182 sal_Bool JobQueue::isCallstackEmpty()
184 MutexGuard guard( m_mutex );
185 return m_lstCallstack.empty();
188 sal_Bool JobQueue::isBusy()
190 return m_nToDo > 0;