2 * Copyright 2016 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <folly/io/async/ScopedEventBaseThread.h>
20 #include <folly/Memory.h>
26 static void run(EventBaseManager* ebm, EventBase* eb) {
28 ebm->setEventBase(eb, false);
30 CHECK_NOTNULL(eb)->loopForever();
32 ebm->clearEventBase();
36 ScopedEventBaseThread::ScopedEventBaseThread(
38 EventBaseManager* ebm) :
45 ScopedEventBaseThread::ScopedEventBaseThread(
46 EventBaseManager* ebm) :
47 ScopedEventBaseThread(true, ebm) {}
49 ScopedEventBaseThread::~ScopedEventBaseThread() {
53 ScopedEventBaseThread::ScopedEventBaseThread(
54 ScopedEventBaseThread&& /* other */) noexcept = default;
56 ScopedEventBaseThread& ScopedEventBaseThread::operator=(
57 ScopedEventBaseThread&& /* other */) noexcept = default;
59 void ScopedEventBaseThread::start() {
63 eventBase_ = make_unique<EventBase>();
64 thread_ = make_unique<thread>(run, ebm_, eventBase_.get());
65 eventBase_->waitUntilRunning();
68 void ScopedEventBaseThread::stop() {
72 eventBase_->terminateLoopSoon();
78 bool ScopedEventBaseThread::running() {
79 CHECK(bool(eventBase_) == bool(thread_));
80 return eventBase_ && thread_;