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 .
24 #include <unordered_map>
26 #include <osl/conditn.hxx>
27 #include <osl/mutex.hxx>
28 #include <rtl/byteseq.hxx>
29 #include <rtl/ref.hxx>
30 #include <salhelper/simplereferenceobject.hxx>
32 #include "jobqueue.hxx"
35 namespace cppu_threadpool
{
40 bool operator () ( const ::rtl::ByteSequence
&a
, const ::rtl::ByteSequence
&b
) const
48 sal_Int32
operator () ( const ::rtl::ByteSequence
&a
) const
50 if( a
.getLength() >= 4 )
52 return *reinterpret_cast<sal_Int32
const *>(a
.getConstArray());
58 typedef std::unordered_map
60 ::rtl::ByteSequence
, // ThreadID
61 std::pair
< JobQueue
* , JobQueue
* >,
68 osl::Condition condition
;
69 rtl::Reference
< ORequestThread
> thread
;
71 explicit WaitingThread(
72 rtl::Reference
<ORequestThread
> const & theThread
);
75 typedef std::deque
< struct ::cppu_threadpool::WaitingThread
* > WaitingThreadDeque
;
77 class DisposedCallerAdmin
;
78 typedef std::shared_ptr
<DisposedCallerAdmin
> DisposedCallerAdminHolder
;
80 class DisposedCallerAdmin
83 ~DisposedCallerAdmin();
85 static DisposedCallerAdminHolder
const & getInstance();
87 void dispose( void const * nDisposeId
);
88 void destroy( void const * nDisposeId
);
89 bool isDisposed( void const * nDisposeId
);
93 std::vector
< void const * > m_vector
;
102 void remove( rtl::Reference
< ORequestThread
> const & );
105 bool add_locked( rtl::Reference
< ORequestThread
> const & );
106 void remove_locked( rtl::Reference
< ORequestThread
> const & );
110 std::deque
< rtl::Reference
< ORequestThread
> > m_deque
;
115 typedef rtl::Reference
<ThreadPool
> ThreadPoolHolder
;
117 class ThreadPool
: public salhelper::SimpleReferenceObject
121 virtual ~ThreadPool() override
;
123 void dispose( void const * nDisposeId
);
124 void destroy( void const * nDisposeId
);
126 bool addJob( const ::rtl::ByteSequence
&aThreadId
,
128 void *pThreadSpecificData
,
129 RequestFun
* doRequest
,
130 void const * disposeId
);
132 void prepare( const ::rtl::ByteSequence
&aThreadId
);
133 void * enter( const ::rtl::ByteSequence
&aThreadId
, void const * nDisposeId
);
136 * @return true, if queue could be successfully revoked.
138 bool revokeQueue( const ::rtl::ByteSequence
& aThreadId
, bool bAsynchron
);
140 void waitInPool( rtl::Reference
< ORequestThread
> const & pThread
);
144 ThreadAdmin
& getThreadAdmin() { return m_aThreadAdmin
; }
147 bool createThread( JobQueue
*pQueue
, const ::rtl::ByteSequence
&aThreadId
, bool bAsynchron
);
150 ThreadIdHashMap m_mapQueue
;
153 std::mutex m_mutexWaitingThreadList
;
154 WaitingThreadDeque m_dequeThreads
;
156 DisposedCallerAdminHolder m_DisposedCallerAdmin
;
157 ThreadAdmin m_aThreadAdmin
;
160 } // end namespace cppu_threadpool
162 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */