1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org 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 Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_cppu.hxx"
30 #include "jobqueue.hxx"
31 #include "threadpool.hxx"
33 #include <osl/diagnose.h>
35 using namespace ::osl
;
37 namespace cppu_threadpool
{
39 JobQueue::JobQueue() :
41 m_bSuspended( sal_False
),
42 m_cndWait( osl_createCondition() )
44 osl_resetCondition( m_cndWait
);
45 m_DisposedCallerAdmin
= DisposedCallerAdmin::getInstance();
50 osl_destroyCondition( m_cndWait
);
54 void JobQueue::add( void *pThreadSpecificData
, RequestFun
* doRequest
)
56 MutexGuard
guard( m_mutex
);
57 Job job
= { pThreadSpecificData
, doRequest
};
58 m_lstJob
.push_back( job
);
61 osl_setCondition( m_cndWait
);
66 void *JobQueue::enter( sal_Int64 nDisposeId
, sal_Bool bReturnWhenNoJob
)
70 // synchronize with the dispose calls
71 MutexGuard
guard( m_mutex
);
72 if( m_DisposedCallerAdmin
->isDisposed( nDisposeId
) )
76 m_lstCallstack
.push_front( nDisposeId
);
82 if( bReturnWhenNoJob
)
84 MutexGuard
guard( m_mutex
);
85 if( m_lstJob
.empty() )
91 osl_waitCondition( m_cndWait
, 0 );
95 // synchronize with add and dispose calls
96 MutexGuard
guard( m_mutex
);
98 if( 0 == m_lstCallstack
.front() )
101 if( m_lstJob
.empty() )
103 osl_resetCondition( m_cndWait
);
108 OSL_ASSERT( ! m_lstJob
.empty() );
109 if( ! m_lstJob
.empty() )
111 job
= m_lstJob
.front();
112 m_lstJob
.pop_front();
114 if( m_lstJob
.empty() )
116 osl_resetCondition( m_cndWait
);
122 job
.doRequest( job
.pThreadSpecificData
);
128 pReturn
= job
.pThreadSpecificData
;
134 // synchronize with the dispose calls
135 MutexGuard
guard( m_mutex
);
136 m_lstCallstack
.pop_front();
142 void JobQueue::dispose( sal_Int64 nDisposeId
)
144 MutexGuard
guard( m_mutex
);
145 for( CallStackList::iterator ii
= m_lstCallstack
.begin() ;
146 ii
!= m_lstCallstack
.end() ;
149 if( (*ii
) == nDisposeId
)
155 if( !m_lstCallstack
.empty() && ! m_lstCallstack
.front() )
157 // The thread is waiting for a disposed pCallerId, let it go
158 osl_setCondition( m_cndWait
);
162 void JobQueue::suspend()
164 MutexGuard
guard( m_mutex
);
165 m_bSuspended
= sal_True
;
168 void JobQueue::resume()
170 MutexGuard
guard( m_mutex
);
171 m_bSuspended
= sal_False
;
172 if( ! m_lstJob
.empty() )
174 osl_setCondition( m_cndWait
);
178 sal_Bool
JobQueue::isEmpty()
180 MutexGuard
guard( m_mutex
);
181 return m_lstJob
.empty();
184 sal_Bool
JobQueue::isCallstackEmpty()
186 MutexGuard
guard( m_mutex
);
187 return m_lstCallstack
.empty();
190 sal_Bool
JobQueue::isBusy()