base,scons: Fixed `stats/hdf5.cc` CXXFlags to `-Wno-deprecated`
[gem5.git] / src / base / pollevent.cc
index 9ed6df4fe5d365754ae959b6073f1bf1e07a3dad..2b63c6b87c5a6ddbe180adc5395a6820746bd7f7 100644 (file)
  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * Authors: Nathan Binkert
  */
 
 #include <sys/ioctl.h>
 #include <sys/types.h>
+
 #if defined(__sun__) || defined(__SUNPRO_CC)
 #include <sys/file.h>
+
 #endif
 
+#include "base/pollevent.hh"
+
 #include <fcntl.h>
 #include <unistd.h>
 
+#include <cerrno>
 #include <csignal>
+#include <cstring>
 
-#include "base/misc.hh"
-#include "base/pollevent.hh"
+#include "base/logging.hh"
 #include "base/types.hh"
 #include "sim/async.hh"
 #include "sim/core.hh"
+#include "sim/eventq.hh"
 #include "sim/serialize.hh"
 
 using namespace std;
@@ -57,6 +61,7 @@ PollEvent::PollEvent(int _fd, int _events)
 {
     pfd.fd = _fd;
     pfd.events = _events;
+    pfd.revents = 0;
 }
 
 PollEvent::~PollEvent()
@@ -86,7 +91,7 @@ PollEvent::enable()
 }
 
 void
-PollEvent::serialize(ostream &os)
+PollEvent::serialize(CheckpointOut &cp) const
 {
     SERIALIZE_SCALAR(pfd.fd);
     SERIALIZE_SCALAR(pfd.events);
@@ -94,7 +99,7 @@ PollEvent::serialize(ostream &os)
 }
 
 void
-PollEvent::unserialize(Checkpoint *cp, const std::string &section)
+PollEvent::unserialize(CheckpointIn &cp)
 {
     UNSERIALIZE_SCALAR(pfd.fd);
     UNSERIALIZE_SCALAR(pfd.events);
@@ -195,25 +200,43 @@ PollQueue::service()
     }
 }
 
+template <class ArgT>
+static int fcntlHelper(int fd, int cmd, ArgT arg)
+{
+    int retval = fcntl(fd, cmd, arg);
+    if (retval == -1) {
+        char *errstr = strerror(errno);
+        panic("fcntl(%d, %d, %s): \"%s\" when setting up async IO.\n",
+              errstr, fd, cmd, arg);
+    }
+    return retval;
+}
+
+static int fcntlHelper(int fd, int cmd)
+{
+    int retval = fcntl(fd, cmd);
+    if (retval == -1) {
+        char *errstr = strerror(errno);
+        panic("fcntl(%d, %d): \"%s\" when setting up async IO.\n",
+              errstr, fd, cmd);
+    }
+    return retval;
+}
+
 void
 PollQueue::setupAsyncIO(int fd, bool set)
 {
-    int flags = fcntl(fd, F_GETFL);
-    if (flags == -1)
-        panic("Could not set up async IO");
+    int flags = fcntlHelper(fd, F_GETFL);
 
     if (set)
         flags |= FASYNC;
     else
         flags &= ~(FASYNC);
 
-    if (set) {
-      if (fcntl(fd, F_SETOWN, getpid()) == -1)
-        panic("Could not set up async IO");
-    }
+    if (set)
+        fcntlHelper(fd, F_SETOWN, getpid());
 
-    if (fcntl(fd, F_SETFL, flags) == -1)
-        panic("Could not set up async IO");
+    fcntlHelper(fd, F_SETFL, flags);
 
     // The file descriptor might already have events pending. We won't
     // see them if they occurred before we set the FASYNC
@@ -223,5 +246,7 @@ PollQueue::setupAsyncIO(int fd, bool set)
     if (set) {
         async_event = true;
         async_io = true;
+        /* Wake up some event queue to handle event */
+        getEventQueue(0)->wakeup();
     }
 }