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