CARMA C++
Pipe.h
Go to the documentation of this file.
1 #ifndef SZA_UTIL_PIPE_H
2 #define SZA_UTIL_PIPE_H
3 
11 #ifdef PIPE_WAIT
12 #undef PIPE_WAIT
13 #endif
14 
15 #ifdef PIPE_NOWAIT
16 #undef PIPE_NOWAIT
17 #endif
18 
19 /*
20  * Enumerate the two special I/O timeout values.
21  */
22 
23 /* Wait to complete the transaction */
24 
25 #define PIPE_WAIT -1
26 
27 /* Return immediately if the transaction would block */
28 
29 #define PIPE_NOWAIT 0
30 
31 /*
32  * Enumerate the return statuses of read_pipe() and write_pipe().
33  */
34 enum PipeState {
35  PIPE_OK, /* The I/O completed successfully */
36  PIPE_BUSY, /* The I/O couldn't be performed without blocking */
37  PIPE_ERROR /* An error occurred */
38 };
39 
40 #include <pthread.h> // Needed for POSIX thread function calls
41 #include <limits.h> // PIPE_BUF
42 
43 namespace sza {
44  namespace util {
45 
49  class Pipe {
50 
51  public:
52 
58  Pipe();
59 
65  virtual ~Pipe();
66 
72  virtual void readPipe(void *buffer, size_t nbyte, long timeout);
73 
79  virtual void writePipe(void *buffer, size_t nbyte, long timeout);
80 
86  virtual PipeState read(void *buffer, size_t nbyte, long timeout=PIPE_NOWAIT);
87 
93  virtual PipeState write(void *buffer, size_t nbyte, long timeout=PIPE_NOWAIT);
94 
98  int fd();
99 
104  fd_set rfds();
105 
106  int readFd() {return readfd_.fd_;};
107 
108  int writeFd() {return writefd_.fd_;};
109 
110  protected:
111 
115  pthread_mutex_t guard_;
116 
121 
126  struct PipeFd {
127 
131  PipeFd();
132 
136  ~PipeFd();
137 
142  pthread_cond_t retry_;
143 
148 
152  int fd_;
153 
159  void fillPipeFd();
160  };
161 
166 
171 
172  /*
173  * One can write up to PIPE_BUF bytes atomically, but there is no such
174  * guarantee for read(). The following buffer allows this guarantee to
175  * be extended to reads. It accumulates data sequentially from one
176  * or more incomplete reads, and hands out request-sized chunks to
177  * any readers that request <= nread bytes, on a first come first
178  * served basis.
179  */
180  char unread_[PIPE_BUF];
181 
185  size_t nread_;
186 
193  void getTimeOfDay(struct timespec* ts);
194 
195  }; // End class Pipe
196 
197  }; // End namespace util
198 }; // End namespace sza
199 
200 #endif // PIPE_H
201 
202 
203 
204 
205 
206 
207 
void getTimeOfDay(struct timespec *ts)
Get the current time of day.
virtual PipeState read(void *buffer, size_t nbyte, long timeout=0)
Read from the pipe.
PipeFd writefd_
File descriptor corresponding to the write end of the pipe.
Definition: Pipe.h:170
fd_set rfds()
Return an intialized set of readable file descriptors associated with this queue. ...
bool guardIsReady_
True when the guard mutex has been initialized.
Definition: Pipe.h:120
int fd()
Return the file descriptor associated with this pipe.
virtual void writePipe(void *buffer, size_t nbyte, long timeout)
Write to the pipe.
int fd_
The file descriptor.
Definition: Pipe.h:152
pthread_cond_t retry_
A condition variable which can be used for other threads to signal when the fd is readable or writabl...
Definition: Pipe.h:142
A class to encapsulate a pipe.
Definition: Pipe.h:49
void fillPipeFd()
Initialize the pipe fds.
PipeFd()
Constructor.
pthread_mutex_t guard_
A mutex guard for the pipe.
Definition: Pipe.h:108
size_t nread_
The number of bytes in buffer[].
Definition: Pipe.h:185
bool retryIsReady_
True when retry has been initialized.
Definition: Pipe.h:147
PipeFd readfd_
File descriptor corresponding to the read end of the pipe.
Definition: Pipe.h:165
Pipe()
Constructor.
virtual void readPipe(void *buffer, size_t nbyte, long timeout)
Read from the pipe.
virtual PipeState write(void *buffer, size_t nbyte, long timeout=0)
Write to the pipe.
Define a struct to encapsulate a file descriptor associated with either end of a pipe.
Definition: Pipe.h:126
virtual ~Pipe()
Destructor.