LibreOffice Module cppu (master) 1
threadpool.hxx
Go to the documentation of this file.
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#pragma once
21
22#include <mutex>
23#include <vector>
24#include <unordered_map>
25
26#include <osl/conditn.hxx>
27#include <osl/mutex.hxx>
28#include <rtl/byteseq.hxx>
29#include <rtl/ref.hxx>
31
32#include "jobqueue.hxx"
33
34
35namespace cppu_threadpool {
36 class ORequestThread;
37
39 {
40 bool operator () ( const ::rtl::ByteSequence &a , const ::rtl::ByteSequence &b ) const
41 {
42 return a == b;
43 }
44 };
45
47 {
48 sal_Int32 operator () ( const ::rtl::ByteSequence &a ) const
49 {
50 if( a.getLength() >= 4 )
51 {
52 return *reinterpret_cast<sal_Int32 const *>(a.getConstArray());
53 }
54 return 0;
55 }
56 };
57
58 typedef std::unordered_map
59 <
60 ::rtl::ByteSequence, // ThreadID
61 std::pair < JobQueue * , JobQueue * >,
62 HashThreadId,
63 EqualThreadId
65
67 {
68 osl::Condition condition;
70
71 explicit WaitingThread(
73 };
74
75 typedef std::deque< struct ::cppu_threadpool::WaitingThread * > WaitingThreadDeque;
76
78 typedef std::shared_ptr<DisposedCallerAdmin> DisposedCallerAdminHolder;
79
81 {
82 public:
84
86
87 void dispose( void const * nDisposeId );
88 void destroy( void const * nDisposeId );
89 bool isDisposed( void const * nDisposeId );
90
91 private:
92 std::mutex m_mutex;
93 std::vector< void const * > m_vector;
94 };
95
97 {
98 public:
100 ~ThreadAdmin ();
101
103 void join();
104
107 std::mutex m_mutex;
108
109 private:
110 std::deque< rtl::Reference< ORequestThread > > m_deque;
112 };
113
114 class ThreadPool;
116
118 {
119 public:
120 ThreadPool();
121 virtual ~ThreadPool() override;
122
123 void dispose( void const * nDisposeId );
124 void destroy( void const * nDisposeId );
125
126 bool addJob( const ::rtl::ByteSequence &aThreadId,
127 bool bAsynchron,
128 void *pThreadSpecificData,
129 RequestFun * doRequest,
130 void const * disposeId );
131
132 void prepare( const ::rtl::ByteSequence &aThreadId );
133 void * enter( const ::rtl::ByteSequence &aThreadId, void const * nDisposeId );
134
135 /********
136 * @return true, if queue could be successfully revoked.
137 ********/
138 bool revokeQueue( const ::rtl::ByteSequence & aThreadId , bool bAsynchron );
139
140 void waitInPool( rtl::Reference< ORequestThread > const & pThread );
141
142 void joinWorkers();
143
145
146 private:
147 bool createThread( JobQueue *pQueue, const ::rtl::ByteSequence &aThreadId, bool bAsynchron);
148
149
151 std::mutex m_mutex;
152
155
158 };
159
160} // end namespace cppu_threadpool
161
162/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static DisposedCallerAdminHolder const & getInstance()
Definition: threadpool.cxx:45
void dispose(void const *nDisposeId)
Definition: threadpool.cxx:56
bool isDisposed(void const *nDisposeId)
Definition: threadpool.cxx:68
std::vector< void const * > m_vector
Definition: threadpool.hxx:93
void destroy(void const *nDisposeId)
Definition: threadpool.cxx:62
void remove_locked(rtl::Reference< ORequestThread > const &)
Definition: thread.cxx:56
void remove(rtl::Reference< ORequestThread > const &)
Definition: thread.cxx:61
std::deque< rtl::Reference< ORequestThread > > m_deque
Definition: threadpool.hxx:110
bool add_locked(rtl::Reference< ORequestThread > const &)
Definition: thread.cxx:46
void * enter(const ::rtl::ByteSequence &aThreadId, void const *nDisposeId)
Definition: threadpool.cxx:290
WaitingThreadDeque m_dequeThreads
Definition: threadpool.hxx:154
void prepare(const ::rtl::ByteSequence &aThreadId)
Definition: threadpool.cxx:273
void destroy(void const *nDisposeId)
Definition: threadpool.cxx:103
DisposedCallerAdminHolder m_DisposedCallerAdmin
Definition: threadpool.hxx:156
ThreadIdHashMap m_mapQueue
Definition: threadpool.hxx:150
void dispose(void const *nDisposeId)
Definition: threadpool.cxx:85
ThreadAdmin & getThreadAdmin()
Definition: threadpool.hxx:144
bool addJob(const ::rtl::ByteSequence &aThreadId, bool bAsynchron, void *pThreadSpecificData, RequestFun *doRequest, void const *disposeId)
Definition: threadpool.cxx:220
bool revokeQueue(const ::rtl::ByteSequence &aThreadId, bool bAsynchron)
Definition: threadpool.cxx:178
void waitInPool(rtl::Reference< ORequestThread > const &pThread)
Definition: threadpool.cxx:113
virtual ~ThreadPool() override
Definition: threadpool.cxx:80
bool createThread(JobQueue *pQueue, const ::rtl::ByteSequence &aThreadId, bool bAsynchron)
Definition: threadpool.cxx:150
uno_Any a
std::shared_ptr< DisposedCallerAdmin > DisposedCallerAdminHolder
Definition: jobqueue.hxx:41
std::deque< struct ::cppu_threadpool::WaitingThread * > WaitingThreadDeque
Definition: threadpool.hxx:75
rtl::Reference< ThreadPool > ThreadPoolHolder
Definition: threadpool.hxx:114
std::unordered_map< ::rtl::ByteSequence, std::pair< JobQueue *, JobQueue * >, HashThreadId, EqualThreadId > ThreadIdHashMap
Definition: threadpool.hxx:64
void() RequestFun(void *)
Definition: jobqueue.hxx:33
bool operator()(const ::rtl::ByteSequence &a, const ::rtl::ByteSequence &b) const
Definition: threadpool.hxx:40
sal_Int32 operator()(const ::rtl::ByteSequence &a) const
Definition: threadpool.hxx:48
WaitingThread(rtl::Reference< ORequestThread > theThread)
Definition: threadpool.cxx:41
rtl::Reference< ORequestThread > thread
Definition: threadpool.hxx:69