proxygen
ScopedEventBaseThread.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2015-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 
18 
19 #include <thread>
20 
21 #include <folly/Function.h>
22 #include <folly/Range.h>
25 
26 using namespace std;
27 
28 namespace folly {
29 
30 static void run(
31  EventBaseManager* ebm,
32  EventBase* eb,
34  const StringPiece& name) {
35  if (name.size()) {
37  }
38 
39  ebm->setEventBase(eb, false);
40  eb->loopForever();
41 
42  // must destruct in io thread for on-destruction callbacks
44  eb->runOnDestruction(&cb);
45  // wait until terminateLoopSoon() is complete
46  stop->wait();
47  eb->~EventBase();
48 }
49 
50 ScopedEventBaseThread::ScopedEventBaseThread()
52 
54  : ScopedEventBaseThread(nullptr, name) {}
55 
57  : ScopedEventBaseThread(ebm, "") {}
58 
60  EventBaseManager* ebm,
61  const StringPiece& name)
62  : ebm_(ebm ? ebm : EventBaseManager::get()) {
63  new (&eb_) EventBase();
64  th_ = thread(run, ebm_, &eb_, &stop_, name);
66 }
67 
70  stop_.post();
71  th_.join();
72 }
73 
74 } // namespace folly
void setEventBase(EventBase *eventBase, bool takeOwnership)
~EventBase() override
Definition: EventBase.cpp:148
STL namespace.
constexpr size_type size() const
Definition: Range.h:431
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
#define nullptr
Definition: http_parser.c:41
const char * name
Definition: http_parser.c:437
FOLLY_ALWAYS_INLINE void wait(const WaitOptions &opt=wait_options()) noexcept
Definition: Baton.h:170
static void stop()
static void run(EventBaseManager *ebm, EventBase *eb, folly::Baton<> *stop, const StringPiece &name)
void terminateLoopSoon()
Definition: EventBase.cpp:493
void post() noexcept
Definition: Baton.h:123
bool setThreadName(std::thread::id tid, StringPiece name)
Definition: ThreadName.cpp:109
void waitUntilRunning()
Definition: EventBase.cpp:249
void runOnDestruction(LoopCallback *callback)
Definition: EventBase.cpp:542
PUSHMI_INLINE_VAR constexpr detail::get_fn< T > get
Definition: submit.h:391