/
Blockable.cpp
192 lines (166 loc) · 3.6 KB
/
Blockable.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include "Blockable.h"
#include <algorithm>
#include "errno.h"
#include <iostream>
#include "stdio.h"
Blockable cinWatcher(0);
PipeUser::PipeUser(void)
{
int pipesFD[2];
pipe(pipesFD);
SetFD(pipesFD[0]);
sender = pipesFD[1];
}
PipeUser::PipeUser (PipeUser const &p)
: Blockable(dup(p.fd)), sender(dup(p.sender))
{
;
}
void PipeUser::Assign(PipeUser const & p)
{
close(sender);
close(GetFD());
SetFD(p.GetFD());
sender = dup(p.sender);
}
PipeUser & PipeUser::operator = (PipeUser const & p)
{
Assign(p);
return *this;
}
PipeUser::~PipeUser ()
{
close(sender);
close(GetFD());
}
// Wait is designed to be non-intrusive. Returns when character is available
void PipeUser::BlockForByte(void)
{
FlexWait f(1,this);
f.Wait();
}
char PipeUser::ConsumeByte(void)
{
char c;
read(GetFD(),&c,1);
return c;
}
void PipeUser::WriteByte(char c)
{
write(sender,&c,1);
}
Event::Event(Event const & e)
:PipeUser(e)
{
;
}
Event & Event::operator = (Event const & e)
{
Assign(e);
return *this;
}
void Event::Trigger(void)
{
PipeUser::WriteByte('E');
}
void Event::Wait(void)
{
PipeUser::BlockForByte();
}
void Event::Reset(void)
{
PipeUser::ConsumeByte();
}
ThreadSem::ThreadSem(int initialState)
:PipeUser()
{
for (int i=0;i<initialState;i++)
Signal();
}
ThreadSem::ThreadSem(ThreadSem const & e)
:PipeUser(e)
{
;
}
ThreadSem & ThreadSem::operator = (ThreadSem const & e)
{
Assign(e);
return *this;
}
void ThreadSem::Wait(void)
{
PipeUser::BlockForByte();
PipeUser::ConsumeByte();
}
void ThreadSem::Signal(void)
{
PipeUser::WriteByte();
}
FlexWait::FlexWait(int n,... )
{
va_list vl;
va_start(vl,n);
for (int i=0;i<n;i++)
{
v.push_back(va_arg(vl,Blockable*));
}
}
// Returns a pointer to a blockable object or throws TerminationException
// Return of 0 means the wait timed out. Time out parameter of zero means
// never time out.
void ShowFD_Set (fd_set & theSet)
{
int total = 1024 / (8 * (int) sizeof (__fd_mask));
for (int i=0;i<total; i++)
{
if (theSet.fds_bits[i])
std::cout << i << " contains something " << std::hex << theSet.fds_bits[i] << std::endl;
}
}
void ShowParams(std::vector<Blockable *> const & v, int maxArg)
{
for (int i=0;i<v.size();i++)
{
std::cout << " fd:" << v[i]->GetFD();
}
std::cout << " maxarg: " << maxArg << std::endl;
}
//
const int FlexWait::FOREVER = -1;
Blockable * FlexWait::Wait(int timeout)
{
timeval tv;
int maxFD = 0;
int fd = 0;
tv.tv_sec = timeout/1000;
tv.tv_usec = (timeout%1000)*1000;
timeval * pTimeout = &tv;
if (timeout == -1)
pTimeout = 0;
fd_set theSet;
FD_ZERO(&theSet);
for (int i=0;i<v.size();i++)
{
fd = v[i]->GetFD();
maxFD = std::max(maxFD,fd);
FD_SET(fd,&theSet);
}
int maxFDArg = maxFD+1;
//ShowParams(v,maxFDArg);
int selectionFD = select(maxFDArg, &theSet, NULL, NULL, pTimeout);
if (selectionFD < 0)
{
perror("select");
ShowParams(v,maxFDArg);
throw std::string("Unexpected error in synchronization object");
}
if (selectionFD == 0)
return 0;
for (int i=0;i<v.size();i++)
{
int fd = v[i]->GetFD();
if (FD_ISSET(fd,&theSet))
return v[i];
}
throw std::string("Unknown error in synchronization object");
}