c++-gtk-utils
timeout.h
Go to the documentation of this file.
1 /* Copyright (C) 2009 and 2014 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 c++-gtk-utils
20  sub-directory); if not, write to the Free Software Foundation, Inc.,
21  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 
23 */
24 
25 #ifndef CGU_TIMEOUT_H
26 #define CGU_TIMEOUT_H
27 
28 /**
29  * @defgroup timeout timeout
30  *
31  * \#include <c++-gtk-utils/timeout.h>
32  *
33  * The start_timeout() function connects a timeout to an event loop
34  * owned by a GMainContext object (normally the main program loop).
35  * By so doing, it provides a convenient way of attaching the callback
36  * for the timeout, and also provides for automatic disconnection when
37  * an object whose function the callback represents is destroyed. The
38  * timeout will keep executing the callback at the intervals set in
39  * start_timeout() until it is terminated in one of the ways mentioned
40  * below.
41  *
42  * start_timeout() is thread-safe (it may be called in any thread)
43  * provided that, if glib < 2.32 is used, the glib main loop has been
44  * made thread-safe by a call to g_thread_init(). glib >= 2.32 does
45  * not require g_thread_init() to be called in order to be
46  * thread-safe.
47  *
48  * start_timeout() takes ownership of the passed Callback object. The
49  * function comes in two versions. The one which takes a
50  * Callback::Releaser object as its third argument provides for
51  * automatic termination of the execution of the callback at the
52  * specified interval if the target object which has the Releaser as a
53  * member is destroyed. (Note that for this to be race free, the
54  * lifetime of the remote target object whose method is to be invoked
55  * must be determined by the thread to whose main loop the timeout has
56  * been attached. When the main loop begins invoking the execution of
57  * the timeout callback, the remote object must either wholly exist,
58  * in which case the callback will be invoked, or have been destroyed,
59  * in which case the callback will be ignored, and not be in some
60  * transient half-state governed by another thread.)
61  *
62  * The connected function encapsulated by the callback passed to
63  * start_timeout() and executed by the main loop should take a single
64  * bool& argument, or have its last argument as a bool& unbound
65  * argument (with all other arguments bound in the callback). If that
66  * bool& argument is set by the connected function to false, then the
67  * timeout calls will be ended and all resources connected with it
68  * deleted without further user action being required (there is no
69  * need for the connected function to set it to true if timeout
70  * execution is to continue, as that is the default). In addition,
71  * the timeout will be ended automatically and resources deleted if
72  * (i) as mentioned above, the callback passed to start_timeout() is
73  * protected by a Releaser object and the target object whose method
74  * is encapsulated by the callback is destroyed, or (ii)
75  * g_source_remove() is called on the source id returned by
76  * start_timeout() (where the timeout is attached to the default main
77  * context) or g_source_destroy() is called on the GSource object
78  * obtained from that id with g_main_context_find_source_by_id()
79  * (where the timeout has been attached to a non-default main
80  * context). If the source has been removed automatically by virtue
81  * of the bool& argument being set to false or by virtue of a Releaser
82  * object releasing, g_source_remove() or g_source_destroy() should
83  * not afterwards be called in respect of the id value returned by
84  * start_timeout() in case it has been reused by the main context
85  * concerned in the meantime.
86  *
87  * The start_timeout_seconds() functions do the same as their
88  * start_timeout() counterparts, except that they use the larger
89  * granularity glib timeout-seconds main loop event sources (and take
90  * seconds and not milliseconds as their timeout argument). The idea
91  * behind the glib timeout-seconds sources is to group long timeout
92  * events which do not have critical timing resolution requirements so
93  * that they are aligned together with one second granularity. This
94  * minimises the number of processor wake-ups required to handle such
95  * events, thereby helping power efficiency. These functions are to
96  * be preferred for long timeouts where one second granularity is
97  * acceptable. These larger granularity functions are only compiled
98  * into the library if glib >= 2.14 is installed.
99  */
100 
101 #include <glib.h>
102 #include <c++-gtk-utils/callback.h>
104 
105 namespace Cgu {
106 
107 class Releaser;
108 
109 /**
110  * Starts a timeout in the glib main loop, and executes the callback
111  * when the timeout expires. It is thread-safe (it may be called in
112  * any thread) provided that, if glib < 2.32 is used, g_thread_init()
113  * has been called. glib >= 2.32 does not require g_thread_init() to
114  * be called to be thread-safe. This function will not throw.
115  * @param millisec The interval of the timeout, in milliseconds.
116  * @param cb The callback object. Ownership is taken of this object,
117  * and it will be deleted when it has been finished with.
118  * @param priority The priority to be given to the timeout in the
119  * main loop. In ascending order of priorities, priorities are
120  * G_PRIORITY_LOW, G_PRIORITY_DEFAULT_IDLE, G_PRIORITY_HIGH_IDLE,
121  * G_PRIORITY_DEFAULT and G_PRIORITY_HIGH. The default is
122  * G_PRIORITY_DEFAULT. This determines the order in which the
123  * callback will appear in the event list in the main loop, not the
124  * priority which the OS will adopt
125  * @param context The glib main context to which the timeout is to be
126  * attached (the default of NULL will cause the timeout to be attached
127  * to the main program loop, and this is almost always what is
128  * wanted).
129  * @return The glib source id of the timeout.
130  * @note 1. Cancellation of the thread to which the timeout is
131  * attached is blocked during execution of the callback.
132  * @note 2. If the callback throws an exception, the exception will be
133  * consumed to protect the main loop and a g_critical() warning will
134  * be issued.
135  * @ingroup timeout
136  *
137  * Since 0.9.2
138  */
139 guint start_timeout(guint millisec, const Callback::CallbackArg<bool&>* cb,
140  gint priority = G_PRIORITY_DEFAULT, GMainContext* context = 0);
141 
142 /**
143  * Starts a timeout in the glib main loop, and executes the callback
144  * when the timeout expires. This version provides for automatic
145  * timeout disconnection when the object whose function the callback
146  * represents is destroyed, via the Releaser object. It is
147  * thread-safe (it may be called in any thread) provided that, if glib
148  * < 2.32 is used, g_thread_init() has been called. glib >= 2.32 does
149  * not require g_thread_init() to be called to be thread-safe.
150  * @param millisec The interval of the timeout, in milliseconds.
151  * @param cb The callback object. Ownership is taken of this object,
152  * and it will be deleted when it has been finished with.
153  * @param r A Releaser object which the protected object has as a
154  * public member.
155  * @param priority The priority to be given to the timeout in the
156  * main loop. In ascending order of priorities, priorities are
157  * G_PRIORITY_LOW, G_PRIORITY_DEFAULT_IDLE, G_PRIORITY_HIGH_IDLE,
158  * G_PRIORITY_DEFAULT and G_PRIORITY_HIGH. The default is
159  * G_PRIORITY_DEFAULT. This determines the order in which the
160  * callback will appear in the event list in the main loop, not the
161  * priority which the OS will adopt
162  * @param context The glib main context to which the timeout is to be
163  * attached (the default of NULL will cause the timeout to be attached
164  * to the main program loop, and this is almost always what is
165  * wanted).
166  * @return The glib source id of the timeout.
167  * @exception std::bad_alloc This function might throw std::bad_alloc
168  * if memory is exhausted and the system throws in that case. If it
169  * does so, the CallbackArg object will be disposed of.
170  * @exception Cgu::Thread::MutexError This function might throw
171  * Cgu:Thread::MutexError if initialisation of the mutex in a
172  * SafeEmitterArg object constructed by this function fails. If it
173  * does so, the CallbackArg object will be disposed of. (It is often
174  * not worth checking for this exception, as it means either memory is
175  * exhausted or pthread has run out of other resources to create new
176  * mutexes.)
177  * @note 1. Cancellation of the thread to which the timeout is
178  * attached is blocked during execution of the callback.
179  * @note 2. If the callback throws an exception, the exception will be
180  * consumed to protect the main loop and a g_critical() warning will
181  * be issued.
182  * @note 3. By virtue of the Releaser object, it is in theory possible
183  * (if memory is exhausted and the system throws in that case) that an
184  * internal SafeEmitterArg object will throw std::bad_alloc when
185  * emitting/executing the timeout callback in the glib main loop, with
186  * the result that the relevant callback will not execute (instead the
187  * exception will be consumed and a g_critical() warning will be
188  * issued) and thus will be delayed until expiry of the next timeout
189  * interval. This is rarely of any relevance because glib will abort
190  * the program if it is itself unable to obtain memory from the
191  * operating system. However, where it is relevant, design the
192  * program so that it is not necessary to provide a releaser object.
193  * @ingroup timeout
194  *
195  * Since 0.9.2
196  */
197 guint start_timeout(guint millisec, const Callback::CallbackArg<bool&>* cb,
198  Releaser& r, gint priority = G_PRIORITY_DEFAULT,
199  GMainContext* context = 0);
200 
201 /**
202  * Starts a timeout in the glib main loop using the higher granularity
203  * glib timeout-seconds event sources, and executes the callback when
204  * the timeout expires. It is thread-safe (it may be called in any
205  * thread) provided that, if glib < 2.32 is used, g_thread_init() has
206  * been called. glib >= 2.32 does not require g_thread_init() to be
207  * called to be thread-safe. This function will not throw.
208  * @param sec The interval of the timeout, in seconds.
209  * @param cb The callback object. Ownership is taken of this object,
210  * and it will be deleted when it has been finished with.
211  * @param priority The priority to be given to the timeout in the
212  * main loop. In ascending order of priorities, priorities are
213  * G_PRIORITY_LOW, G_PRIORITY_DEFAULT_IDLE, G_PRIORITY_HIGH_IDLE,
214  * G_PRIORITY_DEFAULT and G_PRIORITY_HIGH. The default is
215  * G_PRIORITY_DEFAULT. This determines the order in which the
216  * callback will appear in the event list in the main loop, not the
217  * priority which the OS will adopt
218  * @param context The glib main context to which the timeout is to be
219  * attached (the default of NULL will cause the timeout to be attached
220  * to the main program loop, and this is almost always what is
221  * wanted).
222  * @return The glib source id of the timeout.
223  * @note 1. Cancellation of the thread to which the timeout is
224  * attached is blocked during execution of the callback.
225  * @note 2. If the callback throws an exception, the exception will be
226  * consumed to protect the main loop and a g_critical() warning will
227  * be issued.
228  * @note 3. This function is only compiled into the library if glib >=
229  * 2.14 is installed.
230  * @ingroup timeout
231  *
232  * Since 1.2.1
233  */
234 guint start_timeout_seconds(guint sec, const Callback::CallbackArg<bool&>* cb,
235  gint priority = G_PRIORITY_DEFAULT, GMainContext* context = 0);
236 
237 /**
238  * Starts a timeout in the glib main loop using the higher granularity
239  * glib timeout-seconds event sources, and executes the callback when
240  * the timeout expires. This version provides for automatic timeout
241  * disconnection when the object whose function the callback
242  * represents is destroyed, via the Releaser object. It is
243  * thread-safe (it may be called in any thread) provided that, if glib
244  * < 2.32 is used, g_thread_init() has been called. glib >= 2.32 does
245  * not require g_thread_init() to be called to be thread-safe.
246  * @param sec The interval of the timeout, in seconds.
247  * @param cb The callback object. Ownership is taken of this object,
248  * and it will be deleted when it has been finished with.
249  * @param r A Releaser object which the protected object has as a
250  * public member.
251  * @param priority The priority to be given to the timeout in the
252  * main loop. In ascending order of priorities, priorities are
253  * G_PRIORITY_LOW, G_PRIORITY_DEFAULT_IDLE, G_PRIORITY_HIGH_IDLE,
254  * G_PRIORITY_DEFAULT and G_PRIORITY_HIGH. The default is
255  * G_PRIORITY_DEFAULT. This determines the order in which the
256  * callback will appear in the event list in the main loop, not the
257  * priority which the OS will adopt
258  * @param context The glib main context to which the timeout is to be
259  * attached (the default of NULL will cause the timeout to be attached
260  * to the main program loop, and this is almost always what is
261  * wanted).
262  * @return The glib source id of the timeout.
263  * @exception std::bad_alloc This function might throw std::bad_alloc
264  * if memory is exhausted and the system throws in that case. If it
265  * does so, the CallbackArg object will be disposed of.
266  * @exception Cgu::Thread::MutexError This function might throw
267  * Cgu:Thread::MutexError if initialisation of the mutex in a
268  * SafeEmitterArg object constructed by this function fails. If it
269  * does so, the CallbackArg object will be disposed of. (It is often
270  * not worth checking for this exception, as it means either memory is
271  * exhausted or pthread has run out of other resources to create new
272  * mutexes.)
273  * @note 1. Cancellation of the thread to which the timeout is
274  * attached is blocked during execution of the callback.
275  * @note 2. If the callback throws an exception, the exception will be
276  * consumed to protect the main loop and a g_critical() warning will
277  * be issued.
278  * @note 3. This function is only compiled into the library if glib >=
279  * 2.14 is installed.
280  * @note 4. By virtue of the Releaser object, it is in theory possible
281  * (if memory is exhausted and the system throws in that case) that an
282  * internal SafeEmitterArg object will throw std::bad_alloc when
283  * emitting/executing the timeout callback in the glib main loop, with
284  * the result that the relevant callback will not execute (instead the
285  * exception will be consumed and a g_critical() warning will be
286  * issued) and thus will be delayed until expiry of the next timeout
287  * interval. This is rarely of any relevance because glib will abort
288  * the program if it is itself unable to obtain memory from the
289  * operating system. However, where it is relevant, design the
290  * program so that it is not necessary to provide a releaser object.
291  * @ingroup timeout
292  *
293  * Since 1.2.1
294  */
295 guint start_timeout_seconds(guint sec, const Callback::CallbackArg<bool&>* cb,
296  Releaser& r, gint priority = G_PRIORITY_DEFAULT,
297  GMainContext* context = 0);
298 
299 } // namespace Cgu
300 
301 #endif
Cgu
Definition: application.h:45
Cgu::start_timeout_seconds
guint start_timeout_seconds(guint sec, const Callback::CallbackArg< bool & > *cb, gint priority=G_PRIORITY_DEFAULT, GMainContext *context=0)
callback.h
This file provides classes encapsulating callbacks.
Cgu::start_timeout
guint start_timeout(guint millisec, const Callback::CallbackArg< bool & > *cb, gint priority=G_PRIORITY_DEFAULT, GMainContext *context=0)
cgu_config.h