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.
29 #include <sys/ioctl.h>
30 #include <sys/types.h>
32 #if defined(__sun__) || defined(__SUNPRO_CC)
37 #include "base/pollevent.hh"
46 #include "base/logging.hh"
47 #include "base/types.hh"
48 #include "sim/async.hh"
49 #include "sim/core.hh"
50 #include "sim/eventq.hh"
51 #include "sim/serialize.hh"
55 /////////////////////////////////////////////////////
57 PollEvent::PollEvent(int _fd
, int _events
)
58 : queue(NULL
), enabled(true)
65 PollEvent::~PollEvent()
92 PollEvent::serialize(CheckpointOut
&cp
) const
94 SERIALIZE_SCALAR(pfd
.fd
);
95 SERIALIZE_SCALAR(pfd
.events
);
96 SERIALIZE_SCALAR(enabled
);
100 PollEvent::unserialize(CheckpointIn
&cp
)
102 UNSERIALIZE_SCALAR(pfd
.fd
);
103 UNSERIALIZE_SCALAR(pfd
.events
);
104 UNSERIALIZE_SCALAR(enabled
);
107 /////////////////////////////////////////////////////
109 PollQueue::PollQueue()
110 : poll_fds(NULL
), max_size(0), num_fds(0)
113 PollQueue::~PollQueue()
115 for (int i
= 0; i
< num_fds
; i
++)
116 setupAsyncIO(poll_fds
[0].fd
, false);
124 eventvec_t::iterator i
= events
.begin();
125 eventvec_t::iterator end
= events
.end();
131 poll_fds
[num_fds
++] = (*i
)->pfd
;
137 PollQueue::remove(PollEvent
*event
)
139 eventvec_t::iterator i
= events
.begin();
140 eventvec_t::iterator end
= events
.end();
153 panic("Event does not exist. Cannot remove.");
157 PollQueue::schedule(PollEvent
*event
)
160 panic("Event already scheduled!");
163 events
.push_back(event
);
164 setupAsyncIO(event
->pfd
.fd
, true);
166 // if we ran out of space in the fd array, double the capacity
167 // if this is the first time that we've scheduled an event, create
168 // the array with an initial size of 16
169 if (++num_fds
> max_size
) {
177 poll_fds
= new pollfd
[max_size
];
186 int ret
= poll(poll_fds
, num_fds
, 0);
191 for (int i
= 0; i
< num_fds
; i
++) {
192 int revents
= poll_fds
[i
].revents
;
194 events
[i
]->process(revents
);
201 template <class ArgT
>
202 static int fcntlHelper(int fd
, int cmd
, ArgT arg
)
204 int retval
= fcntl(fd
, cmd
, arg
);
206 char *errstr
= strerror(errno
);
207 panic("fcntl(%d, %d, %s): \"%s\" when setting up async IO.\n",
208 errstr
, fd
, cmd
, arg
);
213 static int fcntlHelper(int fd
, int cmd
)
215 int retval
= fcntl(fd
, cmd
);
217 char *errstr
= strerror(errno
);
218 panic("fcntl(%d, %d): \"%s\" when setting up async IO.\n",
225 PollQueue::setupAsyncIO(int fd
, bool set
)
227 int flags
= fcntlHelper(fd
, F_GETFL
);
235 fcntlHelper(fd
, F_SETOWN
, getpid());
237 fcntlHelper(fd
, F_SETFL
, flags
);
239 // The file descriptor might already have events pending. We won't
240 // see them if they occurred before we set the FASYNC
241 // flag. Simulate a SIGIO to ensure that the FD will be polled in
242 // next iteration of the simulation loop. We could just poll it,
243 // but this is much simpler.
247 /* Wake up some event queue to handle event */
248 getEventQueue(0)->wakeup();