misc: Merge branch v20.1.0.3 hotfix into develop
[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
29 #include <sys/ioctl.h>
30 #include <sys/types.h>
31
32 #if defined(__sun__) || defined(__SUNPRO_CC)
33 #include <sys/file.h>
34
35 #endif
36
37 #include "base/pollevent.hh"
38
39 #include <fcntl.h>
40 #include <unistd.h>
41
42 #include <cerrno>
43 #include <csignal>
44 #include <cstring>
45
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"
52
53 PollQueue pollQueue;
54
55 /////////////////////////////////////////////////////
56 //
57 PollEvent::PollEvent(int _fd, int _events)
58 : queue(NULL), enabled(true)
59 {
60 pfd.fd = _fd;
61 pfd.events = _events;
62 pfd.revents = 0;
63 }
64
65 PollEvent::~PollEvent()
66 {
67 if (queue)
68 queue->remove(this);
69 }
70
71 void
72 PollEvent::disable()
73 {
74 if (!enabled) return;
75 enabled = false;
76
77 if (queue)
78 queue->copy();
79 }
80
81 void
82 PollEvent::enable()
83 {
84 if (enabled) return;
85 enabled = true;
86
87 if (queue)
88 queue->copy();
89 }
90
91 void
92 PollEvent::serialize(CheckpointOut &cp) const
93 {
94 SERIALIZE_SCALAR(pfd.fd);
95 SERIALIZE_SCALAR(pfd.events);
96 SERIALIZE_SCALAR(enabled);
97 }
98
99 void
100 PollEvent::unserialize(CheckpointIn &cp)
101 {
102 UNSERIALIZE_SCALAR(pfd.fd);
103 UNSERIALIZE_SCALAR(pfd.events);
104 UNSERIALIZE_SCALAR(enabled);
105 }
106
107 /////////////////////////////////////////////////////
108 //
109 PollQueue::PollQueue()
110 : poll_fds(NULL), max_size(0), num_fds(0)
111 { }
112
113 PollQueue::~PollQueue()
114 {
115 for (int i = 0; i < num_fds; i++)
116 setupAsyncIO(poll_fds[0].fd, false);
117
118 delete [] poll_fds;
119 }
120
121 void
122 PollQueue::copy()
123 {
124 eventvec_t::iterator i = events.begin();
125 eventvec_t::iterator end = events.end();
126
127 num_fds = 0;
128
129 while (i < end) {
130 if ((*i)->enabled)
131 poll_fds[num_fds++] = (*i)->pfd;
132 ++i;
133 }
134 }
135
136 void
137 PollQueue::remove(PollEvent *event)
138 {
139 eventvec_t::iterator i = events.begin();
140 eventvec_t::iterator end = events.end();
141
142 while (i < end) {
143 if (*i == event) {
144 events.erase(i);
145 copy();
146 event->queue = NULL;
147 return;
148 }
149
150 ++i;
151 }
152
153 panic("Event does not exist. Cannot remove.");
154 }
155
156 void
157 PollQueue::schedule(PollEvent *event)
158 {
159 if (event->queue)
160 panic("Event already scheduled!");
161
162 event->queue = this;
163 events.push_back(event);
164 setupAsyncIO(event->pfd.fd, true);
165
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) {
170 if (max_size > 0) {
171 delete [] poll_fds;
172 max_size *= 2;
173 } else {
174 max_size = 16;
175 }
176
177 poll_fds = new pollfd[max_size];
178 }
179
180 copy();
181 }
182
183 void
184 PollQueue::service()
185 {
186 int ret = poll(poll_fds, num_fds, 0);
187
188 if (ret <= 0)
189 return;
190
191 for (int i = 0; i < num_fds; i++) {
192 int revents = poll_fds[i].revents;
193 if (revents) {
194 events[i]->process(revents);
195 if (--ret <= 0)
196 break;
197 }
198 }
199 }
200
201 template <class ArgT>
202 static int fcntlHelper(int fd, int cmd, ArgT arg)
203 {
204 int retval = fcntl(fd, cmd, arg);
205 if (retval == -1) {
206 char *errstr = strerror(errno);
207 panic("fcntl(%d, %d, %s): \"%s\" when setting up async IO.\n",
208 errstr, fd, cmd, arg);
209 }
210 return retval;
211 }
212
213 static int fcntlHelper(int fd, int cmd)
214 {
215 int retval = fcntl(fd, cmd);
216 if (retval == -1) {
217 char *errstr = strerror(errno);
218 panic("fcntl(%d, %d): \"%s\" when setting up async IO.\n",
219 errstr, fd, cmd);
220 }
221 return retval;
222 }
223
224 void
225 PollQueue::setupAsyncIO(int fd, bool set)
226 {
227 int flags = fcntlHelper(fd, F_GETFL);
228
229 if (set)
230 flags |= FASYNC;
231 else
232 flags &= ~(FASYNC);
233
234 if (set)
235 fcntlHelper(fd, F_SETOWN, getpid());
236
237 fcntlHelper(fd, F_SETFL, flags);
238
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.
244 if (set) {
245 async_event = true;
246 async_io = true;
247 /* Wake up some event queue to handle event */
248 getEventQueue(0)->wakeup();
249 }
250 }