Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #ifndef INCLUDED_CPPU_SOURCE_THREADPOOL_THREADPOOL_HXX
21 : #define INCLUDED_CPPU_SOURCE_THREADPOOL_THREADPOOL_HXX
22 :
23 : #include <list>
24 : #include <unordered_map>
25 :
26 : #include <osl/conditn.h>
27 :
28 : #include <rtl/byteseq.hxx>
29 : #include <rtl/ref.hxx>
30 : #include <salhelper/simplereferenceobject.hxx>
31 :
32 : #include <boost/shared_ptr.hpp>
33 :
34 : #include "jobqueue.hxx"
35 :
36 :
37 : namespace cppu_threadpool {
38 : class ORequestThread;
39 :
40 : struct EqualThreadId
41 : {
42 657336 : bool operator () ( const ::rtl::ByteSequence &a , const ::rtl::ByteSequence &b ) const
43 : {
44 657336 : return a == b;
45 : }
46 : };
47 :
48 : struct HashThreadId
49 : {
50 982026 : sal_Int32 operator () ( const ::rtl::ByteSequence &a ) const
51 : {
52 982026 : if( a.getLength() >= 4 )
53 : {
54 982026 : return *reinterpret_cast<sal_Int32 const *>(a.getConstArray());
55 : }
56 0 : return 0;
57 : }
58 : };
59 :
60 : typedef std::unordered_map
61 : <
62 : ::rtl::ByteSequence, // ThreadID
63 : ::std::pair < JobQueue * , JobQueue * >,
64 : HashThreadId,
65 : EqualThreadId
66 : > ThreadIdHashMap;
67 :
68 : typedef ::std::list < sal_Int64 > DisposedCallerList;
69 :
70 :
71 463977 : struct WaitingThread
72 : {
73 : oslCondition condition;
74 : rtl::Reference< ORequestThread > thread;
75 : };
76 :
77 : typedef ::std::list < struct ::cppu_threadpool::WaitingThread * > WaitingThreadList;
78 :
79 : class DisposedCallerAdmin;
80 : typedef boost::shared_ptr<DisposedCallerAdmin> DisposedCallerAdminHolder;
81 :
82 65 : class DisposedCallerAdmin
83 : {
84 : public:
85 : ~DisposedCallerAdmin();
86 :
87 : static DisposedCallerAdminHolder getInstance();
88 :
89 : void dispose( sal_Int64 nDisposeId );
90 : void destroy( sal_Int64 nDisposeId );
91 : bool isDisposed( sal_Int64 nDisposeId );
92 :
93 : private:
94 : ::osl::Mutex m_mutex;
95 : DisposedCallerList m_lst;
96 : };
97 :
98 : class ThreadAdmin
99 : {
100 : public:
101 : ThreadAdmin();
102 : ~ThreadAdmin ();
103 :
104 : bool add( rtl::Reference< ORequestThread > const & );
105 : void remove( rtl::Reference< ORequestThread > const & );
106 : void join();
107 :
108 : void remove_locked( rtl::Reference< ORequestThread > const & );
109 : ::osl::Mutex m_mutex;
110 :
111 : private:
112 : ::std::list< rtl::Reference< ORequestThread > > m_lst;
113 : bool m_disposed;
114 : };
115 :
116 : class ThreadPool;
117 : typedef rtl::Reference<ThreadPool> ThreadPoolHolder;
118 :
119 : class ThreadPool: public salhelper::SimpleReferenceObject
120 : {
121 : public:
122 : ThreadPool();
123 : virtual ~ThreadPool();
124 :
125 : void dispose( sal_Int64 nDisposeId );
126 : void destroy( sal_Int64 nDisposeId );
127 :
128 : bool addJob( const ::rtl::ByteSequence &aThreadId,
129 : bool bAsynchron,
130 : void *pThreadSpecificData,
131 : RequestFun * doRequest );
132 :
133 : void prepare( const ::rtl::ByteSequence &aThreadId );
134 : void * enter( const ::rtl::ByteSequence &aThreadId, sal_Int64 nDisposeId );
135 :
136 : /********
137 : * @return true, if queue could be successfully revoked.
138 : ********/
139 : bool revokeQueue( const ::rtl::ByteSequence & aThreadId , bool bAsynchron );
140 :
141 : void waitInPool( rtl::Reference< ORequestThread > const & pThread );
142 :
143 : void joinWorkers();
144 :
145 1814 : ThreadAdmin & getThreadAdmin() { return m_aThreadAdmin; }
146 :
147 : private:
148 : bool createThread( JobQueue *pQueue, const ::rtl::ByteSequence &aThreadId, bool bAsynchron);
149 :
150 :
151 : ThreadIdHashMap m_mapQueue;
152 : ::osl::Mutex m_mutex;
153 :
154 : ::osl::Mutex m_mutexWaitingThreadList;
155 : WaitingThreadList m_lstThreads;
156 :
157 : DisposedCallerAdminHolder m_DisposedCallerAdmin;
158 : ThreadAdmin m_aThreadAdmin;
159 : };
160 :
161 : } // end namespace cppu_threadpool
162 :
163 : #endif
164 :
165 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|