LibreOffice Module cppu (master) 1
thread.cxx
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#include <sal/config.h>
21
22#include <algorithm>
23#include <cstdlib>
24#include <osl/diagnose.h>
25#include <uno/threadpool.h>
26#include <sal/log.hxx>
27#include <utility>
28
29#include "thread.hxx"
30#include "jobqueue.hxx"
31#include "threadpool.hxx"
32
33using namespace osl;
34using namespace rtl;
35
36namespace cppu_threadpool {
37
38
40
42 {
43 SAL_WARN_IF(m_deque.size(), "cppu.threadpool", m_deque.size() << "Threads left");
44 }
45
47 {
48 if( m_disposed )
49 {
50 return false;
51 }
52 m_deque.push_back( p );
53 return true;
54 }
55
57 {
58 m_deque.erase(std::find( m_deque.begin(), m_deque.end(), p ), m_deque.end());
59 }
60
62 {
63 std::scoped_lock aGuard( m_mutex );
65 }
66
68 {
69 {
70 std::scoped_lock aGuard( m_mutex );
71 m_disposed = true;
72 }
73 for (;;)
74 {
76 {
77 std::scoped_lock aGuard( m_mutex );
78 if( m_deque.empty() )
79 {
80 break;
81 }
82 pCurrent = m_deque.front();
83 m_deque.pop_front();
84 }
85 if (pCurrent->getIdentifier()
86 != osl::Thread::getCurrentIdentifier())
87 {
88 pCurrent->join();
89 }
90 }
91 }
92
93
95 JobQueue *pQueue,
96 ByteSequence aThreadId,
97 bool bAsynchron )
98 : m_aThreadPool(std::move( aThreadPool ))
99 , m_pQueue( pQueue )
100 , m_aThreadId(std::move( aThreadId ))
101 , m_bAsynchron( bAsynchron )
102 {}
103
105
107 const ByteSequence &aThreadId,
108 bool bAsynchron )
109 {
110 m_pQueue = pQueue;
111 m_aThreadId = aThreadId;
112 m_bAsynchron = bAsynchron;
113 }
114
116 {
117 // Assumption is that osl::Thread::create returns normally with a true
118 // return value iff it causes osl::Thread::run to start executing:
119 acquire();
120 ThreadAdmin & rThreadAdmin = m_aThreadPool->getThreadAdmin();
121 std::unique_lock g(rThreadAdmin.m_mutex);
122 if (!rThreadAdmin.add_locked( this )) {
123 return false;
124 }
125 try {
126 if (!create()) {
127 std::abort();
128 }
129 } catch (...) {
130 rThreadAdmin.remove_locked( this );
131 g.release();
132 release();
133 throw;
134 }
135 return true;
136 }
137
139 {
140 m_aThreadPool->getThreadAdmin().remove( this );
141 release();
142 }
143
145 {
146 osl_setThreadName("cppu_threadpool::ORequestThread");
147
148 try
149 {
150 while ( m_pQueue )
151 {
152 if( ! m_bAsynchron )
153 {
154 if ( !uno_bindIdToCurrentThread( m_aThreadId.getHandle() ) )
155 {
156 OSL_ASSERT( false );
157 }
158 }
159
160 while( ! m_pQueue->isEmpty() )
161 {
162 // Note : Oneways should not get a disposable disposeid,
163 // It does not make sense to dispose a call in this state.
164 // That's way we put it a disposeid, that can't be used otherwise.
166 this,
167 true );
168
169 if( m_pQueue->isEmpty() )
170 {
171 m_aThreadPool->revokeQueue( m_aThreadId , m_bAsynchron );
172 // Note : revokeQueue might have failed because m_pQueue.isEmpty()
173 // may be false (race).
174 }
175 }
176
177 delete m_pQueue;
178 m_pQueue = nullptr;
179
180 if( ! m_bAsynchron )
181 {
183 }
184
185 m_aThreadPool->waitInPool( this );
186 }
187 }
188 catch (...)
189 {
190 // Work around the problem that onTerminated is not called if run
191 // throws an exception:
192 onTerminated();
193 throw;
194 }
195 }
196}
197
198/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void * enter(void const *nDisposeId, bool bReturnWhenNoJob=false)
Definition: jobqueue.cxx:48
ThreadPoolHolder m_aThreadPool
Definition: thread.hxx:58
void setTask(JobQueue *, const ::rtl::ByteSequence &aThreadId, bool bAsynchron)
Definition: thread.cxx:106
virtual void SAL_CALL onTerminated() override
Definition: thread.cxx:138
::rtl::ByteSequence m_aThreadId
Definition: thread.hxx:60
virtual ~ORequestThread() override
Definition: thread.cxx:104
ORequestThread(ThreadPoolHolder aThreadPool, JobQueue *, ::rtl::ByteSequence aThreadId, bool bAsynchron)
Definition: thread.cxx:94
virtual void SAL_CALL run() override
Definition: thread.cxx:144
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
bool m_disposed
void * p
#define SAL_WARN_IF(condition, area, stream)
css::uno::Reference< css::deployment::XPackageRegistry > create(css::uno::Reference< css::deployment::XPackageRegistry > const &xRootRegistry, OUString const &context, OUString const &cachePath, css::uno::Reference< css::uno::XComponentContext > const &xComponentContext)
void SAL_CALL uno_releaseIdFromCurrentThread() SAL_THROW_EXTERN_C()
Definition: threadident.cxx:75
sal_Bool SAL_CALL uno_bindIdToCurrentThread(sal_Sequence *pThreadId) SAL_THROW_EXTERN_C()
Definition: threadident.cxx:89