Get the style color and number just once
[LibreOffice.git] / cppu / source / threadpool / threadpool.hxx
blobafcae7a7e35113d3fb1545f5fd57f3577bc27b77
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 <mutex>
23 #include <vector>
24 #include <unordered_map>
26 #include <osl/conditn.hxx>
27 #include <osl/mutex.hxx>
28 #include <rtl/byteseq.hxx>
29 #include <rtl/ref.hxx>
30 #include <salhelper/simplereferenceobject.hxx>
32 #include "jobqueue.hxx"
35 namespace cppu_threadpool {
36 class ORequestThread;
38 struct EqualThreadId
40 bool operator () ( const ::rtl::ByteSequence &a , const ::rtl::ByteSequence &b ) const
42 return a == b;
46 struct HashThreadId
48 sal_Int32 operator () ( const ::rtl::ByteSequence &a ) const
50 if( a.getLength() >= 4 )
52 return *reinterpret_cast<sal_Int32 const *>(a.getConstArray());
54 return 0;
58 typedef std::unordered_map
60 ::rtl::ByteSequence, // ThreadID
61 std::pair < JobQueue * , JobQueue * >,
62 HashThreadId,
63 EqualThreadId
64 > ThreadIdHashMap;
66 struct WaitingThread
68 osl::Condition condition;
69 rtl::Reference< ORequestThread > thread;
71 explicit WaitingThread(
72 rtl::Reference<ORequestThread> theThread);
75 typedef std::deque< struct ::cppu_threadpool::WaitingThread * > WaitingThreadDeque;
77 class DisposedCallerAdmin;
78 typedef std::shared_ptr<DisposedCallerAdmin> DisposedCallerAdminHolder;
80 class DisposedCallerAdmin
82 public:
83 ~DisposedCallerAdmin();
85 static DisposedCallerAdminHolder const & getInstance();
87 void dispose( void const * nDisposeId );
88 void destroy( void const * nDisposeId );
89 bool isDisposed( void const * nDisposeId );
91 private:
92 std::mutex m_mutex;
93 std::vector< void const * > m_vector;
96 class ThreadAdmin
98 public:
99 ThreadAdmin();
100 ~ThreadAdmin ();
102 void remove( rtl::Reference< ORequestThread > const & );
103 void join();
105 bool add_locked( rtl::Reference< ORequestThread > const & );
106 void remove_locked( rtl::Reference< ORequestThread > const & );
107 std::mutex m_mutex;
109 private:
110 std::deque< rtl::Reference< ORequestThread > > m_deque;
111 bool m_disposed;
114 class ThreadPool;
115 typedef rtl::Reference<ThreadPool> ThreadPoolHolder;
117 class ThreadPool: public salhelper::SimpleReferenceObject
119 public:
120 ThreadPool();
121 virtual ~ThreadPool() override;
123 void dispose( void const * nDisposeId );
124 void destroy( void const * nDisposeId );
126 bool addJob( const ::rtl::ByteSequence &aThreadId,
127 bool bAsynchron,
128 void *pThreadSpecificData,
129 RequestFun * doRequest,
130 void const * disposeId );
132 void prepare( const ::rtl::ByteSequence &aThreadId );
133 void * enter( const ::rtl::ByteSequence &aThreadId, void const * nDisposeId );
135 /********
136 * @return true, if queue could be successfully revoked.
137 ********/
138 bool revokeQueue( const ::rtl::ByteSequence & aThreadId , bool bAsynchron );
140 void waitInPool( rtl::Reference< ORequestThread > const & pThread );
142 void joinWorkers();
144 ThreadAdmin & getThreadAdmin() { return m_aThreadAdmin; }
146 private:
147 bool createThread( JobQueue *pQueue, const ::rtl::ByteSequence &aThreadId, bool bAsynchron);
150 ThreadIdHashMap m_mapQueue;
151 std::mutex m_mutex;
153 std::mutex m_mutexWaitingThreadList;
154 WaitingThreadDeque m_dequeThreads;
156 DisposedCallerAdminHolder m_DisposedCallerAdmin;
157 ThreadAdmin m_aThreadAdmin;
160 } // end namespace cppu_threadpool
162 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */