fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / cppu / source / threadpool / jobqueue.cxx
blobec7b932d7b11f8592bac16473317c55c93c4b8a7
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "jobqueue.hxx"
21 #include "threadpool.hxx"
23 #include <osl/diagnose.h>
25 using namespace ::osl;
27 namespace cppu_threadpool {
29 JobQueue::JobQueue() :
30 m_nToDo( 0 ),
31 m_bSuspended( false ),
32 m_cndWait( osl_createCondition() )
34 osl_resetCondition( m_cndWait );
35 m_DisposedCallerAdmin = DisposedCallerAdmin::getInstance();
38 JobQueue::~JobQueue()
40 osl_destroyCondition( m_cndWait );
44 void JobQueue::add( void *pThreadSpecificData, RequestFun * doRequest )
46 MutexGuard guard( m_mutex );
47 Job job = { pThreadSpecificData , doRequest };
48 m_lstJob.push_back( job );
49 if( ! m_bSuspended )
51 osl_setCondition( m_cndWait );
53 m_nToDo ++;
56 void *JobQueue::enter( sal_Int64 nDisposeId , bool bReturnWhenNoJob )
58 void *pReturn = 0;
60 // synchronize with the dispose calls
61 MutexGuard guard( m_mutex );
62 if( m_DisposedCallerAdmin->isDisposed( nDisposeId ) )
64 return 0;
66 m_lstCallstack.push_front( nDisposeId );
70 while( true )
72 if( bReturnWhenNoJob )
74 MutexGuard guard( m_mutex );
75 if( m_lstJob.empty() )
77 break;
81 osl_waitCondition( m_cndWait , 0 );
83 struct Job job={0,0};
85 // synchronize with add and dispose calls
86 MutexGuard guard( m_mutex );
88 if( 0 == m_lstCallstack.front() )
90 // disposed !
91 if( m_lstJob.empty()
92 && (m_lstCallstack.empty()
93 || m_lstCallstack.front() != 0) )
95 osl_resetCondition( m_cndWait );
97 break;
100 OSL_ASSERT( ! m_lstJob.empty() );
101 if( ! m_lstJob.empty() )
103 job = m_lstJob.front();
104 m_lstJob.pop_front();
106 if( m_lstJob.empty()
107 && (m_lstCallstack.empty() || m_lstCallstack.front() != 0) )
109 osl_resetCondition( m_cndWait );
113 if( job.doRequest )
115 job.doRequest( job.pThreadSpecificData );
116 MutexGuard guard( m_mutex );
117 m_nToDo --;
119 else
121 pReturn = job.pThreadSpecificData;
122 MutexGuard guard( m_mutex );
123 m_nToDo --;
124 break;
129 // synchronize with the dispose calls
130 MutexGuard guard( m_mutex );
131 m_lstCallstack.pop_front();
134 return pReturn;
137 void JobQueue::dispose( sal_Int64 nDisposeId )
139 MutexGuard guard( m_mutex );
140 for( CallStackList::iterator ii = m_lstCallstack.begin() ;
141 ii != m_lstCallstack.end() ;
142 ++ii )
144 if( (*ii) == nDisposeId )
146 (*ii) = 0;
150 if( !m_lstCallstack.empty() && ! m_lstCallstack.front() )
152 // The thread is waiting for a disposed pCallerId, let it go
153 osl_setCondition( m_cndWait );
157 void JobQueue::suspend()
159 MutexGuard guard( m_mutex );
160 m_bSuspended = true;
163 void JobQueue::resume()
165 MutexGuard guard( m_mutex );
166 m_bSuspended = false;
167 if( ! m_lstJob.empty() )
169 osl_setCondition( m_cndWait );
173 bool JobQueue::isEmpty() const
175 MutexGuard guard( m_mutex );
176 return m_lstJob.empty();
179 bool JobQueue::isCallstackEmpty() const
181 MutexGuard guard( m_mutex );
182 return m_lstCallstack.empty();
185 bool JobQueue::isBusy() const
187 MutexGuard guard( m_mutex );
188 return m_nToDo > 0;
194 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */