Version 7.1.7.1, tag libreoffice-7.1.7.1
[LibreOffice.git] / cppu / source / threadpool / threadpool.hxx
blobe112f05805d007fd27a575b7a85adefe19b7d575
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 #pragma once
22 #include <vector>
23 #include <unordered_map>
25 #include <osl/conditn.hxx>
26 #include <osl/mutex.hxx>
27 #include <rtl/byteseq.hxx>
28 #include <rtl/ref.hxx>
29 #include <salhelper/simplereferenceobject.hxx>
31 #include "jobqueue.hxx"
34 namespace cppu_threadpool {
35 class ORequestThread;
37 struct EqualThreadId
39 bool operator () ( const ::rtl::ByteSequence &a , const ::rtl::ByteSequence &b ) const
41 return a == b;
45 struct HashThreadId
47 sal_Int32 operator () ( const ::rtl::ByteSequence &a ) const
49 if( a.getLength() >= 4 )
51 return *reinterpret_cast<sal_Int32 const *>(a.getConstArray());
53 return 0;
57 typedef std::unordered_map
59 ::rtl::ByteSequence, // ThreadID
60 std::pair < JobQueue * , JobQueue * >,
61 HashThreadId,
62 EqualThreadId
63 > ThreadIdHashMap;
65 struct WaitingThread
67 osl::Condition condition;
68 rtl::Reference< ORequestThread > thread;
70 explicit WaitingThread(
71 rtl::Reference<ORequestThread> const & theThread);
74 typedef std::deque< struct ::cppu_threadpool::WaitingThread * > WaitingThreadDeque;
76 class DisposedCallerAdmin;
77 typedef std::shared_ptr<DisposedCallerAdmin> DisposedCallerAdminHolder;
79 class DisposedCallerAdmin
81 public:
82 ~DisposedCallerAdmin();
84 static DisposedCallerAdminHolder const & getInstance();
86 void dispose( void const * nDisposeId );
87 void destroy( void const * nDisposeId );
88 bool isDisposed( void const * nDisposeId );
90 private:
91 ::osl::Mutex m_mutex;
92 std::vector< void const * > m_vector;
95 class ThreadAdmin
97 public:
98 ThreadAdmin();
99 ~ThreadAdmin ();
101 bool add( rtl::Reference< ORequestThread > const & );
102 void remove( rtl::Reference< ORequestThread > const & );
103 void join();
105 void remove_locked( rtl::Reference< ORequestThread > const & );
106 ::osl::Mutex m_mutex;
108 private:
109 std::deque< rtl::Reference< ORequestThread > > m_deque;
110 bool m_disposed;
113 class ThreadPool;
114 typedef rtl::Reference<ThreadPool> ThreadPoolHolder;
116 class ThreadPool: public salhelper::SimpleReferenceObject
118 public:
119 ThreadPool();
120 virtual ~ThreadPool() override;
122 void dispose( void const * nDisposeId );
123 void destroy( void const * nDisposeId );
125 bool addJob( const ::rtl::ByteSequence &aThreadId,
126 bool bAsynchron,
127 void *pThreadSpecificData,
128 RequestFun * doRequest,
129 void const * disposeId );
131 void prepare( const ::rtl::ByteSequence &aThreadId );
132 void * enter( const ::rtl::ByteSequence &aThreadId, void const * nDisposeId );
134 /********
135 * @return true, if queue could be successfully revoked.
136 ********/
137 bool revokeQueue( const ::rtl::ByteSequence & aThreadId , bool bAsynchron );
139 void waitInPool( rtl::Reference< ORequestThread > const & pThread );
141 void joinWorkers();
143 ThreadAdmin & getThreadAdmin() { return m_aThreadAdmin; }
145 private:
146 bool createThread( JobQueue *pQueue, const ::rtl::ByteSequence &aThreadId, bool bAsynchron);
149 ThreadIdHashMap m_mapQueue;
150 ::osl::Mutex m_mutex;
152 ::osl::Mutex m_mutexWaitingThreadList;
153 WaitingThreadDeque m_dequeThreads;
155 DisposedCallerAdminHolder m_DisposedCallerAdmin;
156 ThreadAdmin m_aThreadAdmin;
159 } // end namespace cppu_threadpool
161 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */