1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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() :
31 m_bSuspended( false ),
32 m_cndWait( osl_createCondition() )
34 osl_resetCondition( m_cndWait
);
35 m_DisposedCallerAdmin
= DisposedCallerAdmin::getInstance();
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
);
51 osl_setCondition( m_cndWait
);
56 void *JobQueue::enter( sal_Int64 nDisposeId
, bool bReturnWhenNoJob
)
60 // synchronize with the dispose calls
61 MutexGuard
guard( m_mutex
);
62 if( m_DisposedCallerAdmin
->isDisposed( nDisposeId
) )
66 m_lstCallstack
.push_front( nDisposeId
);
72 if( bReturnWhenNoJob
)
74 MutexGuard
guard( m_mutex
);
75 if( m_lstJob
.empty() )
81 osl_waitCondition( m_cndWait
, 0 );
85 // synchronize with add and dispose calls
86 MutexGuard
guard( m_mutex
);
88 if( 0 == m_lstCallstack
.front() )
92 && (m_lstCallstack
.empty()
93 || m_lstCallstack
.front() != 0) )
95 osl_resetCondition( m_cndWait
);
100 OSL_ASSERT( ! m_lstJob
.empty() );
101 if( ! m_lstJob
.empty() )
103 job
= m_lstJob
.front();
104 m_lstJob
.pop_front();
107 && (m_lstCallstack
.empty() || m_lstCallstack
.front() != 0) )
109 osl_resetCondition( m_cndWait
);
115 job
.doRequest( job
.pThreadSpecificData
);
116 MutexGuard
guard( m_mutex
);
121 pReturn
= job
.pThreadSpecificData
;
122 MutexGuard
guard( m_mutex
);
129 // synchronize with the dispose calls
130 MutexGuard
guard( m_mutex
);
131 m_lstCallstack
.pop_front();
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() ;
144 if( (*ii
) == nDisposeId
)
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
);
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
);
194 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */