5adfe52a153858e06cf39ac917b7f3cb9afc8064
[folly.git] / folly / io / async / EventBaseThread.cpp
1 /*
2  * Copyright 2017 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
17 #include <folly/io/async/EventBaseThread.h>
18
19 #include <folly/Memory.h>
20 #include <folly/io/async/ScopedEventBaseThread.h>
21
22 namespace folly {
23
24 EventBaseThread::EventBaseThread() : EventBaseThread(true) {}
25
26 EventBaseThread::EventBaseThread(bool autostart, EventBaseManager* ebm)
27     : ebm_(ebm) {
28   if (autostart) {
29     start();
30   }
31 }
32
33 EventBaseThread::EventBaseThread(EventBaseManager* ebm)
34     : EventBaseThread(true, ebm) {}
35
36 EventBaseThread::~EventBaseThread() = default;
37
38 EventBaseThread::EventBaseThread(EventBaseThread&&) noexcept = default;
39 EventBaseThread& EventBaseThread::operator=(EventBaseThread&&) noexcept =
40     default;
41
42 EventBase* EventBaseThread::getEventBase() const {
43   return th_ ? th_->getEventBase() : nullptr;
44 }
45
46 bool EventBaseThread::running() const {
47   return !!th_;
48 }
49
50 void EventBaseThread::start() {
51   if (th_) {
52     return;
53   }
54   th_ = make_unique<ScopedEventBaseThread>(ebm_);
55 }
56
57 void EventBaseThread::stop() {
58   th_ = nullptr;
59 }
60 }