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