My Project
 All Classes Files Functions Variables Enumerations Enumerator Friends Macros Pages
Pipe.h
Go to the documentation of this file.
1 #ifndef GCP_UTIL_PIPE_H
2 #define GCP_UTIL_PIPE_H
3 
12 /*
13  * Enumerate the two special I/O timeout values.
14  */
15 
16 /* Wait to complete the transaction */
17 
18 #define PIPE_WAIT -1
19 
20 /* Return immediately if the transaction would block */
21 
22 #define PIPE_NOWAIT 0
23 
24 /*
25  * Enumerate the return statuses of read_pipe() and write_pipe().
26  */
27 enum PipeState {
28  PIPE_OK, /* The I/O completed successfully */
29  PIPE_BUSY, /* The I/O couldn't be performed without blocking */
30  PIPE_ERROR /* An error occurred */
31 };
32 
33 #include <pthread.h> // Needed for POSIX thread function calls
34 #include <limits.h> // PIPE_BUF
35 
36 namespace gcp {
37  namespace util {
38 
42  class Pipe {
43 
44  public:
45 
51  Pipe();
52 
58  virtual ~Pipe();
59 
65  virtual void readPipe(void *buffer, size_t nbyte, long timeout);
66 
72  virtual void writePipe(void *buffer, size_t nbyte, long timeout);
73 
79  virtual PipeState read(void *buffer, size_t nbyte, long timeout=PIPE_NOWAIT);
80 
86  virtual PipeState write(void *buffer, size_t nbyte, long timeout=PIPE_NOWAIT);
87 
91  int fd();
92 
97  fd_set rfds();
98 
99  int readFd() {return readfd_.fd_;};
100 
101  int writeFd() {return writefd_.fd_;};
102 
103  protected:
104 
108  pthread_mutex_t guard_;
109 
114 
119  struct PipeFd {
120 
124  PipeFd();
125 
129  ~PipeFd();
130 
135  pthread_cond_t retry_;
136 
141 
145  int fd_;
146 
152  void fillPipeFd();
153  };
154 
159 
164 
165  /*
166  * One can write up to PIPE_BUF bytes atomically, but there is no such
167  * guarantee for read(). The following buffer allows this guarantee to
168  * be extended to reads. It accumulates data sequentially from one
169  * or more incomplete reads, and hands out request-sized chunks to
170  * any readers that request <= nread bytes, on a first come first
171  * served basis.
172  */
173  char unread_[PIPE_BUF];
174 
178  size_t nread_;
179 
186  void getTimeOfDay(struct timespec* ts);
187 
188  }; // End class Pipe
189 
190  }; // End namespace util
191 }; // End namespace gcp
192 
193 #endif // PIPE_H
194 
195 
196 
197 
198 
199 
200 
virtual void readPipe(void *buffer, size_t nbyte, long timeout)
Definition: Pipe.cc:164
fd_set rfds()
Definition: Pipe.cc:1008
void getTimeOfDay(struct timespec *ts)
Definition: Pipe.cc:971
virtual PipeState read(void *buffer, size_t nbyte, long timeout=PIPE_NOWAIT)
Definition: Pipe.cc:383
virtual void writePipe(void *buffer, size_t nbyte, long timeout)
Definition: Pipe.cc:598
pthread_cond_t retry_
Definition: Pipe.h:135
Definition: Pipe.h:42
virtual PipeState write(void *buffer, size_t nbyte, long timeout=PIPE_NOWAIT)
Definition: Pipe.cc:783
PipeFd readfd_
Definition: Pipe.h:158
PipeFd writefd_
Definition: Pipe.h:163
int fd_
Definition: Pipe.h:145
virtual ~Pipe()
Definition: Pipe.cc:148
void fillPipeFd()
Definition: Pipe.cc:99
pthread_mutex_t guard_
Definition: Pipe.h:101
bool guardIsReady_
Definition: Pipe.h:113
~PipeFd()
Definition: Pipe.cc:34
PipeFd()
Definition: Pipe.cc:25
Definition: Pipe.h:119
Pipe()
Definition: Pipe.cc:51
Definition: tVideoCapabilitiesEml.cc:67
size_t nread_
Definition: Pipe.h:178
int fd()
Definition: Pipe.cc:991
bool retryIsReady_
Definition: Pipe.h:140