2 * Copyright (c) 2002-2003 The Regents of The University of Michigan
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.
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.
29 #include <sys/ioctl.h>
30 #include <sys/types.h>
36 #include "sim/async.hh"
37 #include "sim/host.hh"
38 #include "base/misc.hh"
39 #include "base/pollevent.hh"
40 #include "sim/universe.hh"
41 #include "sim/serialize.hh"
47 /////////////////////////////////////////////////////
49 PollEvent::PollEvent(int _fd
, int _events
)
50 : queue(NULL
), enabled(true)
56 PollEvent::~PollEvent()
83 PollEvent::serialize(ostream
&os
)
85 SERIALIZE_SCALAR(pfd
.fd
);
86 SERIALIZE_SCALAR(pfd
.events
);
87 SERIALIZE_SCALAR(enabled
);
91 PollEvent::unserialize(Checkpoint
*cp
, const std::string
§ion
)
93 UNSERIALIZE_SCALAR(pfd
.fd
);
94 UNSERIALIZE_SCALAR(pfd
.events
);
95 UNSERIALIZE_SCALAR(enabled
);
98 /////////////////////////////////////////////////////
100 PollQueue::PollQueue()
101 : poll_fds(NULL
), max_size(0), num_fds(0)
104 PollQueue::~PollQueue()
107 for (int i
= 0; i
< num_fds
; i
++)
108 setupAsyncIO(poll_fds
[0].fd
, false);
116 eventvec_t::iterator i
= events
.begin();
117 eventvec_t::iterator end
= events
.end();
123 poll_fds
[num_fds
++] = (*i
)->pfd
;
129 PollQueue::remove(PollEvent
*event
)
131 eventvec_t::iterator i
= events
.begin();
132 eventvec_t::iterator end
= events
.end();
145 panic("Event does not exist. Cannot remove.");
149 PollQueue::schedule(PollEvent
*event
)
152 panic("Event already scheduled!");
155 events
.push_back(event
);
156 setupAsyncIO(event
->pfd
.fd
, true);
158 // if we ran out of space in the fd array, double the capacity
159 // if this is the first time that we've scheduled an event, create
160 // the array with an initial size of 16
161 if (++num_fds
> max_size
) {
170 poll_fds
= new pollfd
[max_size
];
179 int ret
= poll(poll_fds
, num_fds
, 0);
184 for (int i
= 0; i
< num_fds
; i
++) {
185 int revents
= poll_fds
[i
].revents
;
187 events
[i
]->process(revents
);
194 struct sigaction
PollQueue::oldio
;
195 struct sigaction
PollQueue::oldalrm
;
196 bool PollQueue::handler
= false;
199 PollQueue::setupAsyncIO(int fd
, bool set
)
201 int flags
= fcntl(fd
, F_GETFL
);
203 panic("Could not set up async IO");
210 if (fcntl(fd
, F_SETFL
, flags
) == -1)
211 panic("Could not set up async IO");
214 if (fcntl(fd
, F_SETOWN
, getpid()) == -1)
215 panic("Could not set up async IO");
220 PollQueue::setupHandler()
222 struct sigaction act
;
224 act
.sa_handler
= handleIO
;
225 sigemptyset(&act
.sa_mask
);
226 act
.sa_flags
= SA_RESTART
;
228 if (sigaction(SIGIO
, &act
, &oldio
) == -1)
229 panic("could not do sigaction");
231 act
.sa_handler
= handleALRM
;
232 sigemptyset(&act
.sa_mask
);
233 act
.sa_flags
= SA_RESTART
;
235 if (sigaction(SIGALRM
, &act
, &oldalrm
) == -1)
236 panic("could not do sigaction");
244 PollQueue::removeHandler()
246 if (sigaction(SIGIO
, &oldio
, NULL
) == -1)
247 panic("could not remove handler");
249 if (sigaction(SIGIO
, &oldalrm
, NULL
) == -1)
250 panic("could not remove handler");
254 PollQueue::handleIO(int sig
)
257 panic("Wrong Handler");
264 PollQueue::handleALRM(int sig
)
267 panic("Wrong Handler");