2 * Copyright (c) 2002-2005 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.
28 * Authors: Nathan Binkert
31 #include <sys/ioctl.h>
32 #include <sys/types.h>
33 #if defined(__sun__) || defined(__SUNPRO_CC)
41 #include "base/misc.hh"
42 #include "base/pollevent.hh"
43 #include "base/types.hh"
44 #include "sim/async.hh"
45 #include "sim/core.hh"
46 #include "sim/serialize.hh"
52 /////////////////////////////////////////////////////
54 PollEvent::PollEvent(int _fd
, int _events
)
55 : queue(NULL
), enabled(true)
61 PollEvent::~PollEvent()
88 PollEvent::serialize(ostream
&os
)
90 SERIALIZE_SCALAR(pfd
.fd
);
91 SERIALIZE_SCALAR(pfd
.events
);
92 SERIALIZE_SCALAR(enabled
);
96 PollEvent::unserialize(Checkpoint
*cp
, const std::string
§ion
)
98 UNSERIALIZE_SCALAR(pfd
.fd
);
99 UNSERIALIZE_SCALAR(pfd
.events
);
100 UNSERIALIZE_SCALAR(enabled
);
103 /////////////////////////////////////////////////////
105 PollQueue::PollQueue()
106 : poll_fds(NULL
), max_size(0), num_fds(0)
109 PollQueue::~PollQueue()
112 for (int i
= 0; i
< num_fds
; i
++)
113 setupAsyncIO(poll_fds
[0].fd
, false);
121 eventvec_t::iterator i
= events
.begin();
122 eventvec_t::iterator end
= events
.end();
128 poll_fds
[num_fds
++] = (*i
)->pfd
;
134 PollQueue::remove(PollEvent
*event
)
136 eventvec_t::iterator i
= events
.begin();
137 eventvec_t::iterator end
= events
.end();
150 panic("Event does not exist. Cannot remove.");
154 PollQueue::schedule(PollEvent
*event
)
157 panic("Event already scheduled!");
160 events
.push_back(event
);
161 setupAsyncIO(event
->pfd
.fd
, true);
163 // if we ran out of space in the fd array, double the capacity
164 // if this is the first time that we've scheduled an event, create
165 // the array with an initial size of 16
166 if (++num_fds
> max_size
) {
175 poll_fds
= new pollfd
[max_size
];
184 int ret
= poll(poll_fds
, num_fds
, 0);
189 for (int i
= 0; i
< num_fds
; i
++) {
190 int revents
= poll_fds
[i
].revents
;
192 events
[i
]->process(revents
);
199 struct sigaction
PollQueue::oldio
;
200 struct sigaction
PollQueue::oldalrm
;
201 bool PollQueue::handler
= false;
204 PollQueue::setupAsyncIO(int fd
, bool set
)
206 int flags
= fcntl(fd
, F_GETFL
);
208 panic("Could not set up async IO");
215 if (fcntl(fd
, F_SETFL
, flags
) == -1)
216 panic("Could not set up async IO");
219 if (fcntl(fd
, F_SETOWN
, getpid()) == -1)
220 panic("Could not set up async IO");
225 PollQueue::setupHandler()
227 struct sigaction act
;
229 act
.sa_handler
= handleIO
;
230 sigemptyset(&act
.sa_mask
);
231 act
.sa_flags
= SA_RESTART
;
233 if (sigaction(SIGIO
, &act
, &oldio
) == -1)
234 panic("could not do sigaction");
236 act
.sa_handler
= handleALRM
;
237 sigemptyset(&act
.sa_mask
);
238 act
.sa_flags
= SA_RESTART
;
240 if (sigaction(SIGALRM
, &act
, &oldalrm
) == -1)
241 panic("could not do sigaction");
249 PollQueue::removeHandler()
251 if (sigaction(SIGIO
, &oldio
, NULL
) == -1)
252 panic("could not remove handler");
254 if (sigaction(SIGIO
, &oldalrm
, NULL
) == -1)
255 panic("could not remove handler");
259 PollQueue::handleIO(int sig
)
262 panic("Wrong Handler");
269 PollQueue::handleALRM(int sig
)
272 panic("Wrong Handler");