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 .
23 #include <unordered_map>
25 #include <osl/conditn.hxx>
26 #include <osl/mutex.hxx>
27 #include <rtl/byteseq.hxx>
28 #include <rtl/ref.hxx>
29 #include <salhelper/simplereferenceobject.hxx>
31 #include "jobqueue.hxx"
34 namespace cppu_threadpool
{
39 bool operator () ( const ::rtl::ByteSequence
&a
, const ::rtl::ByteSequence
&b
) const
47 sal_Int32
operator () ( const ::rtl::ByteSequence
&a
) const
49 if( a
.getLength() >= 4 )
51 return *reinterpret_cast<sal_Int32
const *>(a
.getConstArray());
57 typedef std::unordered_map
59 ::rtl::ByteSequence
, // ThreadID
60 std::pair
< JobQueue
* , JobQueue
* >,
67 osl::Condition condition
;
68 rtl::Reference
< ORequestThread
> thread
;
70 explicit WaitingThread(
71 rtl::Reference
<ORequestThread
> const & theThread
);
74 typedef std::deque
< struct ::cppu_threadpool::WaitingThread
* > WaitingThreadDeque
;
76 class DisposedCallerAdmin
;
77 typedef std::shared_ptr
<DisposedCallerAdmin
> DisposedCallerAdminHolder
;
79 class DisposedCallerAdmin
82 ~DisposedCallerAdmin();
84 static DisposedCallerAdminHolder
const & getInstance();
86 void dispose( void const * nDisposeId
);
87 void destroy( void const * nDisposeId
);
88 bool isDisposed( void const * nDisposeId
);
92 std::vector
< void const * > m_vector
;
101 bool add( rtl::Reference
< ORequestThread
> const & );
102 void remove( rtl::Reference
< ORequestThread
> const & );
105 void remove_locked( rtl::Reference
< ORequestThread
> const & );
106 ::osl::Mutex m_mutex
;
109 std::deque
< rtl::Reference
< ORequestThread
> > m_deque
;
114 typedef rtl::Reference
<ThreadPool
> ThreadPoolHolder
;
116 class ThreadPool
: public salhelper::SimpleReferenceObject
120 virtual ~ThreadPool() override
;
122 void dispose( void const * nDisposeId
);
123 void destroy( void const * nDisposeId
);
125 bool addJob( const ::rtl::ByteSequence
&aThreadId
,
127 void *pThreadSpecificData
,
128 RequestFun
* doRequest
,
129 void const * disposeId
);
131 void prepare( const ::rtl::ByteSequence
&aThreadId
);
132 void * enter( const ::rtl::ByteSequence
&aThreadId
, void const * nDisposeId
);
135 * @return true, if queue could be successfully revoked.
137 bool revokeQueue( const ::rtl::ByteSequence
& aThreadId
, bool bAsynchron
);
139 void waitInPool( rtl::Reference
< ORequestThread
> const & pThread
);
143 ThreadAdmin
& getThreadAdmin() { return m_aThreadAdmin
; }
146 bool createThread( JobQueue
*pQueue
, const ::rtl::ByteSequence
&aThreadId
, bool bAsynchron
);
149 ThreadIdHashMap m_mapQueue
;
150 ::osl::Mutex m_mutex
;
152 ::osl::Mutex m_mutexWaitingThreadList
;
153 WaitingThreadDeque m_dequeThreads
;
155 DisposedCallerAdminHolder m_DisposedCallerAdmin
;
156 ThreadAdmin m_aThreadAdmin
;
159 } // end namespace cppu_threadpool
161 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */