1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 #ifndef INCLUDED_VCL_THREADEX_HXX
21 #define INCLUDED_VCL_THREADEX_HXX
23 #include <osl/conditn.hxx>
24 #include <tools/link.hxx>
25 #include <vcl/dllapi.h>
33 class VCL_DLLPUBLIC SolarThreadExecutor
35 osl::Condition m_aStart
;
36 osl::Condition m_aFinish
;
39 DECL_DLLPRIVATE_LINK( worker
, void*, void );
42 SolarThreadExecutor();
43 virtual ~SolarThreadExecutor();
45 virtual void doIt() = 0;
49 namespace solarthread
{
54 template <typename FuncT
, typename ResultT
>
55 class GenericSolarThreadExecutor final
: public SolarThreadExecutor
58 static ResultT
exec( FuncT
const& func
)
60 typedef GenericSolarThreadExecutor
<FuncT
, ResultT
> ExecutorT
;
61 ::std::unique_ptr
<ExecutorT
> const pExecutor( new ExecutorT(func
) );
64 std::rethrow_exception(pExecutor
->m_exc
);
65 return *pExecutor
->m_result
;
69 explicit GenericSolarThreadExecutor( FuncT func
)
70 : m_func(std::move(func
)), m_result() {}
72 virtual void doIt() override
78 m_exc
= std::current_exception();
82 std::exception_ptr m_exc
;
84 FuncT m_func
; // "const" and std::bind() results in Error C3848 expression would lose const-volatile qualifiers
88 // using std::optional here omits the need that ResultT is default
90 ::std::optional
<ResultT
> m_result
;
93 template <typename FuncT
>
94 class GenericSolarThreadExecutor
<FuncT
, void> final
: public SolarThreadExecutor
97 static void exec( FuncT
const& func
)
99 typedef GenericSolarThreadExecutor
<FuncT
, void> ExecutorT
;
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
);
107 explicit GenericSolarThreadExecutor( FuncT func
)
108 : m_func(std::move(func
)) {}
110 virtual void doIt() override
116 m_exc
= std::current_exception();
120 std::exception_ptr m_exc
;
124 } // namespace detail
127 /** This function will execute the passed functor synchronously in the
128 solar thread, thus the calling thread will (eventually) be blocked until
129 the functor has been called.
130 Any exception that came up calling the functor in the solar thread
131 will be caught and rethrown in the calling thread.
132 The result type of this function needs to be default constructable.
133 Please keep in mind not to pass addresses to stack variables
134 (e.g. for out parameters) to foreign threads, use inout_by_ref()
135 for this purpose. For in parameters, this may not affect you, because
136 the functor object is copy constructed into free store. This way
137 you must not use \verbatim std::cref()/std::ref() \endverbatim or similar
138 for objects on your thread's stack.
139 Use inout_by_ref() or inout_by_ptr() for this purpose, e.g.
142 using namespace vcl::solarthread;
145 // calling foo( long & r ):
146 syncExecute( std::bind( &foo, inout_by_ref(n) ) );
147 // calling foo( long * p ):
148 syncExecute( std::bind( &foo, inout_by_ptr(&n) ) );
150 char const* pc = "default";
151 // calling foo( char const** ppc ):
152 syncExecute( std::bind( &foo, inout_by_ptr(&pc) ) );
153 // calling foo( char const*& rpc ):
154 syncExecute( std::bind( &foo, inout_by_ref(pc) ) );
157 @tpl ResultT result type, defaults to FuncT::result_type to seamlessly
158 support mem_fn and bind
159 @tpl FuncT functor type, let your compiler deduce this type
160 @param func functor object to be executed in solar thread
161 @return return value of functor
163 template <typename FuncT
>
164 inline auto syncExecute(FuncT
const& func
) -> decltype(func())
166 return detail::GenericSolarThreadExecutor
<
167 FuncT
, decltype(func())>::exec(func
);
170 } // namespace solarthread
173 #endif // INCLUDED_VCL_THREADEX_HXX
175 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */