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>
34 #if defined(__sun__) || defined(__SUNPRO_CC)
39 #include "base/pollevent.hh"
48 #include "base/logging.hh"
49 #include "base/types.hh"
50 #include "sim/async.hh"
51 #include "sim/core.hh"
52 #include "sim/eventq.hh"
53 #include "sim/serialize.hh"
59 /////////////////////////////////////////////////////
61 PollEvent::PollEvent(int _fd
, int _events
)
62 : queue(NULL
), enabled(true)
69 PollEvent::~PollEvent()
96 PollEvent::serialize(CheckpointOut
&cp
) const
98 SERIALIZE_SCALAR(pfd
.fd
);
99 SERIALIZE_SCALAR(pfd
.events
);
100 SERIALIZE_SCALAR(enabled
);
104 PollEvent::unserialize(CheckpointIn
&cp
)
106 UNSERIALIZE_SCALAR(pfd
.fd
);
107 UNSERIALIZE_SCALAR(pfd
.events
);
108 UNSERIALIZE_SCALAR(enabled
);
111 /////////////////////////////////////////////////////
113 PollQueue::PollQueue()
114 : poll_fds(NULL
), max_size(0), num_fds(0)
117 PollQueue::~PollQueue()
119 for (int i
= 0; i
< num_fds
; i
++)
120 setupAsyncIO(poll_fds
[0].fd
, false);
128 eventvec_t::iterator i
= events
.begin();
129 eventvec_t::iterator end
= events
.end();
135 poll_fds
[num_fds
++] = (*i
)->pfd
;
141 PollQueue::remove(PollEvent
*event
)
143 eventvec_t::iterator i
= events
.begin();
144 eventvec_t::iterator end
= events
.end();
157 panic("Event does not exist. Cannot remove.");
161 PollQueue::schedule(PollEvent
*event
)
164 panic("Event already scheduled!");
167 events
.push_back(event
);
168 setupAsyncIO(event
->pfd
.fd
, true);
170 // if we ran out of space in the fd array, double the capacity
171 // if this is the first time that we've scheduled an event, create
172 // the array with an initial size of 16
173 if (++num_fds
> max_size
) {
181 poll_fds
= new pollfd
[max_size
];
190 int ret
= poll(poll_fds
, num_fds
, 0);
195 for (int i
= 0; i
< num_fds
; i
++) {
196 int revents
= poll_fds
[i
].revents
;
198 events
[i
]->process(revents
);
205 template <class ArgT
>
206 static int fcntlHelper(int fd
, int cmd
, ArgT arg
)
208 int retval
= fcntl(fd
, cmd
, arg
);
210 char *errstr
= strerror(errno
);
211 panic("fcntl(%d, %d, %s): \"%s\" when setting up async IO.\n",
212 errstr
, fd
, cmd
, arg
);
217 static int fcntlHelper(int fd
, int cmd
)
219 int retval
= fcntl(fd
, cmd
);
221 char *errstr
= strerror(errno
);
222 panic("fcntl(%d, %d): \"%s\" when setting up async IO.\n",
229 PollQueue::setupAsyncIO(int fd
, bool set
)
231 int flags
= fcntlHelper(fd
, F_GETFL
);
239 fcntlHelper(fd
, F_SETOWN
, getpid());
241 fcntlHelper(fd
, F_SETFL
, flags
);
243 // The file descriptor might already have events pending. We won't
244 // see them if they occurred before we set the FASYNC
245 // flag. Simulate a SIGIO to ensure that the FD will be polled in
246 // next iteration of the simulation loop. We could just poll it,
247 // but this is much simpler.
251 /* Wake up some event queue to handle event */
252 getEventQueue(0)->wakeup();