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 #ifndef INCLUDED_CPPU_SOURCE_THREADPOOL_THREADPOOL_HXX
21 #define INCLUDED_CPPU_SOURCE_THREADPOOL_THREADPOOL_HXX
24 #include <unordered_map>
26 #include <osl/conditn.h>
28 #include <rtl/byteseq.hxx>
29 #include <rtl/ref.hxx>
30 #include <salhelper/simplereferenceobject.hxx>
32 #include <boost/shared_ptr.hpp>
34 #include "jobqueue.hxx"
37 namespace cppu_threadpool
{
42 bool operator () ( const ::rtl::ByteSequence
&a
, const ::rtl::ByteSequence
&b
) const
50 sal_Int32
operator () ( const ::rtl::ByteSequence
&a
) const
52 if( a
.getLength() >= 4 )
54 return *reinterpret_cast<sal_Int32
const *>(a
.getConstArray());
60 typedef std::unordered_map
62 ::rtl::ByteSequence
, // ThreadID
63 ::std::pair
< JobQueue
* , JobQueue
* >,
68 typedef ::std::list
< sal_Int64
> DisposedCallerList
;
73 oslCondition condition
;
74 rtl::Reference
< ORequestThread
> thread
;
77 typedef ::std::list
< struct ::cppu_threadpool::WaitingThread
* > WaitingThreadList
;
79 class DisposedCallerAdmin
;
80 typedef boost::shared_ptr
<DisposedCallerAdmin
> DisposedCallerAdminHolder
;
82 class DisposedCallerAdmin
85 ~DisposedCallerAdmin();
87 static DisposedCallerAdminHolder
getInstance();
89 void dispose( sal_Int64 nDisposeId
);
90 void destroy( sal_Int64 nDisposeId
);
91 bool isDisposed( sal_Int64 nDisposeId
);
95 DisposedCallerList m_lst
;
104 bool add( rtl::Reference
< ORequestThread
> const & );
105 void remove( rtl::Reference
< ORequestThread
> const & );
108 void remove_locked( rtl::Reference
< ORequestThread
> const & );
109 ::osl::Mutex m_mutex
;
112 ::std::list
< rtl::Reference
< ORequestThread
> > m_lst
;
117 typedef rtl::Reference
<ThreadPool
> ThreadPoolHolder
;
119 class ThreadPool
: public salhelper::SimpleReferenceObject
123 virtual ~ThreadPool();
125 void dispose( sal_Int64 nDisposeId
);
126 void destroy( sal_Int64 nDisposeId
);
128 bool addJob( const ::rtl::ByteSequence
&aThreadId
,
130 void *pThreadSpecificData
,
131 RequestFun
* doRequest
);
133 void prepare( const ::rtl::ByteSequence
&aThreadId
);
134 void * enter( const ::rtl::ByteSequence
&aThreadId
, sal_Int64 nDisposeId
);
137 * @return true, if queue could be successfully revoked.
139 bool revokeQueue( const ::rtl::ByteSequence
& aThreadId
, bool bAsynchron
);
141 void waitInPool( rtl::Reference
< ORequestThread
> const & pThread
);
145 ThreadAdmin
& getThreadAdmin() { return m_aThreadAdmin
; }
148 bool createThread( JobQueue
*pQueue
, const ::rtl::ByteSequence
&aThreadId
, bool bAsynchron
);
151 ThreadIdHashMap m_mapQueue
;
152 ::osl::Mutex m_mutex
;
154 ::osl::Mutex m_mutexWaitingThreadList
;
155 WaitingThreadList m_lstThreads
;
157 DisposedCallerAdminHolder m_DisposedCallerAdmin
;
158 ThreadAdmin m_aThreadAdmin
;
161 } // end namespace cppu_threadpool
165 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */