/*
- * Copyright 2014 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>
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,
TimeoutManager::InternalEnum::NORMAL);
- RequestContext::getStaticContext();
+ RequestContext::saveContext();
}
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;
- timeoutManager_->attachTimeoutManager(
+ if (eventBase) {
+ timeoutManager_->attachTimeoutManager(
this,
TimeoutManager::InternalEnum::NORMAL);
- RequestContext::getStaticContext();
+ }
+ RequestContext::saveContext();
}
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::getStaticContext();
+ RequestContext::saveContext();
}
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::getStaticContext();
+ 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::getStaticContext();
+ RequestContext::saveContext();
}
AsyncTimeout::~AsyncTimeout() {
cancelTimeout();
}
-bool AsyncTimeout::scheduleTimeout(std::chrono::milliseconds timeout) {
+bool AsyncTimeout::scheduleTimeout(TimeoutManager::timeout_type timeout) {
assert(timeoutManager_ != nullptr);
context_ = RequestContext::saveContext();
return timeoutManager_->scheduleTimeout(this, timeout);
}
bool AsyncTimeout::scheduleTimeout(uint32_t milliseconds) {
- return scheduleTimeout(std::chrono::milliseconds(milliseconds));
+ return scheduleTimeout(TimeoutManager::timeout_type(milliseconds));
}
void AsyncTimeout::cancelTimeout() {
// 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_) {
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((timeout->event_.ev_flags & ~EVLIST_INTERNAL) == EVLIST_INIT);
+ 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