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 <sal/config.h>
24 #include "jobqueue.hxx"
25 #include "threadpool.hxx"
27 namespace cppu_threadpool
{
29 JobQueue::JobQueue() :
31 m_bSuspended( false ),
32 m_DisposedCallerAdmin( DisposedCallerAdmin::getInstance() )
36 void JobQueue::add( void *pThreadSpecificData
, RequestFun
* doRequest
)
38 std::scoped_lock
guard( m_mutex
);
39 Job job
= { pThreadSpecificData
, doRequest
};
40 m_lstJob
.push_back( job
);
43 m_cndWait
.notify_all();
48 void *JobQueue::enter( void const * nDisposeId
, bool bReturnWhenNoJob
)
50 void *pReturn
= nullptr;
52 // synchronize with the dispose calls
53 std::scoped_lock
guard( m_mutex
);
54 if( m_DisposedCallerAdmin
->isDisposed( nDisposeId
) )
58 m_lstCallstack
.push_front( nDisposeId
);
64 struct Job job
={nullptr,nullptr};
66 std::unique_lock
guard( m_mutex
);
69 || (m_lstCallstack
.front() != nullptr && !bReturnWhenNoJob
72 m_cndWait
.wait(guard
);
75 if( nullptr == m_lstCallstack
.front() )
78 if (!m_lstJob
.empty() && m_lstJob
.front().doRequest
== nullptr) {
79 // If this thread was waiting for a remote response, that response may or
80 // may not have been enqueued; if it has not been enqueued, there cannot be
81 // another enqueued response, so it is always correct to remove any enqueued
88 if( m_lstJob
.empty() )
90 assert(bReturnWhenNoJob
);
94 job
= m_lstJob
.front();
100 job
.doRequest( job
.pThreadSpecificData
);
101 std::scoped_lock
guard( m_mutex
);
106 pReturn
= job
.pThreadSpecificData
;
107 std::scoped_lock
guard( m_mutex
);
114 // synchronize with the dispose calls
115 std::scoped_lock
guard( m_mutex
);
116 m_lstCallstack
.pop_front();
122 void JobQueue::dispose( void const * nDisposeId
)
124 std::scoped_lock
guard( m_mutex
);
125 for( auto& rId
: m_lstCallstack
)
127 if( rId
== nDisposeId
)
133 if( !m_lstCallstack
.empty() && ! m_lstCallstack
.front() )
135 // The thread is waiting for a disposed pCallerId, let it go
136 m_cndWait
.notify_all();
140 void JobQueue::suspend()
142 std::scoped_lock
guard( m_mutex
);
146 void JobQueue::resume()
148 std::scoped_lock
guard( m_mutex
);
149 m_bSuspended
= false;
150 if( ! m_lstJob
.empty() )
152 m_cndWait
.notify_all();
156 bool JobQueue::isEmpty() const
158 std::scoped_lock
guard( m_mutex
);
159 return m_lstJob
.empty();
162 bool JobQueue::isCallstackEmpty() const
164 std::scoped_lock
guard( m_mutex
);
165 return m_lstCallstack
.empty();
168 bool JobQueue::isBusy() const
170 std::scoped_lock
guard( m_mutex
);
177 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */