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