CPN
Computational Process Networks
WakeupHandle.cc
Go to the documentation of this file.
1 //=============================================================================
2 // Computational Process Networks class library
3 // Copyright (C) 1997-2006 Gregory E. Allen and The University of Texas
4 //
5 // This library is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU Library General Public License as published
7 // by the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Library General Public License for more details.
14 //
15 // The GNU Public License is available in the file LICENSE, or you
16 // can write to the Free Software Foundation, Inc., 59 Temple Place -
17 // Suite 330, Boston, MA 02111-1307, USA, or you can find it on the
18 // World Wide Web at http://www.fsf.org.
19 //=============================================================================
23 #include "common_priv.h"
24 #include <cpn/io/WakeupHandle.h>
27 #include <unistd.h>
28 #include <errno.h>
29 
31  : wfd(-1)
32 {
33  int filedes[2];
34  if (pipe(filedes) != 0) {
35  throw ErrnoException();
36  }
37  FD(filedes[0]);
38  wfd = filedes[1];
39  Readable(false);
40  Writeable(false);
41  SetBlocking(false);
42  SetBlocking(wfd, false);
43 }
44 
46  close(wfd);
47  wfd = -1;
48 }
49 
51  int wfiled;
52  {
54  wfiled = wfd;
55  }
56  char c = 0;
57  int ret = 0;
58  do {
59  ret = write(wfiled, &c, sizeof(c));
60  if (ret < 0) {
61  if (errno == EAGAIN) {
62  // The buffer is full.
63  break;
64  }
65  throw ErrnoException();
66  }
67  } while (ret != sizeof(c));
68 }
69 
71  char c[256];
72  unsigned ret = 0;
73  // If we didn't read all the buffer there isn't more to read
74  do {
75  ret = FileHandle::Read(c, sizeof(c));
76  } while (ret == sizeof(c));
77 }
78 
PthreadMutex file_lock
Definition: FileHandle.h:213
void Read()
Read until not readable anymore.
Definition: WakeupHandle.cc:70
void SetBlocking(bool blocking)
Manipulate how the current file handles blocking.
Definition: FileHandle.cc:111
void SendWakeup()
Causes this handler to become readable any Poll on this FileHandler will then return.
Definition: WakeupHandle.cc:50
bool Writeable() const
Gives the current writability status of the file.
Definition: FileHandle.h:102
int FD() const
Definition: FileHandle.h:106
bool Readable() const
Gives the current readability status of the file.
Definition: FileHandle.h:91
virtual ~WakeupHandle()
Definition: WakeupHandle.cc:45
unsigned Read(void *ptr, unsigned len)
Read data from the file descriptor.
Definition: FileHandle.cc:159