Cache: Remove dangling doWriteback declaration
[gem5.git] / src / base / pollevent.cc
1 /*
2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Nathan Binkert
29 */
30
31 #include <sys/ioctl.h>
32 #include <sys/types.h>
33 #if defined(__sun__) || defined(__SUNPRO_CC)
34 #include <sys/file.h>
35 #endif
36
37 #include <fcntl.h>
38 #include <unistd.h>
39
40 #include <csignal>
41
42 #include "base/misc.hh"
43 #include "base/pollevent.hh"
44 #include "base/types.hh"
45 #include "sim/async.hh"
46 #include "sim/core.hh"
47 #include "sim/serialize.hh"
48
49 using namespace std;
50
51 PollQueue pollQueue;
52
53 /////////////////////////////////////////////////////
54 //
55 PollEvent::PollEvent(int _fd, int _events)
56 : queue(NULL), enabled(true)
57 {
58 pfd.fd = _fd;
59 pfd.events = _events;
60 }
61
62 PollEvent::~PollEvent()
63 {
64 if (queue)
65 queue->remove(this);
66 }
67
68 void
69 PollEvent::disable()
70 {
71 if (!enabled) return;
72 enabled = false;
73
74 if (queue)
75 queue->copy();
76 }
77
78 void
79 PollEvent::enable()
80 {
81 if (enabled) return;
82 enabled = true;
83
84 if (queue)
85 queue->copy();
86 }
87
88 void
89 PollEvent::serialize(ostream &os)
90 {
91 SERIALIZE_SCALAR(pfd.fd);
92 SERIALIZE_SCALAR(pfd.events);
93 SERIALIZE_SCALAR(enabled);
94 }
95
96 void
97 PollEvent::unserialize(Checkpoint *cp, const std::string &section)
98 {
99 UNSERIALIZE_SCALAR(pfd.fd);
100 UNSERIALIZE_SCALAR(pfd.events);
101 UNSERIALIZE_SCALAR(enabled);
102 }
103
104 /////////////////////////////////////////////////////
105 //
106 PollQueue::PollQueue()
107 : poll_fds(NULL), max_size(0), num_fds(0)
108 { }
109
110 PollQueue::~PollQueue()
111 {
112 removeHandler();
113 for (int i = 0; i < num_fds; i++)
114 setupAsyncIO(poll_fds[0].fd, false);
115
116 delete [] poll_fds;
117 }
118
119 void
120 PollQueue::copy()
121 {
122 eventvec_t::iterator i = events.begin();
123 eventvec_t::iterator end = events.end();
124
125 num_fds = 0;
126
127 while (i < end) {
128 if ((*i)->enabled)
129 poll_fds[num_fds++] = (*i)->pfd;
130 ++i;
131 }
132 }
133
134 void
135 PollQueue::remove(PollEvent *event)
136 {
137 eventvec_t::iterator i = events.begin();
138 eventvec_t::iterator end = events.end();
139
140 while (i < end) {
141 if (*i == event) {
142 events.erase(i);
143 copy();
144 event->queue = NULL;
145 return;
146 }
147
148 ++i;
149 }
150
151 panic("Event does not exist. Cannot remove.");
152 }
153
154 void
155 PollQueue::schedule(PollEvent *event)
156 {
157 if (event->queue)
158 panic("Event already scheduled!");
159
160 event->queue = this;
161 events.push_back(event);
162 setupAsyncIO(event->pfd.fd, true);
163
164 // if we ran out of space in the fd array, double the capacity
165 // if this is the first time that we've scheduled an event, create
166 // the array with an initial size of 16
167 if (++num_fds > max_size) {
168 if (max_size > 0) {
169 delete [] poll_fds;
170 max_size *= 2;
171 } else {
172 max_size = 16;
173 setupHandler();
174 }
175
176 poll_fds = new pollfd[max_size];
177 }
178
179 copy();
180 }
181
182 void
183 PollQueue::service()
184 {
185 int ret = poll(poll_fds, num_fds, 0);
186
187 if (ret <= 0)
188 return;
189
190 for (int i = 0; i < num_fds; i++) {
191 int revents = poll_fds[i].revents;
192 if (revents) {
193 events[i]->process(revents);
194 if (--ret <= 0)
195 break;
196 }
197 }
198 }
199
200 struct sigaction PollQueue::oldio;
201 struct sigaction PollQueue::oldalrm;
202 bool PollQueue::handler = false;
203
204 void
205 PollQueue::setupAsyncIO(int fd, bool set)
206 {
207 int flags = fcntl(fd, F_GETFL);
208 if (flags == -1)
209 panic("Could not set up async IO");
210
211 if (set)
212 flags |= FASYNC;
213 else
214 flags &= ~(FASYNC);
215
216 if (fcntl(fd, F_SETFL, flags) == -1)
217 panic("Could not set up async IO");
218
219 if (set) {
220 if (fcntl(fd, F_SETOWN, getpid()) == -1)
221 panic("Could not set up async IO");
222 }
223 }
224
225 void
226 PollQueue::setupHandler()
227 {
228 struct sigaction act;
229
230 act.sa_handler = handleIO;
231 sigemptyset(&act.sa_mask);
232 act.sa_flags = SA_RESTART;
233
234 if (sigaction(SIGIO, &act, &oldio) == -1)
235 panic("could not do sigaction");
236
237 act.sa_handler = handleALRM;
238 sigemptyset(&act.sa_mask);
239 act.sa_flags = SA_RESTART;
240
241 if (sigaction(SIGALRM, &act, &oldalrm) == -1)
242 panic("could not do sigaction");
243
244 alarm(1);
245
246 handler = true;
247 }
248
249 void
250 PollQueue::removeHandler()
251 {
252 if (sigaction(SIGIO, &oldio, NULL) == -1)
253 panic("could not remove handler");
254
255 if (sigaction(SIGIO, &oldalrm, NULL) == -1)
256 panic("could not remove handler");
257 }
258
259 void
260 PollQueue::handleIO(int sig)
261 {
262 if (sig != SIGIO)
263 panic("Wrong Handler");
264
265 async_event = true;
266 async_io = true;
267 }
268
269 void
270 PollQueue::handleALRM(int sig)
271 {
272 if (sig != SIGALRM)
273 panic("Wrong Handler");
274
275 async_event = true;
276 async_alarm = true;
277 alarm(1);
278 }
279