LibreOffice Module vcl (master) 1
threadex.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#ifndef INCLUDED_VCL_THREADEX_HXX
21#define INCLUDED_VCL_THREADEX_HXX
22
23#include <osl/conditn.hxx>
24#include <tools/link.hxx>
25#include <vcl/dllapi.h>
26
27#include <optional>
28#include <memory>
29#include <utility>
30
31namespace vcl
32{
34 {
35 osl::Condition m_aStart;
36 osl::Condition m_aFinish;
38
39 DECL_DLLPRIVATE_LINK( worker, void*, void );
40
41 public:
43 virtual ~SolarThreadExecutor();
44
45 virtual void doIt() = 0;
46 void execute();
47 };
48
49namespace solarthread {
50
52namespace detail {
53
54template <typename FuncT, typename ResultT>
56{
57public:
58 static ResultT exec( FuncT const& func )
59 {
61 ::std::unique_ptr<ExecutorT> const pExecutor( new ExecutorT(func) );
62 pExecutor->execute();
63 if (pExecutor->m_exc)
64 std::rethrow_exception(pExecutor->m_exc);
65 return *pExecutor->m_result;
66 }
67
68private:
69 explicit GenericSolarThreadExecutor( FuncT func )
70 : m_func(std::move(func)), m_result() {}
71
72 virtual void doIt() override
73 {
74 try {
75 m_result = m_func();
76 }
77 catch (...) {
78 m_exc = std::current_exception();
79 }
80 }
81
82 std::exception_ptr m_exc;
83#ifdef _MSC_VER
84 FuncT m_func; // "const" and std::bind() results in Error C3848 expression would lose const-volatile qualifiers
85#else
86 FuncT const m_func;
87#endif
88 // using std::optional here omits the need that ResultT is default
89 // constructable:
90 ::std::optional<ResultT> m_result;
91};
92
93template <typename FuncT>
94class GenericSolarThreadExecutor<FuncT, void> final : public SolarThreadExecutor
95{
96public:
97 static void exec( FuncT const& func )
98 {
100 ::std::unique_ptr<ExecutorT> const pExecutor( new ExecutorT(func) );
101 pExecutor->execute();
102 if (pExecutor->m_exc)
103 std::rethrow_exception(pExecutor->m_exc);
104 }
105
106private:
107 explicit GenericSolarThreadExecutor( FuncT func )
108 : m_func(std::move(func)) {}
109
110 virtual void doIt() override
111 {
112 try {
113 m_func();
114 }
115 catch (...) {
116 m_exc = std::current_exception();
117 }
118 }
119
120 std::exception_ptr m_exc;
121 FuncT const m_func;
122};
123
124} // namespace detail
125
126
163template <typename FuncT>
164inline auto syncExecute(FuncT const& func) -> decltype(func())
165{
167 FuncT, decltype(func())>::exec(func);
168}
169
170} // namespace solarthread
171} // namespace vcl
172
173#endif // INCLUDED_VCL_THREADEX_HXX
174
175/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual void doIt()=0
DECL_DLLPRIVATE_LINK(worker, void *, void)
osl::Condition m_aStart
Definition: threadex.hxx:35
osl::Condition m_aFinish
Definition: threadex.hxx:36
static ResultT exec(FuncT const &func)
Definition: threadex.hxx:58
#define VCL_DLLPUBLIC
Definition: dllapi.h:29
auto syncExecute(FuncT const &func) -> decltype(func())
This function will execute the passed functor synchronously in the solar thread, thus the calling thr...
Definition: threadex.hxx:164