proxygen
AsyncTimeout.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2014-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
20 #include <folly/io/async/Request.h>
21 
22 #include <assert.h>
23 #include <glog/logging.h>
24 
25 namespace folly {
26 
28  : timeoutManager_(timeoutManager) {
30  &event_, -1, EV_TIMEOUT, &AsyncTimeout::libeventCallback, this);
31  event_.ev_base = nullptr;
34 }
35 
38  &event_, -1, EV_TIMEOUT, &AsyncTimeout::libeventCallback, this);
39  event_.ev_base = nullptr;
40  if (eventBase) {
43  }
44 }
45 
47  TimeoutManager* timeoutManager,
48  InternalEnum internal)
49  : timeoutManager_(timeoutManager) {
51  &event_, -1, EV_TIMEOUT, &AsyncTimeout::libeventCallback, this);
52  event_.ev_base = nullptr;
53  timeoutManager_->attachTimeoutManager(this, internal);
54 }
55 
57  : timeoutManager_(eventBase) {
59  &event_, -1, EV_TIMEOUT, &AsyncTimeout::libeventCallback, this);
60  event_.ev_base = nullptr;
61  timeoutManager_->attachTimeoutManager(this, internal);
62 }
63 
66  &event_, -1, EV_TIMEOUT, &AsyncTimeout::libeventCallback, this);
67  event_.ev_base = nullptr;
68 }
69 
71  cancelTimeout();
72 }
73 
75  assert(timeoutManager_ != nullptr);
77  return timeoutManager_->scheduleTimeout(this, timeout);
78 }
79 
81  return scheduleTimeout(TimeoutManager::timeout_type(milliseconds));
82 }
83 
85  if (isScheduled()) {
87  context_.reset();
88  }
89 }
90 
93 }
94 
96  TimeoutManager* timeoutManager,
97  InternalEnum internal) {
98  // This also implies no timeout is scheduled.
99  assert(timeoutManager_ == nullptr);
100  assert(timeoutManager->isInTimeoutManagerThread());
101  timeoutManager_ = timeoutManager;
102 
103  timeoutManager_->attachTimeoutManager(this, internal);
104 }
105 
107  EventBase* eventBase,
108  InternalEnum internal) {
109  attachTimeoutManager(eventBase, internal);
110 }
111 
113  // Only allow the event base to be changed if the timeout is not
114  // currently installed.
115  if (isScheduled()) {
116  // Programmer bug. Abort the program.
117  LOG(FATAL) << "detachEventBase() called on scheduled timeout; aborting";
118  }
119 
120  if (timeoutManager_) {
122  timeoutManager_ = nullptr;
123  }
124 }
125 
128 }
129 
130 void AsyncTimeout::libeventCallback(libevent_fd_t fd, short events, void* arg) {
131  AsyncTimeout* timeout = reinterpret_cast<AsyncTimeout*>(arg);
132  assert(libeventFdToFd(fd) == -1);
133  assert(events == EV_TIMEOUT);
134  // prevent unused variable warnings
135  (void)fd;
136  (void)events;
137 
138  // double check that ev_flags gets reset when the timeout is not running
139  assert((event_ref_flags(&timeout->event_) & ~EVLIST_INTERNAL) == EVLIST_INIT);
140 
141  // this can't possibly fire if timeout->eventBase_ is nullptr
142  timeout->timeoutManager_->bumpHandlingTime();
143 
144  RequestContextScopeGuard rctx(timeout->context_);
145 
146  timeout->timeoutExpired();
147 }
148 
149 } // namespace folly
struct event event_
Definition: AsyncTimeout.h:221
std::chrono::milliseconds timeout_type
virtual bool scheduleTimeout(AsyncTimeout *obj, timeout_type timeout)=0
std::shared_ptr< RequestContext > context_
Definition: AsyncTimeout.h:231
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
#define nullptr
Definition: http_parser.c:41
auto event_ref_flags(struct event *ev) -> decltype(std::ref(ev->ev_flags))
Definition: EventUtil.h:41
void folly_event_set(event *e, int fd, short s, EventSetCallback f, void *arg)
Definition: Event.h:50
virtual void attachTimeoutManager(AsyncTimeout *obj, InternalEnum internal)=0
static std::shared_ptr< RequestContext > saveContext()
Definition: Request.h:196
static void libeventCallback(libevent_fd_t fd, short events, void *arg)
void attachTimeoutManager(TimeoutManager *timeoutManager, InternalEnum internal=InternalEnum::NORMAL)
static bool isEventRegistered(const struct event *ev)
Definition: EventUtil.h:51
void attachEventBase(EventBase *eventBase, InternalEnum internal=InternalEnum::NORMAL)
virtual void timeoutExpired() noexcept=0
virtual bool isInTimeoutManagerThread()=0
virtual void detachTimeoutManager(AsyncTimeout *obj)=0
bool scheduleTimeout(uint32_t milliseconds)
bool isScheduled() const
int libeventFdToFd(libevent_fd_t fd)
Definition: Event.h:44
TimeoutManager * timeoutManager_
Definition: AsyncTimeout.h:228
virtual void bumpHandlingTime()=0
int libevent_fd_t
Definition: Event.h:37
virtual void cancelTimeout(AsyncTimeout *obj)=0