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>
34 class VCL_DLLPUBLIC SolarThreadExecutor
36 osl::Condition m_aStart
;
37 osl::Condition m_aFinish
;
40 DECL_DLLPRIVATE_LINK( worker
, void*, void );
43 SolarThreadExecutor();
44 virtual ~SolarThreadExecutor();
46 virtual void doIt() = 0;
50 namespace solarthread
{
55 template <typename FuncT
, typename ResultT
>
56 class GenericSolarThreadExecutor final
: public SolarThreadExecutor
59 static ResultT
exec( FuncT
const& func
)
61 typedef GenericSolarThreadExecutor
<FuncT
, ResultT
> ExecutorT
;
62 ::std::unique_ptr
<ExecutorT
> const pExecutor( new ExecutorT(func
) );
65 std::rethrow_exception(pExecutor
->m_exc
);
66 return *pExecutor
->m_result
;
70 explicit GenericSolarThreadExecutor( FuncT func
)
71 : m_func(std::move(func
)), m_result() {}
73 virtual void doIt() override
79 m_exc
= std::current_exception();
83 std::exception_ptr m_exc
;
85 FuncT m_func
; // "const" and std::bind() results in Error C3848 expression would lose const-volatile qualifiers
89 // using std::optional here omits the need that ResultT is default
91 ::std::optional
<ResultT
> m_result
;
94 template <typename FuncT
>
95 class GenericSolarThreadExecutor
<FuncT
, void> final
: public SolarThreadExecutor
98 static void exec( FuncT
const& func
)
100 typedef GenericSolarThreadExecutor
<FuncT
, void> ExecutorT
;
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
);
108 explicit GenericSolarThreadExecutor( FuncT func
)
109 : m_func(std::move(func
)) {}
111 virtual void doIt() override
117 m_exc
= std::current_exception();
121 std::exception_ptr m_exc
;
125 } // namespace detail
128 /** This function will execute the passed functor synchronously in the
129 solar thread, thus the calling thread will (eventually) be blocked until
130 the functor has been called.
131 Any exception that came up calling the functor in the solar thread
132 will be caught and rethrown in the calling thread.
133 The result type of this function needs to be default constructable.
134 Please keep in mind not to pass addresses to stack variables
135 (e.g. for out parameters) to foreign threads, use inout_by_ref()
136 for this purpose. For in parameters, this may not affect you, because
137 the functor object is copy constructed into free store. This way
138 you must not use \verbatim std::cref()/std::ref() \endverbatim or similar
139 for objects on your thread's stack.
140 Use inout_by_ref() or inout_by_ptr() for this purpose, e.g.
143 using namespace vcl::solarthread;
146 // calling foo( long & r ):
147 syncExecute( std::bind( &foo, inout_by_ref(n) ) );
148 // calling foo( long * p ):
149 syncExecute( std::bind( &foo, inout_by_ptr(&n) ) );
151 char const* pc = "default";
152 // calling foo( char const** ppc ):
153 syncExecute( std::bind( &foo, inout_by_ptr(&pc) ) );
154 // calling foo( char const*& rpc ):
155 syncExecute( std::bind( &foo, inout_by_ref(pc) ) );
158 @tpl ResultT result type, defaults to FuncT::result_type to seamlessly
159 support mem_fn and bind
160 @tpl FuncT functor type, let your compiler deduce this type
161 @param func functor object to be executed in solar thread
162 @return return value of functor
164 template <typename FuncT
>
165 inline auto syncExecute(FuncT
const& func
) -> decltype(func())
167 return detail::GenericSolarThreadExecutor
<
168 FuncT
, decltype(func())>::exec(func
);
171 } // namespace solarthread
174 #endif // INCLUDED_VCL_THREADEX_HXX
176 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */