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. This method is thread safe and may be called by any
930  * thread (and by more than one thread). It is a cancellation point
931  * if it blocks, and from version 2.0.11 is cancellation safe if the
932  * stack unwinds on cancellation. It is also strongly exception safe:
933  * no data will be lost if extracting the value fails.
934  * @return The value obtained from the function which is represented
935  * by this object
936  * @exception Cgu::Thread::FutureThreadError This method might throw
937  * Cgu::Thread::FutureThreadError if run() has not previously been
938  * called and the thread did not start properly when this function
939  * called run().
940  * @exception std::bad_alloc This method might throw std::bad_alloc if
941  * run() has not previously been called, memory is exhausted and the
942  * system throws in that case. (This exception will not be thrown if
943  * the library has been installed using the
944  * \--with-glib-memory-slices-no-compat configuration option: instead
945  * glib will terminate the program if it is unable to obtain memory
946  * from the operating system.)
947  * @note 1. This method might also throw if the copy constructor of
948  * the returned value type throws.
949  * @note 2. Question: Couldn't this method return the stored value by
950  * lvalue reference to const? Answer: It could. However, because of
951  * return value optimization, which will be implemented by any
952  * compiler capable of compiling this library, no advantage would be
953  * gained by doing so when initializing a local variable with the
954  * return value of this method (the copy constructor will only be
955  * called once whether returning by value or const reference). The
956  * advantage of returning by value is that the call to the copy
957  * constructor is forced to be within this Thread::Future object's
958  * mutex, so different threads' calls to the copy constructor are
959  * serialized, and also with blocked cancellation, so this method is
960  * cancellation safe. All calls to this method by different threads
961  * are therefore isolated and we do not have to worry about the thread
962  * safety of direct access to the stored value via its const methods
963  * outside the mutex (which would not be thread safe if the stored
964  * value has data members declared mutable) nor about the cancellation
965  * safety of the copy constructor. Of course, for objects which do
966  * not have mutable data, a hit arises by returning by value in cases
967  * where it is not intended to initialize a local variable at all nor
968  * to cancel a thread: where, say, only const methods are to be called
969  * on the return value (which could be done directly if this method
970  * returned by const reference). However, in many use cases this will
971  * be mitigated by the move_get() method.
972  */
973  Val get();
974 
975 /**
976  * Gets the stored value obtained from the function or callable object
977  * which is represented by this object by a move operation, if the
978  * return type implements a move constructor (otherwise this method
979  * does the same as the get() method). It is provided as an option
980  * for cases where a move is required for efficiency reasons, but
981  * although it may be called by any thread, a move from this
982  * Thread::Future object may normally only be made once (except where
983  * the return type has been designed to be moved more than once for
984  * the limited purpose of inspecting a flag indicating whether its
985  * value is valid or not). If this method is to be called then no
986  * calls to get() by another thread should normally be made and no
987  * calls to when() should be made. If the worker thread launched by
988  * the call to run() has not completed, then this method will block
989  * until it has completed. If run() has not been called, then run()
990  * will be called (and this method will block until the launched
991  * worker thread completes). If the function or callable object which
992  * is represented by this Cgu::Thread::Future object throws
993  * Cgu::Thread::Exit or an uncaught exception derived from
994  * std::exception, or if any of those exceptions are thrown either by
995  * the copy constructor of an argument taken by value by that function
996  * or object, or by the move assignment operator (or if none, copy
997  * assignment operator) of the return value of that function or
998  * object, then the exception will have been consumed by this
999  * Cgu::Thread::Future object and the error flag will have been set.
1000  * The error flag will also have been set if the worker thread is
1001  * cancelled or the thread wrapper in this Cgu::Thread::Future object
1002  * threw std::bad_alloc. On the error flag being set, this method
1003  * will unblock. This method is a cancellation point if it blocks,
1004  * and is cancellation safe if the stack unwinds on cancellation.
1005  * This method is only exception safe if the return type's move
1006  * constructor is exception safe.
1007  * @return The value obtained from the function which is represented
1008  * by this object
1009  * @exception Cgu::Thread::FutureThreadError This method might throw
1010  * Cgu::Thread::FutureThreadError if run() has not previously been
1011  * called and the thread did not start properly when this function
1012  * called run().
1013  * @exception std::bad_alloc This method might throw std::bad_alloc if
1014  * run() has not previously been called, memory is exhausted and the
1015  * system throws in that case. (This exception will not be thrown if
1016  * the library has been installed using the
1017  * \--with-glib-memory-slices-no-compat configuration option: instead
1018  * glib will terminate the program if it is unable to obtain memory
1019  * from the operating system.)
1020  * @note 1. This method might also throw if the copy or move
1021  * constructor of the returned value type throws.
1022  * @note 2. Question: Couldn't this method return the stored value by
1023  * rvalue reference? Answer: It could. However, because of return
1024  * value optimization, which will be implemented by any compiler
1025  * capable of compiling this library, no advantage would be gained by
1026  * doing so when initializing a local variable with the return value
1027  * of this method (the move constructor will only be called once, and
1028  * no call will be made to the copy constructor, whether returning by
1029  * value or rvalue reference). The advantage of returning by value is
1030  * that the call to the move constructor is forced to be within this
1031  * Thread::Future object's mutex, so different threads' calls to the
1032  * move constructor are serialized, and also with blocked
1033  * cancellation, so this method is cancellation safe. All calls to
1034  * this method by different threads are therefore isolated and we do
1035  * not have to worry about the thread safety of the mutating first
1036  * call to this method, nor about direct access to the stored value
1037  * via a rvalue reference outside the mutex nor the cancellation
1038  * safety of the move constructor.
1039  *
1040  * Since 2.0.11
1041  */
1042  Val move_get();
1043 
1044 /**
1045  * Cancels the worker thread in which the function or callable object
1046  * represented by this object runs, if it has not yet finished. If
1047  * this method is called and the worker thread is still running and is
1048  * cancelled in response to a call to this method, then the error flag
1049  * will be set so that a method calling get() or move_get() can
1050  * examine whether the result is valid. If run() has not yet been
1051  * called or the worker thread has already finished executing the
1052  * function or callable object represented by this object then this
1053  * function does nothing and returns false. This method is thread
1054  * safe and may be called by any thread. It will not throw.
1055  * @return true if run() has previously been called and the worker
1056  * thread has not yet finished executing the function or callable
1057  * object represented by this object, otherwise false (in which case
1058  * this method does nothing).
1059  * @note 1. Use this method with care. When cancelling a thread not
1060  * all thread implementations will unwind the stack, and so run the
1061  * destructors of local objects. This is discussed further in the
1062  * documentation on Cgu::Thread::Thread::cancel().
1063  * @note 2. This method might return true because the worker thread
1064  * has not yet finished, but the error flag might still not be set.
1065  * This is because the worker thread may not meet a cancellation point
1066  * before it ends naturally. It is the error flag which indicates
1067  * definitively whether the worker thread terminated prematurely in
1068  * response to a call to this method.
1069  */
1070  bool cancel() noexcept;
1071 
1072 /**
1073  * A utility enabling the value returned by the thread function
1074  * represented by this Cgu::Thread::Future object to be dealt with
1075  * asynchronously rather than by (or in addition to) a call to the
1076  * get() method. It causes the callback passed as an argument to this
1077  * method (referred to below as the 'when' callback) to be executed by
1078  * a thread's main loop if and when the thread function represented by
1079  * this Cgu::Thread::Future object finishes correctly - the 'when'
1080  * callback is passed that thread function's return value when it is
1081  * invoked. This method is thread safe, and may be called by any
1082  * thread.
1083  *
1084  * This functionality is implemented by connecting an internal
1085  * dispatching callback to the done_emitter object.
1086  *
1087  * The 'when' callback should take a single unbound argument
1088  * comprising a const reference to the return type of the thread
1089  * function represented by this Cgu::Thread::Future object. (So, in
1090  * the case of a Future<int> object, the callback function should take
1091  * a const int& argument as the unbound argument.) The 'when'
1092  * callback can have any number of bound arguments, except that a
1093  * bound argument may not include a copy of this Cgu::Thread::Future
1094  * object held by intrusive pointer as returned by the make() methods
1095  * (that would result in this Cgu::Thread::Future object owning, via
1096  * done_emitter, a reference to itself and so become incapable of
1097  * being freed). The 'when' callback may, however, take a pointer to
1098  * this Cgu::Thread::Future object, as obtained by the
1099  * Cgu::IntrusivePtr::get() method, because this Cgu::Thread::Future
1100  * object is guaranteed to remain in existence until the callback has
1101  * completed executing.
1102  *
1103  * This method cannot be called after the thread function represented
1104  * by this Cgu::Thread::Future object has completed (either
1105  * successfully or unsuccessfully) so that is_done() would return
1106  * true, and if this is attempted a Cgu::Thread::FutureWhenError
1107  * exception will be thrown. Therefore, generally this method should
1108  * be called before the run() method has been called.
1109  *
1110  * Once the run() method has been called, this Cgu::Thread::Future
1111  * object will always stay in existence until the thread function
1112  * represented by it has completed (whether correctly, by cancellation
1113  * or by a thrown exception), and any 'when' callback (and any other
1114  * callbacks connected to the done_emitter object) and any 'fail'
1115  * callback have completed. Accordingly it is safe to use this method
1116  * even if the intrusive pointer object returned by the make() methods
1117  * will go out of scope before the 'when' callback has executed: the
1118  * callback will execute correctly irrespective of that.
1119  *
1120  * Summary: use of this method is safe and has been implemented in a
1121  * way which does not give rise to timing issues.
1122  *
1123  * If memory is exhausted and std::bad_alloc is thrown by the thread
1124  * wrapper of Cgu::Thread::Future after run() is called or by
1125  * done_emitter when emitting, or if the thread function represented
1126  * by this Cgu::Thread::Future object throws Cgu::Thread::Exit, is
1127  * cancelled, exits with an uncaught exception deriving from
1128  * std::exception, takes an argument by value whose copy constructor
1129  * throws such an exception or has a return value whose move
1130  * assignment operator (or if none, copy assignment operator) throws
1131  * such an exception, or if the 'when' callback represents a function
1132  * taking a non-reference argument whose copy constructor throws an
1133  * exception, or if any other callback has been connected to
1134  * done_emitter before this method is called which exits with an
1135  * uncaught exception, then the 'when' callback will not execute
1136  * (instead the exception concerned will be consumed and an error
1137  * indicated). With many systems, swap memory combined with memory
1138  * over-commit makes it pointless to check for std::bad_alloc (and
1139  * even more so in programs using glib, as glib aborts a program where
1140  * it cannot obtain memory from the operating system). So subject to
1141  * that, if the user program is designed so that the thread function
1142  * represented by this Cgu::Thread::Future object does not exit with
1143  * uncaught exceptions, does not take an argument by value which
1144  * throws, does not have a return value whose move assignment operator
1145  * (or if none, copy assignment operator) throws, does not throw
1146  * Cgu::Thread::Exit and is not cancelled, and so that the 'when'
1147  * callback does not exit with an uncaught exception (and a function
1148  * represented by that callback either takes no arguments of class
1149  * type by value or the copy constructors of any of its value
1150  * arguments do not throw), and if this method is called before any
1151  * other callbacks are connected to done_emitter, the possibility of
1152  * failure can be disregarded.
1153  *
1154  * In cases where that is not true and detecting whether a failure has
1155  * occurred is required, a fail() method is provided. It should be
1156  * noted that a callback handed to the fail() method will not execute
1157  * in a case of error if the error comprises the 'when' callback
1158  * exiting with an uncaught exception when it is executed by the main
1159  * loop, or the copy constructor of any value argument of a function
1160  * represented by the 'when' callback throwing (such exceptions would
1161  * be consumed internally in order to protect the main loop and a
1162  * g_critical message issued). If the 'when' callback might exit with
1163  * an uncaught exception when executing or have the copy constructor
1164  * of a value argument throw, and doing something other than consuming
1165  * the exception and issuing a g_critical message is required, then a
1166  * different approach is to start a new thread to wait on the get()
1167  * method which can act on the result of is_error() directly.
1168  *
1169  * If glib < 2.32 is used, the glib main loop must have been made
1170  * thread-safe by a call to g_thread_init() before this function is
1171  * called. glib >= 2.32 does not require g_thread_init() to be called
1172  * in order to be thread safe.
1173  *
1174  * @param cb The 'when' callback (the callback to be executed when the
1175  * function represented by this Cgu::Thread::Future object has
1176  * successfully completed). Ownership is taken of this object, and it
1177  * will be deleted when it has been finished with.
1178  * @param priority The priority to be given to the 'when' callback in
1179  * the main loop after the thread function represented by this
1180  * Cgu::Thread::Future object has successfully completed. In
1181  * ascending order of priorities, priorities are G_PRIORITY_LOW,
1182  * G_PRIORITY_DEFAULT_IDLE, G_PRIORITY_HIGH_IDLE, G_PRIORITY_DEFAULT
1183  * and G_PRIORITY_HIGH. The default is G_PRIORITY_DEFAULT. This
1184  * determines the order in which the callback will appear in the event
1185  * list in the main loop, not the priority which the OS will adopt.
1186  * @param context The glib main context of the thread in whose main
1187  * loop the 'when' callback is to be executed (the default of NULL
1188  * will cause the callback to be executed in the main program loop).
1189  * @return The internal dispatching callback created by this method
1190  * and connected to done_emitter. It is made available as a return
1191  * value so that if wanted it can be disconnected programmatically
1192  * from done_emitter, or block()/unblock() can be called on it (but if
1193  * that is to be done, it must be done before the thread function
1194  * represented by this Cgu::Thread::Future object has completed in
1195  * order for it to be effective).
1196  * @exception Cgu::Thread::FutureWhenError This method will throw
1197  * Cgu::Thread::FutureWhenError if it is called after the thread
1198  * function represented by this Cgu::Thread::Future object has
1199  * completed. If it does so, the 'when' callback will be disposed of.
1200  * @exception std::bad_alloc This method might throw std::bad_alloc if
1201  * memory is exhausted and the system throws in that case. If it does
1202  * so, the 'when' callback will be disposed of.
1203  * @note The return value of the function represented by this
1204  * Cgu::Thread::Future object is stored and passed as an argument to
1205  * the 'when' callback by const reference. If other threads might
1206  * concurrently call this object's get() method, which copies the
1207  * stored value, the stored type's copy constructor must be thread
1208  * safe with respect to the stored type's const methods. This would
1209  * be relevant if the stored type has data members declared mutable
1210  * which would be copied by its copy constructor.
1211  *
1212  * Since 2.0.2
1213  */
1215  gint priority = G_PRIORITY_DEFAULT,
1216  GMainContext* context = 0);
1217 
1218 /**
1219  * A utility enabling the value returned by the thread function
1220  * represented by this Cgu::Thread::Future object to be dealt with
1221  * asynchronously rather than by (or in addition to) a call to the
1222  * get() method. It causes the callable object passed as an argument
1223  * to this method (referred to below as the 'when' callback) to be
1224  * executed by a thread's main loop if and when the thread function
1225  * represented by this Cgu::Thread::Future object finishes correctly -
1226  * the 'when' callback is passed that thread function's return value
1227  * when it is invoked. This method is thread safe, and may be called
1228  * by any thread.
1229  *
1230  * This functionality is implemented by connecting an internal
1231  * dispatching callback to the done_emitter object.
1232  *
1233  * The 'when' callback should take a single unbound argument
1234  * comprising a const reference to the return type of the thread
1235  * function represented by this Cgu::Thread::Future object. (So, in
1236  * the case of a Future<int> object, the callback function should take
1237  * a const int& argument as the unbound argument.) The 'when'
1238  * callback can have any number of bound arguments, except that a
1239  * bound argument may not include a copy of this Cgu::Thread::Future
1240  * object held by intrusive pointer as returned by the make() methods
1241  * (that would result in this Cgu::Thread::Future object owning, via
1242  * done_emitter, a reference to itself and so become incapable of
1243  * being freed). The 'when' callback may, however, take a pointer to
1244  * this Cgu::Thread::Future object, as obtained by the
1245  * Cgu::IntrusivePtr::get() method, because this Cgu::Thread::Future
1246  * object is guaranteed to remain in existence until the callback has
1247  * completed executing.
1248  *
1249  * This method cannot be called after the thread function represented
1250  * by this Cgu::Thread::Future object has completed (either
1251  * successfully or unsuccessfully) so that is_done() would return
1252  * true, and if this is attempted a Cgu::Thread::FutureWhenError
1253  * exception will be thrown. Therefore, generally this method should
1254  * be called before the run() method has been called.
1255  *
1256  * Once the run() method has been called, this Cgu::Thread::Future
1257  * object will always stay in existence until the thread function
1258  * represented by it has completed (whether correctly, by cancellation
1259  * or by a thrown exception), and any 'when' callback (and any other
1260  * callbacks connected to the done_emitter object) and any 'fail'
1261  * callback have completed. Accordingly it is safe to use this method
1262  * even if the intrusive pointer object returned by the make() methods
1263  * will go out of scope before the 'when' callback has executed: the
1264  * callback will execute correctly irrespective of that.
1265  *
1266  * Summary: use of this method is safe and has been implemented in a
1267  * way which does not give rise to timing issues.
1268  *
1269  * If memory is exhausted and std::bad_alloc is thrown by the thread
1270  * wrapper of Cgu::Thread::Future after run() is called or by
1271  * done_emitter when emitting, or if the thread function represented
1272  * by this Cgu::Thread::Future object throws Cgu::Thread::Exit, is
1273  * cancelled, exits with an uncaught exception deriving from
1274  * std::exception, takes an argument by value whose copy constructor
1275  * throws such an exception or has a return value whose move
1276  * assignment operator (or if none, copy assignment operator) throws
1277  * such an exception, or if any other callback has been connected to
1278  * done_emitter before this method is called which exits with an
1279  * uncaught exception, then the 'when' callback will not execute
1280  * (instead the exception concerned will be consumed and an error
1281  * indicated). With many systems, swap memory combined with memory
1282  * over-commit makes it pointless to check for std::bad_alloc (and
1283  * even more so in programs using glib, as glib aborts a program where
1284  * it cannot obtain memory from the operating system). So subject to
1285  * that, if the user program is designed so that the thread function
1286  * represented by this Cgu::Thread::Future object does not exit with
1287  * uncaught exceptions, does not take an argument by value which
1288  * throws, does not have a return value whose move assignment operator
1289  * (or if none, copy assignment operator) throws, does not throw
1290  * Cgu::Thread::Exit and is not cancelled, and so that the 'when'
1291  * callback does not exit with an uncaught exception, and if this
1292  * method is called before any other callbacks are connected to
1293  * done_emitter, the possibility of failure can be disregarded.
1294  *
1295  * In cases where that is not true and detecting whether a failure has
1296  * occurred is required, a fail() method is provided. It should be
1297  * noted that a callback handed to the fail() method will not execute
1298  * in a case of error if the error comprises the 'when' callback
1299  * exiting with an uncaught exception when it is executed by the main
1300  * loop (such exceptions would be consumed internally in order to
1301  * protect the main loop and a g_critical message issued). If the
1302  * 'when' callback might exit with an uncaught exception when
1303  * executing, and doing something other than consuming the exception
1304  * and issuing a g_critical message is required, then a different
1305  * approach is to start a new thread to wait on the get() method which
1306  * can act on the result of is_error() directly.
1307  *
1308  * If glib < 2.32 is used, the glib main loop must have been made
1309  * thread-safe by a call to g_thread_init() before this function is
1310  * called. glib >= 2.32 does not require g_thread_init() to be called
1311  * in order to be thread safe.
1312  *
1313  * @param w A callable object (such as formed by a lambda expression
1314  * or the result of std::bind) representing the 'when' callback (the
1315  * callback to be executed when the function represented by this
1316  * Cgu::Thread::Future object has successfully completed). It should
1317  * take a single unbound argument, namely a reference to const to the
1318  * return type of the thread function represented by this
1319  * Cgu::Thread::Future object.
1320  * @param priority The priority to be given to the 'when' callback in
1321  * the main loop after the thread function represented by this
1322  * Cgu::Thread::Future object has successfully completed. In
1323  * ascending order of priorities, priorities are G_PRIORITY_LOW,
1324  * G_PRIORITY_DEFAULT_IDLE, G_PRIORITY_HIGH_IDLE, G_PRIORITY_DEFAULT
1325  * and G_PRIORITY_HIGH. The default is G_PRIORITY_DEFAULT. This
1326  * determines the order in which the callback will appear in the event
1327  * list in the main loop, not the priority which the OS will adopt.
1328  * @param context The glib main context of the thread in whose main
1329  * loop the 'when' callback is to be executed (the default of NULL
1330  * will cause the callback to be executed in the main program loop).
1331  * @return The internal dispatching callback created by this method
1332  * and connected to done_emitter. It is made available as a return
1333  * value so that if wanted it can be disconnected programmatically
1334  * from done_emitter, or block()/unblock() can be called on it (but if
1335  * that is to be done, it must be done before the thread function
1336  * represented by this Cgu::Thread::Future object has completed in
1337  * order for it to be effective).
1338  * @exception Cgu::Thread::FutureWhenError This method will throw
1339  * Cgu::Thread::FutureWhenError if it is called after the thread
1340  * function represented by this Cgu::Thread::Future object has
1341  * completed.
1342  * @exception std::bad_alloc This method might throw std::bad_alloc if
1343  * memory is exhausted and the system throws in that case.
1344  * @note 1. This method will also throw if the copy or move
1345  * constructor of the 'when' callable object throws.
1346  * @note 2. The return value of the function represented by this
1347  * Cgu::Thread::Future object is stored and passed as an argument to
1348  * the 'when' callback by const reference. If other threads might
1349  * concurrently call this object's get() method, which copies the
1350  * stored value, the stored type's copy constructor must be thread
1351  * safe with respect to the stored type's const methods. This would
1352  * be relevant if the stored type has data members declared mutable
1353  * which would be copied by its copy constructor.
1354  *
1355  * Since 2.1.0
1356  */
1357  // we need to use enable_if so that where this function is passed a
1358  // pointer to non-const Callback::CallbackArg, or some other
1359  // convertible pointer, this templated overload is dropped from the
1360  // overload set, in order to support the Callback::CallbackArg
1361  // overloads of this function. This overload calls into the version
1362  // of this function taking a pointer to const Callback::CallbackArg
1363  // in order to perform type erasure.
1364  template <class When,
1365  class = typename std::enable_if<!std::is_convertible<typename std::remove_reference<When>::type,
1366  const Cgu::Callback::CallbackArg<const Val&>*>::value>::type>
1368  gint priority = G_PRIORITY_DEFAULT,
1369  GMainContext* context = 0) {
1370  return when(Callback::lambda<const Val&>(std::forward<When>(w)),
1371  priority,
1372  context);
1373  }
1374 
1375 /**
1376  * This is a version of the utility enabling the value returned by the
1377  * thread function represented by this Cgu::Thread::Future object to
1378  * be dealt with asynchronously, which takes a Releaser object for
1379  * automatic disconnection of the callback passed as an argument to
1380  * this method (referred to below as the 'when' callback), if the
1381  * object having the 'when' callback function as a member is
1382  * destroyed. For this to be race free, the lifetime of that object
1383  * must be controlled by the thread in whose main loop the 'when'
1384  * callback will execute.
1385  *
1386  * If the 'when' callback has not been released, this method causes it
1387  * to be executed by a thread's main loop if and when the thread
1388  * function represented by this Cgu::Thread::Future object finishes
1389  * correctly - the 'when' callback is passed that thread function's
1390  * return value when it is invoked. This method is thread safe, and
1391  * may be called by any thread.
1392  *
1393  * This functionality is implemented by connecting an internal
1394  * dispatching callback to the done_emitter object.
1395  *
1396  * The 'when' callback should take a single unbound argument
1397  * comprising a const reference to the return type of the thread
1398  * function represented by this Cgu::Thread::Future object. (So, in
1399  * the case of a Future<int> object, the callback function should take
1400  * a const int& argument as the unbound argument.) The 'when'
1401  * callback can have any number of bound arguments, except that a
1402  * bound argument may not include a copy of this Cgu::Thread::Future
1403  * object held by intrusive pointer as returned by the make() methods
1404  * (that would result in this Cgu::Thread::Future object owning, via
1405  * done_emitter, a reference to itself and so become incapable of
1406  * being freed). The 'when' callback may, however, take a pointer to
1407  * this Cgu::Thread::Future object, as obtained by the
1408  * Cgu::IntrusivePtr::get() method, because this Cgu::Thread::Future
1409  * object is guaranteed to remain in existence until the callback has
1410  * completed executing.
1411  *
1412  * This method cannot be called after the thread function represented
1413  * by this Cgu::Thread::Future object has completed (either
1414  * successfully or unsuccessfully) so that is_done() would return
1415  * true, and if this is attempted a Cgu::Thread::FutureWhenError
1416  * exception will be thrown. Therefore, generally this method should
1417  * be called before the run() method has been called.
1418  *
1419  * The documentation for the version of this method which does not
1420  * take a Releaser object gives further details of how this method is
1421  * used.
1422  *
1423  * If glib < 2.32 is used, the glib main loop must have been made
1424  * thread-safe by a call to g_thread_init() before this function is
1425  * called. glib >= 2.32 does not require g_thread_init() to be called
1426  * in order to be thread safe.
1427  *
1428  * @param cb The 'when' callback (the callback to be executed when the
1429  * function represented by this Cgu::Thread::Future object has
1430  * successfully completed). Ownership is taken of this object, and it
1431  * will be deleted when it has been finished with.
1432  * @param r A Releaser object for automatic disconnection of the
1433  * 'when' callback before it executes in a main loop (mainly relevant
1434  * if the callback represents a non-static member function of an
1435  * object which may be destroyed before the callback executes).
1436  * @param priority The priority to be given to the 'when' callback in
1437  * the main loop after the thread function represented by this
1438  * Cgu::Thread::Future object has successfully completed. In
1439  * ascending order of priorities, priorities are G_PRIORITY_LOW,
1440  * G_PRIORITY_DEFAULT_IDLE, G_PRIORITY_HIGH_IDLE, G_PRIORITY_DEFAULT
1441  * and G_PRIORITY_HIGH. The default is G_PRIORITY_DEFAULT. This
1442  * determines the order in which the callback will appear in the event
1443  * list in the main loop, not the priority which the OS will adopt.
1444  * @param context The glib main context of the thread in whose main
1445  * loop the 'when' callback is to be executed (the default of NULL
1446  * will cause the callback to be executed in the main program loop).
1447  * @return The internal dispatching callback created by this method
1448  * and connected to done_emitter. It is made available as a return
1449  * value so that if wanted it can be disconnected programmatically
1450  * from done_emitter, or block()/unblock() can be called on it (but if
1451  * that is to be done, it must be done before the thread function
1452  * represented by this Cgu::Thread::Future object has completed in
1453  * order for it to be effective).
1454  * @exception Cgu::Thread::FutureWhenError This method will throw
1455  * Cgu::Thread::FutureWhenError if it is called after the thread
1456  * function represented by this Cgu::Thread::Future object has
1457  * completed. If it does so, the 'when' callback will be disposed of.
1458  * @exception std::bad_alloc This method might throw std::bad_alloc if
1459  * memory is exhausted and the system throws in that case. If it does
1460  * so, the 'when' callback will be disposed of.
1461  * @exception Cgu::Thread::MutexError This method will throw
1462  * Cgu:Thread::MutexError if initialisation of the mutex in a
1463  * SafeEmitterArg object constructed by this method fails. If it does
1464  * so, the 'when' callback will be disposed of. (It is often not
1465  * worth checking for this, as it means either memory is exhausted or
1466  * pthread has run out of other resources to create new mutexes.)
1467  * @note 1. The return value of the function represented by this
1468  * Cgu::Thread::Future object is stored and passed as an argument to
1469  * the 'when' callback by const reference. If other threads might
1470  * concurrently call this object's get() method, which copies the
1471  * stored value, the stored type's copy constructor must be thread
1472  * safe with respect to the stored type's const methods. This would
1473  * be relevant if the stored type has data members declared mutable
1474  * which would be copied by its copy constructor.
1475  * @note 2. By virtue of the Releaser object, it is in theory possible
1476  * (if memory is exhausted and the system throws in that case) that an
1477  * internal SafeEmitterArg object will throw std::bad_alloc when
1478  * emitting/executing the 'when' callback in the glib main loop, with
1479  * the result that the relevant callback will not execute (instead the
1480  * exception will be consumed and a g_critical() warning will be
1481  * issued). This is rarely of any relevance because glib will abort
1482  * the program if it is itself unable to obtain memory from the
1483  * operating system. However, where it is relevant, design the
1484  * program so that it is not necessary to provide a releaser object.
1485  *
1486  * Since 2.0.2
1487  */
1489  Cgu::Releaser& r,
1490  gint priority = G_PRIORITY_DEFAULT,
1491  GMainContext* context = 0);
1492 
1493 /**
1494  * This is a version of the utility enabling the value returned by the
1495  * thread function represented by this Cgu::Thread::Future object to
1496  * be dealt with asynchronously, which takes a Releaser object for
1497  * automatic disconnection of the callable object passed as an
1498  * argument to this method (referred to below as the 'when' callback),
1499  * if the 'when' callback represents or calls into an object which is
1500  * destroyed. For this to be race free, the lifetime of the object
1501  * called into must be controlled by the thread in whose main loop the
1502  * 'when' callback will execute.
1503  *
1504  * If the 'when' callback has not been released, this method causes it
1505  * to be executed by a thread's main loop if and when the thread
1506  * function represented by this Cgu::Thread::Future object finishes
1507  * correctly - the 'when' callback is passed that thread function's
1508  * return value when it is invoked. This method is thread safe, and
1509  * may be called by any thread.
1510  *
1511  * This functionality is implemented by connecting an internal
1512  * dispatching callback to the done_emitter object.
1513  *
1514  * The 'when' callback should take a single unbound argument
1515  * comprising a const reference to the return type of the thread
1516  * function represented by this Cgu::Thread::Future object. (So, in
1517  * the case of a Future<int> object, the callback function should take
1518  * a const int& argument as the unbound argument.) The 'when'
1519  * callback can have any number of bound arguments, except that a
1520  * bound argument may not include a copy of this Cgu::Thread::Future
1521  * object held by intrusive pointer as returned by the make() methods
1522  * (that would result in this Cgu::Thread::Future object owning, via
1523  * done_emitter, a reference to itself and so become incapable of
1524  * being freed). The 'when' callback may, however, take a pointer to
1525  * this Cgu::Thread::Future object, as obtained by the
1526  * Cgu::IntrusivePtr::get() method, because this Cgu::Thread::Future
1527  * object is guaranteed to remain in existence until the callback has
1528  * completed executing.
1529  *
1530  * This method cannot be called after the thread function represented
1531  * by this Cgu::Thread::Future object has completed (either
1532  * successfully or unsuccessfully) so that is_done() would return
1533  * true, and if this is attempted a Cgu::Thread::FutureWhenError
1534  * exception will be thrown. Therefore, generally this method should
1535  * be called before the run() method has been called.
1536  *
1537  * The documentation for the version of this method which does not
1538  * take a Releaser object gives further details of how this method is
1539  * used.
1540  *
1541  * If glib < 2.32 is used, the glib main loop must have been made
1542  * thread-safe by a call to g_thread_init() before this function is
1543  * called. glib >= 2.32 does not require g_thread_init() to be called
1544  * in order to be thread safe.
1545  *
1546  * @param w A callable object (such as formed by a lambda expression
1547  * or the result of std::bind) representing the 'when' callback (the
1548  * callback to be executed when the function represented by this
1549  * Cgu::Thread::Future object has successfully completed). It should
1550  * take a single unbound argument, namely a reference to const to the
1551  * return type of the thread function represented by this
1552  * Cgu::Thread::Future object.
1553  * @param r A Releaser object for automatic disconnection of the
1554  * 'when' callback before it executes in a main loop (mainly relevant
1555  * if the callback represents or calls into a non-static member
1556  * function of an object which may be destroyed before the callback
1557  * executes).
1558  * @param priority The priority to be given to the 'when' callback in
1559  * the main loop after the thread function represented by this
1560  * Cgu::Thread::Future object has successfully completed. In
1561  * ascending order of priorities, priorities are G_PRIORITY_LOW,
1562  * G_PRIORITY_DEFAULT_IDLE, G_PRIORITY_HIGH_IDLE, G_PRIORITY_DEFAULT
1563  * and G_PRIORITY_HIGH. The default is G_PRIORITY_DEFAULT. This
1564  * determines the order in which the callback will appear in the event
1565  * list in the main loop, not the priority which the OS will adopt.
1566  * @param context The glib main context of the thread in whose main
1567  * loop the 'when' callback is to be executed (the default of NULL
1568  * will cause the callback to be executed in the main program loop).
1569  * @return The internal dispatching callback created by this method
1570  * and connected to done_emitter. It is made available as a return
1571  * value so that if wanted it can be disconnected programmatically
1572  * from done_emitter, or block()/unblock() can be called on it (but if
1573  * that is to be done, it must be done before the thread function
1574  * represented by this Cgu::Thread::Future object has completed in
1575  * order for it to be effective).
1576  * @exception Cgu::Thread::FutureWhenError This method will throw
1577  * Cgu::Thread::FutureWhenError if it is called after the thread
1578  * function represented by this Cgu::Thread::Future object has
1579  * completed.
1580  * @exception std::bad_alloc This method might throw std::bad_alloc if
1581  * memory is exhausted and the system throws in that case.
1582  * @exception Cgu::Thread::MutexError This method will throw
1583  * Cgu:Thread::MutexError if initialisation of the mutex in a
1584  * SafeEmitterArg object constructed by this method fails. If it does
1585  * so, the 'when' callback will be disposed of. (It is often not
1586  * worth checking for this, as it means either memory is exhausted or
1587  * pthread has run out of other resources to create new mutexes.)
1588  * @note 1. This method will also throw if the copy or move
1589  * constructor of the 'when' callable object throws.
1590  * @note 2. The return value of the function represented by this
1591  * Cgu::Thread::Future object is stored and passed as an argument to
1592  * the 'when' callback by const reference. If other threads might
1593  * concurrently call this object's get() method, which copies the
1594  * stored value, the stored type's copy constructor must be thread
1595  * safe with respect to the stored type's const methods. This would
1596  * be relevant if the stored type has data members declared mutable
1597  * which would be copied by its copy constructor.
1598  * @note 3. By virtue of the Releaser object, it is in theory possible
1599  * (if memory is exhausted and the system throws in that case) that an
1600  * internal SafeEmitterArg object will throw std::bad_alloc when
1601  * emitting/executing the 'when' callback in the glib main loop, with
1602  * the result that the relevant callback will not execute (instead the
1603  * exception will be consumed and a g_critical() warning will be
1604  * issued). This is rarely of any relevance because glib will abort
1605  * the program if it is itself unable to obtain memory from the
1606  * operating system. However, where it is relevant, design the
1607  * program so that it is not necessary to provide a releaser object.
1608  *
1609  * Since 2.1.0
1610  */
1611  // we need to use enable_if so that where this function is passed a
1612  // pointer to non-const Callback::CallbackArg, or some other
1613  // convertible pointer, this templated overload is dropped from the
1614  // overload set, in order to support the Callback::CallbackArg
1615  // overloads of this function. This overload calls into the version
1616  // of this function taking a pointer to const Callback::CallbackArg
1617  // in order to perform type erasure.
1618  template <class When,
1619  class = typename std::enable_if<!std::is_convertible<typename std::remove_reference<When>::type,
1620  const Cgu::Callback::CallbackArg<const Val&>*>::value>::type>
1622  Cgu::Releaser& r,
1623  gint priority = G_PRIORITY_DEFAULT,
1624  GMainContext* context = 0) {
1625  return when(Callback::lambda<const Val&>(std::forward<When>(w)),
1626  r,
1627  priority,
1628  context);
1629  }
1630 
1631 /**
1632  * A utility intended to be used where relevant in conjunction with
1633  * the when() methods. It enables a callback to be executed in a glib
1634  * main loop (referred to below as the 'fail' callback) if memory is
1635  * exhausted and std::bad_alloc was thrown by the thread wrapper of
1636  * Cgu::Thread::Future after calling run() or by done_emitter when
1637  * emitting, or if the thread function represented by this
1638  * Cgu::Thread::Future object threw Cgu::Thread::Exit, exited with an
1639  * uncaught exception deriving from std::exception or was cancelled
1640  * (or that function took an argument of class type by value whose
1641  * copy constructor threw such an exception or had a return value of
1642  * class type whose move assignment operator, or if none copy
1643  * assignment operator, threw such an exception), or any callback
1644  * connected to done_emitter exited with an uncaught exception. It
1645  * therefore enables errors to be detected and acted on without having
1646  * a thread wait on the get() method in order to test is_error() or
1647  * is_emitter_error().
1648  *
1649  * It is implemented by attaching a timeout to the main loop which
1650  * polls at 100 millisecond intervals and tests is_done()/is_error()
1651  * and is_emitter_done()/is_emitter_error(). The timeout is
1652  * automatically removed by the implementation once it has been
1653  * detected that an error has occurred and the 'fail' callback is
1654  * executed, or if the thread function represented by this Cgu::Future
1655  * object and all done_emitter emissions (including execution of any
1656  * 'when' callback) have completed successfully.
1657  *
1658  * This method can be called before or after the run() method has been
1659  * called, and whether or not the thread function represented by this
1660  * Cgu::Thread::Future object has completed.
1661  *
1662  * Once this method has been called, this Cgu::Thread::Future object
1663  * will always stay in existence until the timeout has been
1664  * automatically removed by the implementation. Accordingly it is
1665  * safe to use this method even if the intrusive pointer object
1666  * returned by the make() methods will go out of scope before the
1667  * 'fail' callback has executed: the callback will execute correctly
1668  * irrespective of that.
1669  *
1670  * This method does not have a priority argument: as a polling timeout
1671  * is created, a particular priority will normally have no
1672  * significance (in fact, the 'fail' callback will execute in the main
1673  * loop with a priority of G_PRIORITY_DEFAULT). If in a special case
1674  * a different polling interval than 100 milliseconds or a different
1675  * priority is required, users can attach their own polling timeouts
1676  * to a main loop and carry out the tests by hand.
1677  *
1678  * Four other points should be noted. First, if as well as the when()
1679  * method being called some other callback has been connected to
1680  * done_emitter, and that other callback throws, the 'fail' callback
1681  * will execute. Therefore, if the particular program design requires
1682  * that the 'fail' callback should only execute if the 'when' callback
1683  * is not executed (and the 'when' callback only execute if the 'fail'
1684  * callback does not execute), no other callbacks which throw should
1685  * be connected to done_emitter.
1686  *
1687  * Secondly, as mentioned in the documentation on the when() method,
1688  * if the 'when' callback exits with an uncaught exception upon being
1689  * executed by the main loop or it represents a function which takes
1690  * an argument by value whose copy constructor throws, the 'fail'
1691  * callback will not execute (the exception will have been consumed
1692  * internally in order to protect the main loop and a g_critical
1693  * message issued).
1694  *
1695  * Thirdly, avoid if possible having a 'fail' callback which might
1696  * throw, or representing a function which takes an argument by value
1697  * whose copy constructor might throw: such an exception would be
1698  * consumed internally in order to protect the main loop and a
1699  * g_critical message issued, but no other error indication apart from
1700  * the g_critical message will be provided.
1701  *
1702  * Fourthly, unlike the 'when' callback, a copy of this
1703  * Cgu::Thread::Future object held by intrusive pointer as returned by
1704  * the make() methods may safely be bound to the 'fail' callback,
1705  * which would enable the 'fail' callback to determine whether it is
1706  * is_error() or is_emitter_error() which returns false.
1707  *
1708  * If glib < 2.32 is used, the glib main loop must have been made
1709  * thread-safe by a call to g_thread_init() before this function is
1710  * called. glib >= 2.32 does not require g_thread_init() to be called
1711  * in order to be thread safe.
1712  *
1713  * @param cb The 'fail' callback (the callback to be executed if the
1714  * thread function represented by this Cgu::Thread::Future object or a
1715  * done_emitter emission has failed to complete). Ownership is taken
1716  * of this object, and it will be deleted when it has been finished
1717  * with.
1718  * @param context The glib main context of the thread in whose main
1719  * loop the 'fail' callback is to be executed (the default of NULL
1720  * will cause the functor to be executed in the main program loop).
1721  * @exception std::bad_alloc This method might throw std::bad_alloc if
1722  * memory is exhausted and the system throws in that case. If it does
1723  * so, the 'fail' callback will be disposed of.
1724  *
1725  * Since 2.0.2
1726  */
1727  void fail(const Cgu::Callback::Callback* cb,
1728  GMainContext* context = 0);
1729 
1730 /**
1731  * A utility intended to be used where relevant in conjunction with
1732  * the when() methods. It enables a callable object to be executed in
1733  * a glib main loop (referred to below as the 'fail' callback) if
1734  * memory is exhausted and std::bad_alloc was thrown by the thread
1735  * wrapper of Cgu::Thread::Future after calling run() or by
1736  * done_emitter when emitting, or if the thread function represented
1737  * by this Cgu::Thread::Future object threw Cgu::Thread::Exit, exited
1738  * with an uncaught exception deriving from std::exception or was
1739  * cancelled (or that function took an argument of class type by value
1740  * whose copy constructor threw such an exception or had a return
1741  * value of class type whose move assignment operator, or if none copy
1742  * assignment operator, threw such an exception), or any callback
1743  * connected to done_emitter exited with an uncaught exception. It
1744  * therefore enables errors to be detected and acted on without having
1745  * a thread wait on the get() method in order to test is_error() or
1746  * is_emitter_error().
1747  *
1748  * It is implemented by attaching a timeout to the main loop which
1749  * polls at 100 millisecond intervals and tests is_done()/is_error()
1750  * and is_emitter_done()/is_emitter_error(). The timeout is
1751  * automatically removed by the implementation once it has been
1752  * detected that an error has occurred and the 'fail' callback is
1753  * executed, or if the thread function represented by this Cgu::Future
1754  * object and all done_emitter emissions (including execution of any
1755  * 'when' callback) have completed successfully.
1756  *
1757  * This method can be called before or after the run() method has been
1758  * called, and whether or not the thread function represented by this
1759  * Cgu::Thread::Future object has completed.
1760  *
1761  * Once this method has been called, this Cgu::Thread::Future object
1762  * will always stay in existence until the timeout has been
1763  * automatically removed by the implementation. Accordingly it is
1764  * safe to use this method even if the intrusive pointer object
1765  * returned by the make() methods will go out of scope before the
1766  * 'fail' callback has executed: the callback will execute correctly
1767  * irrespective of that.
1768  *
1769  * This method does not have a priority argument: as a polling timeout
1770  * is created, a particular priority will normally have no
1771  * significance (in fact, the 'fail' callback will execute in the main
1772  * loop with a priority of G_PRIORITY_DEFAULT). If in a special case
1773  * a different polling interval than 100 milliseconds or a different
1774  * priority is required, users can attach their own polling timeouts
1775  * to a main loop and carry out the tests by hand.
1776  *
1777  * Four other points should be noted. First, if as well as the when()
1778  * method being called some other callback has been connected to
1779  * done_emitter, and that other callback throws, the 'fail' callback
1780  * will execute. Therefore, if the particular program design requires
1781  * that the 'fail' callback should only execute if the 'when' callback
1782  * is not executed (and the 'when' callback only execute if the 'fail'
1783  * callback does not execute), no other callbacks which throw should
1784  * be connected to done_emitter.
1785  *
1786  * Secondly, as mentioned in the documentation on the when() method,
1787  * if the 'when' callback exits with an uncaught exception upon being
1788  * executed by the main loop or it represents a function which takes
1789  * an argument by value whose copy constructor throws, the 'fail'
1790  * callback will not execute (the exception will have been consumed
1791  * internally in order to protect the main loop and a g_critical
1792  * message issued).
1793  *
1794  * Thirdly, avoid if possible having a 'fail' callback which might
1795  * throw: such an exception would be consumed internally in order to
1796  * protect the main loop and a g_critical message issued, but no other
1797  * error indication apart from the g_critical message will be
1798  * provided.
1799  *
1800  * Fourthly, unlike the 'when' callback, a copy of this
1801  * Cgu::Thread::Future object held by intrusive pointer as returned by
1802  * the make() methods may safely be bound to the 'fail' callback,
1803  * which would enable the 'fail' callback to determine whether it is
1804  * is_error() or is_emitter_error() which returns false.
1805  *
1806  * If glib < 2.32 is used, the glib main loop must have been made
1807  * thread-safe by a call to g_thread_init() before this function is
1808  * called. glib >= 2.32 does not require g_thread_init() to be called
1809  * in order to be thread safe.
1810  * @param f A callable object (such as formed by a lambda expression
1811  * or the result of std::bind) representing the 'fail' callback (the
1812  * callback to be executed if the thread function represented by this
1813  * Cgu::Thread::Future object or a done_emitter emission has failed to
1814  * complete). The callable object should be fully bound (it should
1815  * take no arguments when called).
1816  * @param context The glib main context of the thread in whose main
1817  * loop the 'fail' callback is to be executed (the default of NULL
1818  * will cause the functor to be executed in the main program loop).
1819  * @exception std::bad_alloc This method might throw std::bad_alloc if
1820  * memory is exhausted and the system throws in that case.
1821  * @note This method will also throw if the copy or move constructor
1822  * of the 'fail' callable object throws.
1823  *
1824  * Since 2.1.0
1825  */
1826  // we need to use enable_if so that where this function is passed a
1827  // pointer to non-const Callback::Callback, or some other
1828  // convertible pointer, this templated overload is dropped from the
1829  // overload set, in order to support the Callback::Callback
1830  // overloads of this function. This overload calls into the version
1831  // of this function taking a pointer to const Callback::Callback in
1832  // order to perform type erasure.
1833  template <class Fail,
1834  class = typename std::enable_if<!std::is_convertible<typename std::remove_reference<Fail>::type,
1835  const Cgu::Callback::Callback*>::value>::type>
1836  void fail(Fail&& f,
1837  GMainContext* context = 0) {
1838  fail(Callback::lambda<>(std::forward<Fail>(f)),
1839  context);
1840  }
1841 
1842 /**
1843  * This is a version of the fail() utility for use in conjunction with
1844  * the when() methods, which takes a Releaser object for automatic
1845  * disconnection of the callback functor passed as an argument to this
1846  * method if the object having the callback function as a member is
1847  * destroyed. For this to be race free, the lifetime of that object
1848  * must be controlled by the thread in whose main loop the 'fail'
1849  * callback will execute.
1850  *
1851  * This method enables a callback to be executed in a glib main loop
1852  * if memory is exhausted and std::bad_alloc was thrown by the thread
1853  * wrapper of Cgu::Thread::Future after calling run() or by
1854  * done_emitter when emitting, or if the thread function represented
1855  * by this Cgu::Thread::Future object threw Cgu::Thread::Exit, exited
1856  * with an uncaught exception deriving from std::exception or was
1857  * cancelled (or that function took an argument of class type by value
1858  * whose copy constructor threw such an exception or had a return
1859  * value of class type whose move assignment operator, or if none copy
1860  * assignment operator, threw such an exception), or any callback
1861  * connected to done_emitter exited with an uncaught exception. It
1862  * therefore enables errors to be detected and acted on without having
1863  * a thread wait on the get() method in order to test is_error() or
1864  * is_emitter_error().
1865  *
1866  * This method can be called before or after the run() method has been
1867  * called, and whether or not the thread function represented by this
1868  * Cgu::Thread::Future object has completed.
1869  *
1870  * The documentation for the version of this method which does not
1871  * take a Releaser object gives further details of how this method is
1872  * used.
1873  *
1874  * If glib < 2.32 is used, the glib main loop must have been made
1875  * thread-safe by a call to g_thread_init() before this function is
1876  * called. glib >= 2.32 does not require g_thread_init() to be called
1877  * in order to be thread safe.
1878  *
1879  * @param cb The 'fail' callback (the callback to be executed if the
1880  * thread function represented by this Cgu::Thread::Future object or a
1881  * done_emitter emission has failed to complete). Ownership is taken
1882  * of this object, and it will be deleted when it has been finished
1883  * with.
1884  * @param r A Releaser object for automatic disconnection of the
1885  * 'fail' callback before it executes in a main loop (mainly relevant
1886  * if the callback represents a non-static member function of an
1887  * object which may be destroyed before the callback executes).
1888  * @param context The glib main context of the thread in whose main
1889  * loop the 'fail' callback is to be executed (the default of NULL
1890  * will cause the functor to be executed in the main program loop).
1891  * @exception std::bad_alloc This method might throw std::bad_alloc if
1892  * memory is exhausted and the system throws in that case. If it does
1893  * so, the 'fail' callback will be disposed of.
1894  * @exception Cgu::Thread::MutexError This method will throw
1895  * Cgu:Thread::MutexError if initialisation of the mutex in a
1896  * SafeEmitterArg object constructed by Cgu::start_timeout() fails.
1897  * If it does so, the 'fail' callback will be disposed of. (It is
1898  * often not worth checking for this, as it means either memory is
1899  * exhausted or pthread has run out of other resources to create new
1900  * mutexes.)
1901  * @note By virtue of the Releaser object, it is in theory possible
1902  * (if memory is exhausted and the system throws in that case) that an
1903  * internal SafeEmitterArg object will throw std::bad_alloc when
1904  * emitting/executing the 'fail' callback in the glib main loop, with
1905  * the result that the relevant callback will not execute (instead the
1906  * exception will be consumed and a g_critical() warning will be
1907  * issued). This is rarely of any relevance because glib will abort
1908  * the program if it is itself unable to obtain memory from the
1909  * operating system. However, where it is relevant, design the
1910  * program so that it is not necessary to provide a releaser object.
1911  *
1912  * Since 2.0.2
1913  */
1914  void fail(const Cgu::Callback::Callback* cb,
1915  Cgu::Releaser& r,
1916  GMainContext* context = 0);
1917 
1918 /**
1919  * This is a version of the fail() utility for use in conjunction with
1920  * the when() methods, which takes a Releaser object for automatic
1921  * disconnection of the callable object passed as an argument to this
1922  * method if the 'fail' callback represents or calls into an object
1923  * which is destroyed. For this to be race free, the lifetime of the
1924  * object called into must be controlled by the thread in whose main
1925  * loop the 'fail' callback will execute.
1926  *
1927  * This method enables a callback to be executed in a glib main loop
1928  * if memory is exhausted and std::bad_alloc was thrown by the thread
1929  * wrapper of Cgu::Thread::Future after calling run() or by
1930  * done_emitter when emitting, or if the thread function represented
1931  * by this Cgu::Thread::Future object threw Cgu::Thread::Exit, exited
1932  * with an uncaught exception deriving from std::exception or was
1933  * cancelled (or that function took an argument of class type by value
1934  * whose copy constructor threw such an exception or had a return
1935  * value of class type whose move assignment operator, or if none copy
1936  * assignment operator, threw such an exception), or any callback
1937  * connected to done_emitter exited with an uncaught exception. It
1938  * therefore enables errors to be detected and acted on without having
1939  * a thread wait on the get() method in order to test is_error() or
1940  * is_emitter_error().
1941  *
1942  * This method can be called before or after the run() method has been
1943  * called, and whether or not the thread function represented by this
1944  * Cgu::Thread::Future object has completed.
1945  *
1946  * The documentation for the version of this method which does not
1947  * take a Releaser object gives further details of how this method is
1948  * used.
1949  *
1950  * If glib < 2.32 is used, the glib main loop must have been made
1951  * thread-safe by a call to g_thread_init() before this function is
1952  * called. glib >= 2.32 does not require g_thread_init() to be called
1953  * in order to be thread safe.
1954 
1955  * @param f A callable object (such as formed by a lambda expression
1956  * or the result of std::bind) representing the 'fail' callback (the
1957  * callback to be executed if the thread function represented by this
1958  * Cgu::Thread::Future object or a done_emitter emission has failed to
1959  * complete). The callable object should be fully bound (it should
1960  * take no arguments when called).
1961  * @param r A Releaser object for automatic disconnection of the
1962  * 'fail' callback before it executes in a main loop (mainly relevant
1963  * if the callback represents or calls into a non-static member
1964  * function of an object which may be destroyed before the callback
1965  * executes).
1966  * @param context The glib main context of the thread in whose main
1967  * loop the 'fail' callback is to be executed (the default of NULL
1968  * will cause the functor to be executed in the main program loop).
1969  * @exception std::bad_alloc This method might throw std::bad_alloc if
1970  * memory is exhausted and the system throws in that case.
1971  * @exception Cgu::Thread::MutexError This method will throw
1972  * Cgu:Thread::MutexError if initialisation of the mutex in a
1973  * SafeEmitterArg object constructed by Cgu::start_timeout() fails.
1974  * (It is often not worth checking for this, as it means either memory
1975  * is exhausted or pthread has run out of other resources to create
1976  * new mutexes.)
1977  * @note 1. This method will also throw if the copy or move
1978  * constructor of the 'fail' callable object throws.
1979  * @note 2. By virtue of the Releaser object, it is in theory possible
1980  * (if memory is exhausted and the system throws in that case) that an
1981  * internal SafeEmitterArg object will throw std::bad_alloc when
1982  * emitting/executing the 'fail' callback in the glib main loop, with
1983  * the result that the relevant callback will not execute (instead the
1984  * exception will be consumed and a g_critical() warning will be
1985  * issued). This is rarely of any relevance because glib will abort
1986  * the program if it is itself unable to obtain memory from the
1987  * operating system. However, where it is relevant, design the
1988  * program so that it is not necessary to provide a releaser object.
1989  *
1990  * Since 2.1.0
1991  */
1992  // we need to use enable_if so that where this function is passed a
1993  // pointer to non-const Callback::Callback, or some other
1994  // convertible pointer, this templated overload is dropped from the
1995  // overload set, in order to support the Callback::Callback
1996  // overloads of this function. This overload calls into the version
1997  // of this function taking a pointer to const Callback::Callback in
1998  // order to perform type erasure.
1999  template <class Fail,
2000  class = typename std::enable_if<!std::is_convertible<typename std::remove_reference<Fail>::type,
2001  const Cgu::Callback::Callback*>::value>::type>
2002  void fail(Fail&& f,
2003  Cgu::Releaser& r,
2004  GMainContext* context = 0) {
2005  fail(Callback::lambda<>(std::forward<Fail>(f)),
2006  r,
2007  context);
2008  }
2009 
2010 /**
2011  * @return true if the function or callable object represented by this
2012  * Cgu::Thread::Future object has finished, either by returning
2013  * normally, by cancellation or by virtue of having thrown
2014  * Cgu::Thread::Exit or some exception derived from std::exception.
2015  * Once this method returns true, then it is guaranteed that the get()
2016  * or move_get() method will not block (except as incidental to any
2017  * contention between threads calling get()). Once this method has
2018  * returned true or get() or move_get() has unblocked, then the result
2019  * of is_error() is definitive. This method is thread safe and may be
2020  * called by any thread. It will not throw.
2021  * @note This method will return true even though any callbacks
2022  * connected to done_emitter are still executing or waiting to
2023  * execute. From version 2.0.2 the is_emitter_done() method will
2024  * indicate when done_emitter callbacks (if any) have also completed.
2025  */
2026  bool is_done() const noexcept;
2027 
2028 /**
2029  * @return true if both the function or callable object represented by
2030  * this Cgu::Thread::Future object has finished and any callbacks
2031  * connected to done_emitter have completed. Once this method returns
2032  * true, then the result of is_emitter_error() is definitive. This
2033  * method is thread safe and may be called by any thread. It will not
2034  * throw.
2035  * @note This method will return true automatically if is_error() and
2036  * is_done() return true, because if the function or callable object
2037  * represented by this Cgu::Thread::Future object was cancelled or
2038  * exited with an uncaught exception, done_emitter is never emitted.
2039  * In addition, if this method returns true, then is_done() must also
2040  * return true.
2041  *
2042  * Since 2.0.2
2043  */
2044  bool is_emitter_done() const noexcept;
2045 
2046 /**
2047  * @return true if (a) a Cgu::Thread::Exit exception has been thrown
2048  * by the function or callable object represented by this
2049  * Cgu::Thread::Future object (which will have been consumed by this
2050  * Cgu::Thread::Future object), (b) an exception derived from
2051  * std::exception has been thrown on invoking that function or object
2052  * which was not caught by it (which will have been consumed by this
2053  * Cgu::Thread::Future object), (c) any of those exceptions have been
2054  * thrown either by the copy constructor of an argument taken by value
2055  * by that function or object, or by the move assignment operator (or
2056  * if none, copy assignment operator) of the return value of that
2057  * function or object (which will have been consumed by this
2058  * Cgu::Thread::Future object), (d) the worker thread in which that
2059  * function or callable object executes was cancelled in mid-course
2060  * with a call to cancel() or (e) the thread wrapper implementing the
2061  * worker thread in this Cgu::Thread::Future object threw and then
2062  * consumed std::bad_alloc (this is different from the run() method
2063  * throwing std::bad_alloc). In these cases the value obtained by
2064  * get() or move_get() will not be valid (it will be a default
2065  * constructed object of the return type of the function represented
2066  * by this Cgu::Thread::Future object). Otherwise this method returns
2067  * false. The result of this method is definitive once get() or
2068  * move_get() has unblocked or is_done() returns true. This method is
2069  * thread safe and may be called by any thread. It will not throw.
2070  */
2071  bool is_error() const noexcept;
2072 
2073 /**
2074  * @return true if an uncaught exception arose in emitting @ref
2075  * DoneEmitterAnchor "done_emitter" when executing callbacks connected
2076  * to it. Otherwise this method returns false. The result of this
2077  * method is definitive once is_emitter_done() returns true. This
2078  * method is thread safe and may be called by any thread. It will not
2079  * throw.
2080  * @note This method will return false automatically if is_error()
2081  * returns true, because if the function or callable object
2082  * represented by this Cgu::Thread::Future object was cancelled or
2083  * exited with an uncaught exception, done_emitter is never emitted.
2084  * It follows that if this method returns true, is_error() must return
2085  * false.
2086  */
2087  bool is_emitter_error() const noexcept;
2088 
2089 /**
2090  * A Cgu::SafeEmitter object which is emitted when the function or
2091  * callable object represented by this Cgu::Thread::Future object
2092  * finishes correctly (that is, it is not cancelled and does not throw
2093  * any uncaught exceptions). By itself this emission does not do too
2094  * much as it is emitted (and connected callbacks execute in) the same
2095  * worker thread immediately after the Future function has completed.
2096  * However, any thread can connect a callback object to this
2097  * Cgu::SafeEmitter object and a connected callback can, say, cause
2098  * another callback to be executed in a thread's main loop using
2099  * Cgu::Callback::post(), and from version 2.0.2 when() methods are
2100  * provided which will do this for users automatically. Once the
2101  * run() method has been called, this Cgu::Thread::Future object (and
2102  * so done_emitter) will always stay in existence until the function
2103  * or callable object represented by it has completed (whether
2104  * correctly, by cancellation or by a thrown exception) and any
2105  * callbacks connected to the done_emitter object have completed,
2106  * irrespective of whether the intrusive pointer returned by the
2107  * make() or make_future() functions has gone out of scope.
2108  * @note 1. Cancellation is blocked while the Cgu::SafeEmitter object
2109  * emits and any connected callback executes.
2110  * @note 2. A connected callback can however terminate the worker
2111  * thread by throwing Cgu::Thread::Exit (in which case no subsequent
2112  * callbacks to be executed on that emission will execute either: the
2113  * worker thread will safely terminate and unwind the stack in so
2114  * doing). In that event, the emitter_error flag will be set.
2115  * @note 3. All other uncaught exceptions which might be thrown by the
2116  * Cgu::SafeEmitter object emitting, or by a connected callback
2117  * function executing, are consumed to retain the integrity of the
2118  * Thread::Future object. In the event of such an exception being
2119  * thrown, the emitter_error flag will be set. In summary, the
2120  * emitter_error flag will be set if (a) a connected callback function
2121  * throws Cgu::Thread::Exit, (b) some other uncaught exception escapes
2122  * from a connected callback function or (c) Cgu::SafeEmitter::emit()
2123  * throws std::bad_alloc or the copy constructor of a bound argument
2124  * which is not a reference argument has thrown. If the user knows
2125  * that the callback function does not throw Cgu::Thread::Exit and
2126  * does not allow any other exception to escape, then the cause must
2127  * be a std::bad_alloc memory exception in Cgu::SafeEmitter::emit() or
2128  * the copy constructor of a non-reference bound argument throwing.
2129  * @note 4. An emission is thread safe if the connected callback
2130  * functions are thread safe.
2131  * @note 5. This Cgu::Thread::Future object's mutex is released while
2132  * the Cgu::SafeEmitter object emits. This means that any connected
2133  * callbacks can safely call, say, the Future object's get() or
2134  * is_error() methods. However, a connected callback should not hold
2135  * a bound argument comprising a copy of this Cgu::Thread::Future
2136  * object held by intrusive pointer as returned by the make() or
2137  * make_future() methods (that would result in this
2138  * Cgu::Thread::Future object owning, via done_emitter, a reference to
2139  * itself and so become incapable of being freed). The callback may,
2140  * however, take a pointer to this Cgu::Thread::Future object as a
2141  * bound argument, as obtained by the Cgu::IntrusivePtr::get() method,
2142  * because this Cgu::Thread::Future object is guaranteed to remain in
2143  * existence until all callbacks connected to done_emitter have
2144  * completed executing.
2145  * @anchor DoneEmitterAnchor
2146  */
2148 
2149 /* Only has effect if --with-glib-memory-slices-compat or
2150  * --with-glib-memory-slices-no-compat option picked */
2152 };
2153 
2154 /**
2155  * @deprecated
2156  *
2157  * DEPRECATED. Use the version of make_future() which takes a
2158  * callable object.
2159  *
2160  * A convenience helper function which calls
2161  * Cgu::Thread::Future::make() to obtain a Future object without the
2162  * need to specify the return value of the function represented by the
2163  * new object: that is deduced from the signature of that function.
2164  * This is useful shorthand when also employed with the C++11/14
2165  * 'auto' keyword.
2166  * @exception std::bad_alloc It might throw std::bad_alloc if memory
2167  * is exhausted and the system throws in that case. (This exception
2168  * will not be thrown if the library has been installed using the
2169  * \--with-glib-memory-slices-no-compat configuration option: instead
2170  * glib will terminate the program if it is unable to obtain memory
2171  * from the operating system.)
2172  * @exception Cgu::Thread::MutexError It might throw
2173  * Cgu::Thread::MutexError if initialisation of the contained mutex
2174  * fails. (It is often not worth checking for this, as it means
2175  * either memory is exhausted or pthread has run out of other
2176  * resources to create new mutexes.)
2177  * @exception Cgu::Thread::CondError It might throw
2178  * Cgu::Thread::CondError if initialisation of the contained condition
2179  * variable fails. (It is often not worth checking for this, as it
2180  * means either memory is exhausted or pthread has run out of other
2181  * resources to create new condition variables.)
2182  * @note This method will also throw if the copy or move constructor
2183  * of a bound argument throws, or the default constructor of the
2184  * return value type of the function represented by the new object
2185  * throws.
2186 
2187  *
2188  * Since 2.0.4
2189  */
2190 template <class Obj, class Ret, class... Params, class... Args>
2192  Ret (Obj::*func)(Params...),
2193  Args&&... args) {
2194  return Cgu::Thread::Future<Ret>::make(obj, func, std::forward<Args>(args)...);
2195 }
2196 
2197 /**
2198  * @deprecated
2199  *
2200  * DEPRECATED. Use the version of make_future() which takes a
2201  * callable object.
2202  *
2203  * A convenience helper function which calls
2204  * Cgu::Thread::Future::make() to obtain a Future object without the
2205  * need to specify the return value of the function represented by the
2206  * new object: that is deduced from the signature of that function.
2207  * This is useful shorthand when also employed with the C++11/14
2208  * 'auto' keyword.
2209  * @exception std::bad_alloc It might throw std::bad_alloc if memory
2210  * is exhausted and the system throws in that case. (This exception
2211  * will not be thrown if the library has been installed using the
2212  * \--with-glib-memory-slices-no-compat configuration option: instead
2213  * glib will terminate the program if it is unable to obtain memory
2214  * from the operating system.)
2215  * @exception Cgu::Thread::MutexError It might throw
2216  * Cgu::Thread::MutexError if initialisation of the contained mutex
2217  * fails. (It is often not worth checking for this, as it means
2218  * either memory is exhausted or pthread has run out of other
2219  * resources to create new mutexes.)
2220  * @exception Cgu::Thread::CondError It might throw
2221  * Cgu::Thread::CondError if initialisation of the contained condition
2222  * variable fails. (It is often not worth checking for this, as it
2223  * means either memory is exhausted or pthread has run out of other
2224  * resources to create new condition variables.)
2225  * @note This method will also throw if the copy or move constructor
2226  * of a bound argument throws, or the default constructor of the
2227  * return value type of the function represented by the new object
2228  * throws.
2229  *
2230  * Since 2.0.4
2231  */
2232 template <class Obj, class Ret, class... Params, class... Args>
2234  Ret (Obj::*func)(Params...) const,
2235  Args&&... args) {
2236  return Cgu::Thread::Future<Ret>::make(obj, func, std::forward<Args>(args)...);
2237 }
2238 
2239 /**
2240  * @deprecated
2241  *
2242  * DEPRECATED. Use the version of make_future() which takes a
2243  * callable object.
2244  *
2245  * A convenience helper function which calls
2246  * Cgu::Thread::Future::make() to obtain a Future object without the
2247  * need to specify the return value of the function represented by the
2248  * new object: that is deduced from the signature of that function.
2249  * This is useful shorthand when also employed with the C++11/14
2250  * 'auto' keyword.
2251  * @exception std::bad_alloc It might throw std::bad_alloc if memory
2252  * is exhausted and the system throws in that case. (This exception
2253  * will not be thrown if the library has been installed using the
2254  * \--with-glib-memory-slices-no-compat configuration option: instead
2255  * glib will terminate the program if it is unable to obtain memory
2256  * from the operating system.)
2257  * @exception Cgu::Thread::MutexError It might throw
2258  * Cgu::Thread::MutexError if initialisation of the contained mutex
2259  * fails. (It is often not worth checking for this, as it means
2260  * either memory is exhausted or pthread has run out of other
2261  * resources to create new mutexes.)
2262  * @exception Cgu::Thread::CondError It might throw
2263  * Cgu::Thread::CondError if initialisation of the contained condition
2264  * variable fails. (It is often not worth checking for this, as it
2265  * means either memory is exhausted or pthread has run out of other
2266  * resources to create new condition variables.)
2267  * @note This method will also throw if the copy or move constructor
2268  * of a bound argument throws, or the default constructor of the
2269  * return value type of the function represented by the new object
2270  * throws.
2271  *
2272  * Since 2.0.4
2273  */
2274 template <class Ret, class... Params, class... Args>
2276  Args&&... args) {
2277  return Cgu::Thread::Future<Ret>::make(func, std::forward<Args>(args)...);
2278 }
2279 
2280 /**
2281  * A convenience helper function which calls
2282  * Cgu::Thread::Future::make() to obtain a Future without the need to
2283  * specify the return value of the callable object to be represented
2284  * by it: that is deduced. This is useful shorthand when also
2285  * employed with the C++11/14 'auto' keyword.
2286  *
2287  * @param func A callable object, such as formed by a lambda
2288  * expression or the result of std::bind. It must be fully bound
2289  * (that is, its must take no arguments when called). It should
2290  * return a value (it cannot return void).
2291  * @exception std::bad_alloc It might throw std::bad_alloc if memory
2292  * is exhausted and the system throws in that case. (This exception
2293  * will not be thrown if the library has been installed using the
2294  * \--with-glib-memory-slices-no-compat configuration option: instead
2295  * glib will terminate the program if it is unable to obtain memory
2296  * from the operating system.)
2297  * @exception Cgu::Thread::MutexError It might throw
2298  * Cgu::Thread::MutexError if initialisation of the contained mutex
2299  * fails. (It is often not worth checking for this, as it means
2300  * either memory is exhausted or pthread has run out of other
2301  * resources to create new mutexes.)
2302  * @exception Cgu::Thread::CondError It might throw
2303  * Cgu::Thread::CondError if initialisation of the contained condition
2304  * variable fails. (It is often not worth checking for this, as it
2305  * means either memory is exhausted or pthread has run out of other
2306  * resources to create new condition variables.)
2307  * @note 1. This method will also throw if the copy or move
2308  * constructor of the callable object passed as an argument throws, or
2309  * the default constructor of the return value type of the function
2310  * represented by the new object throws.
2311  * @note 2. If the callable object passed as an argument has both
2312  * const and non-const operator()() methods, the non-const version
2313  * will be called even if the callable object passed is a const
2314  * object.
2315  *
2316  * Since 2.0.14
2317  */
2318 // we don't need this version of make_future() for syntactic reasons -
2319 // the version taking a single template parameter will do by itself
2320 // syntactically because it can use decltype. However, we include
2321 // this version in order to be API compatible with c++-gtk-utils <
2322 // 2.0.14, which required the return type to be specified when this
2323 // method is passed something other than a std::function object.
2324 // SFINAE will take care of the rest, except with a corner case where
2325 // all of the following apply: (i) a function object is passed whose
2326 // operator()() method returns a copy of the function object (or
2327 // another function object of the same type), (ii) the function object
2328 // is passed to this method as a rvalue and not a lvalue, and (iii)
2329 // the user specifically states the return type when instantiating
2330 // this template function. This would give rise to an ambiguity, but
2331 // its happening is extremely unlikely, and cannot happen with a
2332 // lambda or the return value of std::bind, because those types are
2333 // only known to the compiler, and cannot happen with other objects if
2334 // the user lets template deduction take its course.
2335 template <class Ret, class Func>
2337  return Cgu::Thread::Future<Ret>::make(std::forward<Func>(func));
2338 }
2339 
2340 // we don't want to document this function: it provides the type
2341 // deduction of the return value of the passed functor (it deals with
2342 // cases where this is not specified expressly).
2343 #ifndef DOXYGEN_PARSING
2344 template <class Func>
2346  // this function will fail to compile if the return type is a
2347  // reference type: that is a feature, not a bug, as a function
2348  // returning a reference lacks referential transparency, is unlikely
2349  // to be thread-safe and is unsuitable for use as a task function
2350  return Cgu::Thread::Future<decltype(func())>::make(std::forward<Func>(func));
2351 }
2352 #endif
2353 
2354 } // namespace Thread
2355 
2356 } // namespace Cgu
2357 
2358 #include <c++-gtk-utils/future.tpp>
2359 
2360 #endif
Cgu::Callback::SafeFunctor when(When &&w, gint priority=G_PRIORITY_DEFAULT, GMainContext *context=0)
Definition: future.h:1367
SafeEmitter done_emitter
Definition: future.h:2147
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:2191
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:1621
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:2002
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:1836
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:778
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