AsyncIO: CHECK preconditions
authorTudor Bosman <tudorb@fb.com>
Tue, 5 Feb 2013 18:51:48 +0000 (10:51 -0800)
committerJordan DeLong <jdelong@fb.com>
Tue, 19 Mar 2013 00:05:12 +0000 (17:05 -0700)
Test Plan: async_io_test

Reviewed By: delong.j@fb.com

FB internal diff: D698919

folly/experimental/io/AsyncIO.cpp

index 2080b72038e8c15de1e2e8e9cc2d4eeedd844786..1b3b07cb8f54826af0a51fd5717f7d0889977784 100644 (file)
@@ -32,9 +32,7 @@ AsyncIO::AsyncIO(size_t capacity, PollMode pollMode)
     pending_(0),
     capacity_(capacity),
     pollFd_(-1) {
-  if (UNLIKELY(capacity_ == 0)) {
-    throw std::out_of_range("AsyncIO: capacity must not be 0");
-  }
+  CHECK_GT(capacity_, 0);
   completed_.reserve(capacity_);
   if (pollMode == POLLABLE) {
     pollFd_ = eventfd(0, EFD_NONBLOCK);
@@ -100,12 +98,8 @@ void AsyncIO::initializeContext() {
 }
 
 void AsyncIO::submit(Op* op, iocb* cb) {
-  if (UNLIKELY(pending_ >= capacity_)) {
-    throw std::out_of_range("AsyncIO: too many pending requests");
-  }
-  if (UNLIKELY(op->state() != Op::UNINITIALIZED)) {
-    throw std::logic_error("AsyncIO: Invalid Op state in submit");
-  }
+  CHECK_EQ(op->state(), Op::UNINITIALIZED);
+  CHECK_LT(pending_, capacity_) << "too many pending requests";
   initializeContext();  // on demand
   cb->data = op;
   if (pollFd_ != -1) {
@@ -119,23 +113,14 @@ void AsyncIO::submit(Op* op, iocb* cb) {
 }
 
 Range<AsyncIO::Op**> AsyncIO::wait(size_t minRequests) {
-  if (UNLIKELY(!ctx_)) {
-    throw std::logic_error("AsyncIO: wait called with no requests");
-  }
-  if (UNLIKELY(pollFd_ != -1)) {
-    throw std::logic_error("AsyncIO: wait not allowed on pollable object");
-  }
+  CHECK(ctx_);
+  CHECK_EQ(pollFd_, -1) << "wait() only allowed on non-pollable object";
   return doWait(minRequests, pending_);
 }
 
 Range<AsyncIO::Op**> AsyncIO::pollCompleted() {
-  if (UNLIKELY(!ctx_)) {
-    throw std::logic_error("AsyncIO: pollCompleted called with no requests");
-  }
-  if (UNLIKELY(pollFd_ == -1)) {
-    throw std::logic_error(
-        "AsyncIO: pollCompleted not allowed on non-pollable object");
-  }
+  CHECK(ctx_);
+  CHECK_NE(pollFd_, -1) << "pollCompleted() only allowed on pollable object";
   uint64_t numEvents;
   // This sets the eventFd counter to 0, see
   // http://www.kernel.org/doc/man-pages/online/pages/man2/eventfd.2.html
@@ -189,9 +174,7 @@ AsyncIO::Op::Op()
 }
 
 void AsyncIO::Op::reset() {
-  if (UNLIKELY(state_ == PENDING)) {
-    throw std::logic_error("AsyncIO: invalid state for reset");
-  }
+  CHECK_NE(state_, PENDING);
   state_ = UNINITIALIZED;
   result_ = -EINVAL;
 }
@@ -215,9 +198,7 @@ void AsyncIO::Op::complete(ssize_t result) {
 void AsyncIO::Op::onCompleted() { }  // default: do nothing
 
 ssize_t AsyncIO::Op::result() const {
-  if (UNLIKELY(state_ != COMPLETED)) {
-    throw std::logic_error("AsyncIO: Invalid Op state in result");
-  }
+  CHECK_EQ(state_, COMPLETED);
   return result_;
 }