c++-gtk-utils
pipes.h
Go to the documentation of this file.
1 /* Copyright (C) 1999 - 2004, 2009 and 2010 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_PIPES_H
26 #define CGU_PIPES_H
27 
28 #include <unistd.h>
29 
30 #include <exception>
31 
33 
34 namespace Cgu {
35 
36 /**
37  * @class PipeFifo pipes.h c++-gtk-utils/pipes.h
38  * @brief A wrapper for unix anonymous pipes.
39  * @sa SyncPipe
40  *
41  * This class provides a simplified front end for anonymous fifo pipes
42  * (as created by the pipe() system call). The constructor of the
43  * class may be passed an enumerator to indicate whether the pipe is
44  * to read in non-blocking mode.
45  *
46  * With the write(const char*, int) method, taking an array and int
47  * arguments, the value of int (indicating the number of chars in the
48  * array to be written) should usually be less than PIPE_BUF, as POSIX
49  * write() guarantees that if it is less than PIPE_BUF, either all
50  * will be written or, if the pipe is too full, none will be written.
51  * This prevents data interleaving if a number of independent writes
52  * to the fifo are made. The same is true of the write (const char*)
53  * method taking a string - if the string is longer than PIPE_BUF in
54  * size, data interleaving may occur.
55  *
56  * The pipe is set to read in non-blocking mode if the constructor of
57  * PipeFifo, or PipeFifo::open(), is passed the value
58  * PipeFifo::non_block. If constructed in this way, the pipe can also
59  * be set to write in non-block mode (eg to minimise the impact on
60  * another program running in a child process to which the child
61  * process has exec()ed when monitoring its stdin or stderr) by
62  * calling make_write_non_block(). However use this very sparingly --
63  * when set in this way write() will always try to write something.
64  * The atomic guarantees mentioned in the preceding paragraph do not
65  * apply and data can be interleaved or lost.
66  *
67  * PIPE_BUF is defined in limits.h, and is at least 512 bytes, and
68  * usually 4096 bytes, but may be calculated from other files included
69  * by limits.h.
70  *
71  * Where a pipe is used to communicate between parent and child after
72  * a call to fork(), the pipe will normally be used unidirectionally
73  * unless guards or semaphores are used to prevent a process reading
74  * data intended for the other. However, because each process
75  * inherits its own duplicate of the file descriptors, this cannot be
76  * enforced without closing the read or write file descriptor for the
77  * process for which the reading or writing is to be prohibited. This
78  * can be done by the process concerned calling the methods
79  * PipeFifo::make_writeonly() or PipeFifo::make_readonly() after the
80  * fork. If an attempt is made to read or write to a descriptor
81  * closed in this way, the PipeFifo::read() or PipeFifo::write()
82  * method will ensure that no read or write will take place, and
83  * instead -2 will be returned.
84  *
85  * The methods PipeFifo::connect_to_stdin(),
86  * PipeFifo::connect_to_stdout() and PipeFifo::connect_to_stderr() are
87  * available to be used in the child process before it exec()s another
88  * program so as to connect the pipe to that program's stdin, stdout
89  * or stderr. PipeFifo::connect_to_stdin() cannot be used by a
90  * process after that process has called PipeFifo::make_writeonly(),
91  * and PipeFifo::connect_to_stdout() and PipeFifo::connect_to_stderr()
92  * cannot be used after the process has called
93  * PipeFifo::make_readonly(), nor can a pipe in any one process be
94  * connected to more than one of stdin, stdout or stderr. If that is
95  * attempted the methods will return -2. Furthermore, they should
96  * only be used after the process creating the pipe has forked. If
97  * the connection to stdin, stdout or stderr is to be made before the
98  * fork, this must be done by hand using dup2(),
99  * PipeFifo::get_write_fd()/ PipeFifo::get_read_fd() and
100  * PipeFifo::make_read_only()/ PipeFifo::make_write_only().
101  *
102  * If PipeFifo::connect_to_stdin() is called by a method,
103  * PipeFifo::make_readonly() will also be called, and if
104  * PipeFifo::connect_to_stdout() or PipeFifo::connect_to_stderr() are
105  * called, PipeFifo::make_writeonly() will also be called. This will
106  * isolate the use of the pipe by the child process to stdin, stdout
107  * or stderr, as appropriate.
108  *
109  * It uses no static members, so is thread safe as between different
110  * objects, but its methods are not thread safe as regards any one
111  * object in the sense that the read() and write() methods check the
112  * value of read_fd and write_fd respectively (and get_read_fd() and
113  * get_write_fd() return them), and make_writeonly(), make_readonly(),
114  * close(), connect_to_stdin(), open(), connect_to_stdout() and
115  * connect_to_stderr() change those values. Likewise the read() and
116  * write() methods access read_blocking_mode and write_blocking_mode
117  * respectively, and these are changed by open() and
118  * make_write_non_block(). Provided there is no concurrent use of
119  * read(), write(), get_read_fd() or get_write_fd() in one thread with
120  * a call of a method which changes read_fd, write_fd,
121  * read_blocking_mode or write_blocking_mode as described above in
122  * another thread then no mutex is required to ensure thread safety.
123  *
124  * All the read() and write() methods check for an interruption of the
125  * system call from a signal (EINTR is checked), and will continue to
126  * read() or write() where necessary. Users do not need to check
127  * EINTR themselves. Where the write file descriptor is flagged as
128  * blocking (that is, where PipeFifo::make_write_non_block() has not
129  * been called), then in the absence of some other error, everything
130  * passed to PipeFifo::write() will be written (but as mentioned above
131  * there may be data interleaving if this is greater than PIPE_BUF in
132  * size). Where the write file descriptor is flagged as non-blocking,
133  * then the result of Unix write is returned, and less bytes than
134  * those passed to PipeFifo::write() may have been written, or -1 may
135  * be returned with errno set to EAGAIN - it is for the user to check
136  * this.
137  *
138  * If you are using pipes to write and read between processes, you
139  * will probably want to call sigaction() to cause the SIGPIPE signal
140  * to be ignored. SIGPIPE has little use in most programming: if
141  * SIGPIPE is ignored, and an attempt is made to write to a pipe which
142  * has been closed at the read end, then the write will return -1 and
143  * errno will be set to EPIPE.
144  */
145 
146 struct PipeError: public std::exception {
147  virtual const char* what() const throw() {return "PipeError: error opening pipe\n";}
148 };
149 
150 class PipeFifo {
151 public:
153 private:
154  int read_fd;
155  int write_fd;
156  Fifo_mode read_blocking_mode;
157  Fifo_mode write_blocking_mode;
158 
159  // this class cannot be copied - file descriptors are owned and not shared
160  PipeFifo(const PipeFifo&);
161  PipeFifo& operator=(const PipeFifo&);
162 public:
163 /**
164  * Opens read and write file descriptors for the pipe. If the pipe is
165  * already open, it will close the existing file descriptors. This
166  * function call is thread-safe to the extent that all the system
167  * calls made in its implementation are async-signal-safe, so it may
168  * safely be called in a multi-threaded program after a fork() and
169  * before an exec(). However, the manipulation of the file
170  * descriptors and related variables is not protected by a mutex, so
171  * if different threads in the same process are to manipulate the same
172  * pipe object with this method, close(), connect_to_stdin(),
173  * connect_to_stdout() or connect_to_stderr() (which manipulate both
174  * descriptors), make_readonly() or make_write_non_block() (which
175  * manipulate the write file descriptor), or make_writeonly() (which
176  * manipulates the read file descriptor), or read the value of the
177  * file descriptor with get_read_fd() or read() (which access the read
178  * file descriptor) or get_write_fd() or write() (which access the
179  * write file descriptor) concurrently with such a manipulation, then
180  * the user should provide appropriate synchronisation.
181  * @param mode The mode in which the read file descriptor of the pipe
182  * is to be opened - either Cgu::PipeFifo::block or
183  * Cgu::PipeFifo::non_block.
184  * @exception Cgu::PipeError This exception is thrown if the opening
185  * of the pipe fails. No other exceptions will be thrown.
186  */
187  void open(Fifo_mode mode);
188 
189 /**
190  * Closes the read and write file descriptors of the pipe (if they are
191  * open). This function call is thread-safe to the extent that all
192  * the system calls made in its implementation are async-signal-safe,
193  * so it may safely be called in a multi-threaded program after a
194  * fork() and before an exec(). However, the manipulation of the file
195  * descriptors and related variables is not protected by a mutex, so
196  * if different threads in the same process are to manipulate the same
197  * pipe object with this method, open(), connect_to_stdin(),
198  * connect_to_stdout() or connect_to_stderr() (which manipulate both
199  * descriptors), make_readonly() or make_write_non_block() (which
200  * manipulate the write file descriptor), or make_writeonly() (which
201  * manipulates the read file descriptor), or read the value of the
202  * file descriptor with get_read_fd() or read() (which access the read
203  * file descriptor) or get_write_fd() or write() (which access the
204  * write file descriptor) concurrently with such a manipulation, then
205  * the user should provide appropriate synchronisation. No exceptions
206  * will be thrown.
207  */
208  void close();
209 
210 /**
211  * Reads up to max_num characters from the pipe. This read does not
212  * carry on blocking until the read request is met: unless there is an
213  * interrupt, it just calls POSIX read() once and takes what is
214  * available. If a specific number of characters is required, the
215  * user should provide an appropriate do/while loop. It does not
216  * throw. Any reasonable POSIX implementation will do an atomic read
217  * as between threads if max_num is PIPE_BUF or less in size, but the
218  * POSIX standard only requires such atomic reads as between processes
219  * rather than as between threads. The user does not need to check
220  * for EINTR and an interrupt: this method deals with that
221  * automatically. All the system calls made in its implementation are
222  * async-signal-safe, and it is re-entrant subject to errno being
223  * saved. This method does not throw.
224  * @param buf A buffer into which the characters will be placed.
225  * @param max_num The maximum number of characters to be read
226  * (normally the size of the buffer).
227  * @return -2 if the read file descriptor is invalid, otherwise the
228  * result of unix read() - that is, the number of characters read, or
229  * 0 if end of file is reached (ie the write descriptor has been
230  * closed), or -1 if there has been an error (but EINTR never returns
231  * as an error as the implementation handles that). In the event of
232  * an error, errno is set.
233  */
234  ssize_t read(char* buf, size_t max_num);
235 
236 /**
237  * Extracts a character from the pipe. This method does not throw.
238  * All the system calls made in its implementation are
239  * async-signal-safe and it is re-entrant subject to errno being
240  * saved. It is thread safe.
241  * @return The character at the front of the pipe if available,
242  * otherwise -2 if the read file descriptor is invalid, 0 if end of
243  * file is reached (ie the write descriptor has been closed), or -1 if
244  * there has been an error (but EINTR never returns as an error as the
245  * implementation handles that). In the event of an error, errno is
246  * set.
247  */
248  int read();
249 
250 /**
251  * Write a NUL terminated C string to the pipe, excluding the
252  * terminating NUL character. If the write file descriptor is set as
253  * blocking (the default) then this method will carry on blocking
254  * until everything is sent, even if it exceeds PIPE_BUF in size. Any
255  * reasonable POSIX implementation will do an atomic write (with no
256  * interleaving) as between threads if the number of characters in the
257  * string is PIPE_BUF or less in size, but the POSIX standard only
258  * requires such atomic writes as between processes rather than as
259  * between threads. All the system calls made in its implementation
260  * are async-signal-safe and it is re-entrant subject to errno being
261  * saved. This method does not throw.
262  * @param str The string to be written.
263  * @return -2 if write file descriptor invalid, otherwise it returns
264  * the number of bytes written or -1 if there has been an error (but
265  * EINTR never returns as an error as the implementation handles
266  * that). In the event of an error, errno is set.
267  */
268  ssize_t write(const char* str);
269 
270 /**
271  * Write a buffer of characters to the pipe. If the write file
272  * descriptor is set as blocking (the default) then this method will
273  * carry on blocking until everything is sent, even if it exceeds
274  * PIPE_BUF in size. Any reasonable POSIX implementation will do an
275  * atomic write (with no interleaving) as between threads if num is
276  * PIPE_BUF or less in size, but the POSIX standard only requires such
277  * atomic writes as between processes rather than as between threads.
278  * All the system calls made in its implementation are
279  * async-signal-safe and it is re-entrant subject to errno being
280  * saved. This method does not throw.
281  * @param buf The buffer to be written.
282  * @param num The number of characters in the buffer.
283  * @return -2 if write file descriptor invalid, otherwise it returns
284  * the number of bytes written or -1 if there has been an error (but
285  * EINTR never returns as an error as the implementation handles
286  * that). In the event of an error, errno is set.
287  */
288  ssize_t write(const char* buf, size_t num);
289 
290 /**
291  * Write a character to the pipe. All the system calls made in its
292  * implementation are async-signal-safe and it is re-entrant subject
293  * to errno being saved. This method does not throw. It is thread
294  * safe.
295  * @param item The character to be written.
296  * @return 1 if the character was written, -2 if the write file
297  * descriptor is invalid, or -1 if there has been an error (but EINTR
298  * never returns as an error as the implementation handles that). In
299  * the event of an error, errno is set.
300  */
301  int write(char item) {return write(&item, 1);}
302 
303 /**
304  * Make the pipe only available for writing in the calling process, by
305  * closing the read file descriptor. This function call is thread
306  * safe to the extent that all the system calls made in its
307  * implementation are async-signal-safe, so it may safely be called in
308  * a multi-threaded program after a fork() and before an exec().
309  * However, the manipulation of the file descriptor and related
310  * variables is not protected by a mutex, so if different threads in
311  * the same process are to manipulate the same pipe object with
312  * open(), close(), connect_to_stdin(), connect_to_stdout() or
313  * connect_to_stderr() (which manipulate both descriptors), or this
314  * method (which manipulates the read file descriptor), or read the
315  * value of the read file descriptor with get_read_fd() or read()
316  * concurrently with such a manipulation, then the user should provide
317  * appropriate synchronisation. No exceptions will be thrown.
318  */
319  void make_writeonly();
320 
321 /**
322  * Make the pipe only available for reading in the calling process, by
323  * closing the write file descriptor. This function call is
324  * thread-safe to the extent that all the system calls made in its
325  * implementation are async-signal-safe, so it may safely be called in
326  * a multi-threaded program after a fork() and before an exec().
327  * However, the manipulation of the file descriptor and related
328  * variables is not protected by a mutex, so if different threads in
329  * the same process are to manipulate the same pipe object with
330  * open(), close(), connect_to_stdin(), connect_to_stdout() or
331  * connect_to_stderr() (which manipulate both descriptors), or this
332  * method or make_write_non_block() (which manipulate the write file
333  * descriptor), or read the value of the write file descriptor with
334  * get_write_fd() or write() concurrently with such a manipulation,
335  * then the user should provide appropriate synchronisation. No
336  * exceptions will be thrown.
337  */
338  void make_readonly();
339 
340 /**
341  * Makes the write file descriptor non-blocking. This is only very
342  * rarely needed. If the write file descriptor is set non-blocking,
343  * then there are no atomic write guarantees. This function call is
344  * thread safe to the extent that all the system calls made in its
345  * implementation are async-signal-safe, so it may safely be called in
346  * a multi-threaded program after a fork() and before an exec().
347  * However, the manipulation of the file descriptor and related
348  * variables is not protected by a mutex, so if different threads in
349  * the same process are to manipulate the same pipe object with
350  * open(), close(), connect_to_stdin(), connect_to_stdout() or
351  * connect_to_stderr() (which manipulate both descriptors), or this
352  * method or make_readonly() (which manipulate the write file
353  * descriptor), or read the value of the write file descriptor with
354  * get_write_fd() or write() concurrently with such a manipulation,
355  * then the user should provide appropriate synchronisation. No
356  * exceptions will be thrown.
357  * @return 0 if the write file descriptor is open, or -1 otherwise.
358  */
359  int make_write_non_block();
360 
361 /**
362  * Get the read file descriptor (if any). The fetching of the file
363  * descriptor is not protected by a mutex, so if different threads in
364  * the same process are to manipulate the same pipe object with
365  * open(), close(), connect_to_stdin(), connect_to_stdout() or
366  * connect_to_stderr() (which manipulate both descriptors), or
367  * make_writeonly() (which manipulates the read file descriptor), or
368  * read the value of the file descriptor with this method or read()
369  * concurrently with such a manipulation, then the user should provide
370  * appropriate synchronisation. No exceptions will be thrown.
371  * @return The read file descriptor, or -1 is none is open.
372  */
373  int get_read_fd() const {return read_fd;}
374 
375 /**
376  * Get the write file descriptor (if any). The fetching of the file
377  * descriptor is not protected by a mutex, so if different threads in
378  * the same process are to manipulate the same pipe object with
379  * open(), close(), connect_to_stdin(), connect_to_stdout() or
380  * connect_to_stderr() (which manipulate both descriptors), or
381  * make_readonly() or make_write_non_block() (which manipulate the
382  * write file descriptor), or read the value of the file descriptor
383  * with this method or write() concurrently with such a manipulation,
384  * then the user should provide appropriate synchronisation. No
385  * exceptions will be thrown.
386  * @return The write file descriptor, or -1 is none is open.
387  */
388  int get_write_fd() const {return write_fd;}
389 
390 /**
391  * Connect the read file descriptor of the pipe to stdin. Once
392  * called, any read from stdin in the process which calls this method
393  * will be read from the pipe. It should only be called in a child
394  * process or parent process after a fork() where the PipeFifo object
395  * has been created in the parent process, and typically is called by
396  * the child process before one of the exec() functions is invoked by
397  * the child process. It should only be called once, and cannot be
398  * called after connect_to_stdout(), connect_to_stderr() or
399  * make_writeonly() has been called in the same process. Once called,
400  * the write file descriptor is no longer available to the process
401  * calling this method. Typically it would be used in order that when
402  * the new process image created by exec() reads from stdin, it reads
403  * from the pipe, and the parent process (or another child process)
404  * writes to the pipe. This function call is thread safe to the
405  * extent that all the system calls made in its implementation are
406  * async-signal-safe, so it may safely be called in a multi-threaded
407  * program after a fork() and before an exec(). However, the
408  * manipulation of the file descriptors and related variables is not
409  * protected by a mutex, so if different threads in the same process
410  * are to manipulate the same pipe object with this method, open(),
411  * close(), connect_to_stdout() or connect_to_stderr() (which
412  * manipulate both descriptors), make_readonly() or
413  * make_write_non_block() (which manipulate the write file
414  * descriptor), or make_writeonly() (which manipulates the read file
415  * descriptor), or read the value of the file descriptor with
416  * get_read_fd() or read() (which access the read file descriptor) or
417  * get_write_fd() or write() (which access the write file descriptor)
418  * concurrently with such a manipulation, then the user should provide
419  * appropriate synchronisation. It does not throw.
420  * @return -2 if the read file descriptor has previously been closed
421  * or this method, connect_to_stdout() or connect_to_stderr() has
422  * previously been called in the same process, otherwise it returns
423  * the value returned by dup2(), that is 0 if the operation succeeds,
424  * or -1 (with errno set) if not. It does not throw.
425  * @note Prior to version 1.0.0, this function would only return -1 in
426  * case of error.
427  */
428  int connect_to_stdin();
429 
430 /**
431  * Connect the write file descriptor of the pipe to stdout. Once
432  * called, any write to stdout in the process which calls this method
433  * will be written to the pipe. It should only be called in a child
434  * or parent process after a fork() where the PipeFifo object has been
435  * created in the parent process, and typically is called by the child
436  * process before one of the exec() functions is invoked by the child
437  * process. It should only be called once, and cannot be called after
438  * connect_to_stdin(), connect_to_stderr() or make_readonly() has been
439  * called in the same process. Once called, the read file descriptor
440  * is no longer available to the process calling this method.
441  * Typically it would be used in order that when the new process image
442  * created by exec() writes to stdout, it writes to the pipe, and the
443  * parent process (or another child process) reads from the pipe.
444  * This function call is thread safe to the extent that all the system
445  * calls made in its implementation are async-signal-safe, so it may
446  * safely be called in a multi-threaded program after a fork() and
447  * before an exec(). However, the manipulation of the file
448  * descriptors and related variables is not protected by a mutex, so
449  * if different threads in the same process are to manipulate the same
450  * pipe object with this method, open(), close(), connect_to_stdin()
451  * or connect_to_stderr() (which manipulate both descriptors),
452  * make_readonly() or make_write_non_block() (which manipulate the
453  * write file descriptor), or make_writeonly() (which manipulates the
454  * read file descriptor), or read the value of the file descriptor
455  * with get_read_fd() or read() (which access the read file
456  * descriptor) or get_write_fd() or write() (which access the write
457  * file descriptor) concurrently with such a manipulation, then the
458  * user should provide appropriate synchronisation. It does not
459  * throw.
460  * @return -2 if the write file descriptor has previously been closed
461  * or this method, connect_to_stdin() or connect_to_stderr() has
462  * previously been called in the same process, otherwise it returns
463  * the value returned by dup2(), that is 0 if the operation succeeds,
464  * or -1 (with errno set) if not. It does not throw.
465  * @note Prior to version 1.0.0, this function would only return -1 in
466  * case of error.
467  */
468  int connect_to_stdout();
469 
470 /**
471  * Connect the write file descriptor of the pipe to stderr. Once
472  * called, any write to stderr in the process which calls this method
473  * will be written to the pipe. It should only be called in a child
474  * or parent process after a fork() where the PipeFifo object has been
475  * created in the parent process, and typically is called by a child
476  * process before one of the exec() functions is invoked by the child
477  * process. It should only be called once, and cannot be called after
478  * connect_to_stdin(), connect_to_stdout() or make_readonly() has been
479  * called in the same process. Once called, the read file descriptor
480  * is no longer available to the process calling this method.
481  * Typically it would be used in order that when the new process image
482  * created by exec() writes to stderr, it writes to the pipe, and the
483  * parent process (or another child process) reads from the pipe.
484  * This function call is thread safe to the extent that all the system
485  * calls made in its implementation are async-signal-safe, so it may
486  * safely be called in a multi-threaded program after a fork() and
487  * before an exec(). However, the manipulation of the file
488  * descriptors and related variables is not protected by a mutex, so
489  * if different threads in the same process are to manipulate the same
490  * pipe object with this method, open(), close(), connect_to_stdin()
491  * or connect_to_stdout() (which manipulate both descriptors),
492  * make_readonly() or make_write_non_block() (which manipulate the
493  * write file descriptor), or make_writeonly() (which manipulates the
494  * read file descriptor), or read the value of the file descriptor
495  * with get_read_fd() or read() (which access the read file
496  * descriptor) or get_write_fd() or write() (which access the write
497  * file descriptor) concurrently with such a manipulation, then the
498  * user should provide appropriate synchronisation. It does not
499  * throw.
500  * @return -2 if the write file descriptor has previously been closed
501  * or this method, connect_to_stdin() or connect_to_stdout() has
502  * previously been called in the same process, otherwise it returns
503  * the value returned by dup2(), that is 0 if the operation succeeds,
504  * or -1 (with errno set) if not. It does not throw.
505  * @note Prior to version 1.0.0, this function would only return -1 in
506  * case of error.
507  */
508  int connect_to_stderr();
509 
510 /**
511  * This constructor opens read and write file descriptors for the
512  * pipe. All the system calls made in its implementation are
513  * async-signal-safe.
514  * @param mode The mode in which the read file descriptor of the pipe
515  * is to be opened - either Cgu::PipeFifo::block or
516  * Cgu::PipeFifo::non_block.
517  * @exception Cgu::PipeError This exception is thrown if the opening
518  * of the pipe fails. No other exceptions will be thrown.
519  */
520  PipeFifo(Fifo_mode mode);
521 
522 /**
523  * The default constructor does not open any descriptors. open() must
524  * be called before the PipeFifo object can be used. It is
525  * async-signal-safe. It does not throw.
526  */
527  PipeFifo();
528 
529 /**
530  * The destructor does not throw. It is async-signal-safe.
531  */
533 
534 /* Only has effect if --with-glib-memory-slices-compat or
535  * --with-glib-memory-slices-no-compat option picked */
537 };
538 
539 
540 
541 /**
542  * @class SyncPipe pipes.h c++-gtk-utils/pipes.h
543  * @brief A class which uses an anonymous pipe to synchronise between
544  * processes.
545  * @sa PipeFifo
546  *
547  * This class enables synchronisation between processes after
548  * fork()ing. The process to wait on the other one calls wait() at
549  * the point where it wishes to wait, and the other process calls
550  * release() when it wants to enable the other process to continue.
551  * It is one-shot only - once it has released, it cannot re-block
552  * again. It is typically for use when a child process is setting
553  * itself up, or has a specific task to achieve, and needs to
554  * co-ordinate with the parent process or tell the parent process when
555  * it has done this. In such a case, the parent would wait until the
556  * child releases it.
557 */
558 class SyncPipe {
559  PipeFifo pipe_fifo;
560 public:
561 /**
562  * Releases another process waiting on this pipe. If the other
563  * process has not yet called wait(), when it does so wait() will
564  * immediately return. This may safely be called after a
565  * multi-threaded process forks() (all the system calls made in its
566  * implementation are async-signal-safe), but only one thread should
567  * call it. It does not throw.
568  */
569  void release() {pipe_fifo.make_writeonly(); pipe_fifo.make_readonly();}
570 
571 /**
572  * Blocks until another process has called release() on this pipe. If
573  * the other process has already called release(), this function will
574  * immediately return. This may safely be called after a
575  * multi-threaded process forks() (all the system calls made in its
576  * implementation are async-signal-safe), but only one thread should
577  * call it. It does not throw.
578  */
579  void wait();
580 
581 /**
582  * All the system calls made in the constructor are async-signal-safe.
583  * @exception Cgu::PipeError This exception is thrown if the opening
584  * of the pipe fails. No other exceptions will be thrown.
585  */
586  SyncPipe(): pipe_fifo(PipeFifo::block) {}
587 
588 /**
589  * The destructor does not throw. It is async-signal-safe.
590  */
592 
593 /* Only has effect if --with-glib-memory-slices-compat or
594  * --with-glib-memory-slices-no-compat option picked */
596 };
597 
598 } // namespace Cgu
599 
600 #endif
Cgu::SyncPipe::wait
void wait()
Cgu::PipeFifo::make_writeonly
void make_writeonly()
Cgu
Definition: application.h:45
Cgu::SyncPipe::release
void release()
Definition: pipes.h:569
Cgu::PipeFifo::connect_to_stdout
int connect_to_stdout()
Cgu::PipeFifo::block
@ block
Definition: pipes.h:152
Cgu::PipeFifo::make_readonly
void make_readonly()
Cgu::SyncPipe::SyncPipe
SyncPipe()
Definition: pipes.h:586
Cgu::PipeFifo::Fifo_mode
Fifo_mode
Definition: pipes.h:152
Cgu::PipeFifo::make_write_non_block
int make_write_non_block()
Cgu::PipeFifo::connect_to_stderr
int connect_to_stderr()
Cgu::PipeFifo::PipeFifo
PipeFifo()
Cgu::PipeFifo::~PipeFifo
~PipeFifo()
Definition: pipes.h:532
Cgu::PipeFifo::read
int read()
Cgu::PipeFifo::connect_to_stdin
int connect_to_stdin()
Cgu::PipeFifo::write
ssize_t write(const char *str)
Cgu::PipeFifo::non_block
@ non_block
Definition: pipes.h:152
Cgu::PipeFifo
A wrapper for unix anonymous pipes.
Definition: pipes.h:150
Cgu::PipeError
Definition: pipes.h:146
Cgu::PipeFifo::open
void open(Fifo_mode mode)
CGU_GLIB_MEMORY_SLICES_FUNCS
#define CGU_GLIB_MEMORY_SLICES_FUNCS
Definition: cgu_config.h:84
Cgu::PipeFifo::get_read_fd
int get_read_fd() const
Definition: pipes.h:373
Cgu::PipeFifo::close
void close()
Cgu::SyncPipe
A class which uses an anonymous pipe to synchronise between processes.
Definition: pipes.h:558
Cgu::PipeFifo::get_write_fd
int get_write_fd() const
Definition: pipes.h:388
Cgu::PipeError::what
virtual const char * what() const
Definition: pipes.h:147
Cgu::SyncPipe::~SyncPipe
~SyncPipe()
Definition: pipes.h:591
cgu_config.h
Cgu::PipeFifo::write
int write(char item)
Definition: pipes.h:301