allow AsyncSignalHandler to attach and detach from an EventBase
[folly.git] / folly / io / async / AsyncTimeout.cpp
index 7b6ab3db67e81c8af0ada0541f67c2ce9ee06673..28c8bbc14312974c36b059f094386f4333e5c6dc 100644 (file)
@@ -1,23 +1,19 @@
 /*
- * Copyright 2015 Facebook, Inc.
+ * Copyright 2004-present Facebook, Inc.
  *
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
  *
  *   http://www.apache.org/licenses/LICENSE-2.0
  *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
  */
+
 #include <folly/io/async/AsyncTimeout.h>
 #include <folly/io/async/EventBase.h>
 #include <folly/io/async/EventUtil.h>
@@ -31,7 +27,8 @@ namespace folly {
 AsyncTimeout::AsyncTimeout(TimeoutManager* timeoutManager)
     : timeoutManager_(timeoutManager) {
 
-  event_set(&event_, -1, EV_TIMEOUT, &AsyncTimeout::libeventCallback, this);
+  folly_event_set(
+      &event_, -1, EV_TIMEOUT, &AsyncTimeout::libeventCallback, this);
   event_.ev_base = nullptr;
   timeoutManager_->attachTimeoutManager(
       this,
@@ -42,7 +39,8 @@ AsyncTimeout::AsyncTimeout(TimeoutManager* timeoutManager)
 AsyncTimeout::AsyncTimeout(EventBase* eventBase)
     : timeoutManager_(eventBase) {
 
-  event_set(&event_, -1, EV_TIMEOUT, &AsyncTimeout::libeventCallback, this);
+  folly_event_set(
+      &event_, -1, EV_TIMEOUT, &AsyncTimeout::libeventCallback, this);
   event_.ev_base = nullptr;
   if (eventBase) {
     timeoutManager_->attachTimeoutManager(
@@ -56,7 +54,8 @@ AsyncTimeout::AsyncTimeout(TimeoutManager* timeoutManager,
                              InternalEnum internal)
     : timeoutManager_(timeoutManager) {
 
-  event_set(&event_, -1, EV_TIMEOUT, &AsyncTimeout::libeventCallback, this);
+  folly_event_set(
+      &event_, -1, EV_TIMEOUT, &AsyncTimeout::libeventCallback, this);
   event_.ev_base = nullptr;
   timeoutManager_->attachTimeoutManager(this, internal);
   RequestContext::saveContext();
@@ -65,14 +64,16 @@ AsyncTimeout::AsyncTimeout(TimeoutManager* timeoutManager,
 AsyncTimeout::AsyncTimeout(EventBase* eventBase, InternalEnum internal)
     : timeoutManager_(eventBase) {
 
-  event_set(&event_, -1, EV_TIMEOUT, &AsyncTimeout::libeventCallback, this);
+  folly_event_set(
+      &event_, -1, EV_TIMEOUT, &AsyncTimeout::libeventCallback, this);
   event_.ev_base = nullptr;
   timeoutManager_->attachTimeoutManager(this, internal);
   RequestContext::saveContext();
 }
 
 AsyncTimeout::AsyncTimeout(): timeoutManager_(nullptr) {
-  event_set(&event_, -1, EV_TIMEOUT, &AsyncTimeout::libeventCallback, this);
+  folly_event_set(
+      &event_, -1, EV_TIMEOUT, &AsyncTimeout::libeventCallback, this);
   event_.ev_base = nullptr;
   RequestContext::saveContext();
 }
@@ -123,9 +124,7 @@ void AsyncTimeout::detachTimeoutManager() {
   // currently installed.
   if (isScheduled()) {
     // Programmer bug.  Abort the program.
-    LOG(ERROR) << "detachEventBase() called on scheduled timeout; aborting";
-    abort();
-    return;
+    LOG(FATAL) << "detachEventBase() called on scheduled timeout; aborting";
   }
 
   if (timeoutManager_) {
@@ -138,23 +137,23 @@ void AsyncTimeout::detachEventBase() {
   detachTimeoutManager();
 }
 
-void AsyncTimeout::libeventCallback(int fd, short events, void* arg) {
+void AsyncTimeout::libeventCallback(libevent_fd_t fd, short events, void* arg) {
   AsyncTimeout* timeout = reinterpret_cast<AsyncTimeout*>(arg);
-  assert(fd == -1);
+  assert(libeventFdToFd(fd) == -1);
   assert(events == EV_TIMEOUT);
+  // prevent unused variable warnings
+  (void)fd;
+  (void)events;
 
   // double check that ev_flags gets reset when the timeout is not running
   assert((event_ref_flags(&timeout->event_) & ~EVLIST_INTERNAL) == EVLIST_INIT);
 
   // this can't possibly fire if timeout->eventBase_ is nullptr
-  (void) timeout->timeoutManager_->bumpHandlingTime();
+  timeout->timeoutManager_->bumpHandlingTime();
 
-  auto old_ctx =
-    RequestContext::setContext(timeout->context_);
+  RequestContextScopeGuard rctx(timeout->context_);
 
   timeout->timeoutExpired();
-
-  RequestContext::setContext(old_ctx);
 }
 
 } // folly