Use o3tl::convert in Math
[LibreOffice.git] / cppu / source / threadpool / jobqueue.cxx
blob1be424024d4b171673bc4e947d84e043ab4e7020
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 <sal/config.h>
22 #include <cassert>
24 #include "jobqueue.hxx"
25 #include "threadpool.hxx"
27 namespace cppu_threadpool {
29 JobQueue::JobQueue() :
30 m_nToDo( 0 ),
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 );
41 if( ! m_bSuspended )
43 m_cndWait.notify_all();
45 m_nToDo ++;
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 ) )
56 return nullptr;
58 m_lstCallstack.push_front( nDisposeId );
62 while( true )
64 struct Job job={nullptr,nullptr};
66 std::unique_lock guard( m_mutex );
68 while (m_bSuspended
69 || (m_lstCallstack.front() != nullptr && !bReturnWhenNoJob
70 && m_lstJob.empty()))
72 m_cndWait.wait(guard);
75 if( nullptr == m_lstCallstack.front() )
77 // disposed !
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
82 // response here:
83 m_lstJob.pop_front();
85 break;
88 if( m_lstJob.empty() )
90 assert(bReturnWhenNoJob);
91 break;
94 job = m_lstJob.front();
95 m_lstJob.pop_front();
98 if( job.doRequest )
100 job.doRequest( job.pThreadSpecificData );
101 std::scoped_lock guard( m_mutex );
102 m_nToDo --;
104 else
106 pReturn = job.pThreadSpecificData;
107 std::scoped_lock guard( m_mutex );
108 m_nToDo --;
109 break;
114 // synchronize with the dispose calls
115 std::scoped_lock guard( m_mutex );
116 m_lstCallstack.pop_front();
119 return pReturn;
122 void JobQueue::dispose( void const * nDisposeId )
124 std::scoped_lock guard( m_mutex );
125 for( auto& rId : m_lstCallstack )
127 if( rId == nDisposeId )
129 rId = nullptr;
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 );
143 m_bSuspended = true;
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 );
171 return m_nToDo > 0;
177 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */