bump product version to 4.1.6.2
[LibreOffice.git] / cppu / source / threadpool / threadpool.hxx
blob61c6e0ed7b896d4a6025678025f83ca7ee554a09
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 #ifndef INCLUDED_CPPU_SOURCE_THREADPOOL_THREADPOOL_HXX
21 #define INCLUDED_CPPU_SOURCE_THREADPOOL_THREADPOOL_HXX
23 #include <list>
25 #include <boost/unordered_map.hpp>
27 #include <osl/conditn.h>
29 #include <rtl/byteseq.hxx>
30 #include <rtl/ref.hxx>
31 #include <salhelper/simplereferenceobject.hxx>
33 #include <boost/shared_ptr.hpp>
35 #include "jobqueue.hxx"
38 using namespace ::rtl;
39 namespace cppu_threadpool {
40 class ORequestThread;
42 struct EqualThreadId
44 sal_Int32 operator () ( const ::rtl::ByteSequence &a , const ::rtl::ByteSequence &b ) const
46 return a == b;
50 struct HashThreadId
52 sal_Int32 operator () ( const ::rtl::ByteSequence &a ) const
54 if( a.getLength() >= 4 )
56 return *(sal_Int32 *)a.getConstArray();
58 return 0;
62 typedef ::boost::unordered_map
64 ByteSequence, // ThreadID
65 ::std::pair < JobQueue * , JobQueue * >,
66 HashThreadId,
67 EqualThreadId
68 > ThreadIdHashMap;
70 typedef ::std::list < sal_Int64 > DisposedCallerList;
73 struct WaitingThread
75 oslCondition condition;
76 rtl::Reference< ORequestThread > thread;
79 typedef ::std::list < struct ::cppu_threadpool::WaitingThread * > WaitingThreadList;
81 class DisposedCallerAdmin;
82 typedef boost::shared_ptr<DisposedCallerAdmin> DisposedCallerAdminHolder;
84 class DisposedCallerAdmin
86 public:
87 ~DisposedCallerAdmin();
89 static DisposedCallerAdminHolder getInstance();
91 void dispose( sal_Int64 nDisposeId );
92 void destroy( sal_Int64 nDisposeId );
93 sal_Bool isDisposed( sal_Int64 nDisposeId );
95 private:
96 ::osl::Mutex m_mutex;
97 DisposedCallerList m_lst;
100 class ThreadAdmin
102 public:
103 ThreadAdmin();
104 ~ThreadAdmin ();
106 void add( rtl::Reference< ORequestThread > const & );
107 void remove( rtl::Reference< ORequestThread > const & );
108 void join();
110 void remove_locked( rtl::Reference< ORequestThread > const & );
111 ::osl::Mutex m_mutex;
113 private:
114 ::std::list< rtl::Reference< ORequestThread > > m_lst;
115 bool m_disposed;
118 class ThreadPool;
119 typedef rtl::Reference<ThreadPool> ThreadPoolHolder;
121 class ThreadPool: public salhelper::SimpleReferenceObject
123 public:
124 ThreadPool();
125 ~ThreadPool();
127 void dispose( sal_Int64 nDisposeId );
128 void destroy( sal_Int64 nDisposeId );
130 void addJob( const ByteSequence &aThreadId,
131 sal_Bool bAsynchron,
132 void *pThreadSpecificData,
133 RequestFun * doRequest );
135 void prepare( const ByteSequence &aThreadId );
136 void * enter( const ByteSequence &aThreadId, sal_Int64 nDisposeId );
138 /********
139 * @return true, if queue could be successfully revoked.
140 ********/
141 sal_Bool revokeQueue( const ByteSequence & aThreadId , sal_Bool bAsynchron );
143 void waitInPool( rtl::Reference< ORequestThread > const & pThread );
145 void joinWorkers();
147 ThreadAdmin & getThreadAdmin() { return m_aThreadAdmin; }
149 private:
150 void createThread( JobQueue *pQueue, const ByteSequence &aThreadId, sal_Bool bAsynchron);
153 ThreadIdHashMap m_mapQueue;
154 ::osl::Mutex m_mutex;
156 ::osl::Mutex m_mutexWaitingThreadList;
157 WaitingThreadList m_lstThreads;
159 DisposedCallerAdminHolder m_DisposedCallerAdmin;
160 ThreadAdmin m_aThreadAdmin;
163 } // end namespace cppu_threadpool
165 #endif
167 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */