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, std::remove_const,
46  // std::enable_if and std::is_convertible
47 
48 #include <pthread.h>
49 #include <glib.h>
50 
51 #include <c++-gtk-utils/thread.h>
52 #include <c++-gtk-utils/mutex.h>
53 #include <c++-gtk-utils/callback.h>
56 #include <c++-gtk-utils/emitter.h>
57 #include <c++-gtk-utils/timeout.h>
59 
60 namespace Cgu {
61 
62 namespace Thread {
63 
64 struct FutureThreadError: public std::exception {
65  virtual const char* what() const throw() {return "FutureThreadError\n";}
66 };
67 
68 struct FutureWhenError: public std::exception {
69  virtual const char* what() const throw() {return "FutureWhenError\n";}
70 };
71 
72 /**
73  * @class Cgu::Thread::Future future.h c++-gtk-utils/future.h
74  * @brief A class representing a pthread thread which will
75  * provide a value.
76  * @sa Cgu::Thread::Thread Cgu::Thread::JoinableHandle Cgu::AsyncResult Cgu::Thread::make_future() Cgu::Thread::TaskManager
77  *
78  * The Thread::Future class will launch a worker thread, run the
79  * function it represents in that thread until it returns, and store
80  * the return value so that it can be waited on and/or extracted by
81  * another thread. A new Thread::Future object representing the
82  * function to be called is normally created by calling
83  * Cgu::Thread::make_future() with a callable object, such as a lambda
84  * expression or the return value of std::bind. The worker thread is
85  * then started by calling run(), and the value extracted or waited
86  * for by calling get(). The run() method can only be called once,
87  * but any number of threads can wait for and/or extract the return
88  * value by calling the get() method. The class also provides a
89  * move_get() method, and a SafeEmitter @ref DoneEmitterAnchor
90  * "done_emitter" public object which emits when the worker thread has
91  * finished, and an associated when() function.
92  *
93  * The template parameter type of Thread::Future is the type of the
94  * return value of the function or callable object called by the
95  * Thread::Future object. The return value can be any type, including
96  * any arbitrarily large tuple or other struct or standard C++
97  * container.
98  *
99  * A Thread::Future object cannot represent a function with a void
100  * return type - a compilation error will result if that is attempted.
101  * If no return value is wanted, then the Thread::Thread class can be
102  * used directly. (However, if in a particular usage this class is
103  * thought to be more convenient, the function to be represented by it
104  * can be wrapped by another function which provides a dummy return
105  * value, such as a dummy int. One possible case for this is where
106  * more than one thread wants to wait for the worker thread to
107  * terminate, as pthread_join() and so Thread::Thread::join() only
108  * give defined behaviour when called by one thread.)
109  *
110  * A future object can also be constructed with Thread::make_future()
111  * and Thread::Future::make() functions which take a function pointer
112  * (or an object reference and member function pointer) with bound
113  * arguments but these are deprecated in the 2.2 series of the library
114  * as they offer little advantage over using std::bind. (Although
115  * deprecated, there is no plan to remove these functions as they are
116  * there and they work - the deprecation is in effect guidance.)
117  * These deprecated functions can take up to three bound arguments in
118  * the case of a non-static member function, and four bound arguments
119  * in the case of any other function. In the case of a non-static
120  * member function, the referenced object whose member function is to
121  * be called must remain in existence until the worker thread has
122  * completed. The target function passed by pointer (or member
123  * function pointer) can take a reference to const argument, as a copy
124  * of the object to be passed to the argument is taken to avoid
125  * dangling references, but it cannot take a reference to non-const
126  * argument.
127  *
128  * It is to be noted that the target function or callable object to be
129  * represented by a Thread::Future object must not allow any exception
130  * other than Thread::Exit, an exception deriving from std::exception
131  * or a cancellation pseudo-exception to escape from it when it is
132  * executed. This includes ensuring that, for any function's bound
133  * argument which is of class type and not taken by reference, the
134  * argument's copy constructor does not throw anything other than
135  * these, and that the move assignment operator (or if none, copy
136  * assignment operator) of the return value (if of class type) of the
137  * target function or callable object does not throw anything other
138  * than these. (If the target function or callable object, or the
139  * copy constructor of a bound value argument or the move or copy
140  * assignment operator of the return value, throws Thread::Exit or an
141  * exception deriving from std::exception, the exception is safely
142  * consumed and the Thread::Future object's error flag is set.
143  * However, if the move assignment operator or copy assignment
144  * operator, as the case may be, of the return value throws, it should
145  * leave the movee/assignee in a state in which it can safely be
146  * destroyed and in which, if that movee/assignee is further copied or
147  * moved from, the copy or move either throws an exception or produces
148  * an object which can also be destroyed -- but these are minimum
149  * requirements for any reasonable assignment operator, and met by any
150  * assignment operator offering the basic exception guarantee.)
151  *
152  * The Thread::Future object will store the return value of the target
153  * function or callable object, so that it is available to the get()
154  * and move_get() methods and any 'when' callback, and therefore
155  * either move it, or if it has no move assignment operator, copy it
156  * once.
157  *
158  * For safety reasons, the get() method returns by value and so will
159  * cause the return value to be copied once more, so for return values
160  * comprising complex class objects which are to be extracted using
161  * the get() method, it is often better if the function represented by
162  * the Thread::Future object allocates the return value on free store
163  * and returns it by pointer, by Cgu::SharedLockPtr, or by a
164  * std::shared_ptr implementation which has a thread-safe reference
165  * count. Alternatively, from version 2.0.11 a move_get() method is
166  * provided which will make a move operation instead of a copy if the
167  * return type implements a move constructor, but see the
168  * documentation on move_get() for the caveats with respect to its
169  * use: in particular, if move_get() is to be called by a thread, then
170  * get() may not normally be called by another thread, nor should the
171  * when() method be called.
172  *
173  * It should be noted that where the when() method is used, the return
174  * value is passed to the 'when' callback by reference to const and so
175  * without the copying carried out by the get() method: therefore, if
176  * the return value has a move assignment operator and the when()
177  * method is to be employed, and the 'when' callback only needs to
178  * call const methods of the return value, it may be more efficient
179  * not to allocate the return value on free store.
180  *
181  * This is a usage example:
182  *
183  * @code
184  * std::vector<long> get_primes(int n); // calculates the first n primes
185  *
186  * // get the first 1,000 primes
187  * using namespace Cgu;
188  *
189  * auto future = Thread::make_future([] () {return get_primes(1000);});
190  *
191  * future->run();
192  * ... [ do something else ] ...
193  * std::vector<long> result(future->move_get());
194  * std::for_each(result.begin(), result.end(), [](long l) {std::cout << l << std::endl;});
195  * @endcode
196  *
197  * The Cgu::Thread::Future::when() functions
198  * -----------------------------------------
199  *
200  * The return value of the thread function represented by
201  * Cgu::Thread::Future can be obtained asynchronously using
202  * Cgu::Thread::Future::when() to execute a function in a glib main
203  * loop when the thread function completes. The above example could
204  * be reimplemented as:
205  *
206  * @code
207  * std::vector<long> get_primes(int n); // calculates the first n primes
208  *
209  * using namespace Cgu;
210  *
211  * auto future = Thread::make_future([] () {return get_primes(1000);});
212  * future->when([](const std::vector<long>& vec) {
213  * for (const auto& elt: vec) {std::cout << elt << std::endl;}
214  * });
215  * future->run();
216  * @endcode
217  *
218  * The Cgu::Thread::Future::fail() functions
219  * -----------------------------------------
220  *
221  * The Thread::Future::when() functions have an associated optional
222  * Thread::Future::fail() function which causes a 'fail' callback to
223  * execute in a glib main loop in the event of certain exceptions
224  * arising in executing the thread function or a thread being
225  * cancelled (the documentation on Thread::Future::fail() gives
226  * further details). The 'fail' callback must be fully bound. Whilst
227  * a worker thread can pass error status to the 'fail' callback via
228  * shared data bound to both the thread function and the 'fail'
229  * callback (held by, say, a SharedLockPtr object), or a global error
230  * stack, 'fail' callbacks are generally best reserved either for use
231  * with entirely unexpected exceptions, where the most reasonable
232  * course is to perform some orderly logging and shutdown, or to
233  * report thread cancellation. For handlable exceptions, in an
234  * asynchronous environment the best course is often to catch them and
235  * deal with them in the thread function itself and return a value of
236  * the return type for the 'when' callback indicating no result.
237  */
238 
239 namespace FutureHelper {
240 
241 // the sole purpose of this struct is to enable a callback object to
242 // be constructed with Callback::make_ref() which takes an argument
243 // which can be mutated when the callback is executed. Normally this
244 // would be unsafe: however in this particular use it is fine as the
245 // callback is only ever executed once, via Future::run().
246 template <class Val>
248  mutable std::unique_ptr<const Cgu::Callback::CallbackArg<const Val&>> when;
249  // TODO: these constructors are a work-around for a bug in gcc <
250  // 4.6. At any API break where the required version of gcc is
251  // increased to gcc-4.6 or higher, remove them.
252  WhenWrapperArg(std::unique_ptr<const Cgu::Callback::CallbackArg<const Val&>>&& when_) :
253  when(std::move(when_)) {}
254  WhenWrapperArg(WhenWrapperArg&& w): when(std::move(w.when)) {}
255 };
256 
257 // the sole purpose of this struct is to enable a callback object to
258 // be constructed with Callback::make_ref() which takes an argument
259 // which can be mutated when the callback is executed. Normally this
260 // would be unsafe: however in this particular use it is fine as the
261 // callback is only ever executed once, via Future::run().
262 template <class Val>
264  mutable std::unique_ptr<Cgu::SafeEmitterArg<const Val&>> when;
265  // TODO: these constructors are a work-around for a bug in gcc <
266  // 4.6. At any API break where the required version of gcc is
267  // increased to gcc-4.6 or higher, remove them.
269  when(std::move(when_)) {}
270  WhenWrapperArgRel(WhenWrapperArgRel&& w): when(std::move(w.when)) {}
271 };
272 
273 } // namespace FutureHelper
274 
275 
276 template <class Val>
278 
279  std::unique_ptr<Cgu::Thread::Thread> thread_u;
280  std::unique_ptr<Cgu::Callback::Callback> cb_u;
281 
282  mutable Mutex mutex;
283  Cond cond;
284  Val val;
285  bool done;
286  bool running;
287  bool error;
288  bool emitter_error;
289 
290  template <class T, class Ret, class... Args>
291  void run_wrapper(T*, Ret (T::*)(Args...), const Args&...);
292 
293  template <class T, class Ret, class... Args>
294  void run_wrapper_const(const T*, Ret (T::*)(Args...) const, const Args&...);
295 
296  template <class Ret, class... Args>
297  void run_wrapper_static(Ret (*)(Args...), const Args&...);
298 
299  template <class Func>
300  void run_wrapper_functor(Func&);
301 
302  void cancel_cleanup();
303 
304  void execute_done(const std::unique_ptr<const Cgu::Callback::CallbackArg<const Val&>>&);
305  void post_done(const FutureHelper::WhenWrapperArg<Val>&,
306  gint, GMainContext*);
307  void execute_done_rel(const std::unique_ptr<Cgu::SafeEmitterArg<const Val&>>&);
308  void post_done_rel(const FutureHelper::WhenWrapperArgRel<Val>&,
309  gint, GMainContext*);
310 
311  // this is a static function taking the future object by IntrusivePtr to
312  // ensure that the future object remains in existence whilst this
313  // function might execute
314  static void fail_cb(const Cgu::IntrusivePtr<Future<Val>>& future,
315  const std::unique_ptr<const Cgu::Callback::Callback>& func,
316  bool& ret);
317 
318  // private constructor - this class can only be created with Thread::Future::make()
319  Future(): val(), done(false), running(false), error(false), emitter_error(false) {}
320 
321 public:
322 
323  // this class cannot be copied except by smart pointer
324 /**
325  * This class cannot be copied (except by smart pointer). The copy
326  * constructor is deleted.
327  */
328  Future(const Future&) = delete;
329 
330 /**
331  * This class cannot be copied (except by smart pointer). The
332  * assignment operator is deleted.
333  */
334  Future& operator=(const Future&) = delete;
335 
336 /**
337  * @deprecated
338  *
339  * DEPRECATED. Use the version of Future::make() which takes a
340  * callable object.
341  *
342  * Constructs a new Cgu::Thread::Future object (returned by
343  * Cgu::IntrusivePtr<Cgu::Thread::Future<Val>>). The type parameter
344  * Val represents the return value of the function to be represented
345  * by the new object. From version 2.0.4, it will usually be more
346  * convenient to call the Cgu::Thread::make_future() function, which
347  * is a convenience wrapper for this static method.
348  * @exception std::bad_alloc It might throw std::bad_alloc if memory
349  * is exhausted and the system throws in that case. (This exception
350  * will not be thrown if the library has been installed using the
351  * \--with-glib-memory-slices-no-compat configuration option: instead
352  * glib will terminate the program if it is unable to obtain memory
353  * from the operating system.)
354  * @exception Cgu::Thread::MutexError It might throw
355  * Cgu::Thread::MutexError if initialisation of the contained mutex
356  * fails. (It is often not worth checking for this, as it means
357  * either memory is exhausted or pthread has run out of other
358  * resources to create new mutexes.)
359  * @exception Cgu::Thread::CondError It might throw
360  * Cgu::Thread::CondError if initialisation of the contained condition
361  * variable fails. (It is often not worth checking for this, as it
362  * means either memory is exhausted or pthread has run out of other
363  * resources to create new condition variables.)
364  * @note This method will also throw if the default constructor of the
365  * return value type throws.
366  */
367  template <class Ret, class T>
369  Ret (T::*func)());
370 
371 /**
372  * @deprecated
373  *
374  * DEPRECATED. Use the version of Future::make() which takes a
375  * callable object.
376  *
377  * Constructs a new Cgu::Thread::Future object (returned by
378  * Cgu::IntrusivePtr<Cgu::Thread::Future<Val>>). The type parameter
379  * Val represents the return value of the function to be represented
380  * by the new object. From version 2.0.4, it will usually be more
381  * convenient to call the Cgu::Thread::make_future() function, which
382  * is a convenience wrapper for this static method.
383  * @exception std::bad_alloc It might throw std::bad_alloc if memory
384  * is exhausted and the system throws in that case. (This exception
385  * will not be thrown if the library has been installed using the
386  * \--with-glib-memory-slices-no-compat configuration option: instead
387  * glib will terminate the program if it is unable to obtain memory
388  * from the operating system.)
389  * @exception Cgu::Thread::MutexError It might throw
390  * Cgu::Thread::MutexError if initialisation of the contained mutex
391  * fails. (It is often not worth checking for this, as it means
392  * either memory is exhausted or pthread has run out of other
393  * resources to create new mutexes.)
394  * @exception Cgu::Thread::CondError It might throw
395  * Cgu::Thread::CondError if initialisation of the contained condition
396  * variable fails. (It is often not worth checking for this, as it
397  * means either memory is exhausted or pthread has run out of other
398  * resources to create new condition variables.)
399  * @note This method will also throw if the copy or move constructor
400  * of the bound argument throws, or the default constructor of the
401  * return value type throws.
402  */
403  template <class Ret, class Param1, class Arg1, class T>
405  Ret (T::*func)(Param1),
406  Arg1&& arg1);
407 
408 /**
409  * @deprecated
410  *
411  * DEPRECATED. Use the version of Future::make() which takes a
412  * callable object.
413  *
414  * Constructs a new Cgu::Thread::Future object (returned by
415  * Cgu::IntrusivePtr<Cgu::Thread::Future<Val>>). The type parameter
416  * Val represents the return value of the function to be represented
417  * by the new object. From version 2.0.4, it will usually be more
418  * convenient to call the Cgu::Thread::make_future() function, which
419  * is a convenience wrapper for this static method.
420  * @exception std::bad_alloc It might throw std::bad_alloc if memory
421  * is exhausted and the system throws in that case. (This exception
422  * will not be thrown if the library has been installed using the
423  * \--with-glib-memory-slices-no-compat configuration option: instead
424  * glib will terminate the program if it is unable to obtain memory
425  * from the operating system.)
426  * @exception Cgu::Thread::MutexError It might throw
427  * Cgu::Thread::MutexError if initialisation of the contained mutex
428  * fails. (It is often not worth checking for this, as it means
429  * either memory is exhausted or pthread has run out of other
430  * resources to create new mutexes.)
431  * @exception Cgu::Thread::CondError It might throw
432  * Cgu::Thread::CondError if initialisation of the contained condition
433  * variable fails. (It is often not worth checking for this, as it
434  * means either memory is exhausted or pthread has run out of other
435  * resources to create new condition variables.)
436  * @note This method will also throw if the copy or move constructor
437  * of a bound argument throws, or the default constructor of the
438  * return value type throws.
439  */
440  template <class Ret, class Param1, class Param2, class Arg1, class Arg2, class T>
442  Ret (T::*func)(Param1, Param2),
443  Arg1&& arg1,
444  Arg2&& arg2);
445 
446 /**
447  * @deprecated
448  *
449  * DEPRECATED. Use the version of Future::make() which takes a
450  * callable object.
451  *
452  * Constructs a new Cgu::Thread::Future object (returned by
453  * Cgu::IntrusivePtr<Cgu::Thread::Future<Val>>). The type parameter
454  * Val represents the return value of the function to be represented
455  * by the new object. From version 2.0.4, it will usually be more
456  * convenient to call the Cgu::Thread::make_future() function, which
457  * is a convenience wrapper for this static method.
458  * @exception std::bad_alloc It might throw std::bad_alloc if memory
459  * is exhausted and the system throws in that case. (This exception
460  * will not be thrown if the library has been installed using the
461  * \--with-glib-memory-slices-no-compat configuration option: instead
462  * glib will terminate the program if it is unable to obtain memory
463  * from the operating system.)
464  * @exception Cgu::Thread::MutexError It might throw
465  * Cgu::Thread::MutexError if initialisation of the contained mutex
466  * fails. (It is often not worth checking for this, as it means
467  * either memory is exhausted or pthread has run out of other
468  * resources to create new mutexes.)
469  * @exception Cgu::Thread::CondError It might throw
470  * Cgu::Thread::CondError if initialisation of the contained condition
471  * variable fails. (It is often not worth checking for this, as it
472  * means either memory is exhausted or pthread has run out of other
473  * resources to create new condition variables.)
474  * @note This method will also throw if the copy or move constructor
475  * of a bound argument throws, or the default constructor of the
476  * return value type throws.
477  */
478  template <class Ret, class Param1, class Param2, class Param3,
479  class Arg1, class Arg2, class Arg3, class T>
481  Ret (T::*func)(Param1, Param2, Param3),
482  Arg1&& arg1,
483  Arg2&& arg2,
484  Arg3&& arg3);
485 
486 /**
487  * @deprecated
488  *
489  * DEPRECATED. Use the version of Future::make() which takes a
490  * callable object.
491  *
492  * Constructs a new Cgu::Thread::Future object (returned by
493  * Cgu::IntrusivePtr<Cgu::Thread::Future<Val>>). The type parameter
494  * Val represents the return value of the function to be represented
495  * by the new object. From version 2.0.4, it will usually be more
496  * convenient to call the Cgu::Thread::make_future() function, which
497  * is a convenience wrapper for this static method.
498  * @exception std::bad_alloc It might throw std::bad_alloc if memory
499  * is exhausted and the system throws in that case. (This exception
500  * will not be thrown if the library has been installed using the
501  * \--with-glib-memory-slices-no-compat configuration option: instead
502  * glib will terminate the program if it is unable to obtain memory
503  * from the operating system.)
504  * @exception Cgu::Thread::MutexError It might throw
505  * Cgu::Thread::MutexError if initialisation of the contained mutex
506  * fails. (It is often not worth checking for this, as it means
507  * either memory is exhausted or pthread has run out of other
508  * resources to create new mutexes.)
509  * @exception Cgu::Thread::CondError It might throw
510  * Cgu::Thread::CondError if initialisation of the contained condition
511  * variable fails. (It is often not worth checking for this, as it
512  * means either memory is exhausted or pthread has run out of other
513  * resources to create new condition variables.)
514  * @note This method will also throw if the default constructor of the
515  * return value type throws.
516  */
517  template <class Ret, class T>
519  Ret (T::*func)() const);
520 
521 /**
522  * @deprecated
523  *
524  * DEPRECATED. Use the version of Future::make() which takes a
525  * callable object.
526  *
527  * Constructs a new Cgu::Thread::Future object (returned by
528  * Cgu::IntrusivePtr<Cgu::Thread::Future<Val>>). The type parameter
529  * Val represents the return value of the function to be represented
530  * by the new object. From version 2.0.4, it will usually be more
531  * convenient to call the Cgu::Thread::make_future() function, which
532  * is a convenience wrapper for this static method.
533  * @exception std::bad_alloc It might throw std::bad_alloc if memory
534  * is exhausted and the system throws in that case. (This exception
535  * will not be thrown if the library has been installed using the
536  * \--with-glib-memory-slices-no-compat configuration option: instead
537  * glib will terminate the program if it is unable to obtain memory
538  * from the operating system.)
539  * @exception Cgu::Thread::MutexError It might throw
540  * Cgu::Thread::MutexError if initialisation of the contained mutex
541  * fails. (It is often not worth checking for this, as it means
542  * either memory is exhausted or pthread has run out of other
543  * resources to create new mutexes.)
544  * @exception Cgu::Thread::CondError It might throw
545  * Cgu::Thread::CondError if initialisation of the contained condition
546  * variable fails. (It is often not worth checking for this, as it
547  * means either memory is exhausted or pthread has run out of other
548  * resources to create new condition variables.)
549  * @note This method will also throw if the copy or move constructor
550  * of the bound argument throws, or the default constructor of the
551  * return value type throws.
552  */
553  template <class Ret, class Param1, class Arg1, class T>
555  Ret (T::*func)(Param1) const,
556  Arg1&& arg1);
557 
558 /**
559  * @deprecated
560  *
561  * DEPRECATED. Use the version of Future::make() which takes a
562  * callable object.
563  *
564  * Constructs a new Cgu::Thread::Future object (returned by
565  * Cgu::IntrusivePtr<Cgu::Thread::Future<Val>>). The type parameter
566  * Val represents the return value of the function to be represented
567  * by the new object. From version 2.0.4, it will usually be more
568  * convenient to call the Cgu::Thread::make_future() function, which
569  * is a convenience wrapper for this static method.
570  * @exception std::bad_alloc It might throw std::bad_alloc if memory
571  * is exhausted and the system throws in that case. (This exception
572  * will not be thrown if the library has been installed using the
573  * \--with-glib-memory-slices-no-compat configuration option: instead
574  * glib will terminate the program if it is unable to obtain memory
575  * from the operating system.)
576  * @exception Cgu::Thread::MutexError It might throw
577  * Cgu::Thread::MutexError if initialisation of the contained mutex
578  * fails. (It is often not worth checking for this, as it means
579  * either memory is exhausted or pthread has run out of other
580  * resources to create new mutexes.)
581  * @exception Cgu::Thread::CondError It might throw
582  * Cgu::Thread::CondError if initialisation of the contained condition
583  * variable fails. (It is often not worth checking for this, as it
584  * means either memory is exhausted or pthread has run out of other
585  * resources to create new condition variables.)
586  * @note This method will also throw if the copy or move constructor
587  * of a bound argument throws, or the default constructor of the
588  * return value type throws.
589  */
590  template <class Ret, class Param1, class Param2, class Arg1, class Arg2, class T>
592  Ret (T::*func)(Param1, Param2) const,
593  Arg1&& arg1,
594  Arg2&& arg2);
595 
596 /**
597  * @deprecated
598  *
599  * DEPRECATED. Use the version of Future::make() which takes a
600  * callable object.
601  *
602  * Constructs a new Cgu::Thread::Future object (returned by
603  * Cgu::IntrusivePtr<Cgu::Thread::Future<Val>>). The type parameter
604  * Val represents the return value of the function to be represented
605  * by the new object. From version 2.0.4, it will usually be more
606  * convenient to call the Cgu::Thread::make_future() function, which
607  * is a convenience wrapper for this static method.
608  * @exception std::bad_alloc It might throw std::bad_alloc if memory
609  * is exhausted and the system throws in that case. (This exception
610  * will not be thrown if the library has been installed using the
611  * \--with-glib-memory-slices-no-compat configuration option: instead
612  * glib will terminate the program if it is unable to obtain memory
613  * from the operating system.)
614  * @exception Cgu::Thread::MutexError It might throw
615  * Cgu::Thread::MutexError if initialisation of the contained mutex
616  * fails. (It is often not worth checking for this, as it means
617  * either memory is exhausted or pthread has run out of other
618  * resources to create new mutexes.)
619  * @exception Cgu::Thread::CondError It might throw
620  * Cgu::Thread::CondError if initialisation of the contained condition
621  * variable fails. (It is often not worth checking for this, as it
622  * means either memory is exhausted or pthread has run out of other
623  * resources to create new condition variables.)
624  * @note This method will also throw if the copy or move constructor
625  * of a bound argument throws, or the default constructor of the
626  * return value type throws.
627  */
628  template <class Ret, class Param1, class Param2, class Param3,
629  class Arg1, class Arg2, class Arg3, class T>
631  Ret (T::*func)(Param1, Param2, Param3) const,
632  Arg1&& arg1,
633  Arg2&& arg2,
634  Arg3&& arg3);
635 
636 /**
637  * Constructs a new Cgu::Thread::Future object (returned by
638  * Cgu::IntrusivePtr<Cgu::Thread::Future<Val>>). The type parameter
639  * Val represents the return value of the function to be represented
640  * by the new object. From version 2.0.4, it will usually be more
641  * convenient to call the Cgu::Thread::make_future() function, which
642  * is a convenience wrapper for this static method.
643  * @exception std::bad_alloc It might throw std::bad_alloc if memory
644  * is exhausted and the system throws in that case. (This exception
645  * will not be thrown if the library has been installed using the
646  * \--with-glib-memory-slices-no-compat configuration option: instead
647  * glib will terminate the program if it is unable to obtain memory
648  * from the operating system.)
649  * @exception Cgu::Thread::MutexError It might throw
650  * Cgu::Thread::MutexError if initialisation of the contained mutex
651  * fails. (It is often not worth checking for this, as it means
652  * either memory is exhausted or pthread has run out of other
653  * resources to create new mutexes.)
654  * @exception Cgu::Thread::CondError It might throw
655  * Cgu::Thread::CondError if initialisation of the contained condition
656  * variable fails. (It is often not worth checking for this, as it
657  * means either memory is exhausted or pthread has run out of other
658  * resources to create new condition variables.)
659  * @note This method will also throw if the default constructor of the
660  * return value type throws.
661  */
662  template <class Ret>
663  static Cgu::IntrusivePtr<Cgu::Thread::Future<Val>> make(Ret (*func)());
664 
665 /**
666  * @deprecated
667  *
668  * DEPRECATED. Use the version of Future::make() which takes a
669  * callable object.
670  *
671  * Constructs a new Cgu::Thread::Future object (returned by
672  * Cgu::IntrusivePtr<Cgu::Thread::Future<Val>>). The type parameter
673  * Val represents the return value of the function to be represented
674  * by the new object. From version 2.0.4, it will usually be more
675  * convenient to call the Cgu::Thread::make_future() function, which
676  * is a convenience wrapper for this static method.
677  * @exception std::bad_alloc It might throw std::bad_alloc if memory
678  * is exhausted and the system throws in that case. (This exception
679  * will not be thrown if the library has been installed using the
680  * \--with-glib-memory-slices-no-compat configuration option: instead
681  * glib will terminate the program if it is unable to obtain memory
682  * from the operating system.)
683  * @exception Cgu::Thread::MutexError It might throw
684  * Cgu::Thread::MutexError if initialisation of the contained mutex
685  * fails. (It is often not worth checking for this, as it means
686  * either memory is exhausted or pthread has run out of other
687  * resources to create new mutexes.)
688  * @exception Cgu::Thread::CondError It might throw
689  * Cgu::Thread::CondError if initialisation of the contained condition
690  * variable fails. (It is often not worth checking for this, as it
691  * means either memory is exhausted or pthread has run out of other
692  * resources to create new condition variables.)
693  * @note This method will also throw if the copy or move constructor
694  * of the bound argument throws, or the default constructor of the
695  * return value type throws.
696  */
697  template <class Ret, class Param1, class Arg1>
698  static Cgu::IntrusivePtr<Cgu::Thread::Future<Val>> make(Ret (*func)(Param1),
699  Arg1&& arg1);
700 
701 /**
702  * @deprecated
703  *
704  * DEPRECATED. Use the version of Future::make() which takes a
705  * callable object.
706  *
707  * Constructs a new Cgu::Thread::Future object (returned by
708  * Cgu::IntrusivePtr<Cgu::Thread::Future<Val>>). The type parameter
709  * Val represents the return value of the function to be represented
710  * by the new object. From version 2.0.4, it will usually be more
711  * convenient to call the Cgu::Thread::make_future() function, which
712  * is a convenience wrapper for this static method.
713  * @exception std::bad_alloc It might throw std::bad_alloc if memory
714  * is exhausted and the system throws in that case. (This exception
715  * will not be thrown if the library has been installed using the
716  * \--with-glib-memory-slices-no-compat configuration option: instead
717  * glib will terminate the program if it is unable to obtain memory
718  * from the operating system.)
719  * @exception Cgu::Thread::MutexError It might throw
720  * Cgu::Thread::MutexError if initialisation of the contained mutex
721  * fails. (It is often not worth checking for this, as it means
722  * either memory is exhausted or pthread has run out of other
723  * resources to create new mutexes.)
724  * @exception Cgu::Thread::CondError It might throw
725  * Cgu::Thread::CondError if initialisation of the contained condition
726  * variable fails. (It is often not worth checking for this, as it
727  * means either memory is exhausted or pthread has run out of other
728  * resources to create new condition variables.)
729  * @note This method will also throw if the copy or move constructor
730  * of a bound argument throws, or the default constructor of the
731  * return value type throws.
732  */
733  template <class Ret, class Param1, class Param2, class Arg1, class Arg2>
734  static Cgu::IntrusivePtr<Cgu::Thread::Future<Val>> make(Ret (*func)(Param1, Param2),
735  Arg1&& arg1,
736  Arg2&& arg2);
737 
738 /**
739  * @deprecated
740  *
741  * DEPRECATED. Use the version of Future::make() which takes a
742  * callable object.
743  *
744  * Constructs a new Cgu::Thread::Future object (returned by
745  * Cgu::IntrusivePtr<Cgu::Thread::Future<Val>>). The type parameter
746  * Val represents the return value of the function to be represented
747  * by the new object. From version 2.0.4, it will usually be more
748  * convenient to call the Cgu::Thread::make_future() function, which
749  * is a convenience wrapper for this static method.
750  * @exception std::bad_alloc It might throw std::bad_alloc if memory
751  * is exhausted and the system throws in that case. (This exception
752  * will not be thrown if the library has been installed using the
753  * \--with-glib-memory-slices-no-compat configuration option: instead
754  * glib will terminate the program if it is unable to obtain memory
755  * from the operating system.)
756  * @exception Cgu::Thread::MutexError It might throw
757  * Cgu::Thread::MutexError if initialisation of the contained mutex
758  * fails. (It is often not worth checking for this, as it means
759  * either memory is exhausted or pthread has run out of other
760  * resources to create new mutexes.)
761  * @exception Cgu::Thread::CondError It might throw
762  * Cgu::Thread::CondError if initialisation of the contained condition
763  * variable fails. (It is often not worth checking for this, as it
764  * means either memory is exhausted or pthread has run out of other
765  * resources to create new condition variables.)
766  * @note This method will also throw if the copy or move constructor
767  * of a bound argument throws, or the default constructor of the
768  * return value type throws.
769  */
770  template <class Ret, class Param1, class Param2, class Param3,
771  class Arg1, class Arg2, class Arg3>
772  static Cgu::IntrusivePtr<Cgu::Thread::Future<Val>> make(Ret (*func)(Param1, Param2, Param3),
773  Arg1&& arg1,
774  Arg2&& arg2,
775  Arg3&& arg3);
776 
777 /**
778  * @deprecated
779  *
780  * DEPRECATED. Use the version of Future::make() which takes a
781  * callable object.
782  *
783  * Constructs a new Cgu::Thread::Future object (returned by
784  * Cgu::IntrusivePtr<Cgu::Thread::Future<Val>>). The type parameter
785  * Val represents the return value of the function to be represented
786  * by the new object. From version 2.0.4, it will usually be more
787  * convenient to call the Cgu::Thread::make_future() function, which
788  * is a convenience wrapper for this static method.
789  * @exception std::bad_alloc It might throw std::bad_alloc if memory
790  * is exhausted and the system throws in that case. (This exception
791  * will not be thrown if the library has been installed using the
792  * \--with-glib-memory-slices-no-compat configuration option: instead
793  * glib will terminate the program if it is unable to obtain memory
794  * from the operating system.)
795  * @exception Cgu::Thread::MutexError It might throw
796  * Cgu::Thread::MutexError if initialisation of the contained mutex
797  * fails. (It is often not worth checking for this, as it means
798  * either memory is exhausted or pthread has run out of other
799  * resources to create new mutexes.)
800  * @exception Cgu::Thread::CondError It might throw
801  * Cgu::Thread::CondError if initialisation of the contained condition
802  * variable fails. (It is often not worth checking for this, as it
803  * means either memory is exhausted or pthread has run out of other
804  * resources to create new condition variables.)
805  * @note This method will also throw if the copy or move constructor
806  * of a bound argument throws, or the default constructor of the
807  * return value type throws.
808  */
809  template <class Ret, class Param1, class Param2, class Param3, class Param4,
810  class Arg1, class Arg2, class Arg3, class Arg4>
811  static Cgu::IntrusivePtr<Cgu::Thread::Future<Val>> make(Ret (*func)(Param1, Param2, Param3, Param4),
812  Arg1&& arg1,
813  Arg2&& arg2,
814  Arg3&& arg3,
815  Arg4&& arg4);
816 
817 /**
818  * Constructs a new Cgu::Thread::Future object (returned by
819  * Cgu::IntrusivePtr<Cgu::Thread::Future<Val>>). The type parameter
820  * Val represents the return value of the function to be represented
821  * by the new object. It will usually be more convenient to call the
822  * Cgu::Thread::make_future() function, which is a convenience wrapper
823  * for this static method.
824  * @param func A callable object, such as formed by a lambda
825  * expression or the result of std::bind. It must be fully bound
826  * (that is, its must take no arguments when called). It should
827  * return the Val type.
828  * @exception std::bad_alloc It might throw std::bad_alloc if memory
829  * is exhausted and the system throws in that case. (This exception
830  * will not be thrown if the library has been installed using the
831  * \--with-glib-memory-slices-no-compat configuration option: instead
832  * glib will terminate the program if it is unable to obtain memory
833  * from the operating system.)
834  * @exception Cgu::Thread::MutexError It might throw
835  * Cgu::Thread::MutexError if initialisation of the contained mutex
836  * fails. (It is often not worth checking for this, as it means
837  * either memory is exhausted or pthread has run out of other
838  * resources to create new mutexes.)
839  * @exception Cgu::Thread::CondError It might throw
840  * Cgu::Thread::CondError if initialisation of the contained condition
841  * variable fails. (It is often not worth checking for this, as it
842  * means either memory is exhausted or pthread has run out of other
843  * resources to create new condition variables.)
844  * @note 1. This method will also throw if the copy or move
845  * constructor of the callable object passed as an argument throws, or
846  * the default constructor of the return value type throws.
847  * @note 2. If the callable object passed as an argument has both
848  * const and non-const operator()() methods, the non-const version
849  * will be called even if the callable object passed is a const
850  * object.
851  *
852  * Since 2.0.14
853  */
854  template <class Func>
856 
857 /**
858  * Runs the function or callable object represented by this
859  * Cgu::Thread::Future object in a new worker thread. That function
860  * will only be run once. If this is the first time this method has
861  * been called, it will start the worker thread and return true, and
862  * if it has previously been called, this method will do nothing and
863  * return false. This method will not wait for the worker thread to
864  * complete before returning. This method is thread safe and may be
865  * called by a different thread from the one which called make().
866  * @return true if this is the first time this method has been called,
867  * or false if this method has previously been called.
868  * @exception Cgu::Thread::FutureThreadError This method might throw
869  * Cgu::Thread::FutureThreadError if it has not previously been called
870  * and the thread did not start properly. If it does throw, this
871  * Cgu::Thread::Future object is defunct and further attempts to call
872  * this method will return immediately with a false value. (It is
873  * often not worth checking for this exception, as it means either
874  * memory is exhausted, the pthread thread limit has been reached or
875  * pthread has run out of other resources to start new threads.)
876  * @exception std::bad_alloc This method might throw std::bad_alloc if
877  * it has not previously been called, and memory is exhausted and the
878  * system throws in that case. If it does throw, this
879  * Cgu::Thread::Future object is defunct and further attempts to call
880  * this method will return immediately with a false value. (This
881  * exception will not be thrown if the library has been installed
882  * using the \--with-glib-memory-slices-no-compat configuration
883  * option: instead glib will terminate the program if it is unable to
884  * obtain memory from the operating system.)
885  * @note 1. Any Cgu::Thread::Exit exception, or any uncaught exception
886  * derived from std::exception, which is thrown from the worker thread
887  * will be caught and consumed and the error flag will be set. The
888  * worker thread will safely terminate and unwind the stack in so
889  * doing.
890  * @note 2. As this wrapper class can provide error reporting in a way
891  * that Cgu::Thread::Thread of itself cannot, it would be desirable to
892  * consume any other uncaught exceptions. However, this cannot be
893  * done: annoyingly, NPTL's forced stack unwinding does not allow this
894  * if thread cancellation is to be made available. If an uncaught
895  * exception propagates out of a thread when the thread exits, the
896  * C++11/14 standard will cause std::terminate() to be called and so
897  * terminate the entire program. Accordingly, a user must make sure
898  * that no exceptions, other than Cgu::Thread::Exit or those derived
899  * from std::exception or any cancellation pseudo-exception, can
900  * propagate from the function which this Cgu::Thread::Future object
901  * represents, nor from the copy constructor of any argument type that
902  * that function takes by value nor from the move assignment operator
903  * (or if none, copy assignment operator) of the return value of that
904  * function.
905  * @note 3. If the worker thread is cancelled by a call to cancel()
906  * while in the middle of executing the function which this
907  * Cgu::Thread::Future object represents, the error flag will be set.
908  */
909  bool run();
910 
911 /**
912  * Gets the stored value obtained from the function or callable object
913  * which is represented by this object. If the worker thread launched
914  * by the call to run() has not completed, then this method will block
915  * until it has completed. If run() has not been called, then run()
916  * will be called (and this method will block until the launched
917  * worker thread completes). If the function or callable object which
918  * is represented by this Cgu::Thread::Future object throws
919  * Cgu::Thread::Exit or an uncaught exception derived from
920  * std::exception, or if any of those exceptions are thrown either by
921  * the copy constructor of an argument taken by value by that function
922  * or object, or by the move assignment operator (or if none, copy
923  * assignment operator) of the return value of that function or
924  * object, then the exception will have been consumed by this
925  * Cgu::Thread::Future object and the error flag will have been set.
926  * The error flag will also have been set if the worker thread is
927  * cancelled or the thread wrapper in this Cgu::Thread::Future object
928  * threw std::bad_alloc. On the error flag being set, this method
929  * will unblock and return a default constructed object of the return
930  * type. This method is thread safe and may be called by any thread
931  * (and by more than one thread). It is a cancellation point if it
932  * blocks, and from version 2.0.11 is cancellation safe if the stack
933  * unwinds on cancellation. It is also strongly exception safe: no
934  * data will be lost if extracting the value fails.
935  * @return The value obtained from the function which is represented
936  * by this object, or a default constructed object of the return type
937  * if the error flag has been set.
938  * @exception Cgu::Thread::FutureThreadError This method might throw
939  * Cgu::Thread::FutureThreadError if run() has not previously been
940  * called and the thread did not start properly when this function
941  * called run().
942  * @exception std::bad_alloc This method might throw std::bad_alloc if
943  * run() has not previously been called, memory is exhausted and the
944  * system throws in that case. (This exception will not be thrown if
945  * the library has been installed using the
946  * \--with-glib-memory-slices-no-compat configuration option: instead
947  * glib will terminate the program if it is unable to obtain memory
948  * from the operating system.)
949  * @note 1. This method might also throw if the copy constructor of
950  * the returned value type throws.
951  * @note 2. Question: Couldn't this method return the stored value by
952  * lvalue reference to const? Answer: It could. However, because of
953  * return value optimization, which will be implemented by any
954  * compiler capable of compiling this library, no advantage would be
955  * gained by doing so when initializing a local variable with the
956  * return value of this method (the copy constructor will only be
957  * called once whether returning by value or const reference). The
958  * advantage of returning by value is that the call to the copy
959  * constructor is forced to be within this Thread::Future object's
960  * mutex, so different threads' calls to the copy constructor are
961  * serialized, and also with blocked cancellation, so this method is
962  * cancellation safe. All calls to this method by different threads
963  * are therefore isolated and we do not have to worry about the thread
964  * safety of direct access to the stored value via its const methods
965  * outside the mutex (which would not be thread safe if the stored
966  * value has data members declared mutable) nor about the cancellation
967  * safety of the copy constructor. Of course, for objects which do
968  * not have mutable data, a hit arises by returning by value in cases
969  * where it is not intended to initialize a local variable at all nor
970  * to cancel a thread: where, say, only const methods are to be called
971  * on the return value (which could be done directly if this method
972  * returned by const reference). However, in many use cases this will
973  * be mitigated by the move_get() method.
974  */
975  Val get();
976 
977 /**
978  * Gets the stored value obtained from the function or callable object
979  * which is represented by this object by a move operation, if the
980  * return type implements a move constructor (otherwise this method
981  * does the same as the get() method). It is provided as an option
982  * for cases where a move is required for efficiency reasons, but
983  * although it may be called by any thread, a move from this
984  * Thread::Future object may normally only be made once (except where
985  * the return type has been designed to be moved more than once for
986  * the limited purpose of inspecting a flag indicating whether its
987  * value is valid or not). If this method is to be called then no
988  * calls to get() by another thread should normally be made and no
989  * calls to when() should be made. If the worker thread launched by
990  * the call to run() has not completed, then this method will block
991  * until it has completed. If run() has not been called, then run()
992  * will be called (and this method will block until the launched
993  * worker thread completes). If the function or callable object which
994  * is represented by this Cgu::Thread::Future object throws
995  * Cgu::Thread::Exit or an uncaught exception derived from
996  * std::exception, or if any of those exceptions are thrown either by
997  * the copy constructor of an argument taken by value by that function
998  * or object, or by the move assignment operator (or if none, copy
999  * assignment operator) of the return value of that function or
1000  * object, then the exception will have been consumed by this
1001  * Cgu::Thread::Future object and the error flag will have been set.
1002  * The error flag will also have been set if the worker thread is
1003  * cancelled or the thread wrapper in this Cgu::Thread::Future object
1004  * threw std::bad_alloc. On the error flag being set, this method
1005  * will unblock and return a default constructed object of the return
1006  * type. This method is a cancellation point if it blocks, and is
1007  * cancellation safe if the stack unwinds on cancellation. This
1008  * method is only exception safe if the return type's move constructor
1009  * is exception safe.
1010  * @return The value obtained from the function which is represented
1011  * by this object, or a default constructed object of the return type
1012  * if the error flag has been set.
1013  * @exception Cgu::Thread::FutureThreadError This method might throw
1014  * Cgu::Thread::FutureThreadError if run() has not previously been
1015  * called and the thread did not start properly when this function
1016  * called run().
1017  * @exception std::bad_alloc This method might throw std::bad_alloc if
1018  * run() has not previously been called, memory is exhausted and the
1019  * system throws in that case. (This exception will not be thrown if
1020  * the library has been installed using the
1021  * \--with-glib-memory-slices-no-compat configuration option: instead
1022  * glib will terminate the program if it is unable to obtain memory
1023  * from the operating system.)
1024  * @note 1. This method might also throw if the copy or move
1025  * constructor of the returned value type throws.
1026  * @note 2. Question: Couldn't this method return the stored value by
1027  * rvalue reference? Answer: It could. However, because of return
1028  * value optimization, which will be implemented by any compiler
1029  * capable of compiling this library, no advantage would be gained by
1030  * doing so when initializing a local variable with the return value
1031  * of this method (the move constructor will only be called once, and
1032  * no call will be made to the copy constructor, whether returning by
1033  * value or rvalue reference). The advantage of returning by value is
1034  * that the call to the move constructor is forced to be within this
1035  * Thread::Future object's mutex, so different threads' calls to the
1036  * move constructor are serialized, and also with blocked
1037  * cancellation, so this method is cancellation safe. All calls to
1038  * this method by different threads are therefore isolated and we do
1039  * not have to worry about the thread safety of the mutating first
1040  * call to this method, nor about direct access to the stored value
1041  * via a rvalue reference outside the mutex nor the cancellation
1042  * safety of the move constructor.
1043  *
1044  * Since 2.0.11
1045  */
1046  Val move_get();
1047 
1048 /**
1049  * Cancels the worker thread in which the function or callable object
1050  * represented by this object runs, if it has not yet finished. If
1051  * this method is called and the worker thread is still running and is
1052  * cancelled in response to a call to this method, then the error flag
1053  * will be set so that a method calling get() or move_get() can
1054  * examine whether the result is valid. If run() has not yet been
1055  * called or the worker thread has already finished executing the
1056  * function or callable object represented by this object then this
1057  * function does nothing and returns false. This method is thread
1058  * safe and may be called by any thread. It will not throw.
1059  * @return true if run() has previously been called and the worker
1060  * thread has not yet finished executing the function or callable
1061  * object represented by this object, otherwise false (in which case
1062  * this method does nothing).
1063  * @note 1. Use this method with care. When cancelling a thread not
1064  * all thread implementations will unwind the stack, and so run the
1065  * destructors of local objects. This is discussed further in the
1066  * documentation on Cgu::Thread::Thread::cancel().
1067  * @note 2. This method might return true because the worker thread
1068  * has not yet finished, but the error flag might still not be set.
1069  * This is because the worker thread may not meet a cancellation point
1070  * before it ends naturally. It is the error flag which indicates
1071  * definitively whether the worker thread terminated prematurely in
1072  * response to a call to this method.
1073  */
1074  bool cancel() noexcept;
1075 
1076 /**
1077  * A utility enabling the value returned by the thread function
1078  * represented by this Cgu::Thread::Future object to be dealt with
1079  * asynchronously rather than by (or in addition to) a call to the
1080  * get() method. It causes the callback passed as an argument to this
1081  * method (referred to below as the 'when' callback) to be executed by
1082  * a thread's main loop if and when the thread function represented by
1083  * this Cgu::Thread::Future object finishes correctly - the 'when'
1084  * callback is passed that thread function's return value when it is
1085  * invoked. This method is thread safe, and may be called by any
1086  * thread.
1087  *
1088  * This functionality is implemented by connecting an internal
1089  * dispatching callback to the done_emitter object.
1090  *
1091  * The 'when' callback should take a single unbound argument
1092  * comprising a const reference to the return type of the thread
1093  * function represented by this Cgu::Thread::Future object. (So, in
1094  * the case of a Future<int> object, the callback function should take
1095  * a const int& argument as the unbound argument.) The 'when'
1096  * callback can have any number of bound arguments, except that a
1097  * bound argument may not include a copy of this Cgu::Thread::Future
1098  * object held by intrusive pointer as returned by the make() methods
1099  * (that would result in this Cgu::Thread::Future object owning, via
1100  * done_emitter, a reference to itself and so become incapable of
1101  * being freed). The 'when' callback may, however, take a pointer to
1102  * this Cgu::Thread::Future object, as obtained by the
1103  * Cgu::IntrusivePtr::get() method, because this Cgu::Thread::Future
1104  * object is guaranteed to remain in existence until the callback has
1105  * completed executing.
1106  *
1107  * This method cannot be called after the thread function represented
1108  * by this Cgu::Thread::Future object has completed (either
1109  * successfully or unsuccessfully) so that is_done() would return
1110  * true, and if this is attempted a Cgu::Thread::FutureWhenError
1111  * exception will be thrown. Therefore, generally this method should
1112  * be called before the run() method has been called.
1113  *
1114  * Once the run() method has been called, this Cgu::Thread::Future
1115  * object will always stay in existence until the thread function
1116  * represented by it has completed (whether correctly, by cancellation
1117  * or by a thrown exception), and any 'when' callback (and any other
1118  * callbacks connected to the done_emitter object) and any 'fail'
1119  * callback have completed. Accordingly it is safe to use this method
1120  * even if the intrusive pointer object returned by the make() methods
1121  * will go out of scope before the 'when' callback has executed: the
1122  * callback will execute correctly irrespective of that.
1123  *
1124  * Summary: use of this method is safe and has been implemented in a
1125  * way which does not give rise to timing issues.
1126  *
1127  * If memory is exhausted and std::bad_alloc is thrown by the thread
1128  * wrapper of Cgu::Thread::Future after run() is called or by
1129  * done_emitter when emitting, or if the thread function represented
1130  * by this Cgu::Thread::Future object throws Cgu::Thread::Exit, is
1131  * cancelled, exits with an uncaught exception deriving from
1132  * std::exception, takes an argument by value whose copy constructor
1133  * throws such an exception or has a return value whose move
1134  * assignment operator (or if none, copy assignment operator) throws
1135  * such an exception, or if the 'when' callback represents a function
1136  * taking a non-reference argument whose copy constructor throws an
1137  * exception, or if any other callback has been connected to
1138  * done_emitter before this method is called which exits with an
1139  * uncaught exception, then the 'when' callback will not execute
1140  * (instead the exception concerned will be consumed and an error
1141  * indicated). With many systems, swap memory combined with memory
1142  * over-commit makes it pointless to check for std::bad_alloc (and
1143  * even more so in programs using glib, as glib aborts a program where
1144  * it cannot obtain memory from the operating system). So subject to
1145  * that, if the user program is designed so that the thread function
1146  * represented by this Cgu::Thread::Future object does not exit with
1147  * uncaught exceptions, does not take an argument by value which
1148  * throws, does not have a return value whose move assignment operator
1149  * (or if none, copy assignment operator) throws, does not throw
1150  * Cgu::Thread::Exit and is not cancelled, and so that the 'when'
1151  * callback does not exit with an uncaught exception (and a function
1152  * represented by that callback either takes no arguments of class
1153  * type by value or the copy constructors of any of its value
1154  * arguments do not throw), and if this method is called before any
1155  * other callbacks are connected to done_emitter, the possibility of
1156  * failure can be disregarded.
1157  *
1158  * In cases where that is not true and detecting whether a failure has
1159  * occurred is required, a fail() method is provided. It should be
1160  * noted that a callback handed to the fail() method will not execute
1161  * in a case of error if the error comprises the 'when' callback
1162  * exiting with an uncaught exception when it is executed by the main
1163  * loop, or the copy constructor of any value argument of a function
1164  * represented by the 'when' callback throwing (such exceptions would
1165  * be consumed internally in order to protect the main loop and a
1166  * g_critical message issued). If the 'when' callback might exit with
1167  * an uncaught exception when executing or have the copy constructor
1168  * of a value argument throw, and doing something other than consuming
1169  * the exception and issuing a g_critical message is required, then a
1170  * different approach is to start a new thread to wait on the get()
1171  * method which can act on the result of is_error() directly.
1172  *
1173  * If glib < 2.32 is used, the glib main loop must have been made
1174  * thread-safe by a call to g_thread_init() before this function is
1175  * called. glib >= 2.32 does not require g_thread_init() to be called
1176  * in order to be thread safe.
1177  *
1178  * @param cb The 'when' callback (the callback to be executed when the
1179  * function represented by this Cgu::Thread::Future object has
1180  * successfully completed). Ownership is taken of this object, and it
1181  * will be deleted when it has been finished with.
1182  * @param priority The priority to be given to the 'when' callback in
1183  * the main loop after the thread function represented by this
1184  * Cgu::Thread::Future object has successfully completed. In
1185  * ascending order of priorities, priorities are G_PRIORITY_LOW,
1186  * G_PRIORITY_DEFAULT_IDLE, G_PRIORITY_HIGH_IDLE, G_PRIORITY_DEFAULT
1187  * and G_PRIORITY_HIGH. The default is G_PRIORITY_DEFAULT. This
1188  * determines the order in which the callback will appear in the event
1189  * list in the main loop, not the priority which the OS will adopt.
1190  * @param context The glib main context of the thread in whose main
1191  * loop the 'when' callback is to be executed (the default of NULL
1192  * will cause the callback to be executed in the main program loop).
1193  * @return The internal dispatching callback created by this method
1194  * and connected to done_emitter. It is made available as a return
1195  * value so that if wanted it can be disconnected programmatically
1196  * from done_emitter, or block()/unblock() can be called on it (but if
1197  * that is to be done, it must be done before the thread function
1198  * represented by this Cgu::Thread::Future object has completed in
1199  * order for it to be effective).
1200  * @exception Cgu::Thread::FutureWhenError This method will throw
1201  * Cgu::Thread::FutureWhenError if it is called after the thread
1202  * function represented by this Cgu::Thread::Future object has
1203  * completed. If it does so, the 'when' callback will be disposed of.
1204  * @exception std::bad_alloc This method might throw std::bad_alloc if
1205  * memory is exhausted and the system throws in that case. If it does
1206  * so, the 'when' callback will be disposed of.
1207  * @note The return value of the function represented by this
1208  * Cgu::Thread::Future object is stored and passed as an argument to
1209  * the 'when' callback by const reference. If other threads might
1210  * concurrently call this object's get() method, which copies the
1211  * stored value, the stored type's copy constructor must be thread
1212  * safe with respect to the stored type's const methods. This would
1213  * be relevant if the stored type has data members declared mutable
1214  * which would be copied by its copy constructor.
1215  *
1216  * Since 2.0.2
1217  */
1219  gint priority = G_PRIORITY_DEFAULT,
1220  GMainContext* context = 0);
1221 
1222 /**
1223  * A utility enabling the value returned by the thread function
1224  * represented by this Cgu::Thread::Future object to be dealt with
1225  * asynchronously rather than by (or in addition to) a call to the
1226  * get() method. It causes the callable object passed as an argument
1227  * to this method (referred to below as the 'when' callback) to be
1228  * executed by a thread's main loop if and when the thread function
1229  * represented by this Cgu::Thread::Future object finishes correctly -
1230  * the 'when' callback is passed that thread function's return value
1231  * when it is invoked. This method is thread safe, and may be called
1232  * by any thread.
1233  *
1234  * This functionality is implemented by connecting an internal
1235  * dispatching callback to the done_emitter object.
1236  *
1237  * The 'when' callback should take a single unbound argument
1238  * comprising a const reference to the return type of the thread
1239  * function represented by this Cgu::Thread::Future object. (So, in
1240  * the case of a Future<int> object, the callback function should take
1241  * a const int& argument as the unbound argument.) The 'when'
1242  * callback can have any number of bound arguments, except that a
1243  * bound argument may not include a copy of this Cgu::Thread::Future
1244  * object held by intrusive pointer as returned by the make() methods
1245  * (that would result in this Cgu::Thread::Future object owning, via
1246  * done_emitter, a reference to itself and so become incapable of
1247  * being freed). The 'when' callback may, however, take a pointer to
1248  * this Cgu::Thread::Future object, as obtained by the
1249  * Cgu::IntrusivePtr::get() method, because this Cgu::Thread::Future
1250  * object is guaranteed to remain in existence until the callback has
1251  * completed executing.
1252  *
1253  * This method cannot be called after the thread function represented
1254  * by this Cgu::Thread::Future object has completed (either
1255  * successfully or unsuccessfully) so that is_done() would return
1256  * true, and if this is attempted a Cgu::Thread::FutureWhenError
1257  * exception will be thrown. Therefore, generally this method should
1258  * be called before the run() method has been called.
1259  *
1260  * Once the run() method has been called, this Cgu::Thread::Future
1261  * object will always stay in existence until the thread function
1262  * represented by it has completed (whether correctly, by cancellation
1263  * or by a thrown exception), and any 'when' callback (and any other
1264  * callbacks connected to the done_emitter object) and any 'fail'
1265  * callback have completed. Accordingly it is safe to use this method
1266  * even if the intrusive pointer object returned by the make() methods
1267  * will go out of scope before the 'when' callback has executed: the
1268  * callback will execute correctly irrespective of that.
1269  *
1270  * Summary: use of this method is safe and has been implemented in a
1271  * way which does not give rise to timing issues.
1272  *
1273  * If memory is exhausted and std::bad_alloc is thrown by the thread
1274  * wrapper of Cgu::Thread::Future after run() is called or by
1275  * done_emitter when emitting, or if the thread function represented
1276  * by this Cgu::Thread::Future object throws Cgu::Thread::Exit, is
1277  * cancelled, exits with an uncaught exception deriving from
1278  * std::exception, takes an argument by value whose copy constructor
1279  * throws such an exception or has a return value whose move
1280  * assignment operator (or if none, copy assignment operator) throws
1281  * such an exception, or if any other callback has been connected to
1282  * done_emitter before this method is called which exits with an
1283  * uncaught exception, then the 'when' callback will not execute
1284  * (instead the exception concerned will be consumed and an error
1285  * indicated). With many systems, swap memory combined with memory
1286  * over-commit makes it pointless to check for std::bad_alloc (and
1287  * even more so in programs using glib, as glib aborts a program where
1288  * it cannot obtain memory from the operating system). So subject to
1289  * that, if the user program is designed so that the thread function
1290  * represented by this Cgu::Thread::Future object does not exit with
1291  * uncaught exceptions, does not take an argument by value which
1292  * throws, does not have a return value whose move assignment operator
1293  * (or if none, copy assignment operator) throws, does not throw
1294  * Cgu::Thread::Exit and is not cancelled, and so that the 'when'
1295  * callback does not exit with an uncaught exception, and if this
1296  * method is called before any other callbacks are connected to
1297  * done_emitter, the possibility of failure can be disregarded.
1298  *
1299  * In cases where that is not true and detecting whether a failure has
1300  * occurred is required, a fail() method is provided. It should be
1301  * noted that a callback handed to the fail() method will not execute
1302  * in a case of error if the error comprises the 'when' callback
1303  * exiting with an uncaught exception when it is executed by the main
1304  * loop (such exceptions would be consumed internally in order to
1305  * protect the main loop and a g_critical message issued). If the
1306  * 'when' callback might exit with an uncaught exception when
1307  * executing, and doing something other than consuming the exception
1308  * and issuing a g_critical message is required, then a different
1309  * approach is to start a new thread to wait on the get() method which
1310  * can act on the result of is_error() directly.
1311  *
1312  * If glib < 2.32 is used, the glib main loop must have been made
1313  * thread-safe by a call to g_thread_init() before this function is
1314  * called. glib >= 2.32 does not require g_thread_init() to be called
1315  * in order to be thread safe.
1316  *
1317  * @param w A callable object (such as formed by a lambda expression
1318  * or the result of std::bind) representing the 'when' callback (the
1319  * callback to be executed when the function represented by this
1320  * Cgu::Thread::Future object has successfully completed). It should
1321  * take a single unbound argument, namely a reference to const to the
1322  * return type of the thread function represented by this
1323  * Cgu::Thread::Future object.
1324  * @param priority The priority to be given to the 'when' callback in
1325  * the main loop after the thread function represented by this
1326  * Cgu::Thread::Future object has successfully completed. In
1327  * ascending order of priorities, priorities are G_PRIORITY_LOW,
1328  * G_PRIORITY_DEFAULT_IDLE, G_PRIORITY_HIGH_IDLE, G_PRIORITY_DEFAULT
1329  * and G_PRIORITY_HIGH. The default is G_PRIORITY_DEFAULT. This
1330  * determines the order in which the callback will appear in the event
1331  * list in the main loop, not the priority which the OS will adopt.
1332  * @param context The glib main context of the thread in whose main
1333  * loop the 'when' callback is to be executed (the default of NULL
1334  * will cause the callback to be executed in the main program loop).
1335  * @return The internal dispatching callback created by this method
1336  * and connected to done_emitter. It is made available as a return
1337  * value so that if wanted it can be disconnected programmatically
1338  * from done_emitter, or block()/unblock() can be called on it (but if
1339  * that is to be done, it must be done before the thread function
1340  * represented by this Cgu::Thread::Future object has completed in
1341  * order for it to be effective).
1342  * @exception Cgu::Thread::FutureWhenError This method will throw
1343  * Cgu::Thread::FutureWhenError if it is called after the thread
1344  * function represented by this Cgu::Thread::Future object has
1345  * completed.
1346  * @exception std::bad_alloc This method might throw std::bad_alloc if
1347  * memory is exhausted and the system throws in that case.
1348  * @note 1. This method will also throw if the copy or move
1349  * constructor of the 'when' callable object throws.
1350  * @note 2. The return value of the function represented by this
1351  * Cgu::Thread::Future object is stored and passed as an argument to
1352  * the 'when' callback by const reference. If other threads might
1353  * concurrently call this object's get() method, which copies the
1354  * stored value, the stored type's copy constructor must be thread
1355  * safe with respect to the stored type's const methods. This would
1356  * be relevant if the stored type has data members declared mutable
1357  * which would be copied by its copy constructor.
1358  *
1359  * Since 2.1.0
1360  */
1361  // we need to use enable_if so that where this function is passed a
1362  // pointer to non-const Callback::CallbackArg, or some other
1363  // convertible pointer, this templated overload is dropped from the
1364  // overload set, in order to support the Callback::CallbackArg
1365  // overloads of this function. This overload calls into the version
1366  // of this function taking a pointer to const Callback::CallbackArg
1367  // in order to perform type erasure.
1368  template <class When,
1369  class = typename std::enable_if<!std::is_convertible<typename std::remove_reference<When>::type,
1370  const Cgu::Callback::CallbackArg<const Val&>*>::value>::type>
1372  gint priority = G_PRIORITY_DEFAULT,
1373  GMainContext* context = 0) {
1374  return when(Callback::lambda<const Val&>(std::forward<When>(w)),
1375  priority,
1376  context);
1377  }
1378 
1379 /**
1380  * This is a version of the utility enabling the value returned by the
1381  * thread function represented by this Cgu::Thread::Future object to
1382  * be dealt with asynchronously, which takes a Releaser object for
1383  * automatic disconnection of the callback passed as an argument to
1384  * this method (referred to below as the 'when' callback), if the
1385  * object having the 'when' callback function as a member is
1386  * destroyed. For this to be race free, the lifetime of that object
1387  * must be controlled by the thread in whose main loop the 'when'
1388  * callback will execute.
1389  *
1390  * If the 'when' callback has not been released, this method causes it
1391  * to be executed by a thread's main loop if and when the thread
1392  * function represented by this Cgu::Thread::Future object finishes
1393  * correctly - the 'when' callback is passed that thread function's
1394  * return value when it is invoked. This method is thread safe, and
1395  * may be called by any thread.
1396  *
1397  * This functionality is implemented by connecting an internal
1398  * dispatching callback to the done_emitter object.
1399  *
1400  * The 'when' callback should take a single unbound argument
1401  * comprising a const reference to the return type of the thread
1402  * function represented by this Cgu::Thread::Future object. (So, in
1403  * the case of a Future<int> object, the callback function should take
1404  * a const int& argument as the unbound argument.) The 'when'
1405  * callback can have any number of bound arguments, except that a
1406  * bound argument may not include a copy of this Cgu::Thread::Future
1407  * object held by intrusive pointer as returned by the make() methods
1408  * (that would result in this Cgu::Thread::Future object owning, via
1409  * done_emitter, a reference to itself and so become incapable of
1410  * being freed). The 'when' callback may, however, take a pointer to
1411  * this Cgu::Thread::Future object, as obtained by the
1412  * Cgu::IntrusivePtr::get() method, because this Cgu::Thread::Future
1413  * object is guaranteed to remain in existence until the callback has
1414  * completed executing.
1415  *
1416  * This method cannot be called after the thread function represented
1417  * by this Cgu::Thread::Future object has completed (either
1418  * successfully or unsuccessfully) so that is_done() would return
1419  * true, and if this is attempted a Cgu::Thread::FutureWhenError
1420  * exception will be thrown. Therefore, generally this method should
1421  * be called before the run() method has been called.
1422  *
1423  * The documentation for the version of this method which does not
1424  * take a Releaser object gives further details of how this method is
1425  * used.
1426  *
1427  * If glib < 2.32 is used, the glib main loop must have been made
1428  * thread-safe by a call to g_thread_init() before this function is
1429  * called. glib >= 2.32 does not require g_thread_init() to be called
1430  * in order to be thread safe.
1431  *
1432  * @param cb The 'when' callback (the callback to be executed when the
1433  * function represented by this Cgu::Thread::Future object has
1434  * successfully completed). Ownership is taken of this object, and it
1435  * will be deleted when it has been finished with.
1436  * @param r A Releaser object for automatic disconnection of the
1437  * 'when' callback before it executes in a main loop (mainly relevant
1438  * if the callback represents a non-static member function of an
1439  * object which may be destroyed before the callback executes).
1440  * @param priority The priority to be given to the 'when' callback in
1441  * the main loop after the thread function represented by this
1442  * Cgu::Thread::Future object has successfully completed. In
1443  * ascending order of priorities, priorities are G_PRIORITY_LOW,
1444  * G_PRIORITY_DEFAULT_IDLE, G_PRIORITY_HIGH_IDLE, G_PRIORITY_DEFAULT
1445  * and G_PRIORITY_HIGH. The default is G_PRIORITY_DEFAULT. This
1446  * determines the order in which the callback will appear in the event
1447  * list in the main loop, not the priority which the OS will adopt.
1448  * @param context The glib main context of the thread in whose main
1449  * loop the 'when' callback is to be executed (the default of NULL
1450  * will cause the callback to be executed in the main program loop).
1451  * @return The internal dispatching callback created by this method
1452  * and connected to done_emitter. It is made available as a return
1453  * value so that if wanted it can be disconnected programmatically
1454  * from done_emitter, or block()/unblock() can be called on it (but if
1455  * that is to be done, it must be done before the thread function
1456  * represented by this Cgu::Thread::Future object has completed in
1457  * order for it to be effective).
1458  * @exception Cgu::Thread::FutureWhenError This method will throw
1459  * Cgu::Thread::FutureWhenError if it is called after the thread
1460  * function represented by this Cgu::Thread::Future object has
1461  * completed. If it does so, the 'when' callback will be disposed of.
1462  * @exception std::bad_alloc This method might throw std::bad_alloc if
1463  * memory is exhausted and the system throws in that case. If it does
1464  * so, the 'when' callback will be disposed of.
1465  * @exception Cgu::Thread::MutexError This method will throw
1466  * Cgu:Thread::MutexError if initialisation of the mutex in a
1467  * SafeEmitterArg object constructed by this method fails. If it does
1468  * so, the 'when' callback will be disposed of. (It is often not
1469  * worth checking for this, as it means either memory is exhausted or
1470  * pthread has run out of other resources to create new mutexes.)
1471  * @note 1. The return value of the function represented by this
1472  * Cgu::Thread::Future object is stored and passed as an argument to
1473  * the 'when' callback by const reference. If other threads might
1474  * concurrently call this object's get() method, which copies the
1475  * stored value, the stored type's copy constructor must be thread
1476  * safe with respect to the stored type's const methods. This would
1477  * be relevant if the stored type has data members declared mutable
1478  * which would be copied by its copy constructor.
1479  * @note 2. By virtue of the Releaser object, it is in theory possible
1480  * (if memory is exhausted and the system throws in that case) that an
1481  * internal SafeEmitterArg object will throw std::bad_alloc when
1482  * emitting/executing the 'when' callback in the glib main loop, with
1483  * the result that the relevant callback will not execute (instead the
1484  * exception will be consumed and a g_critical() warning will be
1485  * issued). This is rarely of any relevance because glib will abort
1486  * the program if it is itself unable to obtain memory from the
1487  * operating system. However, where it is relevant, design the
1488  * program so that it is not necessary to provide a releaser object.
1489  *
1490  * Since 2.0.2
1491  */
1493  Cgu::Releaser& r,
1494  gint priority = G_PRIORITY_DEFAULT,
1495  GMainContext* context = 0);
1496 
1497 /**
1498  * This is a version of the utility enabling the value returned by the
1499  * thread function represented by this Cgu::Thread::Future object to
1500  * be dealt with asynchronously, which takes a Releaser object for
1501  * automatic disconnection of the callable object passed as an
1502  * argument to this method (referred to below as the 'when' callback),
1503  * if the 'when' callback represents or calls into an object which is
1504  * destroyed. For this to be race free, the lifetime of the object
1505  * called into must be controlled by the thread in whose main loop the
1506  * 'when' callback will execute.
1507  *
1508  * If the 'when' callback has not been released, this method causes it
1509  * to be executed by a thread's main loop if and when the thread
1510  * function represented by this Cgu::Thread::Future object finishes
1511  * correctly - the 'when' callback is passed that thread function's
1512  * return value when it is invoked. This method is thread safe, and
1513  * may be called by any thread.
1514  *
1515  * This functionality is implemented by connecting an internal
1516  * dispatching callback to the done_emitter object.
1517  *
1518  * The 'when' callback should take a single unbound argument
1519  * comprising a const reference to the return type of the thread
1520  * function represented by this Cgu::Thread::Future object. (So, in
1521  * the case of a Future<int> object, the callback function should take
1522  * a const int& argument as the unbound argument.) The 'when'
1523  * callback can have any number of bound arguments, except that a
1524  * bound argument may not include a copy of this Cgu::Thread::Future
1525  * object held by intrusive pointer as returned by the make() methods
1526  * (that would result in this Cgu::Thread::Future object owning, via
1527  * done_emitter, a reference to itself and so become incapable of
1528  * being freed). The 'when' callback may, however, take a pointer to
1529  * this Cgu::Thread::Future object, as obtained by the
1530  * Cgu::IntrusivePtr::get() method, because this Cgu::Thread::Future
1531  * object is guaranteed to remain in existence until the callback has
1532  * completed executing.
1533  *
1534  * This method cannot be called after the thread function represented
1535  * by this Cgu::Thread::Future object has completed (either
1536  * successfully or unsuccessfully) so that is_done() would return
1537  * true, and if this is attempted a Cgu::Thread::FutureWhenError
1538  * exception will be thrown. Therefore, generally this method should
1539  * be called before the run() method has been called.
1540  *
1541  * The documentation for the version of this method which does not
1542  * take a Releaser object gives further details of how this method is
1543  * used.
1544  *
1545  * If glib < 2.32 is used, the glib main loop must have been made
1546  * thread-safe by a call to g_thread_init() before this function is
1547  * called. glib >= 2.32 does not require g_thread_init() to be called
1548  * in order to be thread safe.
1549  *
1550  * @param w A callable object (such as formed by a lambda expression
1551  * or the result of std::bind) representing the 'when' callback (the
1552  * callback to be executed when the function represented by this
1553  * Cgu::Thread::Future object has successfully completed). It should
1554  * take a single unbound argument, namely a reference to const to the
1555  * return type of the thread function represented by this
1556  * Cgu::Thread::Future object.
1557  * @param r A Releaser object for automatic disconnection of the
1558  * 'when' callback before it executes in a main loop (mainly relevant
1559  * if the callback represents or calls into a non-static member
1560  * function of an object which may be destroyed before the callback
1561  * executes).
1562  * @param priority The priority to be given to the 'when' callback in
1563  * the main loop after the thread function represented by this
1564  * Cgu::Thread::Future object has successfully completed. In
1565  * ascending order of priorities, priorities are G_PRIORITY_LOW,
1566  * G_PRIORITY_DEFAULT_IDLE, G_PRIORITY_HIGH_IDLE, G_PRIORITY_DEFAULT
1567  * and G_PRIORITY_HIGH. The default is G_PRIORITY_DEFAULT. This
1568  * determines the order in which the callback will appear in the event
1569  * list in the main loop, not the priority which the OS will adopt.
1570  * @param context The glib main context of the thread in whose main
1571  * loop the 'when' callback is to be executed (the default of NULL
1572  * will cause the callback to be executed in the main program loop).
1573  * @return The internal dispatching callback created by this method
1574  * and connected to done_emitter. It is made available as a return
1575  * value so that if wanted it can be disconnected programmatically
1576  * from done_emitter, or block()/unblock() can be called on it (but if
1577  * that is to be done, it must be done before the thread function
1578  * represented by this Cgu::Thread::Future object has completed in
1579  * order for it to be effective).
1580  * @exception Cgu::Thread::FutureWhenError This method will throw
1581  * Cgu::Thread::FutureWhenError if it is called after the thread
1582  * function represented by this Cgu::Thread::Future object has
1583  * completed.
1584  * @exception std::bad_alloc This method might throw std::bad_alloc if
1585  * memory is exhausted and the system throws in that case.
1586  * @exception Cgu::Thread::MutexError This method will throw
1587  * Cgu:Thread::MutexError if initialisation of the mutex in a
1588  * SafeEmitterArg object constructed by this method fails. If it does
1589  * so, the 'when' callback will be disposed of. (It is often not
1590  * worth checking for this, as it means either memory is exhausted or
1591  * pthread has run out of other resources to create new mutexes.)
1592  * @note 1. This method will also throw if the copy or move
1593  * constructor of the 'when' callable object throws.
1594  * @note 2. The return value of the function represented by this
1595  * Cgu::Thread::Future object is stored and passed as an argument to
1596  * the 'when' callback by const reference. If other threads might
1597  * concurrently call this object's get() method, which copies the
1598  * stored value, the stored type's copy constructor must be thread
1599  * safe with respect to the stored type's const methods. This would
1600  * be relevant if the stored type has data members declared mutable
1601  * which would be copied by its copy constructor.
1602  * @note 3. By virtue of the Releaser object, it is in theory possible
1603  * (if memory is exhausted and the system throws in that case) that an
1604  * internal SafeEmitterArg object will throw std::bad_alloc when
1605  * emitting/executing the 'when' callback in the glib main loop, with
1606  * the result that the relevant callback will not execute (instead the
1607  * exception will be consumed and a g_critical() warning will be
1608  * issued). This is rarely of any relevance because glib will abort
1609  * the program if it is itself unable to obtain memory from the
1610  * operating system. However, where it is relevant, design the
1611  * program so that it is not necessary to provide a releaser object.
1612  *
1613  * Since 2.1.0
1614  */
1615  // we need to use enable_if so that where this function is passed a
1616  // pointer to non-const Callback::CallbackArg, or some other
1617  // convertible pointer, this templated overload is dropped from the
1618  // overload set, in order to support the Callback::CallbackArg
1619  // overloads of this function. This overload calls into the version
1620  // of this function taking a pointer to const Callback::CallbackArg
1621  // in order to perform type erasure.
1622  template <class When,
1623  class = typename std::enable_if<!std::is_convertible<typename std::remove_reference<When>::type,
1624  const Cgu::Callback::CallbackArg<const Val&>*>::value>::type>
1626  Cgu::Releaser& r,
1627  gint priority = G_PRIORITY_DEFAULT,
1628  GMainContext* context = 0) {
1629  return when(Callback::lambda<const Val&>(std::forward<When>(w)),
1630  r,
1631  priority,
1632  context);
1633  }
1634 
1635 /**
1636  * A utility intended to be used where relevant in conjunction with
1637  * the when() methods. It enables a callback to be executed in a glib
1638  * main loop (referred to below as the 'fail' callback) if memory is
1639  * exhausted and std::bad_alloc was thrown by the thread wrapper of
1640  * Cgu::Thread::Future after calling run() or by done_emitter when
1641  * emitting, or if the thread function represented by this
1642  * Cgu::Thread::Future object threw Cgu::Thread::Exit, exited with an
1643  * uncaught exception deriving from std::exception or was cancelled
1644  * (or that function took an argument of class type by value whose
1645  * copy constructor threw such an exception or had a return value of
1646  * class type whose move assignment operator, or if none copy
1647  * assignment operator, threw such an exception), or any callback
1648  * connected to done_emitter exited with an uncaught exception. It
1649  * therefore enables errors to be detected and acted on without having
1650  * a thread wait on the get() method in order to test is_error() or
1651  * is_emitter_error().
1652  *
1653  * It is implemented by attaching a timeout to the main loop which
1654  * polls at 100 millisecond intervals and tests is_done()/is_error()
1655  * and is_emitter_done()/is_emitter_error(). The timeout is
1656  * automatically removed by the implementation once it has been
1657  * detected that an error has occurred and the 'fail' callback is
1658  * executed, or if the thread function represented by this Cgu::Future
1659  * object and all done_emitter emissions (including execution of any
1660  * 'when' callback) have completed successfully.
1661  *
1662  * This method can be called before or after the run() method has been
1663  * called, and whether or not the thread function represented by this
1664  * Cgu::Thread::Future object has completed.
1665  *
1666  * Once this method has been called, this Cgu::Thread::Future object
1667  * will always stay in existence until the timeout has been
1668  * automatically removed by the implementation. Accordingly it is
1669  * safe to use this method even if the intrusive pointer object
1670  * returned by the make() methods will go out of scope before the
1671  * 'fail' callback has executed: the callback will execute correctly
1672  * irrespective of that.
1673  *
1674  * This method does not have a priority argument: as a polling timeout
1675  * is created, a particular priority will normally have no
1676  * significance (in fact, the 'fail' callback will execute in the main
1677  * loop with a priority of G_PRIORITY_DEFAULT). If in a special case
1678  * a different polling interval than 100 milliseconds or a different
1679  * priority is required, users can attach their own polling timeouts
1680  * to a main loop and carry out the tests by hand.
1681  *
1682  * Four other points should be noted. First, if as well as the when()
1683  * method being called some other callback has been connected to
1684  * done_emitter, and that other callback throws, the 'fail' callback
1685  * will execute. Therefore, if the particular program design requires
1686  * that the 'fail' callback should only execute if the 'when' callback
1687  * is not executed (and the 'when' callback only execute if the 'fail'
1688  * callback does not execute), no other callbacks which throw should
1689  * be connected to done_emitter.
1690  *
1691  * Secondly, as mentioned in the documentation on the when() method,
1692  * if the 'when' callback exits with an uncaught exception upon being
1693  * executed by the main loop or it represents a function which takes
1694  * an argument by value whose copy constructor throws, the 'fail'
1695  * callback will not execute (the exception will have been consumed
1696  * internally in order to protect the main loop and a g_critical
1697  * message issued).
1698  *
1699  * Thirdly, avoid if possible having a 'fail' callback which might
1700  * throw, or representing a function which takes an argument by value
1701  * whose copy constructor might throw: such an exception would be
1702  * consumed internally in order to protect the main loop and a
1703  * g_critical message issued, but no other error indication apart from
1704  * the g_critical message will be provided.
1705  *
1706  * Fourthly, unlike the 'when' callback, a copy of this
1707  * Cgu::Thread::Future object held by intrusive pointer as returned by
1708  * the make() methods may safely be bound to the 'fail' callback,
1709  * which would enable the 'fail' callback to determine whether it is
1710  * is_error() or is_emitter_error() which returns false.
1711  *
1712  * If glib < 2.32 is used, the glib main loop must have been made
1713  * thread-safe by a call to g_thread_init() before this function is
1714  * called. glib >= 2.32 does not require g_thread_init() to be called
1715  * in order to be thread safe.
1716  *
1717  * @param cb The 'fail' callback (the callback to be executed if the
1718  * thread function represented by this Cgu::Thread::Future object or a
1719  * done_emitter emission has failed to complete). Ownership is taken
1720  * of this object, and it will be deleted when it has been finished
1721  * with.
1722  * @param context The glib main context of the thread in whose main
1723  * loop the 'fail' callback is to be executed (the default of NULL
1724  * will cause the functor to be executed in the main program loop).
1725  * @exception std::bad_alloc This method might throw std::bad_alloc if
1726  * memory is exhausted and the system throws in that case. If it does
1727  * so, the 'fail' callback will be disposed of.
1728  *
1729  * Since 2.0.2
1730  */
1731  void fail(const Cgu::Callback::Callback* cb,
1732  GMainContext* context = 0);
1733 
1734 /**
1735  * A utility intended to be used where relevant in conjunction with
1736  * the when() methods. It enables a callable object to be executed in
1737  * a glib main loop (referred to below as the 'fail' callback) if
1738  * memory is exhausted and std::bad_alloc was thrown by the thread
1739  * wrapper of Cgu::Thread::Future after calling run() or by
1740  * done_emitter when emitting, or if the thread function represented
1741  * by this Cgu::Thread::Future object threw Cgu::Thread::Exit, exited
1742  * with an uncaught exception deriving from std::exception or was
1743  * cancelled (or that function took an argument of class type by value
1744  * whose copy constructor threw such an exception or had a return
1745  * value of class type whose move assignment operator, or if none copy
1746  * assignment operator, threw such an exception), or any callback
1747  * connected to done_emitter exited with an uncaught exception. It
1748  * therefore enables errors to be detected and acted on without having
1749  * a thread wait on the get() method in order to test is_error() or
1750  * is_emitter_error().
1751  *
1752  * It is implemented by attaching a timeout to the main loop which
1753  * polls at 100 millisecond intervals and tests is_done()/is_error()
1754  * and is_emitter_done()/is_emitter_error(). The timeout is
1755  * automatically removed by the implementation once it has been
1756  * detected that an error has occurred and the 'fail' callback is
1757  * executed, or if the thread function represented by this Cgu::Future
1758  * object and all done_emitter emissions (including execution of any
1759  * 'when' callback) have completed successfully.
1760  *
1761  * This method can be called before or after the run() method has been
1762  * called, and whether or not the thread function represented by this
1763  * Cgu::Thread::Future object has completed.
1764  *
1765  * Once this method has been called, this Cgu::Thread::Future object
1766  * will always stay in existence until the timeout has been
1767  * automatically removed by the implementation. Accordingly it is
1768  * safe to use this method even if the intrusive pointer object
1769  * returned by the make() methods will go out of scope before the
1770  * 'fail' callback has executed: the callback will execute correctly
1771  * irrespective of that.
1772  *
1773  * This method does not have a priority argument: as a polling timeout
1774  * is created, a particular priority will normally have no
1775  * significance (in fact, the 'fail' callback will execute in the main
1776  * loop with a priority of G_PRIORITY_DEFAULT). If in a special case
1777  * a different polling interval than 100 milliseconds or a different
1778  * priority is required, users can attach their own polling timeouts
1779  * to a main loop and carry out the tests by hand.
1780  *
1781  * Four other points should be noted. First, if as well as the when()
1782  * method being called some other callback has been connected to
1783  * done_emitter, and that other callback throws, the 'fail' callback
1784  * will execute. Therefore, if the particular program design requires
1785  * that the 'fail' callback should only execute if the 'when' callback
1786  * is not executed (and the 'when' callback only execute if the 'fail'
1787  * callback does not execute), no other callbacks which throw should
1788  * be connected to done_emitter.
1789  *
1790  * Secondly, as mentioned in the documentation on the when() method,
1791  * if the 'when' callback exits with an uncaught exception upon being
1792  * executed by the main loop or it represents a function which takes
1793  * an argument by value whose copy constructor throws, the 'fail'
1794  * callback will not execute (the exception will have been consumed
1795  * internally in order to protect the main loop and a g_critical
1796  * message issued).
1797  *
1798  * Thirdly, avoid if possible having a 'fail' callback which might
1799  * throw: such an exception would be consumed internally in order to
1800  * protect the main loop and a g_critical message issued, but no other
1801  * error indication apart from the g_critical message will be
1802  * provided.
1803  *
1804  * Fourthly, unlike the 'when' callback, a copy of this
1805  * Cgu::Thread::Future object held by intrusive pointer as returned by
1806  * the make() methods may safely be bound to the 'fail' callback,
1807  * which would enable the 'fail' callback to determine whether it is
1808  * is_error() or is_emitter_error() which returns false.
1809  *
1810  * If glib < 2.32 is used, the glib main loop must have been made
1811  * thread-safe by a call to g_thread_init() before this function is
1812  * called. glib >= 2.32 does not require g_thread_init() to be called
1813  * in order to be thread safe.
1814  * @param f A callable object (such as formed by a lambda expression
1815  * or the result of std::bind) representing the 'fail' callback (the
1816  * callback to be executed if the thread function represented by this
1817  * Cgu::Thread::Future object or a done_emitter emission has failed to
1818  * complete). The callable object should be fully bound (it should
1819  * take no arguments when called).
1820  * @param context The glib main context of the thread in whose main
1821  * loop the 'fail' callback is to be executed (the default of NULL
1822  * will cause the functor to be executed in the main program loop).
1823  * @exception std::bad_alloc This method might throw std::bad_alloc if
1824  * memory is exhausted and the system throws in that case.
1825  * @note This method will also throw if the copy or move constructor
1826  * of the 'fail' callable object throws.
1827  *
1828  * Since 2.1.0
1829  */
1830  // we need to use enable_if so that where this function is passed a
1831  // pointer to non-const Callback::Callback, or some other
1832  // convertible pointer, this templated overload is dropped from the
1833  // overload set, in order to support the Callback::Callback
1834  // overloads of this function. This overload calls into the version
1835  // of this function taking a pointer to const Callback::Callback in
1836  // order to perform type erasure.
1837  template <class Fail,
1838  class = typename std::enable_if<!std::is_convertible<typename std::remove_reference<Fail>::type,
1839  const Cgu::Callback::Callback*>::value>::type>
1840  void fail(Fail&& f,
1841  GMainContext* context = 0) {
1842  fail(Callback::lambda<>(std::forward<Fail>(f)),
1843  context);
1844  }
1845 
1846 /**
1847  * This is a version of the fail() utility for use in conjunction with
1848  * the when() methods, which takes a Releaser object for automatic
1849  * disconnection of the callback functor passed as an argument to this
1850  * method if the object having the callback function as a member is
1851  * destroyed. For this to be race free, the lifetime of that object
1852  * must be controlled by the thread in whose main loop the 'fail'
1853  * callback will execute.
1854  *
1855  * This method enables a callback to be executed in a glib main loop
1856  * if memory is exhausted and std::bad_alloc was thrown by the thread
1857  * wrapper of Cgu::Thread::Future after calling run() or by
1858  * done_emitter when emitting, or if the thread function represented
1859  * by this Cgu::Thread::Future object threw Cgu::Thread::Exit, exited
1860  * with an uncaught exception deriving from std::exception or was
1861  * cancelled (or that function took an argument of class type by value
1862  * whose copy constructor threw such an exception or had a return
1863  * value of class type whose move assignment operator, or if none copy
1864  * assignment operator, threw such an exception), or any callback
1865  * connected to done_emitter exited with an uncaught exception. It
1866  * therefore enables errors to be detected and acted on without having
1867  * a thread wait on the get() method in order to test is_error() or
1868  * is_emitter_error().
1869  *
1870  * This method can be called before or after the run() method has been
1871  * called, and whether or not the thread function represented by this
1872  * Cgu::Thread::Future object has completed.
1873  *
1874  * The documentation for the version of this method which does not
1875  * take a Releaser object gives further details of how this method is
1876  * used.
1877  *
1878  * If glib < 2.32 is used, the glib main loop must have been made
1879  * thread-safe by a call to g_thread_init() before this function is
1880  * called. glib >= 2.32 does not require g_thread_init() to be called
1881  * in order to be thread safe.
1882  *
1883  * @param cb The 'fail' callback (the callback to be executed if the
1884  * thread function represented by this Cgu::Thread::Future object or a
1885  * done_emitter emission has failed to complete). Ownership is taken
1886  * of this object, and it will be deleted when it has been finished
1887  * with.
1888  * @param r A Releaser object for automatic disconnection of the
1889  * 'fail' callback before it executes in a main loop (mainly relevant
1890  * if the callback represents a non-static member function of an
1891  * object which may be destroyed before the callback executes).
1892  * @param context The glib main context of the thread in whose main
1893  * loop the 'fail' callback is to be executed (the default of NULL
1894  * will cause the functor to be executed in the main program loop).
1895  * @exception std::bad_alloc This method might throw std::bad_alloc if
1896  * memory is exhausted and the system throws in that case. If it does
1897  * so, the 'fail' callback will be disposed of.
1898  * @exception Cgu::Thread::MutexError This method will throw
1899  * Cgu:Thread::MutexError if initialisation of the mutex in a
1900  * SafeEmitterArg object constructed by Cgu::start_timeout() fails.
1901  * If it does so, the 'fail' callback will be disposed of. (It is
1902  * often not worth checking for this, as it means either memory is
1903  * exhausted or pthread has run out of other resources to create new
1904  * mutexes.)
1905  * @note By virtue of the Releaser object, it is in theory possible
1906  * (if memory is exhausted and the system throws in that case) that an
1907  * internal SafeEmitterArg object will throw std::bad_alloc when
1908  * emitting/executing the 'fail' callback in the glib main loop, with
1909  * the result that the relevant callback will not execute (instead the
1910  * exception will be consumed and a g_critical() warning will be
1911  * issued). This is rarely of any relevance because glib will abort
1912  * the program if it is itself unable to obtain memory from the
1913  * operating system. However, where it is relevant, design the
1914  * program so that it is not necessary to provide a releaser object.
1915  *
1916  * Since 2.0.2
1917  */
1918  void fail(const Cgu::Callback::Callback* cb,
1919  Cgu::Releaser& r,
1920  GMainContext* context = 0);
1921 
1922 /**
1923  * This is a version of the fail() utility for use in conjunction with
1924  * the when() methods, which takes a Releaser object for automatic
1925  * disconnection of the callable object passed as an argument to this
1926  * method if the 'fail' callback represents or calls into an object
1927  * which is destroyed. For this to be race free, the lifetime of the
1928  * object called into must be controlled by the thread in whose main
1929  * loop the 'fail' callback will execute.
1930  *
1931  * This method enables a callback to be executed in a glib main loop
1932  * if memory is exhausted and std::bad_alloc was thrown by the thread
1933  * wrapper of Cgu::Thread::Future after calling run() or by
1934  * done_emitter when emitting, or if the thread function represented
1935  * by this Cgu::Thread::Future object threw Cgu::Thread::Exit, exited
1936  * with an uncaught exception deriving from std::exception or was
1937  * cancelled (or that function took an argument of class type by value
1938  * whose copy constructor threw such an exception or had a return
1939  * value of class type whose move assignment operator, or if none copy
1940  * assignment operator, threw such an exception), or any callback
1941  * connected to done_emitter exited with an uncaught exception. It
1942  * therefore enables errors to be detected and acted on without having
1943  * a thread wait on the get() method in order to test is_error() or
1944  * is_emitter_error().
1945  *
1946  * This method can be called before or after the run() method has been
1947  * called, and whether or not the thread function represented by this
1948  * Cgu::Thread::Future object has completed.
1949  *
1950  * The documentation for the version of this method which does not
1951  * take a Releaser object gives further details of how this method is
1952  * used.
1953  *
1954  * If glib < 2.32 is used, the glib main loop must have been made
1955  * thread-safe by a call to g_thread_init() before this function is
1956  * called. glib >= 2.32 does not require g_thread_init() to be called
1957  * in order to be thread safe.
1958 
1959  * @param f A callable object (such as formed by a lambda expression
1960  * or the result of std::bind) representing the 'fail' callback (the
1961  * callback to be executed if the thread function represented by this
1962  * Cgu::Thread::Future object or a done_emitter emission has failed to
1963  * complete). The callable object should be fully bound (it should
1964  * take no arguments when called).
1965  * @param r A Releaser object for automatic disconnection of the
1966  * 'fail' callback before it executes in a main loop (mainly relevant
1967  * if the callback represents or calls into a non-static member
1968  * function of an object which may be destroyed before the callback
1969  * executes).
1970  * @param context The glib main context of the thread in whose main
1971  * loop the 'fail' callback is to be executed (the default of NULL
1972  * will cause the functor to be executed in the main program loop).
1973  * @exception std::bad_alloc This method might throw std::bad_alloc if
1974  * memory is exhausted and the system throws in that case.
1975  * @exception Cgu::Thread::MutexError This method will throw
1976  * Cgu:Thread::MutexError if initialisation of the mutex in a
1977  * SafeEmitterArg object constructed by Cgu::start_timeout() fails.
1978  * (It is often not worth checking for this, as it means either memory
1979  * is exhausted or pthread has run out of other resources to create
1980  * new mutexes.)
1981  * @note 1. This method will also throw if the copy or move
1982  * constructor of the 'fail' callable object throws.
1983  * @note 2. By virtue of the Releaser object, it is in theory possible
1984  * (if memory is exhausted and the system throws in that case) that an
1985  * internal SafeEmitterArg object will throw std::bad_alloc when
1986  * emitting/executing the 'fail' callback in the glib main loop, with
1987  * the result that the relevant callback will not execute (instead the
1988  * exception will be consumed and a g_critical() warning will be
1989  * issued). This is rarely of any relevance because glib will abort
1990  * the program if it is itself unable to obtain memory from the
1991  * operating system. However, where it is relevant, design the
1992  * program so that it is not necessary to provide a releaser object.
1993  *
1994  * Since 2.1.0
1995  */
1996  // we need to use enable_if so that where this function is passed a
1997  // pointer to non-const Callback::Callback, or some other
1998  // convertible pointer, this templated overload is dropped from the
1999  // overload set, in order to support the Callback::Callback
2000  // overloads of this function. This overload calls into the version
2001  // of this function taking a pointer to const Callback::Callback in
2002  // order to perform type erasure.
2003  template <class Fail,
2004  class = typename std::enable_if<!std::is_convertible<typename std::remove_reference<Fail>::type,
2005  const Cgu::Callback::Callback*>::value>::type>
2006  void fail(Fail&& f,
2007  Cgu::Releaser& r,
2008  GMainContext* context = 0) {
2009  fail(Callback::lambda<>(std::forward<Fail>(f)),
2010  r,
2011  context);
2012  }
2013 
2014 /**
2015  * @return true if the function or callable object represented by this
2016  * Cgu::Thread::Future object has finished, either by returning
2017  * normally, by cancellation or by virtue of having thrown
2018  * Cgu::Thread::Exit or some exception derived from std::exception.
2019  * Once this method returns true, then it is guaranteed that the get()
2020  * or move_get() method will not block (except as incidental to any
2021  * contention between threads calling get()). Once this method has
2022  * returned true or get() or move_get() has unblocked, then the result
2023  * of is_error() is definitive. This method is thread safe and may be
2024  * called by any thread. It will not throw.
2025  * @note This method will return true even though any callbacks
2026  * connected to done_emitter are still executing or waiting to
2027  * execute. From version 2.0.2 the is_emitter_done() method will
2028  * indicate when done_emitter callbacks (if any) have also completed.
2029  */
2030  bool is_done() const noexcept;
2031 
2032 /**
2033  * @return true if both the function or callable object represented by
2034  * this Cgu::Thread::Future object has finished and any callbacks
2035  * connected to done_emitter have completed. Once this method returns
2036  * true, then the result of is_emitter_error() is definitive. This
2037  * method is thread safe and may be called by any thread. It will not
2038  * throw.
2039  * @note This method will return true automatically if is_error() and
2040  * is_done() return true, because if the function or callable object
2041  * represented by this Cgu::Thread::Future object was cancelled or
2042  * exited with an uncaught exception, done_emitter is never emitted.
2043  * In addition, if this method returns true, then is_done() must also
2044  * return true.
2045  *
2046  * Since 2.0.2
2047  */
2048  bool is_emitter_done() const noexcept;
2049 
2050 /**
2051  * @return true if (a) a Cgu::Thread::Exit exception has been thrown
2052  * by the function or callable object represented by this
2053  * Cgu::Thread::Future object (which will have been consumed by this
2054  * Cgu::Thread::Future object), (b) an exception derived from
2055  * std::exception has been thrown on invoking that function or object
2056  * which was not caught by it (which will have been consumed by this
2057  * Cgu::Thread::Future object), (c) any of those exceptions have been
2058  * thrown either by the copy constructor of an argument taken by value
2059  * by that function or object, or by the move assignment operator (or
2060  * if none, copy assignment operator) of the return value of that
2061  * function or object (which will have been consumed by this
2062  * Cgu::Thread::Future object), (d) the worker thread in which that
2063  * function or callable object executes was cancelled in mid-course
2064  * with a call to cancel() or (e) the thread wrapper implementing the
2065  * worker thread in this Cgu::Thread::Future object threw and then
2066  * consumed std::bad_alloc (this is different from the run() method
2067  * throwing std::bad_alloc). In these cases the value obtained by
2068  * get() or move_get() will not be valid (it will be a default
2069  * constructed object of the return type of the function represented
2070  * by this Cgu::Thread::Future object). Otherwise this method returns
2071  * false. The result of this method is definitive once get() or
2072  * move_get() has unblocked or is_done() returns true. This method is
2073  * thread safe and may be called by any thread. It will not throw.
2074  */
2075  bool is_error() const noexcept;
2076 
2077 /**
2078  * @return true if an uncaught exception arose in emitting @ref
2079  * DoneEmitterAnchor "done_emitter" when executing callbacks connected
2080  * to it. Otherwise this method returns false. The result of this
2081  * method is definitive once is_emitter_done() returns true. This
2082  * method is thread safe and may be called by any thread. It will not
2083  * throw.
2084  * @note This method will return false automatically if is_error()
2085  * returns true, because if the function or callable object
2086  * represented by this Cgu::Thread::Future object was cancelled or
2087  * exited with an uncaught exception, done_emitter is never emitted.
2088  * It follows that if this method returns true, is_error() must return
2089  * false.
2090  */
2091  bool is_emitter_error() const noexcept;
2092 
2093 /**
2094  * A Cgu::SafeEmitter object which is emitted when the function or
2095  * callable object represented by this Cgu::Thread::Future object
2096  * finishes correctly (that is, it is not cancelled and does not throw
2097  * any uncaught exceptions). By itself this emission does not do too
2098  * much as it is emitted (and connected callbacks execute in) the same
2099  * worker thread immediately after the Future function has completed.
2100  * However, any thread can connect a callback object to this
2101  * Cgu::SafeEmitter object and a connected callback can, say, cause
2102  * another callback to be executed in a thread's main loop using
2103  * Cgu::Callback::post(), and from version 2.0.2 when() methods are
2104  * provided which will do this for users automatically. Once the
2105  * run() method has been called, this Cgu::Thread::Future object (and
2106  * so done_emitter) will always stay in existence until the function
2107  * or callable object represented by it has completed (whether
2108  * correctly, by cancellation or by a thrown exception) and any
2109  * callbacks connected to the done_emitter object have completed,
2110  * irrespective of whether the intrusive pointer returned by the
2111  * make() or make_future() functions has gone out of scope.
2112  * @note 1. Cancellation is blocked while the Cgu::SafeEmitter object
2113  * emits and any connected callback executes.
2114  * @note 2. A connected callback can however terminate the worker
2115  * thread by throwing Cgu::Thread::Exit (in which case no subsequent
2116  * callbacks to be executed on that emission will execute either: the
2117  * worker thread will safely terminate and unwind the stack in so
2118  * doing). In that event, the emitter_error flag will be set.
2119  * @note 3. All other uncaught exceptions which might be thrown by the
2120  * Cgu::SafeEmitter object emitting, or by a connected callback
2121  * function executing, are consumed to retain the integrity of the
2122  * Thread::Future object. In the event of such an exception being
2123  * thrown, the emitter_error flag will be set. In summary, the
2124  * emitter_error flag will be set if (a) a connected callback function
2125  * throws Cgu::Thread::Exit, (b) some other uncaught exception escapes
2126  * from a connected callback function or (c) Cgu::SafeEmitter::emit()
2127  * throws std::bad_alloc or the copy constructor of a bound argument
2128  * which is not a reference argument has thrown. If the user knows
2129  * that the callback function does not throw Cgu::Thread::Exit and
2130  * does not allow any other exception to escape, then the cause must
2131  * be a std::bad_alloc memory exception in Cgu::SafeEmitter::emit() or
2132  * the copy constructor of a non-reference bound argument throwing.
2133  * @note 4. An emission is thread safe if the connected callback
2134  * functions are thread safe.
2135  * @note 5. This Cgu::Thread::Future object's mutex is released while
2136  * the Cgu::SafeEmitter object emits. This means that any connected
2137  * callbacks can safely call, say, the Future object's get() or
2138  * is_error() methods. However, a connected callback should not hold
2139  * a bound argument comprising a copy of this Cgu::Thread::Future
2140  * object held by intrusive pointer as returned by the make() or
2141  * make_future() methods (that would result in this
2142  * Cgu::Thread::Future object owning, via done_emitter, a reference to
2143  * itself and so become incapable of being freed). The callback may,
2144  * however, take a pointer to this Cgu::Thread::Future object as a
2145  * bound argument, as obtained by the Cgu::IntrusivePtr::get() method,
2146  * because this Cgu::Thread::Future object is guaranteed to remain in
2147  * existence until all callbacks connected to done_emitter have
2148  * completed executing.
2149  * @anchor DoneEmitterAnchor
2150  */
2152 
2153 /* Only has effect if --with-glib-memory-slices-compat or
2154  * --with-glib-memory-slices-no-compat option picked */
2156 };
2157 
2158 /**
2159  * @deprecated
2160  *
2161  * DEPRECATED. Use the version of make_future() which takes a
2162  * callable object.
2163  *
2164  * A convenience helper function which calls
2165  * Cgu::Thread::Future::make() to obtain a Future object without the
2166  * need to specify the return value of the function represented by the
2167  * new object: that is deduced from the signature of that function.
2168  * This is useful shorthand when also employed with the C++11/14
2169  * 'auto' keyword.
2170  * @exception std::bad_alloc It might throw std::bad_alloc if memory
2171  * is exhausted and the system throws in that case. (This exception
2172  * will not be thrown if the library has been installed using the
2173  * \--with-glib-memory-slices-no-compat configuration option: instead
2174  * glib will terminate the program if it is unable to obtain memory
2175  * from the operating system.)
2176  * @exception Cgu::Thread::MutexError It might throw
2177  * Cgu::Thread::MutexError if initialisation of the contained mutex
2178  * fails. (It is often not worth checking for this, as it means
2179  * either memory is exhausted or pthread has run out of other
2180  * resources to create new mutexes.)
2181  * @exception Cgu::Thread::CondError It might throw
2182  * Cgu::Thread::CondError if initialisation of the contained condition
2183  * variable fails. (It is often not worth checking for this, as it
2184  * means either memory is exhausted or pthread has run out of other
2185  * resources to create new condition variables.)
2186  * @note This method will also throw if the copy or move constructor
2187  * of a bound argument throws, or the default constructor of the
2188  * return value type of the function represented by the new object
2189  * throws.
2190 
2191  *
2192  * Since 2.0.4
2193  */
2194 template <class Obj, class Ret, class... Params, class... Args>
2196  Ret (Obj::*func)(Params...),
2197  Args&&... args) {
2198  return Cgu::Thread::Future<Ret>::make(obj, func, std::forward<Args>(args)...);
2199 }
2200 
2201 /**
2202  * @deprecated
2203  *
2204  * DEPRECATED. Use the version of make_future() which takes a
2205  * callable object.
2206  *
2207  * A convenience helper function which calls
2208  * Cgu::Thread::Future::make() to obtain a Future object without the
2209  * need to specify the return value of the function represented by the
2210  * new object: that is deduced from the signature of that function.
2211  * This is useful shorthand when also employed with the C++11/14
2212  * 'auto' keyword.
2213  * @exception std::bad_alloc It might throw std::bad_alloc if memory
2214  * is exhausted and the system throws in that case. (This exception
2215  * will not be thrown if the library has been installed using the
2216  * \--with-glib-memory-slices-no-compat configuration option: instead
2217  * glib will terminate the program if it is unable to obtain memory
2218  * from the operating system.)
2219  * @exception Cgu::Thread::MutexError It might throw
2220  * Cgu::Thread::MutexError if initialisation of the contained mutex
2221  * fails. (It is often not worth checking for this, as it means
2222  * either memory is exhausted or pthread has run out of other
2223  * resources to create new mutexes.)
2224  * @exception Cgu::Thread::CondError It might throw
2225  * Cgu::Thread::CondError if initialisation of the contained condition
2226  * variable fails. (It is often not worth checking for this, as it
2227  * means either memory is exhausted or pthread has run out of other
2228  * resources to create new condition variables.)
2229  * @note This method will also throw if the copy or move constructor
2230  * of a bound argument throws, or the default constructor of the
2231  * return value type of the function represented by the new object
2232  * throws.
2233  *
2234  * Since 2.0.4
2235  */
2236 template <class Obj, class Ret, class... Params, class... Args>
2238  Ret (Obj::*func)(Params...) const,
2239  Args&&... args) {
2240  return Cgu::Thread::Future<Ret>::make(obj, func, std::forward<Args>(args)...);
2241 }
2242 
2243 /**
2244  * @deprecated
2245  *
2246  * DEPRECATED. Use the version of make_future() which takes a
2247  * callable object.
2248  *
2249  * A convenience helper function which calls
2250  * Cgu::Thread::Future::make() to obtain a Future object without the
2251  * need to specify the return value of the function represented by the
2252  * new object: that is deduced from the signature of that function.
2253  * This is useful shorthand when also employed with the C++11/14
2254  * 'auto' keyword.
2255  * @exception std::bad_alloc It might throw std::bad_alloc if memory
2256  * is exhausted and the system throws in that case. (This exception
2257  * will not be thrown if the library has been installed using the
2258  * \--with-glib-memory-slices-no-compat configuration option: instead
2259  * glib will terminate the program if it is unable to obtain memory
2260  * from the operating system.)
2261  * @exception Cgu::Thread::MutexError It might throw
2262  * Cgu::Thread::MutexError if initialisation of the contained mutex
2263  * fails. (It is often not worth checking for this, as it means
2264  * either memory is exhausted or pthread has run out of other
2265  * resources to create new mutexes.)
2266  * @exception Cgu::Thread::CondError It might throw
2267  * Cgu::Thread::CondError if initialisation of the contained condition
2268  * variable fails. (It is often not worth checking for this, as it
2269  * means either memory is exhausted or pthread has run out of other
2270  * resources to create new condition variables.)
2271  * @note This method will also throw if the copy or move constructor
2272  * of a bound argument throws, or the default constructor of the
2273  * return value type of the function represented by the new object
2274  * throws.
2275  *
2276  * Since 2.0.4
2277  */
2278 template <class Ret, class... Params, class... Args>
2280  Args&&... args) {
2281  return Cgu::Thread::Future<Ret>::make(func, std::forward<Args>(args)...);
2282 }
2283 
2284 /**
2285  * A convenience helper function which calls
2286  * Cgu::Thread::Future::make() to obtain a Future without the need to
2287  * specify the return value of the callable object to be represented
2288  * by it: that is deduced. This is useful shorthand when also
2289  * employed with the C++11/14 'auto' keyword.
2290  *
2291  * @param func A callable object, such as formed by a lambda
2292  * expression or the result of std::bind. It must be fully bound
2293  * (that is, its must take no arguments when called). It should
2294  * return a value (it cannot return void).
2295  * @exception std::bad_alloc It might throw std::bad_alloc if memory
2296  * is exhausted and the system throws in that case. (This exception
2297  * will not be thrown if the library has been installed using the
2298  * \--with-glib-memory-slices-no-compat configuration option: instead
2299  * glib will terminate the program if it is unable to obtain memory
2300  * from the operating system.)
2301  * @exception Cgu::Thread::MutexError It might throw
2302  * Cgu::Thread::MutexError if initialisation of the contained mutex
2303  * fails. (It is often not worth checking for this, as it means
2304  * either memory is exhausted or pthread has run out of other
2305  * resources to create new mutexes.)
2306  * @exception Cgu::Thread::CondError It might throw
2307  * Cgu::Thread::CondError if initialisation of the contained condition
2308  * variable fails. (It is often not worth checking for this, as it
2309  * means either memory is exhausted or pthread has run out of other
2310  * resources to create new condition variables.)
2311  * @note 1. This method will also throw if the copy or move
2312  * constructor of the callable object passed as an argument throws, or
2313  * the default constructor of the return value type of the function
2314  * represented by the new object throws.
2315  * @note 2. If the callable object passed as an argument has both
2316  * const and non-const operator()() methods, the non-const version
2317  * will be called even if the callable object passed is a const
2318  * object.
2319  *
2320  * Since 2.0.14
2321  */
2322 // we don't need this version of make_future() for syntactic reasons -
2323 // the version taking a single template parameter will do by itself
2324 // syntactically because it can use decltype. However, we include
2325 // this version in order to be API compatible with c++-gtk-utils <
2326 // 2.0.14, which required the return type to be specified when this
2327 // method is passed something other than a std::function object.
2328 // SFINAE will take care of the rest, except with a corner case where
2329 // all of the following apply: (i) a function object is passed whose
2330 // operator()() method returns a copy of the function object (or
2331 // another function object of the same type), (ii) the function object
2332 // is passed to this method as a rvalue and not a lvalue, and (iii)
2333 // the user specifically states the return type when instantiating
2334 // this template function. This would give rise to an ambiguity, but
2335 // its happening is extremely unlikely, and cannot happen with a
2336 // lambda or the return value of std::bind, because those types are
2337 // only known to the compiler, and cannot happen with other objects if
2338 // the user lets template deduction take its course.
2339 template <class Ret, class Func>
2341  return Cgu::Thread::Future<Ret>::make(std::forward<Func>(func));
2342 }
2343 
2344 // we don't want to document this function: it provides the type
2345 // deduction of the return value of the passed functor (it deals with
2346 // cases where this is not specified expressly).
2347 #ifndef DOXYGEN_PARSING
2348 template <class Func>
2350  // this function will fail to compile if the return type is a
2351  // reference type: that is a feature, not a bug, as a function
2352  // returning a reference lacks referential transparency, is unlikely
2353  // to be thread-safe and is unsuitable for use as a task function
2354  return Cgu::Thread::Future<decltype(func())>::make(std::forward<Func>(func));
2355 }
2356 #endif
2357 
2358 } // namespace Thread
2359 
2360 } // namespace Cgu
2361 
2362 #include <c++-gtk-utils/future.tpp>
2363 
2364 #endif
Cgu::Callback::SafeFunctor when(When &&w, gint priority=G_PRIORITY_DEFAULT, GMainContext *context=0)
Definition: future.h:1371
SafeEmitter done_emitter
Definition: future.h:2151
Definition: future.h:68
std::unique_ptr< Cgu::SafeEmitterArg< const Val & > > when
Definition: future.h:264
bool is_emitter_error() const noexcept
bool cancel() noexcept
WhenWrapperArg(WhenWrapperArg &&w)
Definition: future.h:254
bool is_done() const noexcept
STL namespace.
Cgu::IntrusivePtr< Cgu::Thread::Future< Ret > > make_future(Obj &obj, Ret(Obj::*func)(Params...), Args &&...args)
Definition: future.h:2195
std::unique_ptr< const Cgu::Callback::CallbackArg< const Val & > > when
Definition: future.h:248
Cgu::Callback::SafeFunctor when(When &&w, Cgu::Releaser &r, gint priority=G_PRIORITY_DEFAULT, GMainContext *context=0)
Definition: future.h:1625
A wrapper class for pthread condition variables.
Definition: mutex.h:449
This file provides classes for type erasure.
WhenWrapperArgRel(std::unique_ptr< Cgu::SafeEmitterArg< const Val & >> &&when_)
Definition: future.h:268
This is a smart pointer for managing objects allocated on freestore which maintain their own referenc...
Definition: intrusive_ptr.h:98
void fail(Fail &&f, Cgu::Releaser &r, GMainContext *context=0)
Definition: future.h:2006
void fail(const Cgu::Callback::Callback *cb, GMainContext *context=0)
Future & operator=(const Future &)=delete
WhenWrapperArg(std::unique_ptr< const Cgu::Callback::CallbackArg< const Val & >> &&when_)
Definition: future.h:252
bool is_emitter_done() const noexcept
A thread-safe class to execute callbacks connected to it, with provision for automatic disconnection...
Definition: emitter.h:327
A wrapper class for pthread mutexes.
Definition: mutex.h:117
void fail(Fail &&f, GMainContext *context=0)
Definition: future.h:1840
This is a counter class providing the ref() and unref() functions required by IntrusivePtr, with a thread safe reference count..
Definition: intrusive_ptr.h:349
A class representing a pthread thread.
Definition: thread.h:166
This file provides a thread-safe signal/slot mechanism, with automatic disconnection.
Provides wrapper classes for pthread mutexes and condition variables, and scoped locking classes for ...
Definition: application.h:44
Functor class holding a Callback::CallbackArg object, with thread-safe reference count.
Definition: callback.h:784
WhenWrapperArgRel(WhenWrapperArgRel &&w)
Definition: future.h:270
virtual const char * what() const
Definition: future.h:65
static Cgu::IntrusivePtr< Cgu::Thread::Future< Val > > make(T &t, Ret(T::*func)())
A class representing a pthread thread which will provide a value.
Definition: future.h:277
Definition: future.h:64
bool is_error() const noexcept
Cgu::Callback::SafeFunctor when(const Cgu::Callback::CallbackArg< const Val & > *cb, gint priority=G_PRIORITY_DEFAULT, GMainContext *context=0)
#define CGU_GLIB_MEMORY_SLICES_FUNCS
Definition: cgu_config.h:84
virtual const char * what() const
Definition: future.h:69
A class used for tracking EmitterArg and SafeEmitterArg connections.
Definition: emitter.h:352