Library of Assembled Shared Sources
future.h
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2023 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43/** Transporting Values and Exceptions between Threads
44 * @class lass::util::experimental::Future
45 *
46 * See:
47 * @arg Peter Dimov: "Transporting Values and Exceptions between Threads",
48 * http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2096.html
49 * @arg Matti Rintala: "Handling Multiple Concurrent Exceptions in C++ Using Futures",
50 * teoksessa Advanced Topics in Exception Handling Techniques,
51 * toim. C. Dony, J. L. Knudsen, A. Romanovsky, A. Tripathi.
52 * LNCS 4419, 301 s, ISBN 3-540-37443-4, DOI 10.1007/11818502_4, Springer-Verlag 2006
53 * http://www.cs.tut.fi/cgi-bin/run/bitti/download/lncs-eh-draft.pdf
54 * @arg Matti Rintala: "Exceptions in remote procedure calls using C++ template metaprogramming",
55 * Software�Practice and Experience, 2007, 37:231-246, 16 s, DOI: 10.1002/spe.754
56 * http://www.cs.tut.fi/cgi-bin/run/bitti/download/spe-mmr-preprint.pdf
57 */
58
59#ifndef LASS_GUARDIAN_OF_INCLUSION_UTIL_FUTURE_H
60#define LASS_GUARDIAN_OF_INCLUSION_UTIL_FUTURE_H
61
62#include "util_common.h"
63#include "shared_ptr.h"
64
65namespace lass
66{
67namespace util
68{
69namespace experimental
70{
71
72/** @ingroup Thread
73 * @relates Future
74 */
75class FutureBindError: public ExceptionMixin<FutureBindError>
76{
77public:
78 FutureBindError(std::string msg, std::string loc): ExceptionMixin<FutureBindError>(std::move(msg), std::move(loc)) {}
79 ~FutureBindError() noexcept {}
80};
81
82
83
84template <typename T>
85class Future
86{
87public:
88
89 Future(): pimpl_(new Impl) {}
90
91 bool isBound() const
92 {
93 TLocker lock(pimpl_->mutex_);
94 return pimpl_->isBound_ || pimpl_->isBadAlloc_ || pimpl_->error_.get();
95 }
96 bool operator!() const { return !isBound(); }
97 explicit operator bool() const { return isBound(); }
98
99 void wait()
100 {
101 while (!isBound())
102 {
103 pimpl_->condition_.wait();
104 }
105 }
106 WaitResult wait(unsigned long milliSeconds)
107 {
108 return pimpl_->condition_.wait(milliSeconds) == waitSuccess && isBound() ? waitSuccess : waitTimeout;
109 }
110
111 const T& operator()() const
112 {
113 wait();
114 TLocker lock(pimpl_->mutex_);
115 if (pimpl_->isBadAlloc_)
116 {
117 throw std::bad_alloc();
118 }
119 if (pimpl_->error_.get())
120 {
121 pimpl_->error_->throwSelf();
122 }
123 return *reinterpret_cast<T*>(pimpl_->value_);
124 }
125
126 void bind(const T& value)
127 {
128 TLocker lock(pimpl_->mutex_);
129 if (pimpl_->isBound())
130 {
131 LASS_THROW_EX(FutureBindError, "Future is already bound");
132 }
133 new (reinterpret_cast<T*>(pimpl_->value_)) T(value);
134 pimpl_->isBound_ = true;
135 pimpl_->condition_.broadcast();
136 }
137 void unbind()
138 {
139 TLocker lock(pimpl_->mutex_);
140 pimpl_->unbind();
141 }
142
143 void error(const RemoteExceptionBase& error)
144 {
145 TLocker lock(pimpl_->mutex_);
146 try
147 {
148 pimpl_->error_ = error.clone();
149 }
150 catch (const std::bad_alloc&)
151 {
152 pimpl_->isBadAlloc_ = true;
153 }
154 pimpl_->condition_.broadcast();
155 }
156 template <typename ExceptionType>
157 void error(const ExceptionType& error)
158 {
159 TLocker lock(pimpl_->mutex_);
160 try
161 {
162 pimpl_->error_ = new RemoteExceptionWrapper<ExceptionType>(error);
163 }
164 catch (const std::bad_alloc&)
165 {
166 pimpl_->isBadAlloc_ = true;
167 }
168 pimpl_->condition_.broadcast();
169 }
170
171private:
172
173 typedef util::Semaphore TMutex;
174 typedef util::Locker<TMutex> TLocker;
175
176 struct Impl
177 {
178 char value_[sizeof(T)];
179 TRemoteExceptionBasePtr error_;
180 Condition condition_;
181 TMutex mutex_;
182 size_t referenceCount_;
183 volatile bool isBound_;
184 volatile bool isBadAlloc_;
185
186 Impl(): isBound_(false) {}
187 ~Impl()
188 {
189 unbind();
190 }
191 void unbind()
192 {
193 if (isBound_)
194 {
195 reinterpret_cast<T*>(value_)->~T();
196 isBound_ = false;
197 }
198 }
199 };
200
202 typedef util::SharedPtr<Impl, util::ObjectStorage, TCounterPolicy> TImplPtr;
203
204 TImplPtr pimpl_;
205};
206
207}
208}
209}
210
211#endif
implementation of CounterPolicy concept, using a counter in the managed object itself.
Lean and mean synchronisation object, without OS support.
Definition thread.h:414
WaitResult
Return code for wait functions.
Definition thread.h:99
@ waitSuccess
Wait is successfully terminated.
Definition thread.h:100
@ waitTimeout
Wait failed because of a timeout.
Definition thread.h:101
general utility, debug facilities, ...
Library for Assembled Shared Sources.
Definition config.h:53