c++-gtk-utils
io_watch.h
Go to the documentation of this file.
1 /* Copyright (C) 2005 to 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_IO_WATCH_H
26 #define CGU_IO_WATCH_H
27 
28 /**
29  */
30 
31 /**
32  * @defgroup io_watch io_watch
33  *
34  * \#include <c++-gtk-utils/io_watch.h>
35  *
36  * The start_iowatch() function connects a Unix file descriptor to an
37  * event loop owned by a GMainContext object (normally the main
38  * program loop). It both saves the overhead of having to construct a
39  * GIOChannel object where the only thing wanted is to execute a
40  * callback when there is something to be read from a pipe, fifo or
41  * socket or a pipe or fifo can be written to, and it also provides
42  * for automatic disconnection when an object whose function the
43  * callback represents is destroyed.
44  *
45  * For the GIOCondition argument of start_iowatch(), G_IO_IN can be
46  * bitwise-ORed with G_IO_HUP, and should be if the callback has the
47  * task of cleaning up if EOF is reached (see
48  * http://www.greenend.org.uk/rjk/2001/06/poll.html ), which is
49  * detected by read() returning 0. A cast will be required to do this
50  * for the third argument of start_iowatch() (that is, pass
51  * GIOCondition(G_IO_IN | G_IO_HUP)). In addition, G_IO_IN and
52  * G_IO_OUT can be bitwise-ORed with G_IO_ERR (and passed as
53  * GIOCondition(G_IO_IN | G_IO_HUP | G_IO_ERR) or
54  * GIOCondition(G_IO_OUT | G_IO_ERR)), which would be detected in the
55  * callback by read() or write() returning -1.
56  *
57  * Two of the four overloaded start_iowatch() functions also take a
58  * Callback object having a GIOCondition type and bool& type as their
59  * unbound arguments (the other two overloads only have callbacks
60  * taking a single bool& unbound argument). The overloads taking a
61  * Callback object with an unbound GIOCondition argument type are,
62  * when the callback function is executed, passed a GIOCondition value
63  * to that argument, representing the bitwise-ORed events which caused
64  * the call: this enables a single watch to be provided for both
65  * reading and writing (if the file descriptor has been opened for
66  * reading and writing), by testing for G_IO_IN and G_IO_OUT on that
67  * argument in the callback function. It also enables, by testing for
68  * G_IO_HUP and G_IO_ERR, a hang-up or error condition to be detected
69  * without having to inspect the return value of read() or write()
70  * (but note the test results referred to in
71  * http://www.greenend.org.uk/rjk/2001/06/poll.html , which show that
72  * the ending of a connection can only reliably be determined by
73  * testing whether read() returns 0, or whether the iostream wrapper
74  * on top of it reports end of file after attempting a read.)
75  *
76  * start_iowatch() is thread-safe (it may be called in any thread)
77  * provided that, if glib < 2.32 is used, the glib main loop has been
78  * made thread-safe by a call to g_thread_init(). glib >= 2.32 does
79  * not require g_thread_init() to be called in order to be
80  * thread-safe.
81  *
82  * start_iowatch() takes ownership of the passed Callback object. Two
83  * of the four start_iowatch() overloads take a Callback::Releaser
84  * object as their third argument, which provides for automatic
85  * ceasing of the watch if the target object which has the Releaser as
86  * a member is destroyed. (Note that for this to be race free, the
87  * lifetime of the remote target object whose method is to be invoked
88  * must be determined by the thread to whose main loop the watch has
89  * been attached. When the main loop begins invoking the execution of
90  * the watch callback, the remote object must either wholly exist, in
91  * which case the callback will be invoked, or have been destroyed, in
92  * which case the callback will be ignored, and not be in some
93  * transient half-state governed by another thread.)
94  *
95  * As mentioned above, the connected function encapsulated by the
96  * callback passed to start_iowatch() must have a final unbound bool&
97  * argument. If that bool& argument is set by the connected function
98  * to false, say because end-of-file has been reached, then the watch
99  * will be ended and all resources connected with it deleted without
100  * further user action being required (there is no need for the
101  * connected function to set it to true if the watch is to continue,
102  * as that is the default). In addition, the watch will be ended
103  * automatically and resources deleted if (i) as mentioned above, the
104  * callback passed to start_iowatch() is protected by a Releaser
105  * object and the target object whose method is encapsulated by the
106  * callback is destroyed, or (ii) g_source_remove() is called on the
107  * source id returned by start_iowatch() (where the watch is attached
108  * to the default main context) or g_source_destroy() is called on the
109  * GSource object obtained from that id with
110  * g_main_context_find_source_by_id() (where the watch has been
111  * attached to a non-default main context). If the source has been
112  * removed automatically by virtue of the bool& argument being set to
113  * false or by virtue of a Releaser object releasing,
114  * g_source_remove() or g_source_destroy() should not afterwards be
115  * called in respect of the id value returned by start_iowatch() in
116  * case it has been reused by the main context concerned in the
117  * meantime.
118  */
119 
120 #include <glib.h>
121 #include <c++-gtk-utils/callback.h>
122 #include <c++-gtk-utils/param.h>
124 
125 namespace Cgu {
126 
127 class Releaser;
128 
129 /**
130  * Starts an io watch in the glib main loop on a file descriptor, and
131  * executes the callback if the condition in io_condition is met. It
132  * is thread-safe (it may be called in any thread) provided that, if
133  * glib < 2.32 is used, g_thread_init() has been called. glib >= 2.32
134  * does not require g_thread_init() to be called to be thread-safe.
135  * From version 1.2.36 of the library this function will not throw.
136  * (Prior to that, it could throw std::bad_alloc if memory was
137  * exhausted and the system threw in that case, or
138  * Cgu::Thread::MutexError if initialisation of the mutex in a
139  * SafeEmitterArg object failed, in which case the CallbackArg object
140  * would be disposed of.)
141  * @param fd The file descriptor.
142  * @param cb The callback object. Ownership is taken of this object,
143  * and it will be deleted when it has been finished with
144  * @param io_condition The condition to be watched for (G_IO_IN may be
145  * bitwise-ored with G_IO_HUP, and G_IO_IN and G_IO_OUT may be
146  * bitwise-ored with G_IO_ERR).
147  * @param priority The priority to be given to the watch in the main
148  * loop. In ascending order of priorities, priorities are
149  * G_PRIORITY_LOW, G_PRIORITY_DEFAULT_IDLE, G_PRIORITY_HIGH_IDLE,
150  * G_PRIORITY_DEFAULT and G_PRIORITY_HIGH. The default is
151  * G_PRIORITY_DEFAULT. This determines the order in which the
152  * callback will appear in the event list in the main loop, not the
153  * priority which the OS will adopt
154  * @param context The glib main context to which the watch is to be
155  * attached (the default of NULL will cause the watch to be attached
156  * to the main program loop, and this is almost always what is
157  * wanted).
158  * @return The glib source id of the watch.
159  * @note 1. Cancellation of the thread to which the watch is attached
160  * is blocked during execution of the callback.
161  * @note 2. If the callback throws an exception, the exception will be
162  * consumed to protect the main loop and a g_critical() warning will
163  * be issued.
164  * @ingroup io_watch
165  *
166  * Since 0.9.2 choice of priority available.
167  */
168 guint start_iowatch(int fd, const Callback::CallbackArg<bool&>* cb,
169  GIOCondition io_condition, gint priority = G_PRIORITY_DEFAULT,
170  GMainContext* context = 0);
171 
172 /**
173  * Starts an io watch in the glib main loop on a file descriptor, and
174  * executes the callback if the condition in io_condition is met.
175  * This version provides for automatic watch disconnection when the
176  * object whose function the callback represents is destroyed, via the
177  * Releaser object. It is thread-safe (it may be called in any
178  * thread) provided that, if glib < 2.32 is used, g_thread_init() has
179  * been called. glib >= 2.32 does not require g_thread_init() to be
180  * called to be thread-safe.
181  * @param fd The file descriptor.
182  * @param cb The callback object. Ownership is taken of this object,
183  * and it will be deleted when it has been finished with.
184  * @param r A Releaser object which the protected object has as a
185  * public member.
186  * @param io_condition The condition to be watched for (G_IO_IN may be
187  * bitwise-ored with G_IO_HUP, and G_IO_IN and G_IO_OUT may be
188  * bitwise-ored with G_IO_ERR).
189  * @param priority The priority to be given to the watch in the main
190  * loop. In ascending order of priorities, priorities are
191  * G_PRIORITY_LOW, G_PRIORITY_DEFAULT_IDLE, G_PRIORITY_HIGH_IDLE,
192  * G_PRIORITY_DEFAULT and G_PRIORITY_HIGH. The default is
193  * G_PRIORITY_DEFAULT. This determines the order in which the
194  * callback will appear in the event list in the main loop, not the
195  * priority which the OS will adopt
196  * @param context The glib main context to which the watch is to be
197  * attached (the default of NULL will cause the watch to be attached
198  * to the main program loop, and this is almost always what is
199  * wanted).
200  * @return The glib source id of the watch.
201  * @exception std::bad_alloc This function might throw std::bad_alloc
202  * if memory is exhausted and the system throws in that case. If it
203  * does so, the CallbackArg object will be disposed of.
204  * @exception Cgu::Thread::MutexError This function might throw
205  * Cgu:Thread::MutexError if initialisation of the mutex in a
206  * SafeEmitterArg object constructed by this function fails. If it
207  * does so, the CallbackArg object will be disposed of. (It is often
208  * not worth checking for this exception, as it means either memory is
209  * exhausted or pthread has run out of other resources to create new
210  * mutexes.)
211  * @note 1. Cancellation of the thread to which the watch is attached
212  * is blocked during execution of the callback.
213  * @note 2. If the callback throws an exception, the exception will be
214  * consumed to protect the main loop and a g_critical() warning will
215  * be issued.
216  * @ingroup io_watch
217  *
218  * Since 0.9.2 choice of priority available.
219  */
220 guint start_iowatch(int fd, const Callback::CallbackArg<bool&>* cb, Releaser& r,
221  GIOCondition io_condition, gint priority = G_PRIORITY_DEFAULT,
222  GMainContext* context = 0);
223 
224 /**
225  * Starts an io watch in the glib main loop on a file descriptor, and
226  * executes the callback if the condition in io_condition is met.
227  * This version provides the GIOCondition status which caused the
228  * callback to be invoked as the first unbound argument of the
229  * callback object. It is thread-safe (it may be called in any
230  * thread) provided that, if glib < 2.32 is used, g_thread_init() has
231  * been called. glib >= 2.32 does not require g_thread_init() to be
232  * called to be thread-safe. From version 1.2.36 of the library this
233  * function will not throw. (Prior to that, it could throw
234  * std::bad_alloc if memory was exhausted and the system threw in that
235  * case, or Cgu::Thread::MutexError if initialisation of the mutex in
236  * a SafeEmitterArg object failed, in which case the CallbackArg
237  * object would be disposed of.)
238  * @param fd The file descriptor.
239  * @param cb The callback object. Ownership is taken of this object,
240  * and it will be deleted when it has been finished with.
241  * @param io_condition The condition(s) to be watched for (G_IO_IN,
242  * G_IO_OUT, G_IO_HUP and G_IO_ERR may all be bitwise-ored).
243  * @param priority The priority to be given to the watch in the main
244  * loop. In ascending order of priorities, priorities are
245  * G_PRIORITY_LOW, G_PRIORITY_DEFAULT_IDLE, G_PRIORITY_HIGH_IDLE,
246  * G_PRIORITY_DEFAULT and G_PRIORITY_HIGH. The default is
247  * G_PRIORITY_DEFAULT. This determines the order in which the
248  * callback will appear in the event list in the main loop, not the
249  * priority which the OS will adopt
250  * @param context The glib main context to which the watch is to be
251  * attached (the default of NULL will cause the watch to be attached
252  * to the main program loop, and this is almost always what is
253  * wanted).
254  * @return The glib source id of the watch.
255  * @note 1. Cancellation of the thread to which the watch is attached
256  * is blocked during execution of the callback.
257  * @note 2. If the callback throws an exception, the exception will be
258  * consumed to protect the main loop and a g_critical() warning will
259  * be issued.
260  * @note 3. This version, whereby the callback function takes an
261  * unbound GIOCondition argument, is not available if the library was
262  * configured with the \--without-type-tuple-args option.
263  * @ingroup io_watch
264  *
265  * Since 1.2.12
266  */
267 guint start_iowatch(int fd, const Callback::CallbackArg<TypeTuple<GIOCondition, bool&> >* cb,
268  GIOCondition io_condition, gint priority = G_PRIORITY_DEFAULT,
269  GMainContext* context = 0);
270 
271 /**
272  * Starts an io watch in the glib main loop on a file descriptor, and
273  * executes the callback if the condition in io_condition is met.
274  * This version provides both automatic watch disconnection when the
275  * object whose function the callback represents is destroyed, via the
276  * Releaser object, and provides the GIOCondition status which caused
277  * the callback to be invoked as the first unbound argument of the
278  * callback object. It is thread-safe (it may be called in any
279  * thread) provided that, if glib < 2.32 is used, g_thread_init() has
280  * been called. glib >= 2.32 does not require g_thread_init() to be
281  * called to be thread-safe.
282  * @param fd The file descriptor.
283  * @param cb The callback object. Ownership is taken of this object,
284  * and it will be deleted when it has been finished with.
285  * @param r A Releaser object which the protected object has as a
286  * public member.
287  * @param io_condition The condition(s) to be watched for (G_IO_IN,
288  * G_IO_OUT, G_IO_HUP and G_IO_ERR may all be bitwise-ored).
289  * @param priority The priority to be given to the watch in the main
290  * loop. In ascending order of priorities, priorities are
291  * G_PRIORITY_LOW, G_PRIORITY_DEFAULT_IDLE, G_PRIORITY_HIGH_IDLE,
292  * G_PRIORITY_DEFAULT and G_PRIORITY_HIGH. The default is
293  * G_PRIORITY_DEFAULT. This determines the order in which the
294  * callback will appear in the event list in the main loop, not the
295  * priority which the OS will adopt
296  * @param context The glib main context to which the watch is to be
297  * attached (the default of NULL will cause the watch to be attached
298  * to the main program loop, and this is almost always what is
299  * wanted).
300  * @return The glib source id of the watch.
301  * @exception std::bad_alloc This function might throw std::bad_alloc
302  * if memory is exhausted and the system throws in that case. If it
303  * does so, the CallbackArg object will be disposed of.
304  * @exception Cgu::Thread::MutexError This function might throw
305  * Cgu:Thread::MutexError if initialisation of the mutex in a
306  * SafeEmitterArg object constructed by this function fails. If it
307  * does so, the CallbackArg object will be disposed of. (It is often
308  * not worth checking for this exception, as it means either memory is
309  * exhausted or pthread has run out of other resources to create new
310  * mutexes.)
311  * @note 1. Cancellation of the thread to which the watch is attached
312  * is blocked during execution of the callback.
313  * @note 2. If the callback throws an exception, the exception will be
314  * consumed to protect the main loop and a g_critical() warning will
315  * be issued.
316  * @note 3. This version, whereby the callback function takes an
317  * unbound GIOCondition argument, is not available if the library was
318  * configured with the \--without-type-tuple-args option.
319  * @ingroup io_watch
320  *
321  * Since 1.2.12
322  */
323 guint start_iowatch(int fd, const Callback::CallbackArg<TypeTuple<GIOCondition, bool&> >* cb,
324  Releaser& r, GIOCondition io_condition, gint priority = G_PRIORITY_DEFAULT,
325  GMainContext* context = 0);
326 
327 /**
328  * @b DEPRECATED: Don't use this function, it is just provided to
329  * maintain API compatibility with version 0.9.0 and 0.9.1 of
330  * c++-gtk-utils and will be dropped at some stage. The signature of
331  * start_iowatch() was changed in version 0.9.2 to permit a priority
332  * to be specified.
333  * @ingroup io_watch
334  */
335 // we have been lucky with this - in the very unlikely event of a user
336 // having previously passed an explicit context argument of 0 (the
337 // previous default) to this deprecated version of start_iowatch()
338 // which will now be interpreted by the compiler on overload
339 // resolution as a call to the undeprecated version with a priority
340 // of 0 and a default GMainContext of NULL, the effect will be
341 // identical since G_PRIORITY_DEFAULT is defined as 0 (phew!)
342 inline guint start_iowatch(int fd, const Callback::CallbackArg<bool&>* cb,
343  GIOCondition io_condition, GMainContext* context) {
344  return start_iowatch(fd, cb, io_condition, G_PRIORITY_DEFAULT, context);
345 }
346 
347 /**
348  * @b DEPRECATED: Don't use this function, it is just provided to
349  * maintain API compatibility with version 0.9.0 and 0.9.1 of
350  * c++-gtk-utils and will be dropped at some stage. The signature of
351  * start_iowatch() was changed in version 0.9.2 to permit a priority
352  * to be specified.
353  * @ingroup io_watch
354  */
355 // we have been lucky with this - in the very unlikely event of a user
356 // having previously passed an explicit context argument of 0 (the
357 // previous default) to this deprecated version of start_iowatch()
358 // which will now be interpreted by the compiler on overload
359 // resolution as a call to the undeprecated version with a priority
360 // of 0 and a default GMainContext of NULL, the effect will be
361 // identical since G_PRIORITY_DEFAULT is defined as 0 (phew!)
362 inline guint start_iowatch(int fd, const Callback::CallbackArg<bool&>* cb, Releaser& r,
363  GIOCondition io_condition, GMainContext* context) {
364  return start_iowatch(fd, cb, r, io_condition, G_PRIORITY_DEFAULT, context);
365 }
366 
367 } // namespace Cgu
368 
369 #endif
Cgu::Callback::CallbackArg
The callback interface class.
Definition: callback.h:904
Cgu::start_iowatch
guint start_iowatch(int fd, const Callback::CallbackArg< bool & > *cb, GIOCondition io_condition, gint priority=G_PRIORITY_DEFAULT, GMainContext *context=0)
Cgu
Definition: application.h:45
callback.h
This file provides classes encapsulating callbacks.
param.h
Cgu::Releaser
A class used for tracking EmitterArg and SafeEmitterArg connections.
Definition: emitter.h:368
cgu_config.h