c++-gtk-utils
future.h
Go to the documentation of this file.
1 /* Copyright (C) 2010 to 2015 Chris Vine
2 
3 The library comprised in this file or of which this file is part is
4 distributed by Chris Vine under the GNU Lesser General Public
5 License as follows:
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Lesser General Public License
9  as published by the Free Software Foundation; either version 2.1 of
10  the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful, but
13  WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Lesser General Public License, version 2.1, for more details.
16 
17  You should have received a copy of the GNU Lesser General Public
18  License, version 2.1, along with this library (see the file LGPL.TXT
19  which came with this source code package in the src/utils sub-directory);
20  if not, write to the Free Software Foundation, Inc.,
21  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 
23 However, it is not intended that the object code of a program whose
24 source code instantiates a template from this file or uses macros or
25 inline functions (of any length) should by reason only of that
26 instantiation or use be subject to the restrictions of use in the GNU
27 Lesser General Public License. With that in mind, the words "and
28 macros, inline functions and instantiations of templates (of any
29 length)" shall be treated as substituted for the words "and small
30 macros and small inline functions (ten lines or less in length)" in
31 the fourth paragraph of section 5 of that licence. This does not
32 affect any other reason why object code may be subject to the
33 restrictions in that licence (nor for the avoidance of doubt does it
34 affect the application of section 2 of that licence to modifications
35 of the source code in this file).
36 
37 */
38 
39 #ifndef CGU_FUTURE_H
40 #define CGU_FUTURE_H
41 
42 #include <memory>
43 #include <exception>
44 #include <utility> // for std::move and std::forward
45 #include <type_traits> // for std::remove_reference and std::remove_const
46 
47 #include <pthread.h>
48 #include <glib.h>
49 
50 #include <c++-gtk-utils/thread.h>
51 #include <c++-gtk-utils/mutex.h>
52 #include <c++-gtk-utils/callback.h>
55 #include <c++-gtk-utils/emitter.h>
56 #include <c++-gtk-utils/timeout.h>
58 
59 namespace Cgu {
60 
61 namespace Thread {
62 
63 struct FutureThreadError: public std::exception {
64  virtual const char* what() const throw() {return "FutureThreadError\n";}
65 };
66 
67 struct FutureWhenError: public std::exception {
68  virtual const char* what() const throw() {return "FutureWhenError\n";}
69 };
70 
71 /**
72  * @class Cgu::Thread::Future future.h c++-gtk-utils/future.h
73  * @brief A class representing a pthread thread which will
74  * provide a value.
75  * @sa Cgu::Thread::Thread Cgu::Thread::JoinableHandle Cgu::AsyncResult Cgu::Thread::make_future() Cgu::Thread::TaskManager
76  *
77  * The Thread::Future class will launch a worker thread, run the
78  * function it represents in that thread until it returns, and store
79  * the return value so that it can be waited on and/or extracted by
80  * another thread. A new Thread::Future object representing the
81  * function to be called is normally created by calling
82  * Cgu::Thread::make_future() with a callable object, such as a lambda
83  * expression or the return value of std::bind. The worker thread is
84  * then started by calling run(), and the value extracted or waited
85  * for by calling get(). The run() method can only be called once,
86  * but any number of threads can wait for and/or extract the return
87  * value by calling the get() method. The class also provides a
88  * move_get() method, and a SafeEmitter @ref DoneEmitterAnchor
89  * "done_emitter" public object which emits when the worker thread has
90  * finished, and an associated when() function.
91  *
92  * The template parameter type of Thread::Future is the type of the
93  * return value of the function or callable object called by the
94  * Thread::Future object. The return value can be any type, including
95  * any arbitrarily large tuple or other struct or standard C++
96  * container.
97  *
98  * A Thread::Future object cannot represent a function with a void
99  * return type - a compilation error will result if that is attempted.
100  * If no return value is wanted, then the Thread::Thread class can be
101  * used directly. (However, if in a particular usage this class is
102  * thought to be more convenient, the function to be represented by it
103  * can be wrapped by another function which provides a dummy return
104  * value, such as a dummy int. One possible case for this is where
105  * more than one thread wants to wait for the worker thread to
106  * terminate, as pthread_join() and so Thread::Thread::join() only
107  * give defined behaviour when called by one thread.)
108  *
109  * A future object can also be constructed with Thread::make_future()
110  * and Thread::Future::make() functions which take a function pointer
111  * (or an object reference and member function pointer) with bound
112  * arguments, but these offer little advantage over using std::bind,
113  * so generally it is easier to pass a callable object. These
114  * functions can take up to three bound arguments in the case of a
115  * non-static member function, and four bound arguments in the case of
116  * any other function. In the case of a non-static member function,
117  * the referenced object whose member function is to be called must
118  * remain in existence until the worker thread has completed. The
119  * target function passed by pointer (or member function pointer) can
120  * take a reference to const argument, as a copy of the object to be
121  * passed to the argument is taken to avoid dangling references, but
122  * it cannot take a reference to non-const argument.
123  *
124  * It is to be noted that the target function or callable object to be
125  * represented by a Thread::Future object must not allow any exception
126  * other than Thread::Exit, an exception deriving from std::exception
127  * or a cancellation pseudo-exception to escape from it when it is
128  * executed. This includes ensuring that, for any function's bound
129  * argument which is of class type and not taken by reference, the
130  * argument's copy constructor does not throw anything other than
131  * these, and that the move assignment operator (or if none, copy
132  * assignment operator) of the return value (if of class type) of the
133  * target function or callable object does not throw anything other
134  * than these. (If the target function or callable object, or the
135  * copy constructor of a bound value argument or the move or copy
136  * assignment operator of the return value, throws Thread::Exit or an
137  * exception deriving from std::exception, the exception is safely
138  * consumed and the Thread::Future object's error flag is set.
139  * However, if the move assignment operator or copy assignment
140  * operator, as the case may be, of the return value throws, it should
141  * leave the movee/assignee in a state in which it can safely be
142  * destroyed and in which, if that movee/assignee is further copied or
143  * moved from, the copy or move either throws an exception or produces
144  * an object which can also be destroyed -- but these are minimum
145  * requirements for any reasonable assignment operator, and met by any
146  * assignment operator offering the basic exception guarantee.)
147  *
148  * The Thread::Future object will store the return value of the target
149  * function or callable object, so that it is available to the get()
150  * and move_get() methods and any 'when' callback, and therefore
151  * either move it, or if it has no move assignment operator, copy it
152  * once.
153  *
154  * For safety reasons, the get() method returns by value and so will
155  * cause the return value to be copied once more, so for return values
156  * comprising complex class objects which are to be extracted using
157  * the get() method, it is often better if the function represented by
158  * the Thread::Future object allocates the return value on free store
159  * and returns it by pointer, by Cgu::SharedLockPtr, or by a
160  * std::shared_ptr implementation which has a thread-safe reference
161  * count. Alternatively, from version 2.0.11 a move_get() method is
162  * provided which will make a move operation instead of a copy if the
163  * return type implements a move constructor, but see the
164  * documentation on move_get() for the caveats with respect to its
165  * use: in particular, if move_get() is to be called by a thread, then
166  * get() may not normally be called by another thread, nor should the
167  * when() method be called.
168  *
169  * It should be noted that where the when() method is used, the return
170  * value is passed to the 'when' callback by reference to const and so
171  * without the copying carried out by the get() method: therefore, if
172  * the return value has a move assignment operator and the when()
173  * method is to be employed, and the 'when' callback only needs to
174  * call const methods of the return value, it may be more efficient
175  * not to allocate the return value on free store.
176  *
177  * This is a usage example:
178  *
179  * @code
180  * std::vector<long> get_primes(int n); // calculates the first n primes
181  *
182  * // get the first 1,000 primes
183  * using namespace Cgu;
184  *
185  * auto future = Thread::make_future([] () {return get_primes(1000);});
186  *
187  * future->run();
188  * ... [ do something else ] ...
189  * std::vector<long> result(future->move_get());
190  * std::for_each(result.begin(), result.end(), [](long l) {std::cout << l << std::endl;});
191  * @endcode
192  *
193  * The Cgu::Thread::Future::when() functions
194  * -----------------------------------------
195  *
196  * From version 2.0.2, the return value of the thread function
197  * represented by Cgu::Thread::Future can be obtained asynchronously
198  * using Cgu::Thread::Future::when() to execute a function in a glib
199  * main loop when the thread function completes. The above example
200  * could be reimplemented as:
201  *
202  * @code
203  * std::vector<long> get_primes(int n); // calculates the first n primes
204  *
205  * using namespace Cgu;
206  *
207  * auto future = Thread::make_future([] () {return get_primes(1000);});
208  * auto w = [](const std::vector<long>& vec) {
209  * for (const auto& elt: vec) {std::cout << elt << std::endl;}
210  * };
211  * future->when(Callback::lambda<const std::vector<long>>(std::move(w)));
212  * future->run();
213  * @endcode
214  *
215  * The Cgu::Thread::Future::fail() functions
216  * -----------------------------------------
217  *
218  * The Thread::Future::when() functions have an associated optional
219  * Thread::Future::fail() function which causes a 'fail' callback to
220  * execute in a glib main loop in the event of certain exceptions
221  * arising in executing the thread function or a thread being
222  * cancelled (the documentation on Thread::Future::fail() gives
223  * further details). The 'fail' callback must be fully bound. Whilst
224  * a worker thread can pass error status to the 'fail' callback via
225  * shared data bound to both the thread function and the 'fail'
226  * callback (held by, say, a SharedLockPtr object), or a global error
227  * stack, 'fail' callbacks are generally best reserved either for use
228  * with entirely unexpected exceptions, where the most reasonable
229  * course is to perform some orderly logging and shutdown, or to
230  * report thread cancellation. For handlable exceptions, in an
231  * asynchronous environment the best course is often to catch them and
232  * deal with them in the thread function itself and return a value of
233  * the return type for the 'when' callback indicating no result.
234  */
235 
236 #ifndef DOXYGEN_PARSING
237 namespace FutureHelper {
238 
239 // the sole purpose of this struct is to enable a callback object to
240 // be constructed with Callback::make_ref() which takes an argument
241 // which can be mutated when the callback is executed. Normally this
242 // would be unsafe: however in this particular use it is fine as the
243 // callback is only ever executed once, via Future::run().
244 template <class Val>
245 struct WhenWrapperArg {
246  mutable std::unique_ptr<const Cgu::Callback::CallbackArg<const Val&>> when;
247  // TODO: these constructors are a work-around for a bug in gcc <
248  // 4.6. At any API break where the required version of gcc is
249  // increased to gcc-4.6 or higher, remove them.
250  WhenWrapperArg(std::unique_ptr<const Cgu::Callback::CallbackArg<const Val&>>&& when_) :
251  when(std::move(when_)) {}
252  WhenWrapperArg(WhenWrapperArg&& w): when(std::move(w.when)) {}
253 };
254 
255 // the sole purpose of this struct is to enable a callback object to
256 // be constructed with Callback::make_ref() which takes an argument
257 // which can be mutated when the callback is executed. Normally this
258 // would be unsafe: however in this particular use it is fine as the
259 // callback is only ever executed once, via Future::run().
260 template <class Val>
261 struct WhenWrapperArgRel {
262  mutable std::unique_ptr<Cgu::SafeEmitterArg<const Val&>> when;
263  // TODO: these constructors are a work-around for a bug in gcc <
264  // 4.6. At any API break where the required version of gcc is
265  // increased to gcc-4.6 or higher, remove them.
266  WhenWrapperArgRel(std::unique_ptr<Cgu::SafeEmitterArg<const Val&>>&& when_) :
267  when(std::move(when_)) {}
268  WhenWrapperArgRel(WhenWrapperArgRel&& w): when(std::move(w.when)) {}
269 };
270 
271 } // namespace FutureHelper
272 #endif // DOXYGEN_PARSING
273 
274 
275 template <class Val>
277 
278  std::unique_ptr<Cgu::Thread::Thread> thread_u;
279  std::unique_ptr<Cgu::Callback::Callback> cb_u;
280 
281  mutable Mutex mutex;
282  Cond cond;
283  Val val;
284  bool done;
285  bool running;
286  bool error;
287  bool emitter_error;
288 
289  template <class T, class Ret, class... Args>
290  void run_wrapper(T*, Ret (T::*)(Args...), const Args&...);
291 
292  template <class T, class Ret, class... Args>
293  void run_wrapper_const(const T*, Ret (T::*)(Args...) const, const Args&...);
294 
295  template <class Ret, class... Args>
296  void run_wrapper_static(Ret (*)(Args...), const Args&...);
297 
298  template <class Func>
299  void run_wrapper_functor(Func&);
300 
301  void cancel_cleanup();
302 
303  void execute_done(const std::unique_ptr<const Cgu::Callback::CallbackArg<const Val&>>&);
304  void post_done(const FutureHelper::WhenWrapperArg<Val>&,
305  gint, GMainContext*);
306  void execute_done_rel(const std::unique_ptr<Cgu::SafeEmitterArg<const Val&>>&);
307  void post_done_rel(const FutureHelper::WhenWrapperArgRel<Val>&,
308  gint, GMainContext*);
309 
310  // this is a static function taking the future object by IntrusivePtr to
311  // ensure that the future object remains in existence whilst this
312  // function might execute
313  static void fail_cb(const Cgu::IntrusivePtr<Future<Val>>& future,
314  const std::unique_ptr<const Cgu::Callback::Callback>& func,
315  bool& ret);
316 
317  // private constructor - this class can only be created with Thread::Future::make()
318  Future(): val(), done(false), running(false), error(false), emitter_error(false) {}
319 
320 public:
321 
322  // this class cannot be copied except by smart pointer
323 /**
324  * This class cannot be copied (except by smart pointer). The copy
325  * constructor is deleted.
326  */
327  Future(const Future&) = delete;
328 
329 /**
330  * This class cannot be copied (except by smart pointer). The
331  * assignment operator is deleted.
332  */
333  Future& operator=(const Future&) = delete;
334 
335 /**
336  * Constructs a new Cgu::Thread::Future object (returned by
337  * Cgu::IntrusivePtr<Cgu::Thread::Future<Val>>). The type parameter
338  * Val represents the return value of the function to be represented
339  * by the new object. From version 2.0.4, it will usually be more
340  * convenient to call the Cgu::Thread::make_future() function, which
341  * is a convenience wrapper for this static method.
342  * @exception std::bad_alloc It might throw std::bad_alloc if memory
343  * is exhausted and the system throws in that case. (This exception
344  * will not be thrown if the library has been installed using the
345  * \--with-glib-memory-slices-no-compat configuration option: instead
346  * glib will terminate the program if it is unable to obtain memory
347  * from the operating system.)
348  * @exception Cgu::Thread::MutexError It might throw
349  * Cgu::Thread::MutexError if initialisation of the contained mutex
350  * fails. (It is often not worth checking for this, as it means
351  * either memory is exhausted or pthread has run out of other
352  * resources to create new mutexes.)
353  * @exception Cgu::Thread::CondError It might throw
354  * Cgu::Thread::CondError if initialisation of the contained condition
355  * variable fails. (It is often not worth checking for this, as it
356  * means either memory is exhausted or pthread has run out of other
357  * resources to create new condition variables.)
358  * @note This method will also throw if the default constructor of the
359  * return value type throws.
360  */
361  template <class Ret, class T>
363  Ret (T::*func)());
364 
365 /**
366  * Constructs a new Cgu::Thread::Future object (returned by
367  * Cgu::IntrusivePtr<Cgu::Thread::Future<Val>>). The type parameter
368  * Val represents the return value of the function to be represented
369  * by the new object. From version 2.0.4, it will usually be more
370  * convenient to call the Cgu::Thread::make_future() function, which
371  * is a convenience wrapper for this static method.
372  * @exception std::bad_alloc It might throw std::bad_alloc if memory
373  * is exhausted and the system throws in that case. (This exception
374  * will not be thrown if the library has been installed using the
375  * \--with-glib-memory-slices-no-compat configuration option: instead
376  * glib will terminate the program if it is unable to obtain memory
377  * from the operating system.)
378  * @exception Cgu::Thread::MutexError It might throw
379  * Cgu::Thread::MutexError if initialisation of the contained mutex
380  * fails. (It is often not worth checking for this, as it means
381  * either memory is exhausted or pthread has run out of other
382  * resources to create new mutexes.)
383  * @exception Cgu::Thread::CondError It might throw
384  * Cgu::Thread::CondError if initialisation of the contained condition
385  * variable fails. (It is often not worth checking for this, as it
386  * means either memory is exhausted or pthread has run out of other
387  * resources to create new condition variables.)
388  * @note This method will also throw if the copy or move constructor
389  * of the bound argument throws, or the default constructor of the
390  * return value type throws.
391  */
392  template <class Ret, class Param1, class Arg1, class T>
394  Ret (T::*func)(Param1),
395  Arg1&& arg1);
396 
397 /**
398  * Constructs a new Cgu::Thread::Future object (returned by
399  * Cgu::IntrusivePtr<Cgu::Thread::Future<Val>>). The type parameter
400  * Val represents the return value of the function to be represented
401  * by the new object. From version 2.0.4, it will usually be more
402  * convenient to call the Cgu::Thread::make_future() function, which
403  * is a convenience wrapper for this static method.
404  * @exception std::bad_alloc It might throw std::bad_alloc if memory
405  * is exhausted and the system throws in that case. (This exception
406  * will not be thrown if the library has been installed using the
407  * \--with-glib-memory-slices-no-compat configuration option: instead
408  * glib will terminate the program if it is unable to obtain memory
409  * from the operating system.)
410  * @exception Cgu::Thread::MutexError It might throw
411  * Cgu::Thread::MutexError if initialisation of the contained mutex
412  * fails. (It is often not worth checking for this, as it means
413  * either memory is exhausted or pthread has run out of other
414  * resources to create new mutexes.)
415  * @exception Cgu::Thread::CondError It might throw
416  * Cgu::Thread::CondError if initialisation of the contained condition
417  * variable fails. (It is often not worth checking for this, as it
418  * means either memory is exhausted or pthread has run out of other
419  * resources to create new condition variables.)
420  * @note This method will also throw if the copy or move constructor
421  * of a bound argument throws, or the default constructor of the
422  * return value type throws.
423  */
424  template <class Ret, class Param1, class Param2, class Arg1, class Arg2, class T>
426  Ret (T::*func)(Param1, Param2),
427  Arg1&& arg1,
428  Arg2&& arg2);
429 
430 /**
431  * Constructs a new Cgu::Thread::Future object (returned by
432  * Cgu::IntrusivePtr<Cgu::Thread::Future<Val>>). The type parameter
433  * Val represents the return value of the function to be represented
434  * by the new object. From version 2.0.4, it will usually be more
435  * convenient to call the Cgu::Thread::make_future() function, which
436  * is a convenience wrapper for this static method.
437  * @exception std::bad_alloc It might throw std::bad_alloc if memory
438  * is exhausted and the system throws in that case. (This exception
439  * will not be thrown if the library has been installed using the
440  * \--with-glib-memory-slices-no-compat configuration option: instead
441  * glib will terminate the program if it is unable to obtain memory
442  * from the operating system.)
443  * @exception Cgu::Thread::MutexError It might throw
444  * Cgu::Thread::MutexError if initialisation of the contained mutex
445  * fails. (It is often not worth checking for this, as it means
446  * either memory is exhausted or pthread has run out of other
447  * resources to create new mutexes.)
448  * @exception Cgu::Thread::CondError It might throw
449  * Cgu::Thread::CondError if initialisation of the contained condition
450  * variable fails. (It is often not worth checking for this, as it
451  * means either memory is exhausted or pthread has run out of other
452  * resources to create new condition variables.)
453  * @note This method will also throw if the copy or move constructor
454  * of a bound argument throws, or the default constructor of the
455  * return value type throws.
456  */
457  template <class Ret, class Param1, class Param2, class Param3,
458  class Arg1, class Arg2, class Arg3, class T>
460  Ret (T::*func)(Param1, Param2, Param3),
461  Arg1&& arg1,
462  Arg2&& arg2,
463  Arg3&& arg3);
464 
465 /**
466  * Constructs a new Cgu::Thread::Future object (returned by
467  * Cgu::IntrusivePtr<Cgu::Thread::Future<Val>>). The type parameter
468  * Val represents the return value of the function to be represented
469  * by the new object. From version 2.0.4, it will usually be more
470  * convenient to call the Cgu::Thread::make_future() function, which
471  * is a convenience wrapper for this static method.
472  * @exception std::bad_alloc It might throw std::bad_alloc if memory
473  * is exhausted and the system throws in that case. (This exception
474  * will not be thrown if the library has been installed using the
475  * \--with-glib-memory-slices-no-compat configuration option: instead
476  * glib will terminate the program if it is unable to obtain memory
477  * from the operating system.)
478  * @exception Cgu::Thread::MutexError It might throw
479  * Cgu::Thread::MutexError if initialisation of the contained mutex
480  * fails. (It is often not worth checking for this, as it means
481  * either memory is exhausted or pthread has run out of other
482  * resources to create new mutexes.)
483  * @exception Cgu::Thread::CondError It might throw
484  * Cgu::Thread::CondError if initialisation of the contained condition
485  * variable fails. (It is often not worth checking for this, as it
486  * means either memory is exhausted or pthread has run out of other
487  * resources to create new condition variables.)
488  * @note This method will also throw if the default constructor of the
489  * return value type throws.
490  */
491  template <class Ret, class T>
493  Ret (T::*func)() const);
494 
495 /**
496  * Constructs a new Cgu::Thread::Future object (returned by
497  * Cgu::IntrusivePtr<Cgu::Thread::Future<Val>>). The type parameter
498  * Val represents the return value of the function to be represented
499  * by the new object. From version 2.0.4, it will usually be more
500  * convenient to call the Cgu::Thread::make_future() function, which
501  * is a convenience wrapper for this static method.
502  * @exception std::bad_alloc It might throw std::bad_alloc if memory
503  * is exhausted and the system throws in that case. (This exception
504  * will not be thrown if the library has been installed using the
505  * \--with-glib-memory-slices-no-compat configuration option: instead
506  * glib will terminate the program if it is unable to obtain memory
507  * from the operating system.)
508  * @exception Cgu::Thread::MutexError It might throw
509  * Cgu::Thread::MutexError if initialisation of the contained mutex
510  * fails. (It is often not worth checking for this, as it means
511  * either memory is exhausted or pthread has run out of other
512  * resources to create new mutexes.)
513  * @exception Cgu::Thread::CondError It might throw
514  * Cgu::Thread::CondError if initialisation of the contained condition
515  * variable fails. (It is often not worth checking for this, as it
516  * means either memory is exhausted or pthread has run out of other
517  * resources to create new condition variables.)
518  * @note This method will also throw if the copy or move constructor
519  * of the bound argument throws, or the default constructor of the
520  * return value type throws.
521  */
522  template <class Ret, class Param1, class Arg1, class T>
524  Ret (T::*func)(Param1) const,
525  Arg1&& arg1);
526 
527 /**
528  * Constructs a new Cgu::Thread::Future object (returned by
529  * Cgu::IntrusivePtr<Cgu::Thread::Future<Val>>). The type parameter
530  * Val represents the return value of the function to be represented
531  * by the new object. From version 2.0.4, it will usually be more
532  * convenient to call the Cgu::Thread::make_future() function, which
533  * is a convenience wrapper for this static method.
534  * @exception std::bad_alloc It might throw std::bad_alloc if memory
535  * is exhausted and the system throws in that case. (This exception
536  * will not be thrown if the library has been installed using the
537  * \--with-glib-memory-slices-no-compat configuration option: instead
538  * glib will terminate the program if it is unable to obtain memory
539  * from the operating system.)
540  * @exception Cgu::Thread::MutexError It might throw
541  * Cgu::Thread::MutexError if initialisation of the contained mutex
542  * fails. (It is often not worth checking for this, as it means
543  * either memory is exhausted or pthread has run out of other
544  * resources to create new mutexes.)
545  * @exception Cgu::Thread::CondError It might throw
546  * Cgu::Thread::CondError if initialisation of the contained condition
547  * variable fails. (It is often not worth checking for this, as it
548  * means either memory is exhausted or pthread has run out of other
549  * resources to create new condition variables.)
550  * @note This method will also throw if the copy or move constructor
551  * of a bound argument throws, or the default constructor of the
552  * return value type throws.
553  */
554  template <class Ret, class Param1, class Param2, class Arg1, class Arg2, class T>
556  Ret (T::*func)(Param1, Param2) const,
557  Arg1&& arg1,
558  Arg2&& arg2);
559 
560 /**
561  * Constructs a new Cgu::Thread::Future object (returned by
562  * Cgu::IntrusivePtr<Cgu::Thread::Future<Val>>). The type parameter
563  * Val represents the return value of the function to be represented
564  * by the new object. From version 2.0.4, it will usually be more
565  * convenient to call the Cgu::Thread::make_future() function, which
566  * is a convenience wrapper for this static method.
567  * @exception std::bad_alloc It might throw std::bad_alloc if memory
568  * is exhausted and the system throws in that case. (This exception
569  * will not be thrown if the library has been installed using the
570  * \--with-glib-memory-slices-no-compat configuration option: instead
571  * glib will terminate the program if it is unable to obtain memory
572  * from the operating system.)
573  * @exception Cgu::Thread::MutexError It might throw
574  * Cgu::Thread::MutexError if initialisation of the contained mutex
575  * fails. (It is often not worth checking for this, as it means
576  * either memory is exhausted or pthread has run out of other
577  * resources to create new mutexes.)
578  * @exception Cgu::Thread::CondError It might throw
579  * Cgu::Thread::CondError if initialisation of the contained condition
580  * variable fails. (It is often not worth checking for this, as it
581  * means either memory is exhausted or pthread has run out of other
582  * resources to create new condition variables.)
583  * @note This method will also throw if the copy or move constructor
584  * of a bound argument throws, or the default constructor of the
585  * return value type throws.
586  */
587  template <class Ret, class Param1, class Param2, class Param3,
588  class Arg1, class Arg2, class Arg3, class T>
590  Ret (T::*func)(Param1, Param2, Param3) const,
591  Arg1&& arg1,
592  Arg2&& arg2,
593  Arg3&& arg3);
594 
595 /**
596  * Constructs a new Cgu::Thread::Future object (returned by
597  * Cgu::IntrusivePtr<Cgu::Thread::Future<Val>>). The type parameter
598  * Val represents the return value of the function to be represented
599  * by the new object. From version 2.0.4, it will usually be more
600  * convenient to call the Cgu::Thread::make_future() function, which
601  * is a convenience wrapper for this static method.
602  * @exception std::bad_alloc It might throw std::bad_alloc if memory
603  * is exhausted and the system throws in that case. (This exception
604  * will not be thrown if the library has been installed using the
605  * \--with-glib-memory-slices-no-compat configuration option: instead
606  * glib will terminate the program if it is unable to obtain memory
607  * from the operating system.)
608  * @exception Cgu::Thread::MutexError It might throw
609  * Cgu::Thread::MutexError if initialisation of the contained mutex
610  * fails. (It is often not worth checking for this, as it means
611  * either memory is exhausted or pthread has run out of other
612  * resources to create new mutexes.)
613  * @exception Cgu::Thread::CondError It might throw
614  * Cgu::Thread::CondError if initialisation of the contained condition
615  * variable fails. (It is often not worth checking for this, as it
616  * means either memory is exhausted or pthread has run out of other
617  * resources to create new condition variables.)
618  * @note This method will also throw if the default constructor of the
619  * return value type throws.
620  */
621  template <class Ret>
622  static Cgu::IntrusivePtr<Cgu::Thread::Future<Val>> make(Ret (*func)());
623 
624 /**
625  * Constructs a new Cgu::Thread::Future object (returned by
626  * Cgu::IntrusivePtr<Cgu::Thread::Future<Val>>). The type parameter
627  * Val represents the return value of the function to be represented
628  * by the new object. From version 2.0.4, it will usually be more
629  * convenient to call the Cgu::Thread::make_future() function, which
630  * is a convenience wrapper for this static method.
631  * @exception std::bad_alloc It might throw std::bad_alloc if memory
632  * is exhausted and the system throws in that case. (This exception
633  * will not be thrown if the library has been installed using the
634  * \--with-glib-memory-slices-no-compat configuration option: instead
635  * glib will terminate the program if it is unable to obtain memory
636  * from the operating system.)
637  * @exception Cgu::Thread::MutexError It might throw
638  * Cgu::Thread::MutexError if initialisation of the contained mutex
639  * fails. (It is often not worth checking for this, as it means
640  * either memory is exhausted or pthread has run out of other
641  * resources to create new mutexes.)
642  * @exception Cgu::Thread::CondError It might throw
643  * Cgu::Thread::CondError if initialisation of the contained condition
644  * variable fails. (It is often not worth checking for this, as it
645  * means either memory is exhausted or pthread has run out of other
646  * resources to create new condition variables.)
647  * @note This method will also throw if the copy or move constructor
648  * of the bound argument throws, or the default constructor of the
649  * return value type throws.
650  */
651  template <class Ret, class Param1, class Arg1>
652  static Cgu::IntrusivePtr<Cgu::Thread::Future<Val>> make(Ret (*func)(Param1),
653  Arg1&& arg1);
654 
655 /**
656  * Constructs a new Cgu::Thread::Future object (returned by
657  * Cgu::IntrusivePtr<Cgu::Thread::Future<Val>>). The type parameter
658  * Val represents the return value of the function to be represented
659  * by the new object. From version 2.0.4, it will usually be more
660  * convenient to call the Cgu::Thread::make_future() function, which
661  * is a convenience wrapper for this static method.
662  * @exception std::bad_alloc It might throw std::bad_alloc if memory
663  * is exhausted and the system throws in that case. (This exception
664  * will not be thrown if the library has been installed using the
665  * \--with-glib-memory-slices-no-compat configuration option: instead
666  * glib will terminate the program if it is unable to obtain memory
667  * from the operating system.)
668  * @exception Cgu::Thread::MutexError It might throw
669  * Cgu::Thread::MutexError if initialisation of the contained mutex
670  * fails. (It is often not worth checking for this, as it means
671  * either memory is exhausted or pthread has run out of other
672  * resources to create new mutexes.)
673  * @exception Cgu::Thread::CondError It might throw
674  * Cgu::Thread::CondError if initialisation of the contained condition
675  * variable fails. (It is often not worth checking for this, as it
676  * means either memory is exhausted or pthread has run out of other
677  * resources to create new condition variables.)
678  * @note This method will also throw if the copy or move constructor
679  * of a bound argument throws, or the default constructor of the
680  * return value type throws.
681  */
682  template <class Ret, class Param1, class Param2, class Arg1, class Arg2>
683  static Cgu::IntrusivePtr<Cgu::Thread::Future<Val>> make(Ret (*func)(Param1, Param2),
684  Arg1&& arg1,
685  Arg2&& arg2);
686 
687 /**
688  * Constructs a new Cgu::Thread::Future object (returned by
689  * Cgu::IntrusivePtr<Cgu::Thread::Future<Val>>). The type parameter
690  * Val represents the return value of the function to be represented
691  * by the new object. From version 2.0.4, it will usually be more
692  * convenient to call the Cgu::Thread::make_future() function, which
693  * is a convenience wrapper for this static method.
694  * @exception std::bad_alloc It might throw std::bad_alloc if memory
695  * is exhausted and the system throws in that case. (This exception
696  * will not be thrown if the library has been installed using the
697  * \--with-glib-memory-slices-no-compat configuration option: instead
698  * glib will terminate the program if it is unable to obtain memory
699  * from the operating system.)
700  * @exception Cgu::Thread::MutexError It might throw
701  * Cgu::Thread::MutexError if initialisation of the contained mutex
702  * fails. (It is often not worth checking for this, as it means
703  * either memory is exhausted or pthread has run out of other
704  * resources to create new mutexes.)
705  * @exception Cgu::Thread::CondError It might throw
706  * Cgu::Thread::CondError if initialisation of the contained condition
707  * variable fails. (It is often not worth checking for this, as it
708  * means either memory is exhausted or pthread has run out of other
709  * resources to create new condition variables.)
710  * @note This method will also throw if the copy or move constructor
711  * of a bound argument throws, or the default constructor of the
712  * return value type throws.
713  */
714  template <class Ret, class Param1, class Param2, class Param3,
715  class Arg1, class Arg2, class Arg3>
716  static Cgu::IntrusivePtr<Cgu::Thread::Future<Val>> make(Ret (*func)(Param1, Param2, Param3),
717  Arg1&& arg1,
718  Arg2&& arg2,
719  Arg3&& arg3);
720 
721 /**
722  * Constructs a new Cgu::Thread::Future object (returned by
723  * Cgu::IntrusivePtr<Cgu::Thread::Future<Val>>). The type parameter
724  * Val represents the return value of the function to be represented
725  * by the new object. From version 2.0.4, it will usually be more
726  * convenient to call the Cgu::Thread::make_future() function, which
727  * is a convenience wrapper for this static method.
728  * @exception std::bad_alloc It might throw std::bad_alloc if memory
729  * is exhausted and the system throws in that case. (This exception
730  * will not be thrown if the library has been installed using the
731  * \--with-glib-memory-slices-no-compat configuration option: instead
732  * glib will terminate the program if it is unable to obtain memory
733  * from the operating system.)
734  * @exception Cgu::Thread::MutexError It might throw
735  * Cgu::Thread::MutexError if initialisation of the contained mutex
736  * fails. (It is often not worth checking for this, as it means
737  * either memory is exhausted or pthread has run out of other
738  * resources to create new mutexes.)
739  * @exception Cgu::Thread::CondError It might throw
740  * Cgu::Thread::CondError if initialisation of the contained condition
741  * variable fails. (It is often not worth checking for this, as it
742  * means either memory is exhausted or pthread has run out of other
743  * resources to create new condition variables.)
744  * @note This method will also throw if the copy or move constructor
745  * of a bound argument throws, or the default constructor of the
746  * return value type throws.
747  */
748  template <class Ret, class Param1, class Param2, class Param3, class Param4,
749  class Arg1, class Arg2, class Arg3, class Arg4>
750  static Cgu::IntrusivePtr<Cgu::Thread::Future<Val>> make(Ret (*func)(Param1, Param2, Param3, Param4),
751  Arg1&& arg1,
752  Arg2&& arg2,
753  Arg3&& arg3,
754  Arg4&& arg4);
755 
756 /**
757  * Constructs a new Cgu::Thread::Future object (returned by
758  * Cgu::IntrusivePtr<Cgu::Thread::Future<Val>>). The type parameter
759  * Val represents the return value of the function to be represented
760  * by the new object. From version 2.0.4, it will usually be more
761  * convenient to call the Cgu::Thread::make_future() function, which
762  * is a convenience wrapper for this static method.
763  * @param functor The callable object to be executed. It should
764  * return the Val type.
765  * @exception std::bad_alloc It might throw std::bad_alloc if memory
766  * is exhausted and the system throws in that case. (This exception
767  * will not be thrown if the library has been installed using the
768  * \--with-glib-memory-slices-no-compat configuration option: instead
769  * glib will terminate the program if it is unable to obtain memory
770  * from the operating system.)
771  * @exception Cgu::Thread::MutexError It might throw
772  * Cgu::Thread::MutexError if initialisation of the contained mutex
773  * fails. (It is often not worth checking for this, as it means
774  * either memory is exhausted or pthread has run out of other
775  * resources to create new mutexes.)
776  * @exception Cgu::Thread::CondError It might throw
777  * Cgu::Thread::CondError if initialisation of the contained condition
778  * variable fails. (It is often not worth checking for this, as it
779  * means either memory is exhausted or pthread has run out of other
780  * resources to create new condition variables.)
781  * @note 1. This method will also throw if the copy or move
782  * constructor of the callable object passed as an argument throws, or
783  * the default constructor of the return value type throws.
784  * @note 2. If the callable object passed as an argument has both
785  * const and non-const operator()() methods, the non-const version
786  * will be called even if the callable object passed is a const
787  * object.
788  */
789  template <class Func>
790  static Cgu::IntrusivePtr<Cgu::Thread::Future<Val>> make(Func&& functor);
791 
792 /**
793  * Runs the function or callable object represented by this
794  * Cgu::Thread::Future object in a new worker thread. That function
795  * will only be run once. If this is the first time this method has
796  * been called, it will start the worker thread and return true, and
797  * if it has previously been called, this method will do nothing and
798  * return false. This method will not wait for the worker thread to
799  * complete before returning. This method is thread safe and may be
800  * called by a different thread from the one which called make().
801  * @return true if this is the first time this method has been called,
802  * or false if this method has previously been called.
803  * @exception Cgu::Thread::FutureThreadError This method might throw
804  * Cgu::Thread::FutureThreadError if it has not previously been called
805  * and the thread did not start properly. If it does throw, this
806  * Cgu::Thread::Future object is defunct and further attempts to call
807  * this method will return immediately with a false value. (It is
808  * often not worth checking for this exception, as it means either
809  * memory is exhausted, the pthread thread limit has been reached or
810  * pthread has run out of other resources to start new threads.)
811  * @exception std::bad_alloc This method might throw std::bad_alloc if
812  * it has not previously been called, and memory is exhausted and the
813  * system throws in that case. If it does throw, this
814  * Cgu::Thread::Future object is defunct and further attempts to call
815  * this method will return immediately with a false value. (This
816  * exception will not be thrown if the library has been installed
817  * using the \--with-glib-memory-slices-no-compat configuration
818  * option: instead glib will terminate the program if it is unable to
819  * obtain memory from the operating system.)
820  * @note 1. Any Cgu::Thread::Exit exception, or any uncaught exception
821  * derived from std::exception, which is thrown from the worker thread
822  * will be caught and consumed and the error flag will be set. The
823  * worker thread will safely terminate and unwind the stack in so
824  * doing.
825  * @note 2. As this wrapper class can provide error reporting in a way
826  * that Cgu::Thread::Thread of itself cannot, it would be desirable to
827  * consume any other uncaught exceptions. However, this cannot be
828  * done: annoyingly, NPTL's forced stack unwinding does not allow this
829  * if thread cancellation is to be made available. If an uncaught
830  * exception propagates out of a thread when the thread exits, the
831  * C++11/14 standard will cause std::terminate() to be called and so
832  * terminate the entire program. Accordingly, a user must make sure
833  * that no exceptions, other than Cgu::Thread::Exit or those derived
834  * from std::exception or any cancellation pseudo-exception, can
835  * propagate from the function which this Cgu::Thread::Future object
836  * represents, nor from the copy constructor of any argument type that
837  * that function takes by value nor from the move assignment operator
838  * (or if none, copy assignment operator) of the return value of that
839  * function.
840  * @note 3. If the worker thread is cancelled by a call to cancel()
841  * while in the middle of executing the function which this
842  * Cgu::Thread::Future object represents, the error flag will be set.
843  */
844  bool run();
845 
846 /**
847  * Gets the stored value obtained from the function or callable object
848  * which is represented by this object. If the worker thread launched
849  * by the call to run() has not completed, then this method will block
850  * until it has completed. If run() has not been called, then run()
851  * will be called (and this method will block until the launched
852  * worker thread completes). If the function or callable object which
853  * is represented by this Cgu::Thread::Future object throws
854  * Cgu::Thread::Exit or an uncaught exception derived from
855  * std::exception, or if any of those exceptions are thrown either by
856  * the copy constructor of an argument taken by value by that function
857  * or object, or by the move assignment operator (or if none, copy
858  * assignment operator) of the return value of that function or
859  * object, then the exception will have been consumed by this
860  * Cgu::Thread::Future object and the error flag will have been set.
861  * The error flag will also have been set if the worker thread is
862  * cancelled or the thread wrapper in this Cgu::Thread::Future object
863  * threw std::bad_alloc. On the error flag being set, this method
864  * will unblock and return a default constructed object of the return
865  * type. This method is thread safe and may be called by any thread
866  * (and by more than one thread). It is a cancellation point if it
867  * blocks, and from version 2.0.11 is cancellation safe if the stack
868  * unwinds on cancellation. It is also strongly exception safe: no
869  * data will be lost if extracting the value fails.
870  * @return The value obtained from the function which is represented
871  * by this object, or a default constructed object of the return type
872  * if the error flag has been set.
873  * @exception Cgu::Thread::FutureThreadError This method might throw
874  * Cgu::Thread::FutureThreadError if run() has not previously been
875  * called and the thread did not start properly when this function
876  * called run().
877  * @exception std::bad_alloc This method might throw std::bad_alloc if
878  * run() has not previously been called, memory is exhausted and the
879  * system throws in that case. (This exception will not be thrown if
880  * the library has been installed using the
881  * \--with-glib-memory-slices-no-compat configuration option: instead
882  * glib will terminate the program if it is unable to obtain memory
883  * from the operating system.)
884  * @note 1. This method might also throw if the copy constructor of
885  * the returned value type throws.
886  * @note 2. Question: Couldn't this method return the stored value by
887  * lvalue reference to const? Answer: It could. However, because of
888  * return value optimization, which will be implemented by any
889  * compiler capable of compiling this library, no advantage would be
890  * gained by doing so when initializing a local variable with the
891  * return value of this method (the copy constructor will only be
892  * called once whether returning by value or const reference). The
893  * advantage of returning by value is that the call to the copy
894  * constructor is forced to be within this Thread::Future object's
895  * mutex, so different threads' calls to the copy constructor are
896  * serialized, and also with blocked cancellation, so this method is
897  * cancellation safe. All calls to this method by different threads
898  * are therefore isolated and we do not have to worry about the thread
899  * safety of direct access to the stored value via its const methods
900  * outside the mutex (which would not be thread safe if the stored
901  * value has data members declared mutable) nor about the cancellation
902  * safety of the copy constructor. Of course, for objects which do
903  * not have mutable data, a hit arises by returning by value in cases
904  * where it is not intended to initialize a local variable at all nor
905  * to cancel a thread: where, say, only const methods are to be called
906  * on the return value (which could be done directly if this method
907  * returned by const reference). However, in many use cases this will
908  * be mitigated by the move_get() method.
909  */
910  Val get();
911 
912 /**
913  * Gets the stored value obtained from the function or callable object
914  * which is represented by this object by a move operation, if the
915  * return type implements a move constructor (otherwise this method
916  * does the same as the get() method). It is provided as an option
917  * for cases where a move is required for efficiency reasons, but
918  * although it may be called by any thread, a move from this
919  * Thread::Future object may normally only be made once (except where
920  * the return type has been designed to be moved more than once for
921  * the limited purpose of inspecting a flag indicating whether its
922  * value is valid or not). If this method is to be called then no
923  * calls to get() by another thread should normally be made and no
924  * calls to when() should be made. If the worker thread launched by
925  * the call to run() has not completed, then this method will block
926  * until it has completed. If run() has not been called, then run()
927  * will be called (and this method will block until the launched
928  * worker thread completes). If the function or callable object which
929  * is represented by this Cgu::Thread::Future object throws
930  * Cgu::Thread::Exit or an uncaught exception derived from
931  * std::exception, or if any of those exceptions are thrown either by
932  * the copy constructor of an argument taken by value by that function
933  * or object, or by the move assignment operator (or if none, copy
934  * assignment operator) of the return value of that function or
935  * object, then the exception will have been consumed by this
936  * Cgu::Thread::Future object and the error flag will have been set.
937  * The error flag will also have been set if the worker thread is
938  * cancelled or the thread wrapper in this Cgu::Thread::Future object
939  * threw std::bad_alloc. On the error flag being set, this method
940  * will unblock and return a default constructed object of the return
941  * type. This method is a cancellation point if it blocks, and is
942  * cancellation safe if the stack unwinds on cancellation. This
943  * method is only exception safe if the return type's move constructor
944  * is exception safe.
945  * @return The value obtained from the function which is represented
946  * by this object, or a default constructed object of the return type
947  * if the error flag has been set.
948  * @exception Cgu::Thread::FutureThreadError This method might throw
949  * Cgu::Thread::FutureThreadError if run() has not previously been
950  * called and the thread did not start properly when this function
951  * called run().
952  * @exception std::bad_alloc This method might throw std::bad_alloc if
953  * run() has not previously been called, memory is exhausted and the
954  * system throws in that case. (This exception will not be thrown if
955  * the library has been installed using the
956  * \--with-glib-memory-slices-no-compat configuration option: instead
957  * glib will terminate the program if it is unable to obtain memory
958  * from the operating system.)
959  * @note 1. This method might also throw if the copy or move
960  * constructor of the returned value type throws.
961  * @note 2. Question: Couldn't this method return the stored value by
962  * rvalue reference? Answer: It could. However, because of return
963  * value optimization, which will be implemented by any compiler
964  * capable of compiling this library, no advantage would be gained by
965  * doing so when initializing a local variable with the return value
966  * of this method (the move constructor will only be called once, and
967  * no call will be made to the copy constructor, whether returning by
968  * value or rvalue reference). The advantage of returning by value is
969  * that the call to the move constructor is forced to be within this
970  * Thread::Future object's mutex, so different threads' calls to the
971  * move constructor are serialized, and also with blocked
972  * cancellation, so this method is cancellation safe. All calls to
973  * this method by different threads are therefore isolated and we do
974  * not have to worry about the thread safety of the mutating first
975  * call to this method, nor about direct access to the stored value
976  * via a rvalue reference outside the mutex nor the cancellation
977  * safety of the move constructor.
978  *
979  * Since 2.0.11
980  */
981  Val move_get();
982 
983 /**
984  * Cancels the worker thread in which the function or callable object
985  * represented by this object runs, if it has not yet finished. If
986  * this method is called and the worker thread is still running and is
987  * cancelled in response to a call to this method, then the error flag
988  * will be set so that a method calling get() or move_get() can
989  * examine whether the result is valid. If run() has not yet been
990  * called or the worker thread has already finished executing the
991  * function or callable object represented by this object then this
992  * function does nothing and returns false. This method is thread
993  * safe and may be called by any thread. It will not throw.
994  * @return true if run() has previously been called and the worker
995  * thread has not yet finished executing the function or callable
996  * object represented by this object, otherwise false (in which case
997  * this method does nothing).
998  * @note 1. Use this method with care. When cancelling a thread not
999  * all thread implementations will unwind the stack, and so run the
1000  * destructors of local objects. This is discussed further in the
1001  * documentation on Cgu::Thread::Thread::cancel().
1002  * @note 2. This method might return true because the worker thread
1003  * has not yet finished, but the error flag might still not be set.
1004  * This is because the worker thread may not meet a cancellation point
1005  * before it ends naturally. It is the error flag which indicates
1006  * definitively whether the worker thread terminated prematurely in
1007  * response to a call to this method.
1008  */
1009  bool cancel();
1010 
1011 /**
1012  * A utility enabling the value returned by the thread function
1013  * represented by this Cgu::Thread::Future object to be dealt with
1014  * asynchronously rather than by (or in addition to) a call to the
1015  * get() method. It causes the callback passed as an argument to this
1016  * method (referred to below as the 'when' callback) to be executed by
1017  * a thread's main loop if and when the thread function represented by
1018  * this Cgu::Thread::Future object finishes correctly - the 'when'
1019  * callback is passed that thread function's return value when it is
1020  * invoked. This method is thread safe, and may be called by any
1021  * thread.
1022  *
1023  * This functionality is implemented by connecting an internal
1024  * dispatching callback to the done_emitter object.
1025  *
1026  * The 'when' callback should take a single unbound argument
1027  * comprising a const reference to the return type of the thread
1028  * function represented by this Cgu::Thread::Future object. (So, in
1029  * the case of a Future<int> object, the callback function should take
1030  * a const int& argument as the unbound argument.) The 'when'
1031  * callback can have any number of bound arguments, except that a
1032  * bound argument may not include a copy of this Cgu::Thread::Future
1033  * object held by intrusive pointer as returned by the make() methods
1034  * (that would result in this Cgu::Thread::Future object owning, via
1035  * done_emitter, a reference to itself and so become incapable of
1036  * being freed). The 'when' callback may, however, take a pointer to
1037  * this Cgu::Thread::Future object, as obtained by the
1038  * Cgu::IntrusivePtr::get() method, because this Cgu::Thread::Future
1039  * object is guaranteed to remain in existence until the callback has
1040  * completed executing.
1041  *
1042  * This method cannot be called after the thread function represented
1043  * by this Cgu::Thread::Future object has completed (either
1044  * successfully or unsuccessfully) so that is_done() would return
1045  * true, and if this is attempted a Cgu::Thread::FutureWhenError
1046  * exception will be thrown. Therefore, generally this method should
1047  * be called before the run() method has been called.
1048  *
1049  * Once the run() method has been called, this Cgu::Thread::Future
1050  * object will always stay in existence until the thread function
1051  * represented by it has completed (whether correctly, by cancellation
1052  * or by a thrown exception), and any 'when' callback (and any other
1053  * callbacks connected to the done_emitter object) and any 'fail'
1054  * callback have completed. Accordingly it is safe to use this method
1055  * even if the intrusive pointer object returned by the make() methods
1056  * will go out of scope before the 'when' callback has executed: the
1057  * callback will execute correctly irrespective of that.
1058  *
1059  * Summary: use of this method is safe and has been implemented in a
1060  * way which does not give rise to timing issues.
1061  *
1062  * If memory is exhausted and std::bad_alloc is thrown by the thread
1063  * wrapper of Cgu::Thread::Future after run() is called or by
1064  * done_emitter when emitting, or if the thread function represented
1065  * by this Cgu::Thread::Future object throws Cgu::Thread::Exit, is
1066  * cancelled, exits with an uncaught exception deriving from
1067  * std::exception, takes an argument by value whose copy constructor
1068  * throws such an exception or has a return value whose move
1069  * assignment operator (or if none, copy assignment operator) throws
1070  * such an exception, or if the 'when' callback represents a function
1071  * taking a non-reference argument whose copy constructor throws an
1072  * exception, or if any other callback has been connected to
1073  * done_emitter before this method is called which exits with an
1074  * uncaught exception, then the 'when' callback will not execute
1075  * (instead the exception concerned will be consumed and an error
1076  * indicated). With many systems, swap memory combined with memory
1077  * over-commit makes it pointless to check for std::bad_alloc (and
1078  * even more so in programs using glib, as glib aborts a program where
1079  * it cannot obtain memory from the operating system). So subject to
1080  * that, if the user program is designed so that the thread function
1081  * represented by this Cgu::Thread::Future object does not exit with
1082  * uncaught exceptions, does not take an argument by value which
1083  * throws, does not have a return value whose move assignment operator
1084  * (or if none, copy assignment operator) throws, does not throw
1085  * Cgu::Thread::Exit and is not cancelled, and so that the 'when'
1086  * callback does not exit with an uncaught exception (and a function
1087  * represented by that callback either takes no arguments of class
1088  * type by value or the copy constructors of any of its value
1089  * arguments do not throw), and if this method is called before any
1090  * other callbacks are connected to done_emitter, the possibility of
1091  * failure can be disregarded.
1092  *
1093  * In cases where that is not true and detecting whether a failure has
1094  * occurred is required, a fail() method is provided. It should be
1095  * noted that a callback handed to the fail() method will not execute
1096  * in a case of error if the error comprises the 'when' callback
1097  * exiting with an uncaught exception when it is executed by the main
1098  * loop, or the copy constructor of any value argument of a function
1099  * represented by the 'when' callback throwing (such exceptions would
1100  * be consumed internally in order to protect the main loop and a
1101  * g_critical message issued). If the 'when' callback might exit with
1102  * an uncaught exception when executing or have the copy constructor
1103  * of a value argument throw, and doing something other than consuming
1104  * the exception and issuing a g_critical message is required, then a
1105  * different approach is to start a new thread to wait on the get()
1106  * method which can act on the result of is_error() directly.
1107  *
1108  * If glib < 2.32 is used, the glib main loop must have been made
1109  * thread-safe by a call to g_thread_init() before this function is
1110  * called. glib >= 2.32 does not require g_thread_init() to be called
1111  * in order to be thread safe.
1112  *
1113  * @param cb The 'when' callback (the callback to be executed when the
1114  * function represented by this Cgu::Thread::Future object has
1115  * successfully completed). Ownership is taken of this object, and it
1116  * will be deleted when it has been finished with.
1117  * @param priority The priority to be given to the 'when' callback in
1118  * the main loop after the thread function represented by this
1119  * Cgu::Thread::Future object has successfully completed. In
1120  * ascending order of priorities, priorities are G_PRIORITY_LOW,
1121  * G_PRIORITY_DEFAULT_IDLE, G_PRIORITY_HIGH_IDLE, G_PRIORITY_DEFAULT
1122  * and G_PRIORITY_HIGH. The default is G_PRIORITY_DEFAULT. This
1123  * determines the order in which the callback will appear in the event
1124  * list in the main loop, not the priority which the OS will adopt.
1125  * @param context The glib main context of the thread in whose main
1126  * loop the 'when' callback is to be executed (the default of NULL
1127  * will cause the callback to be executed in the main program loop).
1128  * @return The internal dispatching callback created by this method
1129  * and connected to done_emitter. It is made available as a return
1130  * value so that if wanted it can be disconnected programmatically
1131  * from done_emitter, or block()/unblock() can be called on it (but if
1132  * that is to be done, it must be done before the thread function
1133  * represented by this Cgu::Thread::Future object has completed in
1134  * order for it to be effective).
1135  * @exception Cgu::Thread::FutureWhenError This method will throw
1136  * Cgu::Thread::FutureWhenError if it is called after the thread
1137  * function represented by this Cgu::Thread::Future object has
1138  * completed. If it does so, the 'when' callback will be disposed of.
1139  * @exception std::bad_alloc This method might throw std::bad_alloc if
1140  * memory is exhausted and the system throws in that case. If it does
1141  * so, the 'when' callback will be disposed of.
1142  * @note The return value of the function represented by this
1143  * Cgu::Thread::Future object is stored and passed as an argument to
1144  * the 'when' callback by const reference. If other threads might
1145  * concurrently call this object's get() method, which copies the
1146  * stored value, the stored type's copy constructor must be thread
1147  * safe with respect to the stored type's const methods. This would
1148  * be relevant if the stored type has data members declared mutable
1149  * which would be copied by its copy constructor.
1150  *
1151  * Since 2.0.2
1152  */
1154  gint priority = G_PRIORITY_DEFAULT,
1155  GMainContext* context = 0);
1156 
1157 /**
1158  * This is a version of the utility enabling the value returned by the
1159  * thread function represented by this Cgu::Thread::Future object to
1160  * be dealt with asynchronously, which takes a Releaser object for
1161  * automatic disconnection of the callback passed as an argument to
1162  * this method (referred to below as the 'when' callback), if the
1163  * object having the 'when' callback function as a member is
1164  * destroyed. For this to be race free, the lifetime of that object
1165  * must be controlled by the thread in whose main loop the 'when'
1166  * callback will execute.
1167  *
1168  * If the 'when' callback has not been released, this method causes it
1169  * to be executed by a thread's main loop if and when the thread
1170  * function represented by this Cgu::Thread::Future object finishes
1171  * correctly - the 'when' callback is passed that thread function's
1172  * return value when it is invoked. This method is thread safe, and
1173  * may be called by any thread.
1174  *
1175  * This functionality is implemented by connecting an internal
1176  * dispatching callback to the done_emitter object.
1177  *
1178  * The 'when' callback should take a single unbound argument
1179  * comprising a const reference to the return type of the thread
1180  * function represented by this Cgu::Thread::Future object. (So, in
1181  * the case of a Future<int> object, the callback function should take
1182  * a const int& argument as the unbound argument.) The 'when'
1183  * callback can have any number of bound arguments, except that a
1184  * bound argument may not include a copy of this Cgu::Thread::Future
1185  * object held by intrusive pointer as returned by the make() methods
1186  * (that would result in this Cgu::Thread::Future object owning, via
1187  * done_emitter, a reference to itself and so become incapable of
1188  * being freed). The 'when' callback may, however, take a pointer to
1189  * this Cgu::Thread::Future object, as obtained by the
1190  * Cgu::IntrusivePtr::get() method, because this Cgu::Thread::Future
1191  * object is guaranteed to remain in existence until the callback has
1192  * completed executing.
1193  *
1194  * This method cannot be called after the thread function represented
1195  * by this Cgu::Thread::Future object has completed (either
1196  * successfully or unsuccessfully) so that is_done() would return
1197  * true, and if this is attempted a Cgu::Thread::FutureWhenError
1198  * exception will be thrown. Therefore, generally this method should
1199  * be called before the run() method has been called.
1200  *
1201  * The documentation for the version of this method which does not
1202  * take a Releaser object gives further details of how this method is
1203  * used.
1204  *
1205  * If glib < 2.32 is used, the glib main loop must have been made
1206  * thread-safe by a call to g_thread_init() before this function is
1207  * called. glib >= 2.32 does not require g_thread_init() to be called
1208  * in order to be thread safe.
1209  *
1210  * @param cb The 'when' callback (the callback to be executed when the
1211  * function represented by this Cgu::Thread::Future object has
1212  * successfully completed). Ownership is taken of this object, and it
1213  * will be deleted when it has been finished with.
1214  * @param r A Releaser object for automatic disconnection of the
1215  * 'when' callback before it executes in a main loop (mainly relevant
1216  * if the callback represents a non-static member function of an
1217  * object which may be destroyed before the callback executes).
1218  * @param priority The priority to be given to the 'when' callback in
1219  * the main loop after the thread function represented by this
1220  * Cgu::Thread::Future object has successfully completed. In
1221  * ascending order of priorities, priorities are G_PRIORITY_LOW,
1222  * G_PRIORITY_DEFAULT_IDLE, G_PRIORITY_HIGH_IDLE, G_PRIORITY_DEFAULT
1223  * and G_PRIORITY_HIGH. The default is G_PRIORITY_DEFAULT. This
1224  * determines the order in which the callback will appear in the event
1225  * list in the main loop, not the priority which the OS will adopt.
1226  * @param context The glib main context of the thread in whose main
1227  * loop the 'when' callback is to be executed (the default of NULL
1228  * will cause the callback to be executed in the main program loop).
1229  * @return The internal dispatching callback created by this method
1230  * and connected to done_emitter. It is made available as a return
1231  * value so that if wanted it can be disconnected programmatically
1232  * from done_emitter, or block()/unblock() can be called on it (but if
1233  * that is to be done, it must be done before the thread function
1234  * represented by this Cgu::Thread::Future object has completed in
1235  * order for it to be effective).
1236  * @exception Cgu::Thread::FutureWhenError This method will throw
1237  * Cgu::Thread::FutureWhenError if it is called after the thread
1238  * function represented by this Cgu::Thread::Future object has
1239  * completed. If it does so, the 'when' callback will be disposed of.
1240  * @exception std::bad_alloc This method might throw std::bad_alloc if
1241  * memory is exhausted and the system throws in that case. If it does
1242  * so, the 'when' callback will be disposed of.
1243  * @exception Cgu::Thread::MutexError This method will throw
1244  * Cgu:Thread::MutexError if initialisation of the mutex in a
1245  * SafeEmitterArg object constructed by this method fails. If it does
1246  * so, the 'when' callback will be disposed of. (It is often not
1247  * worth checking for this, as it means either memory is exhausted or
1248  * pthread has run out of other resources to create new mutexes.)
1249  * @note 1. The return value of the function represented by this
1250  * Cgu::Thread::Future object is stored and passed as an argument to
1251  * the 'when' callback by const reference. If other threads might
1252  * concurrently call this object's get() method, which copies the
1253  * stored value, the stored type's copy constructor must be thread
1254  * safe with respect to the stored type's const methods. This would
1255  * be relevant if the stored type has data members declared mutable
1256  * which would be copied by its copy constructor.
1257  * @note 2. By virtue of the Releaser object, it is in theory possible
1258  * (if memory is exhausted and the system throws in that case) that an
1259  * internal SafeEmitterArg object will throw std::bad_alloc when
1260  * emitting/executing the 'when' callback in the glib main loop, with
1261  * the result that the relevant callback will not execute (instead the
1262  * exception will be consumed and a g_critical() warning will be
1263  * issued). This is rarely of any relevance because glib will abort
1264  * the program if it is itself unable to obtain memory from the
1265  * operating system. However, where it is relevant, design the
1266  * program so that it is not necessary to provide a releaser object.
1267  *
1268  * Since 2.0.2
1269  */
1271  Cgu::Releaser& r,
1272  gint priority = G_PRIORITY_DEFAULT,
1273  GMainContext* context = 0);
1274 
1275 /**
1276  * A utility intended to be used where relevant in conjunction with
1277  * the when() methods. It enables a callback to be executed in a glib
1278  * main loop (referred to below as the 'fail' callback) if memory is
1279  * exhausted and std::bad_alloc was thrown by the thread wrapper of
1280  * Cgu::Thread::Future after calling run() or by done_emitter when
1281  * emitting, or if the thread function represented by this
1282  * Cgu::Thread::Future object threw Cgu::Thread::Exit, exited with an
1283  * uncaught exception deriving from std::exception or was cancelled
1284  * (or that function took an argument of class type by value whose
1285  * copy constructor threw such an exception or had a return value of
1286  * class type whose move assignment operator, or if none copy
1287  * assignment operator, threw such an exception), or any callback
1288  * connected to done_emitter exited with an uncaught exception. It
1289  * therefore enables errors to be detected and acted on without having
1290  * a thread wait on the get() method in order to test is_error() or
1291  * is_emitter_error().
1292  *
1293  * It is implemented by attaching a timeout to the main loop which
1294  * polls at 100 millisecond intervals and tests is_done()/is_error()
1295  * and is_emitter_done()/is_emitter_error(). The timeout is
1296  * automatically removed by the implementation once it has been
1297  * detected that an error has occurred and the 'fail' callback is
1298  * executed, or if the thread function represented by this Cgu::Future
1299  * object and all done_emitter emissions (including execution of any
1300  * 'when' callback) have completed successfully.
1301  *
1302  * This method can be called before or after the run() method has been
1303  * called, and whether or not the thread function represented by this
1304  * Cgu::Thread::Future object has completed.
1305  *
1306  * Once this method has been called, this Cgu::Thread::Future object
1307  * will always stay in existence until the timeout has been
1308  * automatically removed by the implementation. Accordingly it is
1309  * safe to use this method even if the intrusive pointer object
1310  * returned by the make() methods will go out of scope before the
1311  * 'fail' callback has executed: the callback will execute correctly
1312  * irrespective of that.
1313  *
1314  * This method does not have a priority argument: as a polling timeout
1315  * is created, a particular priority will normally have no
1316  * significance (in fact, the 'fail' callback will execute in the main
1317  * loop with a priority of G_PRIORITY_DEFAULT). If in a special case
1318  * a different polling interval than 100 milliseconds or a different
1319  * priority is required, users can attach their own polling timeouts
1320  * to a main loop and carry out the tests by hand.
1321  *
1322  * Four other points should be noted. First, if as well as the when()
1323  * method being called some other callback has been connected to
1324  * done_emitter, and that other callback throws, the 'fail' callback
1325  * will execute. Therefore, if the particular program design requires
1326  * that the 'fail' callback should only execute if the 'when' callback
1327  * is not executed (and the 'when' callback only execute if the 'fail'
1328  * callback does not execute), no other callbacks which throw should
1329  * be connected to done_emitter.
1330  *
1331  * Secondly, as mentioned in the documentation on the when() method,
1332  * if the 'when' callback exits with an uncaught exception upon being
1333  * executed by the main loop or it represents a function which takes
1334  * an argument by value whose copy constructor throws, the 'fail'
1335  * callback will not execute (the exception will have been consumed
1336  * internally in order to protect the main loop and a g_critical
1337  * message issued).
1338  *
1339  * Thirdly, avoid if possible having a 'fail' callback which might
1340  * throw, or representing a function which takes an argument by value
1341  * whose copy constructor might throw: such an exception would be
1342  * consumed internally in order to protect the main loop and a
1343  * g_critical message issued, but no other error indication apart from
1344  * the g_critical message will be provided.
1345  *
1346  * Fourthly, unlike the 'when' callback, a copy of this
1347  * Cgu::Thread::Future object held by intrusive pointer as returned by
1348  * the make() methods may safely be bound to the 'fail' callback,
1349  * which would enable the 'fail' callback to determine whether it is
1350  * is_error() or is_emitter_error() which returns false.
1351  *
1352  * If glib < 2.32 is used, the glib main loop must have been made
1353  * thread-safe by a call to g_thread_init() before this function is
1354  * called. glib >= 2.32 does not require g_thread_init() to be called
1355  * in order to be thread safe.
1356  *
1357  * @param cb The 'fail' callback (the callback to be executed if the
1358  * thread function represented by this Cgu::Thread::Future object or a
1359  * done_emitter emission has failed to complete). Ownership is taken
1360  * of this object, and it will be deleted when it has been finished
1361  * with.
1362  * @param context The glib main context of the thread in whose main
1363  * loop the 'fail' callback is to be executed (the default of NULL
1364  * will cause the functor to be executed in the main program loop).
1365  * @exception std::bad_alloc This method might throw std::bad_alloc if
1366  * memory is exhausted and the system throws in that case. If it does
1367  * so, the 'fail' callback will be disposed of.
1368  *
1369  * Since 2.0.2
1370  */
1371  void fail(const Cgu::Callback::Callback* cb,
1372  GMainContext* context = 0);
1373 
1374 /**
1375  * This is a version of the fail() utility for use in conjunction with
1376  * the when() methods, which takes a Releaser object for automatic
1377  * disconnection of the callback functor passed as an argument to this
1378  * method if the object having the callback function as a member is
1379  * destroyed. For this to be race free, the lifetime of that object
1380  * must be controlled by the thread in whose main loop the 'fail'
1381  * callback will execute.
1382  *
1383  * This method enables a callback to be executed in a glib main loop
1384  * if memory is exhausted and std::bad_alloc was thrown by the thread
1385  * wrapper of Cgu::Thread::Future after calling run() or by
1386  * done_emitter when emitting, or if the thread function represented
1387  * by this Cgu::Thread::Future object threw Cgu::Thread::Exit, exited
1388  * with an uncaught exception deriving from std::exception or was
1389  * cancelled (or that function took an argument of class type by value
1390  * whose copy constructor threw such an exception or had a return
1391  * value of class type whose move assignment operator, or if none copy
1392  * assignment operator, threw such an exception), or any callback
1393  * connected to done_emitter exited with an uncaught exception. It
1394  * therefore enables errors to be detected and acted on without having
1395  * a thread wait on the get() method in order to test is_error() or
1396  * is_emitter_error().
1397  *
1398  * This method can be called before or after the run() method has been
1399  * called, and whether or not the thread function represented by this
1400  * Cgu::Thread::Future object has completed.
1401  *
1402  * The documentation for the version of this method which does not
1403  * take a Releaser object gives further details of how this method is
1404  * used.
1405  *
1406  * If glib < 2.32 is used, the glib main loop must have been made
1407  * thread-safe by a call to g_thread_init() before this function is
1408  * called. glib >= 2.32 does not require g_thread_init() to be called
1409  * in order to be thread safe.
1410  *
1411  * @param cb The 'fail' callback (the callback to be executed if the
1412  * thread function represented by this Cgu::Thread::Future object or a
1413  * done_emitter emission has failed to complete). Ownership is taken
1414  * of this object, and it will be deleted when it has been finished
1415  * with.
1416  * @param r A Releaser object for automatic disconnection of the
1417  * 'fail' callback before it executes in a main loop (mainly relevant
1418  * if the callback represents a non-static member function of an
1419  * object which may be destroyed before the callback executes).
1420  * @param context The glib main context of the thread in whose main
1421  * loop the 'fail' callback is to be executed (the default of NULL
1422  * will cause the functor to be executed in the main program loop).
1423  * @exception std::bad_alloc This method might throw std::bad_alloc if
1424  * memory is exhausted and the system throws in that case. If it does
1425  * so, the 'fail' callback will be disposed of.
1426  * @exception Cgu::Thread::MutexError This method will throw
1427  * Cgu:Thread::MutexError if initialisation of the mutex in a
1428  * SafeEmitterArg object constructed by Cgu::start_timeout() fails.
1429  * If it does so, the 'fail' callback will be disposed of. (It is
1430  * often not worth checking for this, as it means either memory is
1431  * exhausted or pthread has run out of other resources to create new
1432  * mutexes.)
1433  * @note By virtue of the Releaser object, it is in theory possible
1434  * (if memory is exhausted and the system throws in that case) that an
1435  * internal SafeEmitterArg object will throw std::bad_alloc when
1436  * emitting/executing the 'fail' callback in the glib main loop, with
1437  * the result that the relevant callback will not execute (instead the
1438  * exception will be consumed and a g_critical() warning will be
1439  * issued). This is rarely of any relevance because glib will abort
1440  * the program if it is itself unable to obtain memory from the
1441  * operating system. However, where it is relevant, design the
1442  * program so that it is not necessary to provide a releaser object.
1443  *
1444  * Since 2.0.2
1445  */
1446  void fail(const Cgu::Callback::Callback* cb,
1447  Cgu::Releaser& r,
1448  GMainContext* context = 0);
1449 
1450 /**
1451  * @return true if the function or callable object represented by this
1452  * Cgu::Thread::Future object has finished, either by returning
1453  * normally, by cancellation or by virtue of having thrown
1454  * Cgu::Thread::Exit or some exception derived from std::exception.
1455  * Once this method returns true, then it is guaranteed that the get()
1456  * or move_get() method will not block (except as incidental to any
1457  * contention between threads calling get()). Once this method has
1458  * returned true or get() or move_get() has unblocked, then the result
1459  * of is_error() is definitive. This method is thread safe and may be
1460  * called by any thread. It will not throw.
1461  * @note This method will return true even though any callbacks
1462  * connected to done_emitter are still executing or waiting to
1463  * execute. From version 2.0.2 the is_emitter_done() method will
1464  * indicate when done_emitter callbacks (if any) have also completed.
1465  */
1466  bool is_done() const;
1467 
1468 /**
1469  * @return true if both the function or callable object represented by
1470  * this Cgu::Thread::Future object has finished and any callbacks
1471  * connected to done_emitter have completed. Once this method returns
1472  * true, then the result of is_emitter_error() is definitive. This
1473  * method is thread safe and may be called by any thread. It will not
1474  * throw.
1475  * @note This method will return true automatically if is_error() and
1476  * is_done() return true, because if the function or callable object
1477  * represented by this Cgu::Thread::Future object was cancelled or
1478  * exited with an uncaught exception, done_emitter is never emitted.
1479  * In addition, if this method returns true, then is_done() must also
1480  * return true.
1481  *
1482  * Since 2.0.2
1483  */
1484  bool is_emitter_done() const;
1485 
1486 /**
1487  * @return true if (a) a Cgu::Thread::Exit exception has been thrown
1488  * by the function or callable object represented by this
1489  * Cgu::Thread::Future object (which will have been consumed by this
1490  * Cgu::Thread::Future object), (b) an exception derived from
1491  * std::exception has been thrown on invoking that function or object
1492  * which was not caught by it (which will have been consumed by this
1493  * Cgu::Thread::Future object), (c) any of those exceptions have been
1494  * thrown either by the copy constructor of an argument taken by value
1495  * by that function or object, or by the move assignment operator (or
1496  * if none, copy assignment operator) of the return value of that
1497  * function or object (which will have been consumed by this
1498  * Cgu::Thread::Future object), (d) the worker thread in which that
1499  * function or callable object executes was cancelled in mid-course
1500  * with a call to cancel() or (e) the thread wrapper implementing the
1501  * worker thread in this Cgu::Thread::Future object threw and then
1502  * consumed std::bad_alloc (this is different from the run() method
1503  * throwing std::bad_alloc). In these cases the value obtained by
1504  * get() or move_get() will not be valid (it will be a default
1505  * constructed object of the return type of the function represented
1506  * by this Cgu::Thread::Future object). Otherwise this method returns
1507  * false. The result of this method is definitive once get() or
1508  * move_get() has unblocked or is_done() returns true. This method is
1509  * thread safe and may be called by any thread. It will not throw.
1510  */
1511  bool is_error() const;
1512 
1513 /**
1514  * @return true if an uncaught exception arose in emitting @ref
1515  * DoneEmitterAnchor "done_emitter" when executing callbacks connected
1516  * to it. Otherwise this method returns false. The result of this
1517  * method is definitive once is_emitter_done() returns true. This
1518  * method is thread safe and may be called by any thread. It will not
1519  * throw.
1520  * @note This method will return false automatically if is_error()
1521  * returns true, because if the function or callable object
1522  * represented by this Cgu::Thread::Future object was cancelled or
1523  * exited with an uncaught exception, done_emitter is never emitted.
1524  * It follows that if this method returns true, is_error() must return
1525  * false.
1526  */
1527  bool is_emitter_error() const;
1528 
1529 /**
1530  * A Cgu::SafeEmitter object which is emitted when the function or
1531  * callable object represented by this Cgu::Thread::Future object
1532  * finishes correctly (that is, it is not cancelled and does not throw
1533  * any uncaught exceptions). By itself this emission does not do too
1534  * much as it is emitted (and connected callbacks execute in) the same
1535  * worker thread immediately after the Future function has completed.
1536  * However, any thread can connect a callback object to this
1537  * Cgu::SafeEmitter object and a connected callback can, say, cause
1538  * another callback to be executed in a thread's main loop using
1539  * Cgu::Callback::post(), and from version 2.0.2 when() methods are
1540  * provided which will do this for users automatically. Once the
1541  * run() method has been called, this Cgu::Thread::Future object (and
1542  * so done_emitter) will always stay in existence until the function
1543  * or callable object represented by it has completed (whether
1544  * correctly, by cancellation or by a thrown exception) and any
1545  * callbacks connected to the done_emitter object have completed,
1546  * irrespective of whether the intrusive pointer returned by the
1547  * make() or make_future() functions has gone out of scope.
1548  * @note 1. Cancellation is blocked while the Cgu::SafeEmitter object
1549  * emits and any connected callback executes.
1550  * @note 2. A connected callback can however terminate the worker
1551  * thread by throwing Cgu::Thread::Exit (in which case no subsequent
1552  * callbacks to be executed on that emission will execute either: the
1553  * worker thread will safely terminate and unwind the stack in so
1554  * doing). In that event, the emitter_error flag will be set.
1555  * @note 3. All other uncaught exceptions which might be thrown by the
1556  * Cgu::SafeEmitter object emitting, or by a connected callback
1557  * function executing, are consumed to retain the integrity of the
1558  * Thread::Future object. In the event of such an exception being
1559  * thrown, the emitter_error flag will be set. In summary, the
1560  * emitter_error flag will be set if (a) a connected callback function
1561  * throws Cgu::Thread::Exit, (b) some other uncaught exception escapes
1562  * from a connected callback function or (c) Cgu::SafeEmitter::emit()
1563  * throws std::bad_alloc or the copy constructor of a bound argument
1564  * which is not a reference argument has thrown. If the user knows
1565  * that the callback function does not throw Cgu::Thread::Exit and
1566  * does not allow any other exception to escape, then the cause must
1567  * be a std::bad_alloc memory exception in Cgu::SafeEmitter::emit() or
1568  * the copy constructor of a non-reference bound argument throwing.
1569  * @note 4. An emission is thread safe if the connected callback
1570  * functions are thread safe.
1571  * @note 5. This Cgu::Thread::Future object's mutex is released while
1572  * the Cgu::SafeEmitter object emits. This means that any connected
1573  * callbacks can safely call, say, the Future object's get() or
1574  * is_error() methods. However, a connected callback should not hold
1575  * a bound argument comprising a copy of this Cgu::Thread::Future
1576  * object held by intrusive pointer as returned by the make() or
1577  * make_future() methods (that would result in this
1578  * Cgu::Thread::Future object owning, via done_emitter, a reference to
1579  * itself and so become incapable of being freed). The callback may,
1580  * however, take a pointer to this Cgu::Thread::Future object as a
1581  * bound argument, as obtained by the Cgu::IntrusivePtr::get() method,
1582  * because this Cgu::Thread::Future object is guaranteed to remain in
1583  * existence until all callbacks connected to done_emitter have
1584  * completed executing.
1585  * @anchor DoneEmitterAnchor
1586  */
1588 
1589 /* Only has effect if --with-glib-memory-slices-compat or
1590  * --with-glib-memory-slices-no-compat option picked */
1592 };
1593 
1594 /**
1595  * A convenience helper function which calls
1596  * Cgu::Thread::Future::make() to obtain a Future object without the
1597  * need to specify the return value of the function represented by the
1598  * new object: that is deduced from the signature of that function.
1599  * This is useful shorthand when also employed with the C++11/14
1600  * 'auto' keyword.
1601  * @exception std::bad_alloc It might throw std::bad_alloc if memory
1602  * is exhausted and the system throws in that case. (This exception
1603  * will not be thrown if the library has been installed using the
1604  * \--with-glib-memory-slices-no-compat configuration option: instead
1605  * glib will terminate the program if it is unable to obtain memory
1606  * from the operating system.)
1607  * @exception Cgu::Thread::MutexError It might throw
1608  * Cgu::Thread::MutexError if initialisation of the contained mutex
1609  * fails. (It is often not worth checking for this, as it means
1610  * either memory is exhausted or pthread has run out of other
1611  * resources to create new mutexes.)
1612  * @exception Cgu::Thread::CondError It might throw
1613  * Cgu::Thread::CondError if initialisation of the contained condition
1614  * variable fails. (It is often not worth checking for this, as it
1615  * means either memory is exhausted or pthread has run out of other
1616  * resources to create new condition variables.)
1617  * @note This method will also throw if the copy or move constructor
1618  * of a bound argument throws, or the default constructor of the
1619  * return value type of the function represented by the new object
1620  * throws.
1621 
1622  *
1623  * Since 2.0.4
1624  */
1625 template <class Obj, class Ret, class... Params, class... Args>
1627  Ret (Obj::*func)(Params...),
1628  Args&&... args) {
1629  return Cgu::Thread::Future<Ret>::make(obj, func, std::forward<Args>(args)...);
1630 }
1631 
1632 /**
1633  * A convenience helper function which calls
1634  * Cgu::Thread::Future::make() to obtain a Future object without the
1635  * need to specify the return value of the function represented by the
1636  * new object: that is deduced from the signature of that function.
1637  * This is useful shorthand when also employed with the C++11/14
1638  * 'auto' keyword.
1639  * @exception std::bad_alloc It might throw std::bad_alloc if memory
1640  * is exhausted and the system throws in that case. (This exception
1641  * will not be thrown if the library has been installed using the
1642  * \--with-glib-memory-slices-no-compat configuration option: instead
1643  * glib will terminate the program if it is unable to obtain memory
1644  * from the operating system.)
1645  * @exception Cgu::Thread::MutexError It might throw
1646  * Cgu::Thread::MutexError if initialisation of the contained mutex
1647  * fails. (It is often not worth checking for this, as it means
1648  * either memory is exhausted or pthread has run out of other
1649  * resources to create new mutexes.)
1650  * @exception Cgu::Thread::CondError It might throw
1651  * Cgu::Thread::CondError if initialisation of the contained condition
1652  * variable fails. (It is often not worth checking for this, as it
1653  * means either memory is exhausted or pthread has run out of other
1654  * resources to create new condition variables.)
1655  * @note This method will also throw if the copy or move constructor
1656  * of a bound argument throws, or the default constructor of the
1657  * return value type of the function represented by the new object
1658  * throws.
1659  *
1660  * Since 2.0.4
1661  */
1662 template <class Obj, class Ret, class... Params, class... Args>
1664  Ret (Obj::*func)(Params...) const,
1665  Args&&... args) {
1666  return Cgu::Thread::Future<Ret>::make(obj, func, std::forward<Args>(args)...);
1667 }
1668 
1669 /**
1670  * A convenience helper function which calls
1671  * Cgu::Thread::Future::make() to obtain a Future object without the
1672  * need to specify the return value of the function represented by the
1673  * new object: that is deduced from the signature of that function.
1674  * This is useful shorthand when also employed with the C++11/14
1675  * 'auto' keyword.
1676  * @exception std::bad_alloc It might throw std::bad_alloc if memory
1677  * is exhausted and the system throws in that case. (This exception
1678  * will not be thrown if the library has been installed using the
1679  * \--with-glib-memory-slices-no-compat configuration option: instead
1680  * glib will terminate the program if it is unable to obtain memory
1681  * from the operating system.)
1682  * @exception Cgu::Thread::MutexError It might throw
1683  * Cgu::Thread::MutexError if initialisation of the contained mutex
1684  * fails. (It is often not worth checking for this, as it means
1685  * either memory is exhausted or pthread has run out of other
1686  * resources to create new mutexes.)
1687  * @exception Cgu::Thread::CondError It might throw
1688  * Cgu::Thread::CondError if initialisation of the contained condition
1689  * variable fails. (It is often not worth checking for this, as it
1690  * means either memory is exhausted or pthread has run out of other
1691  * resources to create new condition variables.)
1692  * @note This method will also throw if the copy or move constructor
1693  * of a bound argument throws, or the default constructor of the
1694  * return value type of the function represented by the new object
1695  * throws.
1696  *
1697  * Since 2.0.4
1698  */
1699 template <class Ret, class... Params, class... Args>
1701  Args&&... args) {
1702  return Cgu::Thread::Future<Ret>::make(func, std::forward<Args>(args)...);
1703 }
1704 
1705 /**
1706  * A convenience helper function which calls
1707  * Cgu::Thread::Future::make() to obtain a Future without the need to
1708  * specify the return value of the callable object to be represented
1709  * by it: that is deduced. This is useful shorthand when also
1710  * employed with the C++11/14 'auto' keyword.
1711  *
1712  * From version 2.0.14, this method takes the callable object as a
1713  * template parameter, and in prior versions it took it as a
1714  * std::function object. Before version 2.0.14 it was necessary to
1715  * specify the return value of any callable object which was not a
1716  * std::function object as a specific template parameter: this is not
1717  * necessary in version 2.0.14, as it is deduced automatically.
1718  *
1719  * @param functor The callable object to be executed. It should
1720  * return a value (it cannot return void).
1721  * @exception std::bad_alloc It might throw std::bad_alloc if memory
1722  * is exhausted and the system throws in that case. (This exception
1723  * will not be thrown if the library has been installed using the
1724  * \--with-glib-memory-slices-no-compat configuration option: instead
1725  * glib will terminate the program if it is unable to obtain memory
1726  * from the operating system.)
1727  * @exception Cgu::Thread::MutexError It might throw
1728  * Cgu::Thread::MutexError if initialisation of the contained mutex
1729  * fails. (It is often not worth checking for this, as it means
1730  * either memory is exhausted or pthread has run out of other
1731  * resources to create new mutexes.)
1732  * @exception Cgu::Thread::CondError It might throw
1733  * Cgu::Thread::CondError if initialisation of the contained condition
1734  * variable fails. (It is often not worth checking for this, as it
1735  * means either memory is exhausted or pthread has run out of other
1736  * resources to create new condition variables.)
1737  * @note 1. This method will also throw if the copy or move
1738  * constructor of the callable object passed as an argument throws, or
1739  * the default constructor of the return value type of the function
1740  * represented by the new object throws.
1741  * @note 2. If the callable object passed as an argument has both
1742  * const and non-const operator()() methods, the non-const version
1743  * will be called even if the callable object passed is a const
1744  * object.
1745  *
1746  * Since 2.0.4
1747  */
1748 // we don't need this version of make_future() for syntactic reasons -
1749 // the version taking a single template parameter will do by itself
1750 // syntactically because it can use decltype. However, we include
1751 // this version in order to be API compatible with c++-gtk-utils <
1752 // 2.0.14, which required the return type to be specified when this
1753 // method is passed something other than a std::function object.
1754 // SFINAE will take care of the rest, except with a corner case where
1755 // all of the following apply: (i) a function object is passed whose
1756 // operator()() method returns a copy of the function object (or
1757 // another function object of the same type), (ii) the function object
1758 // is passed to this method as a rvalue and not a lvalue, and (iii)
1759 // the user specifically states the return type when instantiating
1760 // this template function. This would give rise to an ambiguity, but
1761 // its happening is extremely unlikely, and cannot happen with a
1762 // lambda or the return value of std::bind, because those types are
1763 // only known to the compiler, and cannot happen with other objects if
1764 // the user lets template deduction take its course.
1765 template <class Ret, class Func>
1767  return Cgu::Thread::Future<Ret>::make(std::forward<Func>(functor));
1768 }
1769 
1770 // we don't want to document this function: it provides the type
1771 // deduction of the return value of the passed functor (it deals with
1772 // cases where this is not specified expressly).
1773 #ifndef DOXYGEN_PARSING
1774 template <class Func>
1775 auto make_future(Func&& functor) -> Cgu::IntrusivePtr<Cgu::Thread::Future<decltype(functor())>> {
1776  // this function will fail to compile if the return type is a
1777  // reference type: that is a feature, not a bug, as a function
1778  // returning a reference lacks referential transparency, is unlikely
1779  // to be thread-safe and is unsuitable for use as a task function
1780  return Cgu::Thread::Future<decltype(functor())>::make(std::forward<Func>(functor));
1781 }
1782 #endif
1783 
1784 } // namespace Thread
1785 
1786 } // namespace Cgu
1787 
1788 #include <c++-gtk-utils/future.tpp>
1789 
1790 #endif
Cgu::Thread::Future::is_done
bool is_done() const
Cgu::Thread::FutureWhenError::what
virtual const char * what() const
Definition: future.h:68
Cgu::Callback::CallbackArg
The callback interface class.
Definition: callback.h:522
Cgu
Definition: application.h:44
Cgu::Thread::Future::done_emitter
SafeEmitter done_emitter
Definition: future.h:1587
Cgu::Thread::FutureWhenError
Definition: future.h:67
Cgu::Callback::make
CallbackArg< FreeArgs... > * make(T &t, void(T::*func)(FreeArgs...))
Definition: callback.h:1659
Cgu::Thread::make_future
Cgu::IntrusivePtr< Cgu::Thread::Future< Ret > > make_future(Obj &obj, Ret(Obj::*func)(Params...), Args &&... args)
Definition: future.h:1626
shared_ptr.h
timeout.h
Cgu::Thread::Cond
A wrapper class for pthread condition variables.
Definition: mutex.h:449
Cgu::IntrusivePtr
This is a smart pointer for managing objects allocated on freestore which maintain their own referenc...
Definition: intrusive_ptr.h:98
Cgu::Thread::Future::is_error
bool is_error() const
callback.h
This file provides classes for type erasure.
Cgu::Thread::Future::cancel
bool cancel()
Cgu::Thread::Future::fail
void fail(const Cgu::Callback::Callback *cb, GMainContext *context=0)
intrusive_ptr.h
Cgu::Thread::FutureThreadError
Definition: future.h:63
Cgu::Thread::Future::operator=
Future & operator=(const Future &)=delete
Cgu::SafeEmitterArg
A thread-safe class to execute callbacks connected to it, with provision for automatic disconnection.
Definition: emitter.h:308
Cgu::Thread::Future::move_get
Val move_get()
Cgu::Callback::SafeFunctorArg
Functor class holding a Callback::CallbackArg object, with thread-safe reference count.
Definition: callback.h:726
Cgu::Thread::Future
A class representing a pthread thread which will provide a value.
Definition: future.h:276
Cgu::Thread::Future::is_emitter_error
bool is_emitter_error() const
CGU_GLIB_MEMORY_SLICES_FUNCS
#define CGU_GLIB_MEMORY_SLICES_FUNCS
Definition: cgu_config.h:84
Cgu::IntrusiveLockCounter
This is a counter class providing the ref() and unref() functions required by IntrusivePtr,...
Definition: intrusive_ptr.h:349
mutex.h
Provides wrapper classes for pthread mutexes and condition variables, and scoped locking classes for ...
Cgu::Thread::Future::run
bool run()
Cgu::Thread::Future::when
Cgu::Callback::SafeFunctor when(const Cgu::Callback::CallbackArg< const Val & > *cb, gint priority=G_PRIORITY_DEFAULT, GMainContext *context=0)
Cgu::Releaser
A class used for tracking EmitterArg and SafeEmitterArg connections.
Definition: emitter.h:333
Cgu::Thread::Future::is_emitter_done
bool is_emitter_done() const
Cgu::Thread::Future::get
Val get()
thread.h
emitter.h
This file provides a thread-safe signal/slot mechanism, with automatic disconnection.
Cgu::Thread::Mutex
A wrapper class for pthread mutexes.
Definition: mutex.h:117
Cgu::Thread::Thread
A class representing a pthread thread.
Definition: thread.h:166
Cgu::Thread::FutureThreadError::what
virtual const char * what() const
Definition: future.h:64
Cgu::Thread::Future::make
static Cgu::IntrusivePtr< Cgu::Thread::Future< Val > > make(T &t, Ret(T::*func)())
cgu_config.h