cpu: Fix retries on barrier/store in Minor's store buffer
[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 * Authors: Nathan Binkert
29 */
30
31 #include <sys/ioctl.h>
32 #include <sys/types.h>
33 #if defined(__sun__) || defined(__SUNPRO_CC)
34 #include <sys/file.h>
35 #endif
36
37 #include <fcntl.h>
38 #include <unistd.h>
39
40 #include <csignal>
41
42 #include "base/misc.hh"
43 #include "base/pollevent.hh"
44 #include "base/types.hh"
45 #include "sim/async.hh"
46 #include "sim/core.hh"
47 #include "sim/eventq.hh"
48 #include "sim/serialize.hh"
49
50 using namespace std;
51
52 PollQueue pollQueue;
53
54 /////////////////////////////////////////////////////
55 //
56 PollEvent::PollEvent(int _fd, int _events)
57 : queue(NULL), enabled(true)
58 {
59 pfd.fd = _fd;
60 pfd.events = _events;
61 pfd.revents = 0;
62 }
63
64 PollEvent::~PollEvent()
65 {
66 if (queue)
67 queue->remove(this);
68 }
69
70 void
71 PollEvent::disable()
72 {
73 if (!enabled) return;
74 enabled = false;
75
76 if (queue)
77 queue->copy();
78 }
79
80 void
81 PollEvent::enable()
82 {
83 if (enabled) return;
84 enabled = true;
85
86 if (queue)
87 queue->copy();
88 }
89
90 void
91 PollEvent::serialize(ostream &os)
92 {
93 SERIALIZE_SCALAR(pfd.fd);
94 SERIALIZE_SCALAR(pfd.events);
95 SERIALIZE_SCALAR(enabled);
96 }
97
98 void
99 PollEvent::unserialize(Checkpoint *cp, const std::string &section)
100 {
101 UNSERIALIZE_SCALAR(pfd.fd);
102 UNSERIALIZE_SCALAR(pfd.events);
103 UNSERIALIZE_SCALAR(enabled);
104 }
105
106 /////////////////////////////////////////////////////
107 //
108 PollQueue::PollQueue()
109 : poll_fds(NULL), max_size(0), num_fds(0)
110 { }
111
112 PollQueue::~PollQueue()
113 {
114 for (int i = 0; i < num_fds; i++)
115 setupAsyncIO(poll_fds[0].fd, false);
116
117 delete [] poll_fds;
118 }
119
120 void
121 PollQueue::copy()
122 {
123 eventvec_t::iterator i = events.begin();
124 eventvec_t::iterator end = events.end();
125
126 num_fds = 0;
127
128 while (i < end) {
129 if ((*i)->enabled)
130 poll_fds[num_fds++] = (*i)->pfd;
131 ++i;
132 }
133 }
134
135 void
136 PollQueue::remove(PollEvent *event)
137 {
138 eventvec_t::iterator i = events.begin();
139 eventvec_t::iterator end = events.end();
140
141 while (i < end) {
142 if (*i == event) {
143 events.erase(i);
144 copy();
145 event->queue = NULL;
146 return;
147 }
148
149 ++i;
150 }
151
152 panic("Event does not exist. Cannot remove.");
153 }
154
155 void
156 PollQueue::schedule(PollEvent *event)
157 {
158 if (event->queue)
159 panic("Event already scheduled!");
160
161 event->queue = this;
162 events.push_back(event);
163 setupAsyncIO(event->pfd.fd, true);
164
165 // if we ran out of space in the fd array, double the capacity
166 // if this is the first time that we've scheduled an event, create
167 // the array with an initial size of 16
168 if (++num_fds > max_size) {
169 if (max_size > 0) {
170 delete [] poll_fds;
171 max_size *= 2;
172 } else {
173 max_size = 16;
174 }
175
176 poll_fds = new pollfd[max_size];
177 }
178
179 copy();
180 }
181
182 void
183 PollQueue::service()
184 {
185 int ret = poll(poll_fds, num_fds, 0);
186
187 if (ret <= 0)
188 return;
189
190 for (int i = 0; i < num_fds; i++) {
191 int revents = poll_fds[i].revents;
192 if (revents) {
193 events[i]->process(revents);
194 if (--ret <= 0)
195 break;
196 }
197 }
198 }
199
200 void
201 PollQueue::setupAsyncIO(int fd, bool set)
202 {
203 int flags = fcntl(fd, F_GETFL);
204 if (flags == -1)
205 panic("Could not set up async IO");
206
207 if (set)
208 flags |= FASYNC;
209 else
210 flags &= ~(FASYNC);
211
212 if (set) {
213 if (fcntl(fd, F_SETOWN, getpid()) == -1)
214 panic("Could not set up async IO");
215 }
216
217 if (fcntl(fd, F_SETFL, flags) == -1)
218 panic("Could not set up async IO");
219
220 // The file descriptor might already have events pending. We won't
221 // see them if they occurred before we set the FASYNC
222 // flag. Simulate a SIGIO to ensure that the FD will be polled in
223 // next iteration of the simulation loop. We could just poll it,
224 // but this is much simpler.
225 if (set) {
226 async_event = true;
227 async_io = true;
228 /* Wake up some event queue to handle event */
229 getEventQueue(0)->wakeup();
230 }
231 }